long nearestPowerOf2(number)
long number;
{
  int power;
  for (power = 0; power < 32; power++) {
    if (number <= (1L << power)) {
      return (1L << power);
    }
  }
  return (-1);
}


x86 assembly (clang, -O0):
<nearestPowerOf2>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x20,-0x14(%rbp)
jge    115d <nearestPowerOf2+0x5d>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%rdx
mov    %rdx,%rcx
cmp    %rcx,%rax
jg     114a <nearestPowerOf2+0x4a>
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
mov    $0x1,%eax
shl    %cl,%rax
mov    %rax,-0x8(%rbp)
jmp    1165 <nearestPowerOf2+0x65>
jmp    114f <nearestPowerOf2+0x4f>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    110f <nearestPowerOf2+0xf>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<nearestPowerOf2>:
mov    $0x1,%eax
cmp    $0x2,%rdi
jl     1308 <nearestPowerOf2+0x208>
mov    $0x2,%eax
je     1308 <nearestPowerOf2+0x208>
mov    $0x4,%eax
cmp    $0x5,%rdi
jb     1308 <nearestPowerOf2+0x208>
mov    $0x8,%eax
cmp    $0x9,%rdi
jb     1308 <nearestPowerOf2+0x208>
mov    $0x10,%eax
cmp    $0x11,%rdi
jb     1308 <nearestPowerOf2+0x208>
mov    $0x20,%eax
cmp    $0x21,%rdi
jb     1308 <nearestPowerOf2+0x208>
mov    $0x40,%eax
cmp    $0x41,%rdi
jb     1308 <nearestPowerOf2+0x208>
mov    $0x80,%eax
cmp    $0x81,%rdi
jb     1308 <nearestPowerOf2+0x208>
mov    $0x100,%eax
cmp    $0x101,%rdi
jb     1308 <nearestPowerOf2+0x208>
mov    $0x200,%eax
cmp    $0x201,%rdi
jb     1308 <nearestPowerOf2+0x208>
mov    $0x400,%eax
cmp    $0x401,%rdi
jb     1308 <nearestPowerOf2+0x208>
mov    $0x800,%eax
cmp    $0x801,%rdi
jb     1308 <nearestPowerOf2+0x208>
mov    $0x1000,%eax
cmp    $0x1001,%rdi
jb     1308 <nearestPowerOf2+0x208>
mov    $0x2000,%eax
cmp    $0x2001,%rdi
jb     1308 <nearestPowerOf2+0x208>
mov    $0x4000,%eax
cmp    $0x4001,%rdi
jb     1308 <nearestPowerOf2+0x208>
mov    $0x8000,%eax
cmp    $0x8001,%rdi
jb     1308 <nearestPowerOf2+0x208>
mov    $0x10000,%eax
cmp    $0x10001,%rdi
jb     1308 <nearestPowerOf2+0x208>
mov    $0x20000,%eax
cmp    $0x20001,%rdi
jb     1308 <nearestPowerOf2+0x208>
mov    $0x40000,%eax
cmp    $0x40001,%rdi
jb     1308 <nearestPowerOf2+0x208>
mov    $0x80000,%eax
cmp    $0x80001,%rdi
jb     1308 <nearestPowerOf2+0x208>
mov    $0x100000,%eax
cmp    $0x100001,%rdi
jb     1308 <nearestPowerOf2+0x208>
mov    $0x200000,%eax
cmp    $0x200001,%rdi
jb     1308 <nearestPowerOf2+0x208>
mov    $0x400000,%eax
cmp    $0x400001,%rdi
jb     1308 <nearestPowerOf2+0x208>
mov    $0x800000,%eax
cmp    $0x800001,%rdi
jb     1308 <nearestPowerOf2+0x208>
mov    $0x1000000,%eax
cmp    $0x1000001,%rdi
jb     1308 <nearestPowerOf2+0x208>
mov    $0x2000000,%eax
cmp    $0x2000001,%rdi
jb     1308 <nearestPowerOf2+0x208>
mov    $0x4000000,%eax
cmp    $0x4000001,%rdi
jb     1308 <nearestPowerOf2+0x208>
mov    $0x8000000,%eax
cmp    $0x8000001,%rdi
jb     1308 <nearestPowerOf2+0x208>
mov    $0x10000000,%eax
cmp    $0x10000001,%rdi
jb     1308 <nearestPowerOf2+0x208>
mov    $0x20000000,%eax
cmp    $0x20000001,%rdi
jb     1308 <nearestPowerOf2+0x208>
mov    $0x40000000,%eax
cmp    $0x40000001,%rdi
jb     1308 <nearestPowerOf2+0x208>
mov    $0x80000000,%ecx
cmp    %rcx,%rdi
mov    $0xffffffffffffffff,%rax
cmovbe %rcx,%rax
ret

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

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