int next_highest_power_of_two(int n) {
  int result;
  if (n <= 0)
    return 0;
  if (n <= 1)
    return 1;
  if (n <= 2)
    return 2;
  if (n <= 4)
    return 4;
  if (n <= 8)
    return 8;
  if (n <= 16)
    return 16;
  if (n <= 32)
    return 32;
  if (n <= 64)
    return 64;
  if (n <= 128)
    return 128;
  if (n <= 256)
    return 256;
  if (n <= 512)
    return 512;
  if (n <= 1024)
    return 1024;
  if (n <= 2048)
    return 2048;
  if (n <= 4096)
    return 4096;
  if (n <= 8192)
    return 8192;
  if (n <= 16384)
    return 16384;
  if (n <= 32768)
    return 32768;
  result = 65536;
  n--;
  n >>= 16;
  while (n) {
    result <<= 1;
    n >>= 1;
  }
  return result;
}


x86 assembly (clang, -O0):
<next_highest_power_of_two>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jg     111d <next_highest_power_of_two+0x1d>
movl   $0x0,-0x4(%rbp)
jmp    12d8 <next_highest_power_of_two+0x1d8>
cmpl   $0x1,-0x8(%rbp)
jg     1133 <next_highest_power_of_two+0x33>
movl   $0x1,-0x4(%rbp)
jmp    12d8 <next_highest_power_of_two+0x1d8>
cmpl   $0x2,-0x8(%rbp)
jg     1149 <next_highest_power_of_two+0x49>
movl   $0x2,-0x4(%rbp)
jmp    12d8 <next_highest_power_of_two+0x1d8>
cmpl   $0x4,-0x8(%rbp)
jg     115f <next_highest_power_of_two+0x5f>
movl   $0x4,-0x4(%rbp)
jmp    12d8 <next_highest_power_of_two+0x1d8>
cmpl   $0x8,-0x8(%rbp)
jg     1175 <next_highest_power_of_two+0x75>
movl   $0x8,-0x4(%rbp)
jmp    12d8 <next_highest_power_of_two+0x1d8>
cmpl   $0x10,-0x8(%rbp)
jg     118b <next_highest_power_of_two+0x8b>
movl   $0x10,-0x4(%rbp)
jmp    12d8 <next_highest_power_of_two+0x1d8>
cmpl   $0x20,-0x8(%rbp)
jg     11a1 <next_highest_power_of_two+0xa1>
movl   $0x20,-0x4(%rbp)
jmp    12d8 <next_highest_power_of_two+0x1d8>
cmpl   $0x40,-0x8(%rbp)
jg     11b7 <next_highest_power_of_two+0xb7>
movl   $0x40,-0x4(%rbp)
jmp    12d8 <next_highest_power_of_two+0x1d8>
cmpl   $0x80,-0x8(%rbp)
jg     11d0 <next_highest_power_of_two+0xd0>
movl   $0x80,-0x4(%rbp)
jmp    12d8 <next_highest_power_of_two+0x1d8>
cmpl   $0x100,-0x8(%rbp)
jg     11e9 <next_highest_power_of_two+0xe9>
movl   $0x100,-0x4(%rbp)
jmp    12d8 <next_highest_power_of_two+0x1d8>
cmpl   $0x200,-0x8(%rbp)
jg     1202 <next_highest_power_of_two+0x102>
movl   $0x200,-0x4(%rbp)
jmp    12d8 <next_highest_power_of_two+0x1d8>
cmpl   $0x400,-0x8(%rbp)
jg     121b <next_highest_power_of_two+0x11b>
movl   $0x400,-0x4(%rbp)
jmp    12d8 <next_highest_power_of_two+0x1d8>
cmpl   $0x800,-0x8(%rbp)
jg     1234 <next_highest_power_of_two+0x134>
movl   $0x800,-0x4(%rbp)
jmp    12d8 <next_highest_power_of_two+0x1d8>
cmpl   $0x1000,-0x8(%rbp)
jg     124d <next_highest_power_of_two+0x14d>
movl   $0x1000,-0x4(%rbp)
jmp    12d8 <next_highest_power_of_two+0x1d8>
cmpl   $0x2000,-0x8(%rbp)
jg     1266 <next_highest_power_of_two+0x166>
movl   $0x2000,-0x4(%rbp)
jmp    12d8 <next_highest_power_of_two+0x1d8>
cmpl   $0x4000,-0x8(%rbp)
jg     127f <next_highest_power_of_two+0x17f>
movl   $0x4000,-0x4(%rbp)
jmp    12d8 <next_highest_power_of_two+0x1d8>
cmpl   $0x8000,-0x8(%rbp)
jg     1298 <next_highest_power_of_two+0x198>
movl   $0x8000,-0x4(%rbp)
jmp    12d8 <next_highest_power_of_two+0x1d8>
movl   $0x10000,-0xc(%rbp)
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
sar    $0x10,%eax
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     12d2 <next_highest_power_of_two+0x1d2>
mov    -0xc(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
sar    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    12b1 <next_highest_power_of_two+0x1b1>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<next_highest_power_of_two>:
test   %edi,%edi
jle    120d <next_highest_power_of_two+0x10d>
mov    $0x1,%eax
cmp    $0x1,%edi
je     120c <next_highest_power_of_two+0x10c>
mov    $0x2,%eax
cmp    $0x3,%edi
jb     120c <next_highest_power_of_two+0x10c>
mov    $0x4,%eax
cmp    $0x5,%edi
jb     120c <next_highest_power_of_two+0x10c>
mov    $0x8,%eax
cmp    $0x9,%edi
jb     120c <next_highest_power_of_two+0x10c>
mov    $0x10,%eax
cmp    $0x11,%edi
jb     120c <next_highest_power_of_two+0x10c>
mov    $0x20,%eax
cmp    $0x21,%edi
jb     120c <next_highest_power_of_two+0x10c>
mov    $0x40,%eax
cmp    $0x41,%edi
jb     120c <next_highest_power_of_two+0x10c>
mov    $0x80,%eax
cmp    $0x81,%edi
jb     120c <next_highest_power_of_two+0x10c>
mov    $0x100,%eax
cmp    $0x101,%edi
jb     120c <next_highest_power_of_two+0x10c>
mov    $0x200,%eax
cmp    $0x201,%edi
jb     120c <next_highest_power_of_two+0x10c>
mov    $0x400,%eax
cmp    $0x401,%edi
jb     120c <next_highest_power_of_two+0x10c>
mov    $0x800,%eax
cmp    $0x801,%edi
jb     120c <next_highest_power_of_two+0x10c>
mov    $0x1000,%eax
cmp    $0x1001,%edi
jb     120c <next_highest_power_of_two+0x10c>
mov    $0x2000,%eax
cmp    $0x2001,%edi
jb     120c <next_highest_power_of_two+0x10c>
mov    $0x4000,%eax
cmp    $0x4001,%edi
jb     120c <next_highest_power_of_two+0x10c>
mov    $0x8000,%eax
cmp    $0x8001,%edi
jb     120c <next_highest_power_of_two+0x10c>
add    $0xffffffff,%edi
mov    $0x10000,%eax
cmp    $0x10000,%edi
jb     120c <next_highest_power_of_two+0x10c>
shr    $0x10,%edi
mov    $0x10000,%eax
mov    %edi,%ecx
add    %eax,%eax
shr    %ecx
cmp    $0x1,%edi
mov    %ecx,%edi
ja     1201 <next_highest_power_of_two+0x101>
ret
xor    %eax,%eax
ret

LLVM IR (-O0):
define i32 @next_highest_power_of_two(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = icmp sle i32 %5, 0
br i1 %6, label %7, label %8
7: ; preds = %1
store i32 0, i32* %2
br label %87
8: ; preds = %1
%9 = load i32, i32* %3
%10 = icmp sle i32 %9, 1
br i1 %10, label %11, label %12
11: ; preds = %8
store i32 1, i32* %2
br label %87
12: ; preds = %8
%13 = load i32, i32* %3
%14 = icmp sle i32 %13, 2
br i1 %14, label %15, label %16
15: ; preds = %12
store i32 2, i32* %2
br label %87
16: ; preds = %12
%17 = load i32, i32* %3
%18 = icmp sle i32 %17, 4
br i1 %18, label %19, label %20
19: ; preds = %16
store i32 4, i32* %2
br label %87
20: ; preds = %16
%21 = load i32, i32* %3
%22 = icmp sle i32 %21, 8
br i1 %22, label %23, label %24
23: ; preds = %20
store i32 8, i32* %2
br label %87
24: ; preds = %20
%25 = load i32, i32* %3
%26 = icmp sle i32 %25, 16
br i1 %26, label %27, label %28
27: ; preds = %24
store i32 16, i32* %2
br label %87
28: ; preds = %24
%29 = load i32, i32* %3
%30 = icmp sle i32 %29, 32
br i1 %30, label %31, label %32
31: ; preds = %28
store i32 32, i32* %2
br label %87
32: ; preds = %28
%33 = load i32, i32* %3
%34 = icmp sle i32 %33, 64
br i1 %34, label %35, label %36
35: ; preds = %32
store i32 64, i32* %2
br label %87
36: ; preds = %32
%37 = load i32, i32* %3
%38 = icmp sle i32 %37, 128
br i1 %38, label %39, label %40
39: ; preds = %36
store i32 128, i32* %2
br label %87
40: ; preds = %36
%41 = load i32, i32* %3
%42 = icmp sle i32 %41, 256
br i1 %42, label %43, label %44
43: ; preds = %40
store i32 256, i32* %2
br label %87
44: ; preds = %40
%45 = load i32, i32* %3
%46 = icmp sle i32 %45, 512
br i1 %46, label %47, label %48
47: ; preds = %44
store i32 512, i32* %2
br label %87
48: ; preds = %44
%49 = load i32, i32* %3
%50 = icmp sle i32 %49, 1024
br i1 %50, label %51, label %52
51: ; preds = %48
store i32 1024, i32* %2
br label %87
52: ; preds = %48
%53 = load i32, i32* %3
%54 = icmp sle i32 %53, 2048
br i1 %54, label %55, label %56
55: ; preds = %52
store i32 2048, i32* %2
br label %87
56: ; preds = %52
%57 = load i32, i32* %3
%58 = icmp sle i32 %57, 4096
br i1 %58, label %59, label %60
59: ; preds = %56
store i32 4096, i32* %2
br label %87
60: ; preds = %56
%61 = load i32, i32* %3
%62 = icmp sle i32 %61, 8192
br i1 %62, label %63, label %64
63: ; preds = %60
store i32 8192, i32* %2
br label %87
64: ; preds = %60
%65 = load i32, i32* %3
%66 = icmp sle i32 %65, 16384
br i1 %66, label %67, label %68
67: ; preds = %64
store i32 16384, i32* %2
br label %87
68: ; preds = %64
%69 = load i32, i32* %3
%70 = icmp sle i32 %69, 32768
br i1 %70, label %71, label %72
71: ; preds = %68
store i32 32768, i32* %2
br label %87
72: ; preds = %68
store i32 65536, i32* %4
%73 = load i32, i32* %3
%74 = add nsw i32 %73, -1
store i32 %74, i32* %3
%75 = load i32, i32* %3
%76 = ashr i32 %75, 16
store i32 %76, i32* %3
br label %77
77: ; preds = %80, %72
%78 = load i32, i32* %3
%79 = icmp ne i32 %78, 0
br i1 %79, label %80, label %85
80: ; preds = %77
%81 = load i32, i32* %4
%82 = shl i32 %81, 1
store i32 %82, i32* %4
%83 = load i32, i32* %3
%84 = ashr i32 %83, 1
store i32 %84, i32* %3
br label %77
85: ; preds = %77
%86 = load i32, i32* %4
store i32 %86, i32* %2
br label %87
87: ; preds = %85, %71, %67, %63, %59, %55, %51, %47, %43, %39, %35, %31, %27, %23, %19, %15, %11, %7
%88 = load i32, i32* %2
ret i32 %88
}

LLVM IR (-O2):
define i32 @next_highest_power_of_two(i32 %0) {
%2 = icmp slt i32 %0, 1
br i1 %2, label %46, label %3
3: ; preds = %1
%4 = icmp eq i32 %0, 1
br i1 %4, label %46, label %5
5: ; preds = %3
%6 = icmp ult i32 %0, 3
br i1 %6, label %46, label %7
7: ; preds = %5
%8 = icmp ult i32 %0, 5
br i1 %8, label %46, label %9
9: ; preds = %7
%10 = icmp ult i32 %0, 9
br i1 %10, label %46, label %11
11: ; preds = %9
%12 = icmp ult i32 %0, 17
br i1 %12, label %46, label %13
13: ; preds = %11
%14 = icmp ult i32 %0, 33
br i1 %14, label %46, label %15
15: ; preds = %13
%16 = icmp ult i32 %0, 65
br i1 %16, label %46, label %17
17: ; preds = %15
%18 = icmp ult i32 %0, 129
br i1 %18, label %46, label %19
19: ; preds = %17
%20 = icmp ult i32 %0, 257
br i1 %20, label %46, label %21
21: ; preds = %19
%22 = icmp ult i32 %0, 513
br i1 %22, label %46, label %23
23: ; preds = %21
%24 = icmp ult i32 %0, 1025
br i1 %24, label %46, label %25
25: ; preds = %23
%26 = icmp ult i32 %0, 2049
br i1 %26, label %46, label %27
27: ; preds = %25
%28 = icmp ult i32 %0, 4097
br i1 %28, label %46, label %29
29: ; preds = %27
%30 = icmp ult i32 %0, 8193
br i1 %30, label %46, label %31
31: ; preds = %29
%32 = icmp ult i32 %0, 16385
br i1 %32, label %46, label %33
33: ; preds = %31
%34 = icmp ult i32 %0, 32769
br i1 %34, label %46, label %35
35: ; preds = %33
%36 = add nsw i32 %0, -1
%37 = icmp ult i32 %36, 65536
br i1 %37, label %46, label %38
38: ; preds = %35
%39 = lshr i32 %36, 16
br label %40
40: ; preds = %38, %40
%41 = phi i32 [ %43, %40 ], [ 65536, %38 ]
%42 = phi i32 [ %44, %40 ], [ %39, %38 ]
%43 = shl i32 %41, 1
%44 = lshr i32 %42, 1
%45 = icmp ult i32 %42, 2
br i1 %45, label %46, label %40
46: ; preds = %40, %35, %33, %31, %29, %27, %25, %23, %21, %19, %17, %15, %13, %11, %9, %7, %5, %3, %1
%47 = phi i32 [ 0, %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 ], [ %43, %40 ]
ret i32 %47
}