#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*/ dump_error(char *);
double exactfactln(int);
double gammln(int);
double factln(int n) {
  static double a[10001];
  if (n < 0)
    dump_error("Negative factorial in routine FACTLN");
  if (n <= 1)
    return 0.0;
  if (n <= 40)
    return a[n] ? a[n] : (a[n] = exactfactln(n));
  if (n <= 10000)
    return a[n] ? a[n] : (a[n] = gammln(n + 1.0));
  else
    return gammln(n + 1.0);
}


x86 assembly (clang, -O0):
<factln>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1151 <factln+0x21>
lea    0xebc(%rip),%rdi
call   1030 <dump_error@plt>
cmpl   $0x1,-0xc(%rbp)
jg     1168 <factln+0x38>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    128c <factln+0x15c>
cmpl   $0x28,-0xc(%rbp)
jg     11e0 <factln+0xb0>
movslq -0xc(%rbp),%rcx
lea    0x2ed3(%rip),%rax
movsd  (%rax,%rcx,8),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    119a <factln+0x6a>
jp     119a <factln+0x6a>
jmp    11b4 <factln+0x84>
movslq -0xc(%rbp),%rcx
lea    0x2eab(%rip),%rax
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x18(%rbp)
jmp    11d1 <factln+0xa1>
mov    -0xc(%rbp),%edi
call   1040 <exactfactln@plt>
movslq -0xc(%rbp),%rcx
lea    0x2e89(%rip),%rax
movsd  %xmm0,(%rax,%rcx,8)
movsd  %xmm0,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    128c <factln+0x15c>
cmpl   $0x2710,-0xc(%rbp)
jg     126d <factln+0x13d>
movslq -0xc(%rbp),%rcx
lea    0x2e58(%rip),%rax
movsd  (%rax,%rcx,8),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    1215 <factln+0xe5>
jp     1215 <factln+0xe5>
jmp    122f <factln+0xff>
movslq -0xc(%rbp),%rcx
lea    0x2e30(%rip),%rax
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x20(%rbp)
jmp    125e <factln+0x12e>
cvtsi2sdl -0xc(%rbp),%xmm0
movsd  0xdc4(%rip),%xmm1
addsd  %xmm1,%xmm0
cvttsd2si %xmm0,%edi
call   1050 <gammln@plt>
movslq -0xc(%rbp),%rcx
lea    0x2dfc(%rip),%rax
movsd  %xmm0,(%rax,%rcx,8)
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    128c <factln+0x15c>
cvtsi2sdl -0xc(%rbp),%xmm0
movsd  0xd86(%rip),%xmm1
addsd  %xmm1,%xmm0
cvttsd2si %xmm0,%edi
call   1050 <gammln@plt>
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<factln>:
push   %r14
push   %rbx
push   %rax
test   %edi,%edi
js     1168 <factln+0x38>
xorpd  %xmm0,%xmm0
cmp    $0x2,%edi
jb     1178 <factln+0x48>
cmp    $0x28,%edi
ja     1180 <factln+0x50>
mov    %edi,%ebx
lea    0x2f01(%rip),%r14
movsd  (%r14,%rbx,8),%xmm0
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    1178 <factln+0x48>
jp     1178 <factln+0x48>
call   1040 <exactfactln@plt>
jmp    11ab <factln+0x7b>
lea    0xe91(%rip),%rdi
call   1030 <dump_error@plt>
xorpd  %xmm0,%xmm0
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
cmp    $0x2710,%edi
ja     11b3 <factln+0x83>
mov    %edi,%ebx
lea    0x2ebf(%rip),%r14
movsd  (%r14,%rbx,8),%xmm0
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    1178 <factln+0x48>
jp     1178 <factln+0x48>
add    $0x1,%edi
call   1050 <gammln@plt>
movsd  %xmm0,(%r14,%rbx,8)
jmp    1178 <factln+0x48>
add    $0x1,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1050 <gammln@plt>

