Skip to content

Instantly share code, notes, and snippets.

@kaityo256
Created January 25, 2019 01:43
Show Gist options
  • Save kaityo256/11c5bb56da06e0d7e8036350ba74781f to your computer and use it in GitHub Desktop.
Save kaityo256/11c5bb56da06e0d7e8036350ba74781f to your computer and use it in GitHub Desktop.
demons fly out of your nose
// Modified from https://qiita.com/tkmtSo/items/de3148dd1dcb70f38d6a
#include <cstdio>
int collatz(int n) {
while (true) {
if (n == 1)
return 1;
if (n % 2 == 0) {
n /= 2;
} else {
n = 3 * n + 1;
}
}
}
int test(int n) {
return collatz(n);
}
@kaityo256
Copy link
Author

$  g++ -O3 -S collatz.cpp;c++filt <collatz.s  > collatz.g++.s
test(int):
LFB2:
        jmp     L27
        .align 4,0x90
L29:
        movl    %edi, %eax
        shrl    $31, %eax
        addl    %eax, %edi
        sarl    %edi
L27:
        cmpl    $1, %edi
        je      L28
L18:
        testb   $1, %dil
        je      L29
        leal    1(%rdi,%rdi,2), %edi
        jmp     L18
        .align 4,0x90
L28:
        movl    $1, %eax
        ret

@kaityo256
Copy link
Author

$ clang++ -O3 -S collatz.cpp;c++filt <collatz.s  > collatz.clang.s  

The function test returns 1 without any calculation!

test(int):                              ## @_Z4testi
        .cfi_startproc
## %bb.0:
        pushq   %rbp
        .cfi_def_cfa_offset 16
        .cfi_offset %rbp, -16
        movq    %rsp, %rbp
        .cfi_def_cfa_register %rbp
        movl    $1, %eax
        popq    %rbp
        retq
        .cfi_endproc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment