#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
double log(int);
int pow(int, int);
int close_power_of_two(int pw, int n) {
  int size = ((log(pow(2, pw) / n) / log(2)) >= 0
                  ? (long)((log(pow(2, pw) / n) / log(2)) + 0.5)
                  : (long)((log(pow(2, pw) / n) / log(2)) - 0.5));
  if (pow(2, size) * n < pow(2, pw))
    size += 1;
  return size;
}


x86 assembly (clang, -O0):
<close_power_of_two>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%esi
mov    $0x2,%edi
call   1040 <pow@plt>
cltd
idivl  -0x8(%rbp)
mov    %eax,%edi
call   1030 <log@plt>
movsd  %xmm0,-0x18(%rbp)
mov    $0x2,%edi
call   1030 <log@plt>
movaps %xmm0,%xmm1
movsd  -0x18(%rbp),%xmm0
divsd  %xmm1,%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jb     11bb <close_power_of_two+0x9b>
mov    -0x4(%rbp),%esi
mov    $0x2,%edi
call   1040 <pow@plt>
cltd
idivl  -0x8(%rbp)
mov    %eax,%edi
call   1030 <log@plt>
movsd  %xmm0,-0x28(%rbp)
mov    $0x2,%edi
call   1030 <log@plt>
movaps %xmm0,%xmm1
movsd  -0x28(%rbp),%xmm0
divsd  %xmm1,%xmm0
movsd  0xe57(%rip),%xmm1
addsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rax
mov    %rax,-0x20(%rbp)
jmp    1203 <close_power_of_two+0xe3>
mov    -0x4(%rbp),%esi
mov    $0x2,%edi
call   1040 <pow@plt>
cltd
idivl  -0x8(%rbp)
mov    %eax,%edi
call   1030 <log@plt>
movsd  %xmm0,-0x30(%rbp)
mov    $0x2,%edi
call   1030 <log@plt>
movaps %xmm0,%xmm1
movsd  -0x30(%rbp),%xmm0
divsd  %xmm1,%xmm0
movsd  0xe0a(%rip),%xmm1
subsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
mov    $0x2,%edi
call   1040 <pow@plt>
imul   -0x8(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x4(%rbp),%esi
mov    $0x2,%edi
call   1040 <pow@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
cmp    %ecx,%eax
jge    1241 <close_power_of_two+0x121>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<close_power_of_two>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %edi,%r14d
mov    $0x2,%edi
mov    %r14d,%esi
call   1040 <pow@plt>
cltd
idiv   %r15d
mov    %eax,%edi
call   1030 <log@plt>
movsd  %xmm0,(%rsp)
mov    $0x2,%edi
call   1030 <log@plt>
movsd  (%rsp),%xmm1
divsd  %xmm0,%xmm1
xorpd  %xmm0,%xmm0
xor    %ebx,%ebx
ucomisd %xmm0,%xmm1
setb   %bl
mov    $0x2,%edi
mov    %r14d,%esi
call   1040 <pow@plt>
cltd
idiv   %r15d
mov    %eax,%edi
call   1030 <log@plt>
movsd  %xmm0,(%rsp)
mov    $0x2,%edi
call   1030 <log@plt>
movsd  (%rsp),%xmm1
divsd  %xmm0,%xmm1
lea    0xe5f(%rip),%rax
addsd  (%rax,%rbx,8),%xmm1
cvttsd2si %xmm1,%rbx
mov    $0x2,%edi
mov    %ebx,%esi
call   1040 <pow@plt>
mov    %eax,%ebp
imul   %r15d,%ebp
mov    $0x2,%edi
mov    %r14d,%esi
call   1040 <pow@plt>
xor    %ecx,%ecx
cmp    %eax,%ebp
setl   %cl
add    %ebx,%ecx
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @close_power_of_two(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%6 = load i32, i32* %3
%7 = call i32 @pow(i32 2, i32 %6)
%8 = load i32, i32* %4
%9 = sdiv i32 %7, %8
%10 = call double @log(i32 %9)
%11 = call double @log(i32 2)
%12 = fdiv double %10, %11
%13 = fcmp oge double %12, 0.000000e+00
br i1 %13, label %14, label %24
14: ; preds = %2
%15 = load i32, i32* %3
%16 = call i32 @pow(i32 2, i32 %15)
%17 = load i32, i32* %4
%18 = sdiv i32 %16, %17
%19 = call double @log(i32 %18)
%20 = call double @log(i32 2)
%21 = fdiv double %19, %20
%22 = fadd double %21, 5.000000e-01
%23 = fptosi double %22 to i64
br label %34
24: ; preds = %2
%25 = load i32, i32* %3
%26 = call i32 @pow(i32 2, i32 %25)
%27 = load i32, i32* %4
%28 = sdiv i32 %26, %27
%29 = call double @log(i32 %28)
%30 = call double @log(i32 2)
%31 = fdiv double %29, %30
%32 = fsub double %31, 5.000000e-01
%33 = fptosi double %32 to i64
br label %34
34: ; preds = %24, %14
%35 = phi i64 [ %23, %14 ], [ %33, %24 ]
%36 = trunc i64 %35 to i32
store i32 %36, i32* %5
%37 = load i32, i32* %5
%38 = call i32 @pow(i32 2, i32 %37)
%39 = load i32, i32* %4
%40 = mul nsw i32 %38, %39
%41 = load i32, i32* %3
%42 = call i32 @pow(i32 2, i32 %41)
%43 = icmp slt i32 %40, %42
br i1 %43, label %44, label %47
44: ; preds = %34
%45 = load i32, i32* %5
%46 = add nsw i32 %45, 1
store i32 %46, i32* %5
br label %47
47: ; preds = %44, %34
%48 = load i32, i32* %5
ret i32 %48
}

LLVM IR (-O2):
define i32 @close_power_of_two(i32 %0, i32 %1) {
%3 = tail call i32 @pow(i32 2, i32 %0)
%4 = sdiv i32 %3, %1
%5 = tail call double @log(i32 %4)
%6 = tail call double @log(i32 2)
%7 = fdiv double %5, %6
%8 = fcmp ult double %7, 0.000000e+00
%9 = tail call i32 @pow(i32 2, i32 %0)
%10 = sdiv i32 %9, %1
%11 = tail call double @log(i32 %10)
%12 = tail call double @log(i32 2)
%13 = fdiv double %11, %12
%14 = select i1 %8, double -5.000000e-01, double 5.000000e-01
%15 = fadd double %13, %14
%16 = fptosi double %15 to i64
%17 = trunc i64 %16 to i32
%18 = tail call i32 @pow(i32 2, i32 %17)
%19 = mul nsw i32 %18, %1
%20 = tail call i32 @pow(i32 2, i32 %0)
%21 = icmp slt i32 %19, %20
%22 = zext i1 %21 to i32
%23 = add nsw i32 %22, %17
ret i32 %23
}