#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 */
float gammln(int);
int /*<<< orphan*/ nrerror(char *);
float factln(int n) {
  static float a[1001];
  if (n < 0)
    nrerror("Negative factorial in routine factln");
  if (n <= 1)
    return 0.0;
  if (n <= 1000) {
    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    $0x10,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1141 <factln+0x21>
lea    0xecc(%rip),%rdi
call   1030 <nrerror@plt>
cmpl   $0x1,-0x8(%rbp)
jg     1158 <factln+0x38>
xorps  %xmm0,%xmm0
movss  %xmm0,-0x4(%rbp)
jmp    1203 <factln+0xe3>
cmpl   $0x3e8,-0x8(%rbp)
jg     11e4 <factln+0xc4>
movslq -0x8(%rbp),%rcx
lea    0x2ed0(%rip),%rax
movss  (%rax,%rcx,4),%xmm0
xorps  %xmm1,%xmm1
ucomiss %xmm1,%xmm0
jne    118c <factln+0x6c>
jp     118c <factln+0x6c>
jmp    11a6 <factln+0x86>
movslq -0x8(%rbp),%rcx
lea    0x2ea9(%rip),%rax
movss  (%rax,%rcx,4),%xmm0
movss  %xmm0,-0xc(%rbp)
jmp    11d5 <factln+0xb5>
cvtsi2sdl -0x8(%rbp),%xmm0
movsd  0xe4d(%rip),%xmm1
addsd  %xmm1,%xmm0
cvttsd2si %xmm0,%edi
call   1040 <gammln@plt>
movslq -0x8(%rbp),%rcx
lea    0x2e75(%rip),%rax
movss  %xmm0,(%rax,%rcx,4)
movss  %xmm0,-0xc(%rbp)
movss  -0xc(%rbp),%xmm0
movss  %xmm0,-0x4(%rbp)
jmp    1203 <factln+0xe3>
cvtsi2sdl -0x8(%rbp),%xmm0
movsd  0xe0f(%rip),%xmm1
addsd  %xmm1,%xmm0
cvttsd2si %xmm0,%edi
call   1040 <gammln@plt>
movss  %xmm0,-0x4(%rbp)
movss  -0x4(%rbp),%xmm0
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<factln>:
push   %r14
push   %rbx
push   %rax
test   %edi,%edi
js     1161 <factln+0x41>
xorps  %xmm0,%xmm0
cmp    $0x2,%edi
jb     1170 <factln+0x50>
cmp    $0x3e8,%edi
ja     1178 <factln+0x58>
mov    %edi,%ebx
lea    0x2eff(%rip),%r14
movss  (%r14,%rbx,4),%xmm0
xorps  %xmm1,%xmm1
ucomiss %xmm1,%xmm0
jne    1170 <factln+0x50>
jp     1170 <factln+0x50>
add    $0x1,%edi
call   1040 <gammln@plt>
movss  %xmm0,(%r14,%rbx,4)
jmp    1170 <factln+0x50>
lea    0xe98(%rip),%rdi
call   1030 <nrerror@plt>
xorps  %xmm0,%xmm0
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
add    $0x1,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <gammln@plt>

LLVM IR (-O0):
@factln.a = internal global [1001 x float] zeroinitializer
@.str = [37 x i8] c"Negative factorial in routine factln\00"
define float @factln(i32 %0) {
%2 = alloca float
%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 @nrerror(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 float 0.000000e+00, float* %2
br label %43
12: ; preds = %8
%13 = load i32, i32* %3
%14 = icmp sle i32 %13, 1000
br i1 %14, label %15, label %37
15: ; preds = %12
%16 = load i32, i32* %3
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds [1001 x float], [1001 x float]* @factln.a, i64 0, i64 %17
%19 = load float, float* %18
%20 = fcmp une float %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 [1001 x float], [1001 x float]* @factln.a, i64 0, i64 %23
%25 = load float, float* %24
br label %35
26: ; preds = %15
%27 = load i32, i32* %3
%28 = sitofp i32 %27 to double
%29 = fadd double %28, 1.000000e+00
%30 = fptosi double %29 to i32
%31 = call float @gammln(i32 %30)
%32 = load i32, i32* %3
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [1001 x float], [1001 x float]* @factln.a, i64 0, i64 %33
store float %31, float* %34
br label %35
35: ; preds = %26, %21
%36 = phi float [ %25, %21 ], [ %31, %26 ]
store float %36, float* %2
br label %43
37: ; preds = %12
%38 = load i32, i32* %3
%39 = sitofp i32 %38 to double
%40 = fadd double %39, 1.000000e+00
%41 = fptosi double %40 to i32
%42 = call float @gammln(i32 %41)
store float %42, float* %2
br label %43
43: ; preds = %37, %35, %11
%44 = load float, float* %2
ret float %44
}

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