int log_base2(int n) {
  int power = 0;
  if (n <= 0 || (n & (n - 1)) != 0)
    _panic("/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
           "2022-01-22/02-19-57/repos/t-kelter/Chronos-Multicore/refs/heads/"
           "master/cfg/misc.c",
           __FUNCTION__, 265,
           "log2() only works for positive power of two values");
  while (n >>= 1)
    power++;
  return power;
}


x86 assembly (clang, -O0):
<log_base2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0x0,-0x4(%rbp)
jle    1140 <log_base2+0x30>
mov    -0x4(%rbp),%eax
mov    -0x4(%rbp),%ecx
sub    $0x1,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     1161 <log_base2+0x51>
lea    0xeb9(%rip),%rdi
lea    0xf44(%rip),%rsi
mov    $0x109,%edx
lea    0xf42(%rip),%rcx
mov    $0x0,%al
call   1030 <_panic@plt>
jmp    1166 <log_base2+0x56>
mov    -0x4(%rbp),%eax
sar    $0x1,%eax
mov    %eax,-0x4(%rbp)
cmp    $0x0,%eax
je     1186 <log_base2+0x76>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1166 <log_base2+0x56>
mov    -0x8(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<log_base2>:
push   %rbx
mov    %edi,%ebx
test   %edi,%edi
jle    1150 <log_base2+0x40>
mov    %ebx,%eax
shr    %eax
and    $0x55555555,%eax
mov    %ebx,%ecx
sub    %eax,%ecx
mov    %ecx,%eax
and    $0x33333333,%eax
shr    $0x2,%ecx
and    $0x33333333,%ecx
add    %eax,%ecx
mov    %ecx,%eax
shr    $0x4,%eax
add    %ecx,%eax
and    $0xf0f0f0f,%eax
imul   $0x1010101,%eax,%eax
shr    $0x18,%eax
cmp    $0x2,%eax
jb     1171 <log_base2+0x61>
lea    0xea9(%rip),%rdi
lea    0xf34(%rip),%rsi
lea    0xf37(%rip),%rcx
mov    $0x109,%edx
xor    %eax,%eax
call   1030 <_panic@plt>
xor    %eax,%eax
cmp    $0x2,%ebx
jb     118c <log_base2+0x7c>
xor    %eax,%eax
mov    %ebx,%ecx
nopl   0x0(%rax)
sar    %ecx
add    $0x1,%eax
cmp    $0x3,%ebx
mov    %ecx,%ebx
ja     1180 <log_base2+0x70>
pop    %rbx
ret

LLVM IR (-O0):
@.str = [146 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/t-kelter/Chronos-Multicore/refs/heads/master/cfg/misc.c\00"
@__FUNCTION__.log_base2 = [10 x i8] c"log_base2\00"
@.str.1 = [51 x i8] c"log2() only works for positive power of two values\00"
define i32 @log_base2(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %3
%4 = load i32, i32* %2
%5 = icmp sle i32 %4, 0
br i1 %5, label %12, label %6
6: ; preds = %1
%7 = load i32, i32* %2
%8 = load i32, i32* %2
%9 = sub nsw i32 %8, 1
%10 = and i32 %7, %9
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %6, %1
%13 = call i32 (i8*, i8*, i32, i8*, ...) bitcast (i32 (...)* @_panic to i32 (i8*, i8*, i32, i8*, ...)*)(i8* getelementptr inbounds ([146 x i8], [146 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @__FUNCTION__.log_base2, i64 0, i64 0), i32 265, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %6
br label %15
15: ; preds = %19, %14
%16 = load i32, i32* %2
%17 = ashr i32 %16, 1
store i32 %17, i32* %2
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i32, i32* %3
%21 = add nsw i32 %20, 1
store i32 %21, i32* %3
br label %15
22: ; preds = %15
%23 = load i32, i32* %3
ret i32 %23
}

LLVM IR (-O2):
@.str = [146 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/t-kelter/Chronos-Multicore/refs/heads/master/cfg/misc.c\00"
@__FUNCTION__.log_base2 = [10 x i8] c"log_base2\00"
@.str.1 = [51 x i8] c"log2() only works for positive power of two values\00"
define i32 @log_base2(i32 %0) {
%2 = icmp sgt i32 %0, 0
%3 = tail call i32 @llvm.ctpop.i32(i32 %0)
%4 = icmp ult i32 %3, 2
%5 = and i1 %2, %4
br i1 %5, label %8, label %6
6: ; preds = %1
%7 = tail call i32 (i8*, i8*, i32, i8*, ...) bitcast (i32 (...)* @_panic to i32 (i8*, i8*, i32, i8*, ...)*)(i8* getelementptr inbounds ([146 x i8], [146 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @__FUNCTION__.log_base2, i64 0, i64 0), i32 265, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0))
br label %8
8: ; preds = %1, %6
%9 = icmp ult i32 %0, 2
br i1 %9, label %16, label %10
10: ; preds = %8, %10
%11 = phi i32 [ %14, %10 ], [ 0, %8 ]
%12 = phi i32 [ %13, %10 ], [ %0, %8 ]
%13 = ashr i32 %12, 1
%14 = add nuw nsw i32 %11, 1
%15 = icmp ult i32 %12, 4
br i1 %15, label %16, label %10
16: ; preds = %10, %8
%17 = phi i32 [ 0, %8 ], [ %14, %10 ]
ret i32 %17
}