#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 INFINITY;
double NAN;
double fabs(double);
scalar_t__ isinf(double);
scalar_t__ isnan(double);
double sd2eps(unsigned int);
double logex(double num, unsigned int sigdig) {
  if (sigdig < 1 || num < 0) {
    return NAN;
  }
  if (num == 0) {
    return INFINITY;
  }
  if (isnan(num) || isinf(num)) {
    return num;
  }
  double f = (num - 1) / (num + 1);
  double f2 = f * f;
  double sum = f;
  double presult = 0;
  double result = 2 * f;
  double k = 1;
  double eps = sd2eps(sigdig + 1);
  while (fabs(result - presult) > (presult * eps)) {
    k += 2;
    f *= f2;
    sum += f / k;
    presult = result;
    result = 2 * sum;
  }
  return result;
}


x86 assembly (clang, -O0):
<logex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movsd  %xmm0,-0x10(%rbp)
mov    %edi,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jb     1158 <logex+0x28>
xorps  %xmm0,%xmm0
ucomisd -0x10(%rbp),%xmm0
jbe    116d <logex+0x3d>
mov    0x2e99(%rip),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    12d1 <logex+0x1a1>
movsd  -0x10(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    119a <logex+0x6a>
jp     119a <logex+0x6a>
mov    0x2e54(%rip),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    12d1 <logex+0x1a1>
movsd  -0x10(%rbp),%xmm0
call   1040 <isnan@plt>
cmp    $0x0,%rax
jne    11c2 <logex+0x92>
movsd  -0x10(%rbp),%xmm0
call   1050 <isinf@plt>
cmp    $0x0,%rax
je     11d1 <logex+0xa1>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    12d1 <logex+0x1a1>
movsd  -0x10(%rbp),%xmm0
movsd  0xe22(%rip),%xmm1
subsd  %xmm1,%xmm0
movsd  0xe16(%rip),%xmm1
addsd  -0x10(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
mulsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  0xde7(%rip),%xmm0
mulsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  0xdcd(%rip),%xmm0
movsd  %xmm0,-0x48(%rbp)
mov    -0x14(%rbp),%edi
add    $0x1,%edi
call   1030 <sd2eps@plt>
movsd  %xmm0,-0x50(%rbp)
movsd  -0x40(%rbp),%xmm0
subsd  -0x38(%rbp),%xmm0
movaps 0xdb7(%rip),%xmm1
pand   %xmm1,%xmm0
movsd  -0x38(%rbp),%xmm1
mulsd  -0x50(%rbp),%xmm1
ucomisd %xmm1,%xmm0
jbe    12c7 <logex+0x197>
movsd  0xd8f(%rip),%xmm0
addsd  -0x48(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x28(%rbp),%xmm0
mulsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
divsd  -0x48(%rbp),%xmm0
addsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  0xd50(%rip),%xmm0
mulsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
jmp    1248 <logex+0x118>
movsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<logex>:
push   %rbx
sub    $0x10,%rsp
xorpd  %xmm1,%xmm1
ucomisd %xmm0,%xmm1
ja     115a <logex+0x2a>
mov    %edi,%ebx
test   %edi,%edi
je     115a <logex+0x2a>
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    116f <logex+0x3f>
jp     116f <logex+0x3f>
mov    0x2e88(%rip),%rax
jmp    1161 <logex+0x31>
mov    0x2e97(%rip),%rax
movsd  (%rax),%xmm1
movapd %xmm1,%xmm0
add    $0x10,%rsp
pop    %rbx
ret
movsd  %xmm0,0x8(%rsp)
call   1040 <isnan@plt>
test   %rax,%rax
jne    118f <logex+0x5f>
movsd  0x8(%rsp),%xmm0
call   1050 <isinf@plt>
test   %rax,%rax
je     1197 <logex+0x67>
movsd  0x8(%rsp),%xmm1
jmp    1165 <logex+0x35>
movsd  0xe61(%rip),%xmm1
movsd  0x8(%rsp),%xmm0
addsd  %xmm0,%xmm1
addsd  0xe57(%rip),%xmm0
divsd  %xmm0,%xmm1
movsd  %xmm1,0x8(%rsp)
add    $0x1,%ebx
mov    %ebx,%edi
call   1030 <sd2eps@plt>
movsd  0x8(%rsp),%xmm2
movapd %xmm2,%xmm4
addsd  %xmm2,%xmm4
movapd 0xe45(%rip),%xmm1
andpd  %xmm4,%xmm1
xorpd  %xmm3,%xmm3
mulsd  %xmm0,%xmm3
ucomisd %xmm3,%xmm1
jbe    125d <logex+0x12d>
movapd %xmm2,%xmm5
mulsd  %xmm2,%xmm5
movsd  0xe0b(%rip),%xmm3
movsd  0xe0a(%rip),%xmm8
movapd 0xe11(%rip),%xmm9
movapd %xmm2,%xmm6
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mulsd  %xmm5,%xmm6
addsd  %xmm8,%xmm3
movapd %xmm6,%xmm7
divsd  %xmm3,%xmm7
addsd  %xmm7,%xmm2
movapd %xmm2,%xmm1
addsd  %xmm2,%xmm1
movapd %xmm1,%xmm7
subsd  %xmm4,%xmm7
andpd  %xmm9,%xmm7
mulsd  %xmm0,%xmm4
ucomisd %xmm4,%xmm7
movapd %xmm1,%xmm4
ja     1220 <logex+0xf0>
jmp    1165 <logex+0x35>
movapd %xmm4,%xmm1
jmp    1165 <logex+0x35>

LLVM IR (-O0):
@NAN = global double 0.000000e+00
@INFINITY = global double 0.000000e+00
define double @logex(double %0, i32 %1) {
%3 = alloca double
%4 = alloca double
%5 = alloca i32
%6 = alloca double
%7 = alloca double
%8 = alloca double
%9 = alloca double
%10 = alloca double
%11 = alloca double
%12 = alloca double
store double %0, double* %4
store i32 %1, i32* %5
%13 = load i32, i32* %5
%14 = icmp ult i32 %13, 1
br i1 %14, label %18, label %15
15: ; preds = %2
%16 = load double, double* %4
%17 = fcmp olt double %16, 0.000000e+00
br i1 %17, label %18, label %20
18: ; preds = %15, %2
%19 = load double, double* @NAN
store double %19, double* %3
br label %75
20: ; preds = %15
%21 = load double, double* %4
%22 = fcmp oeq double %21, 0.000000e+00
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = load double, double* @INFINITY
store double %24, double* %3
br label %75
25: ; preds = %20
%26 = load double, double* %4
%27 = call i64 @isnan(double %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %33, label %29
29: ; preds = %25
%30 = load double, double* %4
%31 = call i64 @isinf(double %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %29, %25
%34 = load double, double* %4
store double %34, double* %3
br label %75
35: ; preds = %29
%36 = load double, double* %4
%37 = fsub double %36, 1.000000e+00
%38 = load double, double* %4
%39 = fadd double %38, 1.000000e+00
%40 = fdiv double %37, %39
store double %40, double* %6
%41 = load double, double* %6
%42 = load double, double* %6
%43 = fmul double %41, %42
store double %43, double* %7
%44 = load double, double* %6
store double %44, double* %8
store double 0.000000e+00, double* %9
%45 = load double, double* %6
%46 = fmul double 2.000000e+00, %45
store double %46, double* %10
store double 1.000000e+00, double* %11
%47 = load i32, i32* %5
%48 = add i32 %47, 1
%49 = call double @sd2eps(i32 %48)
store double %49, double* %12
br label %50
50: ; preds = %59, %35
%51 = load double, double* %10
%52 = load double, double* %9
%53 = fsub double %51, %52
%54 = call double @llvm.fabs.f64(double %53)
%55 = load double, double* %9
%56 = load double, double* %12
%57 = fmul double %55, %56
%58 = fcmp ogt double %54, %57
br i1 %58, label %59, label %73
59: ; preds = %50
%60 = load double, double* %11
%61 = fadd double %60, 2.000000e+00
store double %61, double* %11
%62 = load double, double* %7
%63 = load double, double* %6
%64 = fmul double %63, %62
store double %64, double* %6
%65 = load double, double* %6
%66 = load double, double* %11
%67 = fdiv double %65, %66
%68 = load double, double* %8
%69 = fadd double %68, %67
store double %69, double* %8
%70 = load double, double* %10
store double %70, double* %9
%71 = load double, double* %8
%72 = fmul double 2.000000e+00, %71
store double %72, double* %10
br label %50
73: ; preds = %50
%74 = load double, double* %10
store double %74, double* %3
br label %75
75: ; preds = %73, %33, %23, %18
%76 = load double, double* %3
ret double %76
}

LLVM IR (-O2):
@NAN = global double 0.000000e+00
@INFINITY = global double 0.000000e+00
define double @logex(double %0, i32 %1) {
%3 = icmp eq i32 %1, 0
%4 = fcmp olt double %0, 0.000000e+00
%5 = or i1 %4, %3
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = load double, double* @NAN
br label %43
8: ; preds = %2
%9 = fcmp oeq double %0, 0.000000e+00
br i1 %9, label %10, label %12
10: ; preds = %8
%11 = load double, double* @INFINITY
br label %43
12: ; preds = %8
%13 = tail call i64 @isnan(double %0)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %43
15: ; preds = %12
%16 = tail call i64 @isinf(double %0)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %43
18: ; preds = %15
%19 = fadd double %0, -1.000000e+00
%20 = fadd double %0, 1.000000e+00
%21 = fdiv double %19, %20
%22 = fmul double %21, %21
%23 = add i32 %1, 1
%24 = tail call double @sd2eps(i32 %23)
%25 = fmul double %21, 2.000000e+00
%26 = tail call double @llvm.fabs.f64(double %25)
%27 = fmul double %24, 0.000000e+00
%28 = fcmp ogt double %26, %27
br i1 %28, label %29, label %43
29: ; preds = %18, %29
%30 = phi double [ %38, %29 ], [ %25, %18 ]
%31 = phi double [ %34, %29 ], [ 1.000000e+00, %18 ]
%32 = phi double [ %37, %29 ], [ %21, %18 ]
%33 = phi double [ %35, %29 ], [ %21, %18 ]
%34 = fadd double %31, 2.000000e+00
%35 = fmul double %22, %33
%36 = fdiv double %35, %34
%37 = fadd double %32, %36
%38 = fmul double %37, 2.000000e+00
%39 = fsub double %38, %30
%40 = tail call double @llvm.fabs.f64(double %39)
%41 = fmul double %24, %30
%42 = fcmp ogt double %40, %41
br i1 %42, label %29, label %43
43: ; preds = %29, %18, %12, %15, %10, %6
%44 = phi double [ %7, %6 ], [ %11, %10 ], [ %0, %15 ], [ %0, %12 ], [ %25, %18 ], [ %38, %29 ]
ret double %44
}