LLVM IR (-O0):
@factln.a = internal global [10001 x double] zeroinitializer
@.str = [37 x i8] c"Negative factorial in routine FACTLN\00"
define double @factln(i32 %0) {
%2 = alloca double
%3 = alloca i32
store i32 %0, i32* %3
%4 = load i32, i32* %3
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = call i32 @dump_error(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %1
%9 = load i32, i32* %3
%10 = icmp sle i32 %9, 1
br i1 %10, label %11, label %12
11: ; preds = %8
store double 0.000000e+00, double* %2
br label %65
12: ; preds = %8
%13 = load i32, i32* %3
%14 = icmp sle i32 %13, 40
br i1 %14, label %15, label %34
15: ; preds = %12
%16 = load i32, i32* %3
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds [10001 x double], [10001 x double]* @factln.a, i64 0, i64 %17
%19 = load double, double* %18
%20 = fcmp une double %19, 0.000000e+00
br i1 %20, label %21, label %26
21: ; preds = %15
%22 = load i32, i32* %3
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds [10001 x double], [10001 x double]* @factln.a, i64 0, i64 %23
%25 = load double, double* %24
br label %32
26: ; preds = %15
%27 = load i32, i32* %3
%28 = call double @exactfactln(i32 %27)
%29 = load i32, i32* %3
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [10001 x double], [10001 x double]* @factln.a, i64 0, i64 %30
store double %28, double* %31
br label %32
32: ; preds = %26, %21
%33 = phi double [ %25, %21 ], [ %28, %26 ]
store double %33, double* %2
br label %65
34: ; preds = %12
%35 = load i32, i32* %3
%36 = icmp sle i32 %35, 10000
br i1 %36, label %37, label %59
37: ; preds = %34
%38 = load i32, i32* %3
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [10001 x double], [10001 x double]* @factln.a, i64 0, i64 %39
%41 = load double, double* %40
%42 = fcmp une double %41, 0.000000e+00
br i1 %42, label %43, label %48
43: ; preds = %37
%44 = load i32, i32* %3
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds [10001 x double], [10001 x double]* @factln.a, i64 0, i64 %45
%47 = load double, double* %46
br label %57
48: ; preds = %37
%49 = load i32, i32* %3
%50 = sitofp i32 %49 to double
%51 = fadd double %50, 1.000000e+00
%52 = fptosi double %51 to i32
%53 = call double @gammln(i32 %52)
%54 = load i32, i32* %3
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds [10001 x double], [10001 x double]* @factln.a, i64 0, i64 %55
store double %53, double* %56
br label %57
57: ; preds = %48, %43
%58 = phi double [ %47, %43 ], [ %53, %48 ]
store double %58, double* %2
br label %65
59: ; preds = %34
%60 = load i32, i32* %3
%61 = sitofp i32 %60 to double
%62 = fadd double %61, 1.000000e+00
%63 = fptosi double %62 to i32
%64 = call double @gammln(i32 %63)
store double %64, double* %2
br label %65
65: ; preds = %59, %57, %32, %11
%66 = load double, double* %2
ret double %66
}

LLVM IR (-O2):
@factln.a = internal unnamed_addr global [10001 x double] zeroinitializer
@.str = [37 x i8] c"Negative factorial in routine FACTLN\00"
define double @factln(i32 %0) {
%2 = icmp slt i32 %0, 0
br i1 %2, label %3, label %5
3: ; preds = %1
%4 = tail call i32 @dump_error(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
br label %29
5: ; preds = %1
%6 = icmp ult i32 %0, 2
br i1 %6, label %29, label %7
7: ; preds = %5
%8 = icmp ult i32 %0, 41
br i1 %8, label %9, label %16
9: ; preds = %7
%10 = zext i32 %0 to i64
%11 = getelementptr inbounds [10001 x double], [10001 x double]* @factln.a, i64 0, i64 %10
%12 = load double, double* %11
%13 = fcmp une double %12, 0.000000e+00
br i1 %13, label %29, label %14
14: ; preds = %9
%15 = tail call double @exactfactln(i32 %0)
store double %15, double* %11
br label %29
16: ; preds = %7
%17 = icmp ult i32 %0, 10001
br i1 %17, label %18, label %26
18: ; preds = %16
%19 = zext i32 %0 to i64
%20 = getelementptr inbounds [10001 x double], [10001 x double]* @factln.a, i64 0, i64 %19
%21 = load double, double* %20
%22 = fcmp une double %21, 0.000000e+00
br i1 %22, label %29, label %23
23: ; preds = %18
%24 = add i32 %0, 1
%25 = tail call double @gammln(i32 %24)
store double %25, double* %20
br label %29
26: ; preds = %16
%27 = add i32 %0, 1
%28 = tail call double @gammln(i32 %27)
br label %29
29: ; preds = %3, %23, %18, %14, %9, %5, %26
%30 = phi double [ %28, %26 ], [ 0.000000e+00, %5 ], [ %15, %14 ], [ %12, %9 ], [ %25, %23 ], [ %21, %18 ], [ 0.000000e+00, %3 ]
ret double %30
}