#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 log(int);
double lfact(int x) {
  static double logf[10000];
  static int first = 1;
  int i;
  if (first) {
    first = 0;
    logf[0] = 0;
    for (i = 1; i < 10000; i++)
      logf[i] = log(i) + logf[i - 1];
  }
  return logf[x];
}


x86 assembly (clang, -O0):
<lfact>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x0,0x2f06(%rip)
je     1191 <lfact+0x81>
movl   $0x0,0x2ef6(%rip)
xorps  %xmm0,%xmm0
movsd  %xmm0,0x2f03(%rip)
movl   $0x1,-0x8(%rbp)
cmpl   $0x2710,-0x8(%rbp)
jge    118c <lfact+0x7c>
mov    -0x8(%rbp),%edi
call   1030 <log@plt>
mov    -0x8(%rbp),%eax
sub    $0x1,%eax
movslq %eax,%rcx
lea    0x2ed7(%rip),%rax
addsd  (%rax,%rcx,8),%xmm0
movslq -0x8(%rbp),%rcx
lea    0x2ec7(%rip),%rax
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1144 <lfact+0x34>
jmp    1191 <lfact+0x81>
movslq -0x4(%rbp),%rcx
lea    0x2ea4(%rip),%rax
movsd  (%rax,%rcx,8),%xmm0
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lfact>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%r14d
cmpb   $0x0,0x167a2(%rip)
jne    1160 <lfact+0x50>
movb   $0x1,0x16799(%rip)
movq   $0x0,0x2f0e(%rip)
mov    $0x1,%ebx
lea    0x2f02(%rip),%rbp
xchg   %ax,%ax
mov    %ebx,%edi
call   1030 <log@plt>
addsd  -0x8(%rbp,%rbx,8),%xmm0
movsd  %xmm0,0x0(%rbp,%rbx,8)
add    $0x1,%rbx
cmp    $0x2710,%rbx
jne    1140 <lfact+0x30>
movslq %r14d,%rax
lea    0x2ed6(%rip),%rcx
movsd  (%rcx,%rax,8),%xmm0
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@lfact.logf = internal global [10000 x double] zeroinitializer
@lfact.first = internal global i32 1
define double @lfact(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
%4 = load i32, i32* @lfact.first
%5 = icmp ne i32 %4, 0
br i1 %5, label %6, label %26
6: ; preds = %1
store i32 0, i32* @lfact.first
store double 0.000000e+00, double* getelementptr inbounds ([10000 x double], [10000 x double]* @lfact.logf, i64 0, i64 0)
store i32 1, i32* %3
br label %7
7: ; preds = %22, %6
%8 = load i32, i32* %3
%9 = icmp slt i32 %8, 10000
br i1 %9, label %10, label %25
10: ; preds = %7
%11 = load i32, i32* %3
%12 = call double @log(i32 %11)
%13 = load i32, i32* %3
%14 = sub nsw i32 %13, 1
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds [10000 x double], [10000 x double]* @lfact.logf, i64 0, i64 %15
%17 = load double, double* %16
%18 = fadd double %12, %17
%19 = load i32, i32* %3
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds [10000 x double], [10000 x double]* @lfact.logf, i64 0, i64 %20
store double %18, double* %21
br label %22
22: ; preds = %10
%23 = load i32, i32* %3
%24 = add nsw i32 %23, 1
store i32 %24, i32* %3
br label %7
25: ; preds = %7
br label %26
26: ; preds = %25, %1
%27 = load i32, i32* %2
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [10000 x double], [10000 x double]* @lfact.logf, i64 0, i64 %28
%30 = load double, double* %29
ret double %30
}

LLVM IR (-O2):
@lfact.logf = internal unnamed_addr global [10000 x double] zeroinitializer
@lfact.first = internal unnamed_addr global i1 false
define double @lfact(i32 %0) {
%2 = load i1, i1* @lfact.first
br i1 %2, label %15, label %3
3: ; preds = %1
store i1 true, i1* @lfact.first
store double 0.000000e+00, double* getelementptr inbounds ([10000 x double], [10000 x double]* @lfact.logf, i64 0, i64 0)
br label %4
4: ; preds = %3, %4
%5 = phi i64 [ 1, %3 ], [ %13, %4 ]
%6 = trunc i64 %5 to i32
%7 = tail call double @log(i32 %6)
%8 = add nsw i64 %5, -1
%9 = getelementptr inbounds [10000 x double], [10000 x double]* @lfact.logf, i64 0, i64 %8
%10 = load double, double* %9
%11 = fadd double %7, %10
%12 = getelementptr inbounds [10000 x double], [10000 x double]* @lfact.logf, i64 0, i64 %5
store double %11, double* %12
%13 = add nuw nsw i64 %5, 1
%14 = icmp eq i64 %13, 10000
br i1 %14, label %15, label %4
15: ; preds = %4, %1
%16 = sext i32 %0 to i64
%17 = getelementptr inbounds [10000 x double], [10000 x double]* @lfact.logf, i64 0, i64 %16
%18 = load double, double* %17
ret double %18
}