#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 to_power(unsigned int, int);
int ceil_log(unsigned base, double d) {
  int log;
  d = (d < 0.0) ? -d : d;
  if (d < 1.0 && d >= 0.5) {
    return 0;
  }
  if (d > 0.5) {
    for (log = 1; to_power(base, log) < d; ++log)
      ;
    return log;
  }
  for (log = 0; d < 0.5; --log) {
    d *= base;
  }
  return log;
}


x86 assembly (clang, -O0):
<ceil_log>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movsd  %xmm0,-0x10(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x10(%rbp),%xmm0
jbe    1154 <ceil_log+0x44>
movsd  -0x10(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
movsd  %xmm0,-0x20(%rbp)
jmp    115e <ceil_log+0x4e>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  0xe90(%rip),%xmm0
ucomisd -0x10(%rbp),%xmm0
jbe    119e <ceil_log+0x8e>
movsd  -0x10(%rbp),%xmm0
movsd  0xe80(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     119e <ceil_log+0x8e>
movl   $0x0,-0x4(%rbp)
jmp    1237 <ceil_log+0x127>
movsd  -0x10(%rbp),%xmm0
movsd  0xe5d(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    11f7 <ceil_log+0xe7>
movl   $0x1,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1030 <to_power@plt>
movaps %xmm0,%xmm1
movsd  -0x10(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    11ec <ceil_log+0xdc>
jmp    11de <ceil_log+0xce>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11bc <ceil_log+0xac>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1237 <ceil_log+0x127>
movl   $0x0,-0x14(%rbp)
movsd  0xe02(%rip),%xmm0
ucomisd -0x10(%rbp),%xmm0
jbe    1231 <ceil_log+0x121>
mov    -0x8(%rbp),%eax
cvtsi2sd %rax,%xmm0
mulsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    11fe <ceil_log+0xee>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ceil_log>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebp
movapd 0xee0(%rip),%xmm1
xorpd  %xmm0,%xmm1
xorpd  %xmm2,%xmm2
movapd %xmm0,%xmm3
cmpltsd %xmm2,%xmm3
andpd  %xmm3,%xmm1
andnpd %xmm0,%xmm3
orpd   %xmm1,%xmm3
xor    %ebx,%ebx
movsd  0xec9(%rip),%xmm0
ucomisd %xmm3,%xmm0
jbe    1157 <ceil_log+0x47>
ucomisd 0xec3(%rip),%xmm3
jae    11bd <ceil_log+0xad>
ucomisd 0xeb9(%rip),%xmm3
jbe    1189 <ceil_log+0x79>
xor    %ebx,%ebx
movapd %xmm3,(%rsp)
nopl   0x0(%rax,%rax,1)
add    $0x1,%ebx
mov    %ebp,%edi
mov    %ebx,%esi
call   1030 <to_power@plt>
movapd (%rsp),%xmm3
ucomisd %xmm0,%xmm3
ja     1170 <ceil_log+0x60>
jmp    11bd <ceil_log+0xad>
movsd  0xe87(%rip),%xmm0
ucomisd %xmm3,%xmm0
jbe    11bd <ceil_log+0xad>
mov    %ebp,%eax
xorps  %xmm1,%xmm1
cvtsi2sd %rax,%xmm1
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mulsd  %xmm1,%xmm3
add    $0xffffffff,%ebx
ucomisd %xmm3,%xmm0
ja     11b0 <ceil_log+0xa0>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @ceil_log(i32 %0, double %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca double
%6 = alloca i32
store i32 %0, i32* %4
store double %1, double* %5
%7 = load double, double* %5
%8 = fcmp olt double %7, 0.000000e+00
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load double, double* %5
%11 = fneg double %10
br label %14
12: ; preds = %2
%13 = load double, double* %5
br label %14
14: ; preds = %12, %9
%15 = phi double [ %11, %9 ], [ %13, %12 ]
store double %15, double* %5
%16 = load double, double* %5
%17 = fcmp olt double %16, 1.000000e+00
br i1 %17, label %18, label %22
18: ; preds = %14
%19 = load double, double* %5
%20 = fcmp oge double %19, 5.000000e-01
br i1 %20, label %21, label %22
21: ; preds = %18
store i32 0, i32* %3
br label %52
22: ; preds = %18, %14
%23 = load double, double* %5
%24 = fcmp ogt double %23, 5.000000e-01
br i1 %24, label %25, label %38
25: ; preds = %22
store i32 1, i32* %6
br label %26
26: ; preds = %33, %25
%27 = load i32, i32* %4
%28 = load i32, i32* %6
%29 = call double @to_power(i32 %27, i32 %28)
%30 = load double, double* %5
%31 = fcmp olt double %29, %30
br i1 %31, label %32, label %36
32: ; preds = %26
br label %33
33: ; preds = %32
%34 = load i32, i32* %6
%35 = add nsw i32 %34, 1
store i32 %35, i32* %6
br label %26
36: ; preds = %26
%37 = load i32, i32* %6
store i32 %37, i32* %3
br label %52
38: ; preds = %22
store i32 0, i32* %6
br label %39
39: ; preds = %47, %38
%40 = load double, double* %5
%41 = fcmp olt double %40, 5.000000e-01
br i1 %41, label %42, label %50
42: ; preds = %39
%43 = load i32, i32* %4
%44 = uitofp i32 %43 to double
%45 = load double, double* %5
%46 = fmul double %45, %44
store double %46, double* %5
br label %47
47: ; preds = %42
%48 = load i32, i32* %6
%49 = add nsw i32 %48, -1
store i32 %49, i32* %6
br label %39
50: ; preds = %39
%51 = load i32, i32* %6
store i32 %51, i32* %3
br label %52
52: ; preds = %50, %36, %21
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
define i32 @ceil_log(i32 %0, double %1) {
%3 = fcmp olt double %1, 0.000000e+00
%4 = fneg double %1
%5 = select i1 %3, double %4, double %1
%6 = fcmp olt double %5, 1.000000e+00
%7 = fcmp oge double %5, 5.000000e-01
%8 = and i1 %6, %7
br i1 %8, label %26, label %9
9: ; preds = %2
%10 = fcmp ogt double %5, 5.000000e-01
br i1 %10, label %15, label %11
11: ; preds = %9
%12 = fcmp olt double %5, 5.000000e-01
br i1 %12, label %13, label %26
13: ; preds = %11
%14 = uitofp i32 %0 to double
br label %20
15: ; preds = %9, %15
%16 = phi i32 [ %19, %15 ], [ 1, %9 ]
%17 = tail call double @to_power(i32 %0, i32 %16)
%18 = fcmp olt double %17, %5
%19 = add nuw nsw i32 %16, 1
br i1 %18, label %15, label %26
20: ; preds = %13, %20
%21 = phi i32 [ 0, %13 ], [ %24, %20 ]
%22 = phi double [ %5, %13 ], [ %23, %20 ]
%23 = fmul double %22, %14
%24 = add nsw i32 %21, -1
%25 = fcmp olt double %23, 5.000000e-01
br i1 %25, label %20, label %26
26: ; preds = %20, %15, %11, %2
%27 = phi i32 [ 0, %2 ], [ 0, %11 ], [ %16, %15 ], [ %24, %20 ]
ret i32 %27
}