int round_up_to_next_power_of_2(int x) {
  int exponent;
  if (x < 0)
    return -1;
  for (exponent = 0; exponent < 31; exponent++) {
    int power_of_two = 1 << exponent;
    if (x <= power_of_two)
      return power_of_two;
  }
  return -1;
}


x86 assembly (clang, -O0):
<round_up_to_next_power_of_2>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    111d <round_up_to_next_power_of_2+0x1d>
movl   $0xffffffff,-0x4(%rbp)
jmp    116c <round_up_to_next_power_of_2+0x6c>
movl   $0x0,-0xc(%rbp)
cmpl   $0x1f,-0xc(%rbp)
jge    1165 <round_up_to_next_power_of_2+0x65>
mov    -0xc(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x10(%rbp),%eax
jg     1152 <round_up_to_next_power_of_2+0x52>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    116c <round_up_to_next_power_of_2+0x6c>
jmp    1157 <round_up_to_next_power_of_2+0x57>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1124 <round_up_to_next_power_of_2+0x24>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<round_up_to_next_power_of_2>:
test   %edi,%edi
js     12de <round_up_to_next_power_of_2+0x1de>
mov    $0x1,%eax
cmp    $0x2,%edi
jb     12dd <round_up_to_next_power_of_2+0x1dd>
mov    $0x2,%eax
je     12dd <round_up_to_next_power_of_2+0x1dd>
mov    $0x4,%eax
cmp    $0x5,%edi
jb     12dd <round_up_to_next_power_of_2+0x1dd>
mov    $0x8,%eax
cmp    $0x9,%edi
jb     12dd <round_up_to_next_power_of_2+0x1dd>
mov    $0x10,%eax
cmp    $0x11,%edi
jb     12dd <round_up_to_next_power_of_2+0x1dd>
mov    $0x20,%eax
cmp    $0x21,%edi
jb     12dd <round_up_to_next_power_of_2+0x1dd>
mov    $0x40,%eax
cmp    $0x41,%edi
jb     12dd <round_up_to_next_power_of_2+0x1dd>
mov    $0x80,%eax
cmp    $0x81,%edi
jb     12dd <round_up_to_next_power_of_2+0x1dd>
mov    $0x100,%eax
cmp    $0x101,%edi
jb     12dd <round_up_to_next_power_of_2+0x1dd>
mov    $0x200,%eax
cmp    $0x201,%edi
jb     12dd <round_up_to_next_power_of_2+0x1dd>
mov    $0x400,%eax
cmp    $0x401,%edi
jb     12dd <round_up_to_next_power_of_2+0x1dd>
mov    $0x800,%eax
cmp    $0x801,%edi
jb     12dd <round_up_to_next_power_of_2+0x1dd>
mov    $0x1000,%eax
cmp    $0x1001,%edi
jb     12dd <round_up_to_next_power_of_2+0x1dd>
mov    $0x2000,%eax
cmp    $0x2001,%edi
jb     12dd <round_up_to_next_power_of_2+0x1dd>
mov    $0x4000,%eax
cmp    $0x4001,%edi
jb     12dd <round_up_to_next_power_of_2+0x1dd>
mov    $0x8000,%eax
cmp    $0x8001,%edi
jb     12dd <round_up_to_next_power_of_2+0x1dd>
mov    $0x10000,%eax
cmp    $0x10001,%edi
jb     12dd <round_up_to_next_power_of_2+0x1dd>
mov    $0x20000,%eax
cmp    $0x20001,%edi
jb     12dd <round_up_to_next_power_of_2+0x1dd>
mov    $0x40000,%eax
cmp    $0x40001,%edi
jb     12dd <round_up_to_next_power_of_2+0x1dd>
mov    $0x80000,%eax
cmp    $0x80001,%edi
jb     12dd <round_up_to_next_power_of_2+0x1dd>
mov    $0x100000,%eax
cmp    $0x100001,%edi
jb     12dd <round_up_to_next_power_of_2+0x1dd>
mov    $0x200000,%eax
cmp    $0x200001,%edi
jb     12dd <round_up_to_next_power_of_2+0x1dd>
mov    $0x400000,%eax
cmp    $0x400001,%edi
jb     12dd <round_up_to_next_power_of_2+0x1dd>
mov    $0x800000,%eax
cmp    $0x800001,%edi
jb     12dd <round_up_to_next_power_of_2+0x1dd>
mov    $0x1000000,%eax
cmp    $0x1000001,%edi
jb     12dd <round_up_to_next_power_of_2+0x1dd>
mov    $0x2000000,%eax
cmp    $0x2000001,%edi
jb     12dd <round_up_to_next_power_of_2+0x1dd>
mov    $0x4000000,%eax
cmp    $0x4000001,%edi
jb     12dd <round_up_to_next_power_of_2+0x1dd>
mov    $0x8000000,%eax
cmp    $0x8000001,%edi
jb     12dd <round_up_to_next_power_of_2+0x1dd>
mov    $0x10000000,%eax
cmp    $0x10000001,%edi
jb     12dd <round_up_to_next_power_of_2+0x1dd>
mov    $0x20000000,%eax
cmp    $0x20000001,%edi
jb     12dd <round_up_to_next_power_of_2+0x1dd>
cmp    $0x40000001,%edi
mov    $0xffffffff,%ecx
mov    $0x40000000,%eax
cmovae %ecx,%eax
ret
mov    $0xffffffff,%eax
ret

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

LLVM IR (-O2):
define i32 @round_up_to_next_power_of_2(i32 %0) {
%2 = icmp slt i32 %0, 0
br i1 %2, label %66, label %3
3: ; preds = %1
%4 = icmp ugt i32 %0, 1
br i1 %4, label %5, label %66
5: ; preds = %3
%6 = icmp eq i32 %0, 2
br i1 %6, label %66, label %7
7: ; preds = %5
%8 = icmp ugt i32 %0, 4
br i1 %8, label %9, label %66
9: ; preds = %7
%10 = icmp ugt i32 %0, 8
br i1 %10, label %11, label %66
11: ; preds = %9
%12 = icmp ugt i32 %0, 16
br i1 %12, label %13, label %66
13: ; preds = %11
%14 = icmp ugt i32 %0, 32
br i1 %14, label %15, label %66
15: ; preds = %13
%16 = icmp ugt i32 %0, 64
br i1 %16, label %17, label %66
17: ; preds = %15
%18 = icmp ugt i32 %0, 128
br i1 %18, label %19, label %66
19: ; preds = %17
%20 = icmp ugt i32 %0, 256
br i1 %20, label %21, label %66
21: ; preds = %19
%22 = icmp ugt i32 %0, 512
br i1 %22, label %23, label %66
23: ; preds = %21
%24 = icmp ugt i32 %0, 1024
br i1 %24, label %25, label %66
25: ; preds = %23
%26 = icmp ugt i32 %0, 2048
br i1 %26, label %27, label %66
27: ; preds = %25
%28 = icmp ugt i32 %0, 4096
br i1 %28, label %29, label %66
29: ; preds = %27
%30 = icmp ugt i32 %0, 8192
br i1 %30, label %31, label %66
31: ; preds = %29
%32 = icmp ugt i32 %0, 16384
br i1 %32, label %33, label %66
33: ; preds = %31
%34 = icmp ugt i32 %0, 32768
br i1 %34, label %35, label %66
35: ; preds = %33
%36 = icmp ugt i32 %0, 65536
br i1 %36, label %37, label %66
37: ; preds = %35
%38 = icmp ugt i32 %0, 131072
br i1 %38, label %39, label %66
39: ; preds = %37
%40 = icmp ugt i32 %0, 262144
br i1 %40, label %41, label %66
41: ; preds = %39
%42 = icmp ugt i32 %0, 524288
br i1 %42, label %43, label %66
43: ; preds = %41
%44 = icmp ugt i32 %0, 1048576
br i1 %44, label %45, label %66
45: ; preds = %43
%46 = icmp ugt i32 %0, 2097152
br i1 %46, label %47, label %66
47: ; preds = %45
%48 = icmp ugt i32 %0, 4194304
br i1 %48, label %49, label %66
49: ; preds = %47
%50 = icmp ugt i32 %0, 8388608
br i1 %50, label %51, label %66
51: ; preds = %49
%52 = icmp ugt i32 %0, 16777216
br i1 %52, label %53, label %66
53: ; preds = %51
%54 = icmp ugt i32 %0, 33554432
br i1 %54, label %55, label %66
55: ; preds = %53
%56 = icmp ugt i32 %0, 67108864
br i1 %56, label %57, label %66
57: ; preds = %55
%58 = icmp ugt i32 %0, 134217728
br i1 %58, label %59, label %66
59: ; preds = %57
%60 = icmp ugt i32 %0, 268435456
br i1 %60, label %61, label %66
61: ; preds = %59
%62 = icmp ugt i32 %0, 536870912
br i1 %62, label %63, label %66
63: ; preds = %61
%64 = icmp ugt i32 %0, 1073741824
%65 = select i1 %64, i32 -1, i32 1073741824
br label %66
66: ; preds = %63, %3, %5, %7, %9, %11, %13, %15, %17, %19, %21, %23, %25, %27, %29, %31, %33, %35, %37, %39, %41, %43, %45, %47, %49, %51, %53, %55, %57, %59, %61, %1
%67 = phi i32 [ -1, %1 ], [ 1, %3 ], [ 2, %5 ], [ 4, %7 ], [ 8, %9 ], [ 16, %11 ], [ 32, %13 ], [ 64, %15 ], [ 128, %17 ], [ 256, %19 ], [ 512, %21 ], [ 1024, %23 ], [ 2048, %25 ], [ 4096, %27 ], [ 8192, %29 ], [ 16384, %31 ], [ 32768, %33 ], [ 65536, %35 ], [ 131072, %37 ], [ 262144, %39 ], [ 524288, %41 ], [ 1048576, %43 ], [ 2097152, %45 ], [ 4194304, %47 ], [ 8388608, %49 ], [ 16777216, %51 ], [ 33554432, %53 ], [ 67108864, %55 ], [ 134217728, %57 ], [ 268435456, %59 ], [ 536870912, %61 ], [ %65, %63 ]
ret i32 %67
}