int test_isPower2(int x) {
  int i;
  for (i = 0; i < 31; i++) {
    if (x == 1 << i)
      return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<test_isPower2>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x1f,-0xc(%rbp)
jge    114e <test_isPower2+0x4e>
mov    -0x8(%rbp),%eax
mov    -0xc(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
cmp    %ecx,%eax
jne    113b <test_isPower2+0x3b>
movl   $0x1,-0x4(%rbp)
jmp    1155 <test_isPower2+0x55>
jmp    1140 <test_isPower2+0x40>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    110e <test_isPower2+0xe>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_isPower2>:
mov    $0x1,%eax
cmp    $0x7fff,%edi
jle    1142 <test_isPower2+0x42>
cmp    $0x7fffff,%edi
jg     1174 <test_isPower2+0x74>
cmp    $0x7ffff,%edi
jg     11c2 <test_isPower2+0xc2>
cmp    $0x1ffff,%edi
jg     123d <test_isPower2+0x13d>
cmp    $0x8000,%edi
je     1141 <test_isPower2+0x41>
cmp    $0x10000,%edi
jne    12da <test_isPower2+0x1da>
ret
cmp    $0x7f,%edi
jle    11a1 <test_isPower2+0xa1>
cmp    $0x7ff,%edi
jg     11eb <test_isPower2+0xeb>
cmp    $0x1ff,%edi
jg     125a <test_isPower2+0x15a>
cmp    $0x80,%edi
je     1141 <test_isPower2+0x41>
cmp    $0x100,%edi
je     1141 <test_isPower2+0x41>
jmp    12da <test_isPower2+0x1da>
cmp    $0x7ffffff,%edi
jg     1214 <test_isPower2+0x114>
cmp    $0x1ffffff,%edi
jg     1274 <test_isPower2+0x174>
cmp    $0x800000,%edi
je     1141 <test_isPower2+0x41>
cmp    $0x1000000,%edi
je     1141 <test_isPower2+0x41>
jmp    12da <test_isPower2+0x1da>
add    $0xffffffff,%edi
cmp    $0x3f,%edi
ja     12da <test_isPower2+0x1da>
movabs $0x800000008000808b,%rcx
bt     %rdi,%rcx
jb     1141 <test_isPower2+0x41>
jmp    12da <test_isPower2+0x1da>
cmp    $0x1fffff,%edi
jg     128e <test_isPower2+0x18e>
cmp    $0x80000,%edi
je     1141 <test_isPower2+0x41>
cmp    $0x100000,%edi
je     1141 <test_isPower2+0x41>
jmp    12da <test_isPower2+0x1da>
cmp    $0x1fff,%edi
jg     12a8 <test_isPower2+0x1a8>
cmp    $0x800,%edi
je     1141 <test_isPower2+0x41>
cmp    $0x1000,%edi
je     1141 <test_isPower2+0x41>
jmp    12da <test_isPower2+0x1da>
cmp    $0x1fffffff,%edi
jg     12c2 <test_isPower2+0x1c2>
cmp    $0x8000000,%edi
je     1141 <test_isPower2+0x41>
cmp    $0x10000000,%edi
je     1141 <test_isPower2+0x41>
jmp    12da <test_isPower2+0x1da>
cmp    $0x20000,%edi
je     1141 <test_isPower2+0x41>
cmp    $0x40000,%edi
je     1141 <test_isPower2+0x41>
jmp    12da <test_isPower2+0x1da>
cmp    $0x200,%edi
je     1141 <test_isPower2+0x41>
cmp    $0x400,%edi
je     1141 <test_isPower2+0x41>
jmp    12da <test_isPower2+0x1da>
cmp    $0x2000000,%edi
je     1141 <test_isPower2+0x41>
cmp    $0x4000000,%edi
je     1141 <test_isPower2+0x41>
jmp    12da <test_isPower2+0x1da>
cmp    $0x200000,%edi
je     1141 <test_isPower2+0x41>
cmp    $0x400000,%edi
je     1141 <test_isPower2+0x41>
jmp    12da <test_isPower2+0x1da>
cmp    $0x2000,%edi
je     1141 <test_isPower2+0x41>
cmp    $0x4000,%edi
je     1141 <test_isPower2+0x41>
jmp    12da <test_isPower2+0x1da>
cmp    $0x20000000,%edi
je     1141 <test_isPower2+0x41>
cmp    $0x40000000,%edi
je     1141 <test_isPower2+0x41>
xor    %eax,%eax
ret

LLVM IR (-O0):
define i32 @test_isPower2(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
store i32 0, i32* %4
br label %5
5: ; preds = %15, %1
%6 = load i32, i32* %4
%7 = icmp slt i32 %6, 31
br i1 %7, label %8, label %18
8: ; preds = %5
%9 = load i32, i32* %3
%10 = load i32, i32* %4
%11 = shl i32 1, %10
%12 = icmp eq i32 %9, %11
br i1 %12, label %13, label %14
13: ; preds = %8
store i32 1, i32* %2
br label %19
14: ; preds = %8
br label %15
15: ; preds = %14
%16 = load i32, i32* %4
%17 = add nsw i32 %16, 1
store i32 %17, i32* %4
br label %5
18: ; preds = %5
store i32 0, i32* %2
br label %19
19: ; preds = %18, %13
%20 = load i32, i32* %2
ret i32 %20
}

LLVM IR (-O2):
define i32 @test_isPower2(i32 %0) {
switch i32 %0, label %2 [
i32 1, label %3
i32 2, label %3
i32 4, label %3
i32 8, label %3
i32 16, label %3
i32 32, label %3
i32 64, label %3
i32 128, label %3
i32 256, label %3
i32 512, label %3
i32 1024, label %3
i32 2048, label %3
i32 4096, label %3
i32 8192, label %3
i32 16384, label %3
i32 32768, label %3
i32 65536, label %3
i32 131072, label %3
i32 262144, label %3
i32 524288, label %3
i32 1048576, label %3
i32 2097152, label %3
i32 4194304, label %3
i32 8388608, label %3
i32 16777216, label %3
i32 33554432, label %3
i32 67108864, label %3
i32 134217728, label %3
i32 268435456, label %3
i32 536870912, label %3
i32 1073741824, label %3
]
2: ; preds = %1
br label %3
3: ; preds = %1, %1, %1, %1, %1, %1, %1, %1, %1, %1, %1, %1, %1, %1, %1, %1, %1, %1, %1, %1, %1, %1, %1, %1, %1, %1, %1, %1, %1, %1, %1, %2
%4 = phi i32 [ 1, %1 ], [ 0, %2 ], [ 1, %1 ], [ 1, %1 ], [ 1, %1 ], [ 1, %1 ], [ 1, %1 ], [ 1, %1 ], [ 1, %1 ], [ 1, %1 ], [ 1, %1 ], [ 1, %1 ], [ 1, %1 ], [ 1, %1 ], [ 1, %1 ], [ 1, %1 ], [ 1, %1 ], [ 1, %1 ], [ 1, %1 ], [ 1, %1 ], [ 1, %1 ], [ 1, %1 ], [ 1, %1 ], [ 1, %1 ], [ 1, %1 ], [ 1, %1 ], [ 1, %1 ], [ 1, %1 ], [ 1, %1 ], [ 1, %1 ], [ 1, %1 ], [ 1, %1 ]
ret i32 %4
}