#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 */
int /*<<< orphan*/ error2(char *);
double factorial(int);
double log(double);
int /*<<< orphan*/ printf(char *, double);
int /*<<< orphan*/ puts(char *);
double LnGamma(double x) {
  double f = 0, fneg = 0, z, lng;
  int nx = (int)x;
  if ((double)nx == x && nx >= 0 && nx <= 11)
    lng = log(factorial(nx - 1));
  else {
    if (x <= 0) {
      printf("LnGamma(%.6f) not implemented", x);
      if ((int)x - x == 0) {
        puts("lnGamma undefined");
        return (-1);
      }
      for (fneg = 1; x < 0; x++)
        fneg /= x;
      if (fneg < 0)
        error2("strange!! check lngamma");
      fneg = log(fneg);
    }
    if (x < 7) {
      f = 1;
      z = x - 1;
      while (++z < 7)
        f *= z;
      x = z;
      f = -log(f);
    }
    z = 1 / (x * x);
    lng = fneg + f + (x - 0.5) * log(x) - x + .918938533204673 +
          (((-.000595238095238 * z + .000793650793651) * z - .002777777777778) *
               z +
           .083333333333333) /
              x;
  }
  return lng;
}


x86 assembly (clang, -O0):
<LnGamma>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movsd  %xmm0,-0x10(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x18(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
cvttsd2si -0x10(%rbp),%eax
mov    %eax,-0x34(%rbp)
cvtsi2sdl -0x34(%rbp),%xmm0
ucomisd -0x10(%rbp),%xmm0
jne    11b9 <LnGamma+0x69>
jp     11b9 <LnGamma+0x69>
cmpl   $0x0,-0x34(%rbp)
jl     11b9 <LnGamma+0x69>
cmpl   $0xb,-0x34(%rbp)
jg     11b9 <LnGamma+0x69>
mov    -0x34(%rbp),%edi
sub    $0x1,%edi
call   1040 <factorial@plt>
call   1070 <log@plt>
movsd  %xmm0,-0x30(%rbp)
jmp    13e8 <LnGamma+0x298>
xorps  %xmm0,%xmm0
ucomisd -0x10(%rbp),%xmm0
jb     1285 <LnGamma+0x135>
movsd  -0x10(%rbp),%xmm0
lea    0xe75(%rip),%rdi
call   1050 <printf@plt>
cvttsd2si -0x10(%rbp),%eax
cvtsi2sd %eax,%xmm0
subsd  -0x10(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    1217 <LnGamma+0xc7>
jp     1217 <LnGamma+0xc7>
lea    0xe66(%rip),%rdi
call   1030 <puts@plt>
movsd  0xe33(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    13f2 <LnGamma+0x2a2>
movsd  0xde1(%rip),%xmm0
movsd  %xmm0,-0x20(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x10(%rbp),%xmm0
jbe    125c <LnGamma+0x10c>
movsd  -0x10(%rbp),%xmm1
movsd  -0x20(%rbp),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  0xdb3(%rip),%xmm0
addsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
jmp    1224 <LnGamma+0xd4>
xorps  %xmm0,%xmm0
ucomisd -0x20(%rbp),%xmm0
jbe    1276 <LnGamma+0x126>
lea    0xe07(%rip),%rdi
call   1060 <error2@plt>
movsd  -0x20(%rbp),%xmm0
call   1070 <log@plt>
movsd  %xmm0,-0x20(%rbp)
movsd  0xd7b(%rip),%xmm0
ucomisd -0x10(%rbp),%xmm0
jbe    1323 <LnGamma+0x1d3>
movsd  0xd60(%rip),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  0xd4e(%rip),%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  0xd3d(%rip),%xmm1
addsd  -0x28(%rbp),%xmm1
movsd  %xmm1,-0x28(%rbp)
movsd  0xd33(%rip),%xmm0
ucomisd %xmm1,%xmm0
jbe    12f3 <LnGamma+0x1a3>
movsd  -0x28(%rbp),%xmm0
mulsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
jmp    12bb <LnGamma+0x16b>
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x18(%rbp),%xmm0
call   1070 <log@plt>
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x10(%rbp),%xmm1
mulsd  -0x10(%rbp),%xmm1
movsd  0xccb(%rip),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x20(%rbp),%xmm0
addsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  0xcde(%rip),%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x10(%rbp),%xmm0
call   1070 <log@plt>
movsd  -0x48(%rbp),%xmm1
movaps %xmm0,%xmm2
movsd  -0x40(%rbp),%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
subsd  -0x10(%rbp),%xmm0
movsd  0xca1(%rip),%xmm1
addsd  %xmm1,%xmm0
movsd  -0x28(%rbp),%xmm1
movsd  0xc80(%rip),%xmm2
mulsd  %xmm2,%xmm1
movsd  0xc7c(%rip),%xmm2
addsd  %xmm2,%xmm1
movsd  -0x28(%rbp),%xmm2
mulsd  %xmm2,%xmm1
movsd  0xc57(%rip),%xmm2
addsd  %xmm2,%xmm1
movsd  -0x28(%rbp),%xmm2
mulsd  %xmm2,%xmm1
movsd  0xc3a(%rip),%xmm2
addsd  %xmm2,%xmm1
divsd  -0x10(%rbp),%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<LnGamma>:
sub    $0x38,%rsp
movapd %xmm0,%xmm3
cvttsd2si %xmm0,%edi
cvttpd2dq %xmm0,%xmm0
cvtdq2pd %xmm0,%xmm1
cmp    $0xb,%edi
ja     1182 <LnGamma+0x32>
ucomisd %xmm3,%xmm1
jne    1182 <LnGamma+0x32>
jp     1182 <LnGamma+0x32>
add    $0xffffffff,%edi
call   1040 <factorial@plt>
add    $0x38,%rsp
jmp    1070 <log@plt>
xorpd  %xmm0,%xmm0
ucomisd %xmm3,%xmm0
jae    119a <LnGamma+0x4a>
xorpd  %xmm1,%xmm1
movsd  %xmm1,(%rsp)
jmp    1252 <LnGamma+0x102>
lea    0xebf(%rip),%rdi
movapd %xmm3,%xmm0
movapd %xmm3,0x10(%rsp)
movapd %xmm1,(%rsp)
call   1050 <printf@plt>
movapd 0x10(%rsp),%xmm2
movapd (%rsp),%xmm1
subsd  %xmm2,%xmm1
xorpd  %xmm0,%xmm0
ucomisd %xmm0,%xmm1
jne    11e9 <LnGamma+0x99>
jp     11e9 <LnGamma+0x99>
lea    0xea7(%rip),%rdi
call   1030 <puts@plt>
movsd  0xe24(%rip),%xmm0
add    $0x38,%rsp
ret
ucomisd %xmm2,%xmm0
jbe    1236 <LnGamma+0xe6>
movsd  0xe09(%rip),%xmm3
xorpd  %xmm1,%xmm1
movapd %xmm3,%xmm0
nop
divsd  %xmm2,%xmm0
addsd  %xmm3,%xmm2
ucomisd %xmm2,%xmm1
ja     1200 <LnGamma+0xb0>
movapd %xmm2,0x10(%rsp)
xorpd  %xmm1,%xmm1
ucomisd %xmm0,%xmm1
jbe    123e <LnGamma+0xee>
lea    0xe6b(%rip),%rdi
movsd  %xmm0,(%rsp)
call   1060 <error2@plt>
movsd  (%rsp),%xmm0
jmp    123e <LnGamma+0xee>
movsd  0xdc2(%rip),%xmm0
call   1070 <log@plt>
movsd  %xmm0,(%rsp)
movapd 0x10(%rsp),%xmm3
xorpd  %xmm0,%xmm0
movsd  0xdb6(%rip),%xmm1
ucomisd %xmm3,%xmm1
jbe    12b7 <LnGamma+0x167>
addsd  0xda0(%rip),%xmm3
movsd  0xd90(%rip),%xmm0
addsd  %xmm0,%xmm3
ucomisd %xmm3,%xmm1
jbe    129e <LnGamma+0x14e>
movsd  0xd7e(%rip),%xmm2
movapd %xmm2,%xmm0
cs nopw 0x0(%rax,%rax,1)
mulsd  %xmm3,%xmm0
addsd  %xmm2,%xmm3
ucomisd %xmm3,%xmm1
ja     1290 <LnGamma+0x140>
movapd %xmm3,0x10(%rsp)
call   1070 <log@plt>
movapd 0x10(%rsp),%xmm3
xorpd  0xd99(%rip),%xmm0
movapd %xmm3,0x10(%rsp)
movsd  (%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,(%rsp)
movapd %xmm3,%xmm0
mulsd  %xmm3,%xmm0
movsd  0xd25(%rip),%xmm1
divsd  %xmm0,%xmm1
movsd  %xmm1,0x30(%rsp)
movsd  0xd2b(%rip),%xmm0
addsd  %xmm3,%xmm0
movsd  %xmm0,0x28(%rsp)
movapd %xmm3,%xmm0
call   1070 <log@plt>
movapd %xmm0,%xmm1
mulsd  0x28(%rsp),%xmm1
addsd  (%rsp),%xmm1
movapd 0x10(%rsp),%xmm3
subsd  %xmm3,%xmm1
addsd  0xcff(%rip),%xmm1
movsd  0xcff(%rip),%xmm0
movsd  0x30(%rsp),%xmm2
mulsd  %xmm2,%xmm0
addsd  0xcf5(%rip),%xmm0
mulsd  %xmm2,%xmm0
addsd  0xcf1(%rip),%xmm0
mulsd  %xmm2,%xmm0
addsd  0xced(%rip),%xmm0
divsd  %xmm3,%xmm0
addsd  %xmm1,%xmm0
add    $0x38,%rsp
ret

LLVM IR (-O0):
@.str = [30 x i8] c"LnGamma(%.6f) not implemented\00"
@.str.1 = [18 x i8] c"lnGamma undefined\00"
@.str.2 = [24 x i8] c"strange!! check lngamma\00"
define double @LnGamma(double %0) {
%2 = alloca double
%3 = alloca double
%4 = alloca double
%5 = alloca double
%6 = alloca double
%7 = alloca double
%8 = alloca i32
store double %0, double* %3
store double 0.000000e+00, double* %4
store double 0.000000e+00, double* %5
%9 = load double, double* %3
%10 = fptosi double %9 to i32
store i32 %10, i32* %8
%11 = load i32, i32* %8
%12 = sitofp i32 %11 to double
%13 = load double, double* %3
%14 = fcmp oeq double %12, %13
br i1 %14, label %15, label %26
15: ; preds = %1
%16 = load i32, i32* %8
%17 = icmp sge i32 %16, 0
br i1 %17, label %18, label %26
18: ; preds = %15
%19 = load i32, i32* %8
%20 = icmp sle i32 %19, 11
br i1 %20, label %21, label %26
21: ; preds = %18
%22 = load i32, i32* %8
%23 = sub nsw i32 %22, 1
%24 = call double @factorial(i32 %23)
%25 = call double @log(double %24)
store double %25, double* %7
br label %103
26: ; preds = %18, %15, %1
%27 = load double, double* %3
%28 = fcmp ole double %27, 0.000000e+00
br i1 %28, label %29, label %59
29: ; preds = %26
%30 = load double, double* %3
%31 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), double %30)
%32 = load double, double* %3
%33 = fptosi double %32 to i32
%34 = sitofp i32 %33 to double
%35 = load double, double* %3
%36 = fsub double %34, %35
%37 = fcmp oeq double %36, 0.000000e+00
br i1 %37, label %38, label %40
38: ; preds = %29
%39 = call i32 @puts(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
store double -1.000000e+00, double* %2
br label %105
40: ; preds = %29
store double 1.000000e+00, double* %5
br label %41
41: ; preds = %48, %40
%42 = load double, double* %3
%43 = fcmp olt double %42, 0.000000e+00
br i1 %43, label %44, label %51
44: ; preds = %41
%45 = load double, double* %3
%46 = load double, double* %5
%47 = fdiv double %46, %45
store double %47, double* %5
br label %48
48: ; preds = %44
%49 = load double, double* %3
%50 = fadd double %49, 1.000000e+00
store double %50, double* %3
br label %41
51: ; preds = %41
%52 = load double, double* %5
%53 = fcmp olt double %52, 0.000000e+00
br i1 %53, label %54, label %56
54: ; preds = %51
%55 = call i32 @error2(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %56
56: ; preds = %54, %51
%57 = load double, double* %5
%58 = call double @log(double %57)
store double %58, double* %5
br label %59
59: ; preds = %56, %26
%60 = load double, double* %3
%61 = fcmp olt double %60, 7.000000e+00
br i1 %61, label %62, label %78
62: ; preds = %59
store double 1.000000e+00, double* %4
%63 = load double, double* %3
%64 = fsub double %63, 1.000000e+00
store double %64, double* %6
br label %65
65: ; preds = %69, %62
%66 = load double, double* %6
%67 = fadd double %66, 1.000000e+00
store double %67, double* %6
%68 = fcmp olt double %67, 7.000000e+00
br i1 %68, label %69, label %73
69: ; preds = %65
%70 = load double, double* %6
%71 = load double, double* %4
%72 = fmul double %71, %70
store double %72, double* %4
br label %65
73: ; preds = %65
%74 = load double, double* %6
store double %74, double* %3
%75 = load double, double* %4
%76 = call double @log(double %75)
%77 = fneg double %76
store double %77, double* %4
br label %78
78: ; preds = %73, %59
%79 = load double, double* %3
%80 = load double, double* %3
%81 = fmul double %79, %80
%82 = fdiv double 1.000000e+00, %81
store double %82, double* %6
%83 = load double, double* %5
%84 = load double, double* %4
%85 = fadd double %83, %84
%86 = load double, double* %3
%87 = fsub double %86, 5.000000e-01
%88 = load double, double* %3
%89 = call double @log(double %88)
%90 = call double @llvm.fmuladd.f64(double %87, double %89, double %85)
%91 = load double, double* %3
%92 = fsub double %90, %91
%93 = fadd double %92, 0x3FED67F1C864BEB7
%94 = load double, double* %6
%95 = call double @llvm.fmuladd.f64(double 0xBF438138138134A5, double %94, double 0x3F4A01A01A01A789)
%96 = load double, double* %6
%97 = call double @llvm.fmuladd.f64(double %95, double %96, double 0xBF66C16C16C16E17)
%98 = load double, double* %6
%99 = call double @llvm.fmuladd.f64(double %97, double %98, double 0x3FB555555555553D)
%100 = load double, double* %3
%101 = fdiv double %99, %100
%102 = fadd double %93, %101
store double %102, double* %7
br label %103
103: ; preds = %78, %21
%104 = load double, double* %7
store double %104, double* %2
br label %105
105: ; preds = %103, %38
%106 = load double, double* %2
ret double %106
}

LLVM IR (-O2):
@.str = [30 x i8] c"LnGamma(%.6f) not implemented\00"
@.str.1 = [18 x i8] c"lnGamma undefined\00"
@.str.2 = [24 x i8] c"strange!! check lngamma\00"
define double @LnGamma(double %0) {
%2 = fptosi double %0 to i32
%3 = sitofp i32 %2 to double
%4 = fcmp oeq double %3, %0
%5 = icmp ult i32 %2, 12
%6 = and i1 %5, %4
br i1 %6, label %7, label %11
7: ; preds = %1
%8 = add nsw i32 %2, -1
%9 = tail call double @factorial(i32 %8)
%10 = tail call double @log(double %9)
br label %70
11: ; preds = %1
%12 = fcmp ugt double %0, 0.000000e+00
br i1 %12, label %35, label %13
13: ; preds = %11
%14 = tail call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), double %0)
%15 = fsub double %3, %0
%16 = fcmp oeq double %15, 0.000000e+00
br i1 %16, label %19, label %17
17: ; preds = %13
%18 = fcmp olt double %0, 0.000000e+00
br i1 %18, label %21, label %31
19: ; preds = %13
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %70
21: ; preds = %17, %21
%22 = phi double [ %24, %21 ], [ 1.000000e+00, %17 ]
%23 = phi double [ %25, %21 ], [ %0, %17 ]
%24 = fdiv double %22, %23
%25 = fadd double %23, 1.000000e+00
%26 = fcmp olt double %25, 0.000000e+00
br i1 %26, label %21, label %27
27: ; preds = %21
%28 = fcmp olt double %24, 0.000000e+00
br i1 %28, label %29, label %31
29: ; preds = %27
%30 = tail call i32 @error2(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %31
31: ; preds = %17, %29, %27
%32 = phi double [ %24, %29 ], [ %24, %27 ], [ 1.000000e+00, %17 ]
%33 = phi double [ %25, %29 ], [ %25, %27 ], [ %0, %17 ]
%34 = tail call double @log(double %32)
br label %35
35: ; preds = %31, %11
%36 = phi double [ %33, %31 ], [ %0, %11 ]
%37 = phi double [ %34, %31 ], [ 0.000000e+00, %11 ]
%38 = fcmp olt double %36, 7.000000e+00
br i1 %38, label %39, label %54
39: ; preds = %35
%40 = fadd double %36, -1.000000e+00
%41 = fadd double %40, 1.000000e+00
%42 = fcmp olt double %41, 7.000000e+00
br i1 %42, label %43, label %49
43: ; preds = %39, %43
%44 = phi double [ %47, %43 ], [ %41, %39 ]
%45 = phi double [ %46, %43 ], [ 1.000000e+00, %39 ]
%46 = fmul double %45, %44
%47 = fadd double %44, 1.000000e+00
%48 = fcmp olt double %47, 7.000000e+00
br i1 %48, label %43, label %49
49: ; preds = %43, %39
%50 = phi double [ 1.000000e+00, %39 ], [ %46, %43 ]
%51 = phi double [ %41, %39 ], [ %47, %43 ]
%52 = tail call double @log(double %50)
%53 = fneg double %52
br label %54
54: ; preds = %49, %35
%55 = phi double [ %51, %49 ], [ %36, %35 ]
%56 = phi double [ %53, %49 ], [ 0.000000e+00, %35 ]
%57 = fmul double %55, %55
%58 = fdiv double 1.000000e+00, %57
%59 = fadd double %37, %56
%60 = fadd double %55, -5.000000e-01
%61 = tail call double @log(double %55)
%62 = tail call double @llvm.fmuladd.f64(double %60, double %61, double %59)
%63 = fsub double %62, %55
%64 = fadd double %63, 0x3FED67F1C864BEB7
%65 = tail call double @llvm.fmuladd.f64(double %58, double 0xBF438138138134A5, double 0x3F4A01A01A01A789)
%66 = tail call double @llvm.fmuladd.f64(double %65, double %58, double 0xBF66C16C16C16E17)
%67 = tail call double @llvm.fmuladd.f64(double %66, double %58, double 0x3FB555555555553D)
%68 = fdiv double %67, %55
%69 = fadd double %64, %68
br label %70
70: ; preds = %7, %54, %19
%71 = phi double [ -1.000000e+00, %19 ], [ %10, %7 ], [ %69, %54 ]
ret double %71
}