#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 */
scalar_t__ log(int);
double logfactorial(int x) {
  double tmp = 0.0;
  if (x <= 0) {
    return 0.0;
  } else {
    for (int i = 1; i <= x; i++) {
      tmp += log(i);
    }
    return tmp;
  }
}


x86 assembly (clang, -O0):
<logfactorial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x18(%rbp)
cmpl   $0x0,-0xc(%rbp)
jg     113a <logfactorial+0x2a>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    117c <logfactorial+0x6c>
movl   $0x1,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jg     1172 <logfactorial+0x62>
mov    -0x1c(%rbp),%edi
call   1030 <log@plt>
cvtsi2sd %rax,%xmm0
addsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1141 <logfactorial+0x31>
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<logfactorial>:
push   %r14
push   %rbx
push   %rax
test   %edi,%edi
jle    116c <logfactorial+0x5c>
mov    %edi,%r14d
neg    %r14d
xorps  %xmm0,%xmm0
mov    $0x1,%ebx
cs nopw 0x0(%rax,%rax,1)
movsd  %xmm0,(%rsp)
mov    %ebx,%edi
call   1030 <log@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
movsd  (%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,(%rsp)
movsd  (%rsp),%xmm0
lea    (%r14,%rbx,1),%eax
add    $0x1,%eax
mov    %ebx,%ecx
add    $0x1,%ecx
mov    %ecx,%ebx
cmp    $0x1,%eax
jne    1130 <logfactorial+0x20>
jmp    116f <logfactorial+0x5f>
xorps  %xmm0,%xmm0
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define double @logfactorial(i32 %0) {
%2 = alloca double
%3 = alloca i32
%4 = alloca double
%5 = alloca i32
store i32 %0, i32* %3
store double 0.000000e+00, double* %4
%6 = load i32, i32* %3
%7 = icmp sle i32 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %1
store double 0.000000e+00, double* %2
br label %25
9: ; preds = %1
store i32 1, i32* %5
br label %10
10: ; preds = %20, %9
%11 = load i32, i32* %5
%12 = load i32, i32* %3
%13 = icmp sle i32 %11, %12
br i1 %13, label %14, label %23
14: ; preds = %10
%15 = load i32, i32* %5
%16 = call i64 @log(i32 %15)
%17 = sitofp i64 %16 to double
%18 = load double, double* %4
%19 = fadd double %18, %17
store double %19, double* %4
br label %20
20: ; preds = %14
%21 = load i32, i32* %5
%22 = add nsw i32 %21, 1
store i32 %22, i32* %5
br label %10
23: ; preds = %10
%24 = load double, double* %4
store double %24, double* %2
br label %25
25: ; preds = %23, %8
%26 = load double, double* %2
ret double %26
}

LLVM IR (-O2):
define double @logfactorial(i32 %0) {
%2 = icmp slt i32 %0, 1
br i1 %2, label %11, label %3
3: ; preds = %1, %3
%4 = phi i32 [ %9, %3 ], [ 1, %1 ]
%5 = phi double [ %8, %3 ], [ 0.000000e+00, %1 ]
%6 = tail call i64 @log(i32 %4)
%7 = sitofp i64 %6 to double
%8 = fadd double %5, %7
%9 = add nuw i32 %4, 1
%10 = icmp eq i32 %4, %0
br i1 %10, label %11, label %3
11: ; preds = %3, %1
%12 = phi double [ 0.000000e+00, %1 ], [ %8, %3 ]
ret double %12
}