#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 */
unsigned int log(unsigned int);
double ln_factorial(unsigned int n) {
  if (n == 0)
    return 0;
  return log((2 * n + 1 / 3.0) * 3.1415926) / 2 + n * log(n) - n;
}


x86 assembly (clang, -O0):
<ln_factorial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    1132 <ln_factorial+0x22>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    118c <ln_factorial+0x7c>
mov    -0xc(%rbp),%eax
add    %eax,%eax
cvtsi2sd %rax,%xmm0
movsd  0xebc(%rip),%xmm1
addsd  %xmm1,%xmm0
movsd  0xeb8(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rax
mov    %eax,%edi
call   1030 <log@plt>
shr    %eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%edi
mov    %edi,-0x14(%rbp)
call   1030 <log@plt>
mov    -0x14(%rbp),%ecx
mov    %eax,%edx
mov    -0x10(%rbp),%eax
imul   %edx,%ecx
add    %ecx,%eax
mov    -0xc(%rbp),%ecx
sub    %ecx,%eax
cvtsi2sd %rax,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ln_factorial>:
test   %edi,%edi
je     1163 <ln_factorial+0x53>
push   %r14
push   %rbx
push   %rax
mov    %edi,%r14d
lea    (%r14,%r14,1),%eax
cvtsi2sd %rax,%xmm0
addsd  0xed4(%rip),%xmm0
mulsd  0xed4(%rip),%xmm0
cvttsd2si %xmm0,%rdi
call   1030 <log@plt>
mov    %eax,%ebx
shr    %ebx
mov    %r14d,%edi
call   1030 <log@plt>
imul   %r14d,%eax
sub    %r14d,%ebx
add    %eax,%ebx
xorps  %xmm0,%xmm0
cvtsi2sd %rbx,%xmm0
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
xorps  %xmm0,%xmm0
ret

LLVM IR (-O0):
define double @ln_factorial(i32 %0) {
%2 = alloca double
%3 = alloca i32
store i32 %0, i32* %3
%4 = load i32, i32* %3
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %7
6: ; preds = %1
store double 0.000000e+00, double* %2
br label %24
7: ; preds = %1
%8 = load i32, i32* %3
%9 = mul i32 2, %8
%10 = uitofp i32 %9 to double
%11 = fadd double %10, 0x3FD5555555555555
%12 = fmul double %11, 0x400921FB4D12D84A
%13 = fptoui double %12 to i32
%14 = call i32 @log(i32 %13)
%15 = udiv i32 %14, 2
%16 = load i32, i32* %3
%17 = load i32, i32* %3
%18 = call i32 @log(i32 %17)
%19 = mul i32 %16, %18
%20 = add i32 %15, %19
%21 = load i32, i32* %3
%22 = sub i32 %20, %21
%23 = uitofp i32 %22 to double
store double %23, double* %2
br label %24
24: ; preds = %7, %6
%25 = load double, double* %2
ret double %25
}

LLVM IR (-O2):
define double @ln_factorial(i32 %0) {
%2 = icmp eq i32 %0, 0
br i1 %2, label %16, label %3
3: ; preds = %1
%4 = shl i32 %0, 1
%5 = uitofp i32 %4 to double
%6 = fadd double %5, 0x3FD5555555555555
%7 = fmul double %6, 0x400921FB4D12D84A
%8 = fptoui double %7 to i32
%9 = tail call i32 @log(i32 %8)
%10 = lshr i32 %9, 1
%11 = tail call i32 @log(i32 %0)
%12 = mul i32 %11, %0
%13 = sub i32 %10, %0
%14 = add i32 %13, %12
%15 = uitofp i32 %14 to double
br label %16
16: ; preds = %1, %3
%17 = phi double [ %15, %3 ], [ 0.000000e+00, %1 ]
ret double %17
}