int most_significant_bit_u64(unsigned long long int arg) {
  int msb_bit_ix = 31;
  int delta_shift_size = 16;
  int iteration;
  for (iteration = 0; iteration < 6; iteration++) {
    if (arg >> msb_bit_ix == 0)
      msb_bit_ix -= delta_shift_size;
    else if (arg >> msb_bit_ix == 1)
      break;
    else
      msb_bit_ix += delta_shift_size;
    delta_shift_size = (delta_shift_size + 1) >> 1;
  }
  return msb_bit_ix;
}


x86 assembly (clang, -O0):
<most_significant_bit_u64>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
movl   $0x1f,-0xc(%rbp)
movl   $0x10,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x6,-0x14(%rbp)
jge    118c <most_significant_bit_u64+0x8c>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
shr    %cl,%rax
cmp    $0x0,%rax
jne    114b <most_significant_bit_u64+0x4b>
mov    -0x10(%rbp),%ecx
mov    -0xc(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0xc(%rbp)
jmp    1172 <most_significant_bit_u64+0x72>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
shr    %cl,%rax
cmp    $0x1,%rax
jne    1164 <most_significant_bit_u64+0x64>
jmp    118c <most_significant_bit_u64+0x8c>
mov    -0x10(%rbp),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    1172 <most_significant_bit_u64+0x72>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
sar    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    111d <most_significant_bit_u64+0x1d>
mov    -0xc(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<most_significant_bit_u64>:
mov    %rdi,%rax
shr    $0x1f,%rax
je     111f <most_significant_bit_u64+0x1f>
mov    $0x1f,%ecx
cmp    $0x1,%rax
je     11bb <most_significant_bit_u64+0xbb>
mov    $0x2f,%ecx
jmp    1124 <most_significant_bit_u64+0x24>
mov    $0xf,%ecx
mov    %rdi,%rax
shr    %cl,%rax
test   %rax,%rax
je     1140 <most_significant_bit_u64+0x40>
cmp    $0x1,%rax
je     11bb <most_significant_bit_u64+0xbb>
mov    $0x8,%eax
jmp    1145 <most_significant_bit_u64+0x45>
mov    $0xfffffff8,%eax
add    %eax,%ecx
mov    %rdi,%rax
shr    %cl,%rax
test   %rax,%rax
je     115f <most_significant_bit_u64+0x5f>
cmp    $0x1,%rax
je     11bb <most_significant_bit_u64+0xbb>
mov    $0x4,%eax
jmp    1164 <most_significant_bit_u64+0x64>
mov    $0xfffffffc,%eax
add    %eax,%ecx
mov    %rdi,%rax
shr    %cl,%rax
test   %rax,%rax
je     117e <most_significant_bit_u64+0x7e>
cmp    $0x1,%rax
je     11bb <most_significant_bit_u64+0xbb>
mov    $0x2,%eax
jmp    1183 <most_significant_bit_u64+0x83>
mov    $0xfffffffe,%eax
add    %eax,%ecx
mov    %rdi,%rax
shr    %cl,%rax
test   %rax,%rax
je     119d <most_significant_bit_u64+0x9d>
cmp    $0x1,%rax
je     11bb <most_significant_bit_u64+0xbb>
mov    $0x1,%eax
jmp    11a2 <most_significant_bit_u64+0xa2>
mov    $0xffffffff,%eax
add    %eax,%ecx
shr    %cl,%rdi
cmp    $0x1,%rdi
je     11bb <most_significant_bit_u64+0xbb>
test   %rdi,%rdi
jne    11b8 <most_significant_bit_u64+0xb8>
add    $0xffffffff,%ecx
mov    %ecx,%eax
ret
or     $0x1,%ecx
mov    %ecx,%eax
ret

LLVM IR (-O0):
define i32 @most_significant_bit_u64(i64 %0) {
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i64 %0, i64* %2
store i32 31, i32* %3
store i32 16, i32* %4
store i32 0, i32* %5
br label %6
6: ; preds = %35, %1
%7 = load i32, i32* %5
%8 = icmp slt i32 %7, 6
br i1 %8, label %9, label %38
9: ; preds = %6
%10 = load i64, i64* %2
%11 = load i32, i32* %3
%12 = zext i32 %11 to i64
%13 = lshr i64 %10, %12
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %9
%16 = load i32, i32* %4
%17 = load i32, i32* %3
%18 = sub nsw i32 %17, %16
store i32 %18, i32* %3
br label %31
19: ; preds = %9
%20 = load i64, i64* %2
%21 = load i32, i32* %3
%22 = zext i32 %21 to i64
%23 = lshr i64 %20, %22
%24 = icmp eq i64 %23, 1
br i1 %24, label %25, label %26
25: ; preds = %19
br label %38
26: ; preds = %19
%27 = load i32, i32* %4
%28 = load i32, i32* %3
%29 = add nsw i32 %28, %27
store i32 %29, i32* %3
br label %30
30: ; preds = %26
br label %31
31: ; preds = %30, %15
%32 = load i32, i32* %4
%33 = add nsw i32 %32, 1
%34 = ashr i32 %33, 1
store i32 %34, i32* %4
br label %35
35: ; preds = %31
%36 = load i32, i32* %5
%37 = add nsw i32 %36, 1
store i32 %37, i32* %5
br label %6
38: ; preds = %25, %6
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
define i32 @most_significant_bit_u64(i64 %0) {
%2 = lshr i64 %0, 31
switch i64 %2, label %3 [
i64 0, label %4
i64 1, label %36
]
3: ; preds = %1
br label %4
4: ; preds = %1, %3
%5 = phi i32 [ 47, %3 ], [ 15, %1 ]
%6 = zext i32 %5 to i64
%7 = lshr i64 %0, %6
switch i64 %7, label %8 [
i64 0, label %9
i64 1, label %36
]
8: ; preds = %4
br label %9
9: ; preds = %4, %8
%10 = phi i32 [ 8, %8 ], [ -8, %4 ]
%11 = add nsw i32 %5, %10
%12 = zext i32 %11 to i64
%13 = lshr i64 %0, %12
switch i64 %13, label %14 [
i64 0, label %15
i64 1, label %36
]
14: ; preds = %9
br label %15
15: ; preds = %9, %14
%16 = phi i32 [ 4, %14 ], [ -4, %9 ]
%17 = add nsw i32 %11, %16
%18 = zext i32 %17 to i64
%19 = lshr i64 %0, %18
switch i64 %19, label %20 [
i64 0, label %21
i64 1, label %36
]
20: ; preds = %15
br label %21
21: ; preds = %15, %20
%22 = phi i32 [ 2, %20 ], [ -2, %15 ]
%23 = add nsw i32 %17, %22
%24 = zext i32 %23 to i64
%25 = lshr i64 %0, %24
switch i64 %25, label %26 [
i64 0, label %27
i64 1, label %36
]
26: ; preds = %21
br label %27
27: ; preds = %21, %26
%28 = phi i32 [ 1, %26 ], [ -1, %21 ]
%29 = add nsw i32 %23, %28
%30 = zext i32 %29 to i64
%31 = lshr i64 %0, %30
switch i64 %31, label %34 [
i64 0, label %32
i64 1, label %36
]
32: ; preds = %27
%33 = add nsw i32 %29, -1
br label %36
34: ; preds = %27
%35 = or i32 %29, 1
br label %36
36: ; preds = %32, %34, %27, %21, %15, %9, %4, %1
%37 = phi i32 [ 31, %1 ], [ %5, %4 ], [ %11, %9 ], [ %17, %15 ], [ %23, %21 ], [ %29, %27 ], [ %33, %32 ], [ %35, %34 ]
ret i32 %37
}