#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 pow(double, int);
double log10(double x) {
  int i = 0;
  if (x < 0)
    return -1;
  else {
    double sn = 0;
    while (sn != x) {
      if (i >= (int)x)
        break;
      sn = pow(10.0, i);
      i++;
    }
  }
  return i - 1;
}


x86 assembly (clang, -O0):
<log10>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movsd  %xmm0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x10(%rbp),%xmm0
jbe    1144 <log10+0x34>
movsd  0xece(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11b3 <log10+0xa3>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
ucomisd -0x10(%rbp),%xmm0
jne    1167 <log10+0x57>
jp     1167 <log10+0x57>
jmp    119f <log10+0x8f>
mov    -0x14(%rbp),%eax
cvttsd2si -0x10(%rbp),%ecx
cmp    %ecx,%eax
jl     117c <log10+0x6c>
jmp    119f <log10+0x8f>
mov    -0x14(%rbp),%edi
movsd  0xe79(%rip),%xmm0
call   1030 <pow@plt>
movsd  %xmm0,-0x20(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    114c <log10+0x3c>
jmp    11a4 <log10+0x94>
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<log10>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
movapd %xmm0,%xmm1
xorpd  %xmm0,%xmm0
ucomisd %xmm1,%xmm0
jbe    1130 <log10+0x20>
movsd  0xed2(%rip),%xmm0
jmp    1191 <log10+0x81>
ucomisd %xmm0,%xmm1
mov    $0xffffffff,%ebx
jne    113d <log10+0x2d>
jnp    118a <log10+0x7a>
cvttsd2si %xmm1,%r14d
test   %r14d,%r14d
jle    118a <log10+0x7a>
mov    $0xffffffff,%ebp
movsd  %xmm1,0x8(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
lea    0x1(%rbp),%ebx
movsd  0xe9d(%rip),%xmm0
mov    %ebx,%edi
call   1030 <pow@plt>
movsd  0x8(%rsp),%xmm1
ucomisd %xmm1,%xmm0
jne    1180 <log10+0x70>
jnp    118a <log10+0x7a>
add    $0x2,%ebp
cmp    %r14d,%ebp
mov    %ebx,%ebp
jl     1160 <log10+0x50>
xorps  %xmm0,%xmm0
cvtsi2sd %ebx,%xmm0
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define double @log10(double %0) {
%2 = alloca double
%3 = alloca double
%4 = alloca i32
%5 = alloca double
store double %0, double* %3
store i32 0, i32* %4
%6 = load double, double* %3
%7 = fcmp olt double %6, 0.000000e+00
br i1 %7, label %8, label %9
8: ; preds = %1
store double -1.000000e+00, double* %2
br label %30
9: ; preds = %1
store double 0.000000e+00, double* %5
br label %10
10: ; preds = %20, %9
%11 = load double, double* %5
%12 = load double, double* %3
%13 = fcmp une double %11, %12
br i1 %13, label %14, label %25
14: ; preds = %10
%15 = load i32, i32* %4
%16 = load double, double* %3
%17 = fptosi double %16 to i32
%18 = icmp sge i32 %15, %17
br i1 %18, label %19, label %20
19: ; preds = %14
br label %25
20: ; preds = %14
%21 = load i32, i32* %4
%22 = call double @pow(double 1.000000e+01, i32 %21)
store double %22, double* %5
%23 = load i32, i32* %4
%24 = add nsw i32 %23, 1
store i32 %24, i32* %4
br label %10
25: ; preds = %19, %10
br label %26
26: ; preds = %25
%27 = load i32, i32* %4
%28 = sub nsw i32 %27, 1
%29 = sitofp i32 %28 to double
store double %29, double* %2
br label %30
30: ; preds = %26, %8
%31 = load double, double* %2
ret double %31
}

LLVM IR (-O2):
define double @log10(double %0) {
%2 = fcmp olt double %0, 0.000000e+00
br i1 %2, label %18, label %3
3: ; preds = %1
%4 = fptosi double %0 to i32
%5 = fcmp une double %0, 0.000000e+00
%6 = icmp sgt i32 %4, 0
%7 = select i1 %5, i1 %6, i1 false
br i1 %7, label %8, label %15
8: ; preds = %3, %8
%9 = phi i32 [ %11, %8 ], [ 0, %3 ]
%10 = tail call double @pow(double 1.000000e+01, i32 %9)
%11 = add nuw nsw i32 %9, 1
%12 = fcmp une double %10, %0
%13 = icmp slt i32 %11, %4
%14 = select i1 %12, i1 %13, i1 false
br i1 %14, label %8, label %15
15: ; preds = %8, %3
%16 = phi i32 [ -1, %3 ], [ %9, %8 ]
%17 = sitofp i32 %16 to double
br label %18
18: ; preds = %1, %15
%19 = phi double [ %17, %15 ], [ -1.000000e+00, %1 ]
ret double %19
}