#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 fac(long);
double log(int);
double logfac(long x) {
  const double pi = 3.1415926535897932384626;
  double invx, invx2, invx3, invx5, invx7;
  double sum;
  if (x <= 1l)
    x = 1l;
  if (x < 12l)
    return log(fac(x));
  else {
    invx = (double)1.0 / (double)x;
    invx2 = invx * invx;
    invx3 = invx2 * invx;
    invx5 = invx3 * invx2;
    invx7 = invx5 * invx2;
    sum = ((x + 0.5) * log(x)) - x;
    sum += log(2 * pi) * 0.5;
    sum += (invx / 12.0) - (invx3 / 360.0);
    sum += (invx5 / 1260.0) - (invx7 / 1680.0);
    return (sum);
  }
}


x86 assembly (clang, -O0):
<logfac>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
movsd  0xecc(%rip),%xmm0
movsd  %xmm0,-0x18(%rbp)
cmpq   $0x1,-0x10(%rbp)
jg     114c <logfac+0x2c>
movq   $0x1,-0x10(%rbp)
cmpq   $0xc,-0x10(%rbp)
jge    1171 <logfac+0x51>
mov    -0x10(%rbp),%rdi
call   1030 <fac@plt>
mov    %eax,%edi
call   1040 <log@plt>
movsd  %xmm0,-0x8(%rbp)
jmp    12a6 <logfac+0x186>
cvtsi2sdq -0x10(%rbp),%xmm1
movsd  0xeb1(%rip),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
mulsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
mulsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
mulsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x38(%rbp),%xmm0
mulsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
cvtsi2sdq -0x10(%rbp),%xmm0
movsd  0xe56(%rip),%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x50(%rbp)
mov    -0x10(%rbp),%rax
mov    %eax,%edi
call   1040 <log@plt>
movaps %xmm0,%xmm2
movsd  -0x50(%rbp),%xmm0
cvtsi2sdq -0x10(%rbp),%xmm1
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x48(%rbp)
mov    $0x6,%edi
call   1040 <log@plt>
movsd  -0x48(%rbp),%xmm1
movsd  0xdf9(%rip),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  0xdd7(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  -0x30(%rbp),%xmm1
movsd  0xdbe(%rip),%xmm2
divsd  %xmm2,%xmm1
subsd  %xmm1,%xmm0
addsd  -0x48(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x38(%rbp),%xmm0
movsd  0xd97(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  -0x40(%rbp),%xmm1
movsd  0xd7e(%rip),%xmm2
divsd  %xmm2,%xmm1
subsd  %xmm1,%xmm0
addsd  -0x48(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x48(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<logfac>:
sub    $0x58,%rsp
mov    %rdi,%rax
test   %rdi,%rdi
mov    $0x1,%edi
cmovg  %rax,%rdi
cmp    $0xb,%rdi
ja     1149 <logfac+0x29>
call   1030 <fac@plt>
mov    %eax,%edi
add    $0x58,%rsp
jmp    1040 <log@plt>
cvtsi2sd %rdi,%xmm0
movsd  0xeaa(%rip),%xmm1
divsd  %xmm0,%xmm1
movapd %xmm0,%xmm3
movsd  %xmm0,(%rsp)
movapd %xmm1,0x30(%rsp)
movapd %xmm1,%xmm0
mulsd  %xmm1,%xmm0
movapd %xmm1,%xmm2
mulsd  %xmm0,%xmm2
movapd %xmm2,0x40(%rsp)
movapd %xmm0,%xmm1
mulsd  %xmm2,%xmm1
movapd %xmm1,0x10(%rsp)
mulsd  %xmm1,%xmm0
movapd %xmm0,0x20(%rsp)
addsd  0xe69(%rip),%xmm3
movsd  %xmm3,0x8(%rsp)
call   1040 <log@plt>
mulsd  0x8(%rsp),%xmm0
subsd  (%rsp),%xmm0
movsd  %xmm0,(%rsp)
mov    $0x6,%edi
call   1040 <log@plt>
mulsd  0xe3c(%rip),%xmm0
movapd 0x10(%rsp),%xmm1
unpcklpd 0x30(%rsp),%xmm1
divpd  0xe30(%rip),%xmm1
addsd  (%rsp),%xmm0
movapd 0x20(%rsp),%xmm2
unpcklpd 0x40(%rsp),%xmm2
divpd  0xe27(%rip),%xmm2
addpd  %xmm1,%xmm2
movapd %xmm2,%xmm1
unpckhpd %xmm2,%xmm1
addsd  %xmm1,%xmm0
addsd  %xmm2,%xmm0
add    $0x58,%rsp
ret

LLVM IR (-O0):
define double @logfac(i64 %0) {
%2 = alloca double
%3 = alloca i64
%4 = alloca double
%5 = alloca double
%6 = alloca double
%7 = alloca double
%8 = alloca double
%9 = alloca double
%10 = alloca double
store i64 %0, i64* %3
store double 0x400921FB54442D18, double* %4
%11 = load i64, i64* %3
%12 = icmp sle i64 %11, 1
br i1 %12, label %13, label %14
13: ; preds = %1
store i64 1, i64* %3
br label %14
14: ; preds = %13, %1
%15 = load i64, i64* %3
%16 = icmp slt i64 %15, 12
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = load i64, i64* %3
%19 = call i32 @fac(i64 %18)
%20 = call double @log(i32 %19)
store double %20, double* %2
br label %65
21: ; preds = %14
%22 = load i64, i64* %3
%23 = sitofp i64 %22 to double
%24 = fdiv double 1.000000e+00, %23
store double %24, double* %5
%25 = load double, double* %5
%26 = load double, double* %5
%27 = fmul double %25, %26
store double %27, double* %6
%28 = load double, double* %6
%29 = load double, double* %5
%30 = fmul double %28, %29
store double %30, double* %7
%31 = load double, double* %7
%32 = load double, double* %6
%33 = fmul double %31, %32
store double %33, double* %8
%34 = load double, double* %8
%35 = load double, double* %6
%36 = fmul double %34, %35
store double %36, double* %9
%37 = load i64, i64* %3
%38 = sitofp i64 %37 to double
%39 = fadd double %38, 5.000000e-01
%40 = load i64, i64* %3
%41 = trunc i64 %40 to i32
%42 = call double @log(i32 %41)
%43 = load i64, i64* %3
%44 = sitofp i64 %43 to double
%45 = fneg double %44
%46 = call double @llvm.fmuladd.f64(double %39, double %42, double %45)
store double %46, double* %10
%47 = call double @log(i32 6)
%48 = load double, double* %10
%49 = call double @llvm.fmuladd.f64(double %47, double 5.000000e-01, double %48)
store double %49, double* %10
%50 = load double, double* %5
%51 = fdiv double %50, 1.200000e+01
%52 = load double, double* %7
%53 = fdiv double %52, 3.600000e+02
%54 = fsub double %51, %53
%55 = load double, double* %10
%56 = fadd double %55, %54
store double %56, double* %10
%57 = load double, double* %8
%58 = fdiv double %57, 1.260000e+03
%59 = load double, double* %9
%60 = fdiv double %59, 1.680000e+03
%61 = fsub double %58, %60
%62 = load double, double* %10
%63 = fadd double %62, %61
store double %63, double* %10
%64 = load double, double* %10
store double %64, double* %2
br label %65
65: ; preds = %21, %17
%66 = load double, double* %2
ret double %66
}

LLVM IR (-O2):
define double @logfac(i64 %0) {
%2 = icmp sgt i64 %0, 1
%3 = select i1 %2, i64 %0, i64 1
%4 = icmp ult i64 %3, 12
br i1 %4, label %5, label %8
5: ; preds = %1
%6 = tail call i32 @fac(i64 %3)
%7 = tail call double @log(i32 %6)
br label %33
8: ; preds = %1
%9 = sitofp i64 %3 to double
%10 = fdiv double 1.000000e+00, %9
%11 = fmul double %10, %10
%12 = fmul double %10, %11
%13 = fmul double %11, %12
%14 = fmul double %11, %13
%15 = fadd double %9, 5.000000e-01
%16 = trunc i64 %3 to i32
%17 = tail call double @log(i32 %16)
%18 = fneg double %9
%19 = tail call double @llvm.fmuladd.f64(double %15, double %17, double %18)
%20 = tail call double @log(i32 6)
%21 = tail call double @llvm.fmuladd.f64(double %20, double 5.000000e-01, double %19)
%22 = insertelement <2 x double> poison, double %13, i64 0
%23 = insertelement <2 x double> %22, double %10, i64 1
%24 = fdiv <2 x double> %23, <double 1.260000e+03, double 1.200000e+01>
%25 = insertelement <2 x double> poison, double %14, i64 0
%26 = insertelement <2 x double> %25, double %12, i64 1
%27 = fdiv <2 x double> %26, <double 1.680000e+03, double 3.600000e+02>
%28 = fsub <2 x double> %24, %27
%29 = extractelement <2 x double> %28, i64 1
%30 = fadd double %29, %21
%31 = extractelement <2 x double> %28, i64 0
%32 = fadd double %31, %30
br label %33
33: ; preds = %8, %5
%34 = phi double [ %7, %5 ], [ %32, %8 ]
ret double %34
}