#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 */
typedef int /*<<< orphan*/ memory;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ printf(char *);
double factorial(int val) {
  static double memory[64] = {1.0};
  static int have_entry = 0;
  int k;
  if (val < 0) {
    printf("Oops : val < 0.\n");
    exit(1);
  };
  if (val > ((int)(sizeof(memory) / sizeof(memory[0])))) {
    printf("Oops : val > ARRAY_LEN (memory).\n");
    exit(1);
  };
  if (val < have_entry)
    return memory[val];
  for (k = have_entry + 1; k <= val; k++)
    memory[k] = k * memory[k - 1];
  return memory[val];
}


x86 assembly (clang, -O0):
<factorial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    114b <factorial+0x2b>
lea    0xec4(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1040 <exit@plt>
cmpl   $0x40,-0xc(%rbp)
jle    116b <factorial+0x4b>
lea    0xeb5(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1040 <exit@plt>
mov    -0xc(%rbp),%eax
cmp    0x30d0(%rip),%eax
jge    1194 <factorial+0x74>
movslq -0xc(%rbp),%rcx
lea    0x2ebb(%rip),%rax
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11f9 <factorial+0xd9>
mov    0x30aa(%rip),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jg     11e4 <factorial+0xc4>
cvtsi2sdl -0x10(%rbp),%xmm0
mov    -0x10(%rbp),%eax
sub    $0x1,%eax
movslq %eax,%rcx
lea    0x2e7f(%rip),%rax
mulsd  (%rax,%rcx,8),%xmm0
movslq -0x10(%rbp),%rcx
lea    0x2e6f(%rip),%rax
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11a0 <factorial+0x80>
movslq -0xc(%rbp),%rcx
lea    0x2e51(%rip),%rax
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<factorial>:
push   %rax
test   %edi,%edi
js     1212 <factorial+0xf2>
cmp    $0x40,%edi
ja     121b <factorial+0xfb>
test   %edi,%edi
je     1201 <factorial+0xe1>
mov    %edi,%r9d
movsd  0x2efb(%rip),%xmm0
lea    -0x1(%r9),%rax
mov    %r9d,%r8d
and    $0x3,%r8d
mov    $0x1,%ecx
cmp    $0x3,%rax
jb     11c9 <factorial+0xa9>
and    $0xfffffffc,%r9d
mov    $0x1,%ecx
xor    %esi,%esi
lea    0x2ed3(%rip),%rax
nopl   (%rax)
xorps  %xmm1,%xmm1
cvtsi2sd %ecx,%xmm1
mulsd  %xmm0,%xmm1
lea    0x1(%rcx),%edx
xorps  %xmm0,%xmm0
cvtsi2sd %edx,%xmm0
movsd  %xmm1,(%rax,%rcx,8)
mulsd  %xmm1,%xmm0
movsd  %xmm0,0x8(%rax,%rcx,8)
lea    0x2(%rcx),%edx
xorps  %xmm1,%xmm1
cvtsi2sd %edx,%xmm1
mulsd  %xmm0,%xmm1
movsd  %xmm1,0x10(%rax,%rcx,8)
lea    0x3(%rcx),%edx
xorps  %xmm0,%xmm0
cvtsi2sd %edx,%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,0x18(%rax,%rcx,8)
add    $0x4,%rcx
add    $0x4,%rsi
cmp    %r9,%rsi
jne    1170 <factorial+0x50>
test   %r8,%r8
je     1201 <factorial+0xe1>
lea    0x0(,%rcx,8),%rax
xor    %edx,%edx
lea    0x2e61(%rip),%r9
nop
lea    (%rcx,%rdx,1),%esi
xorps  %xmm1,%xmm1
cvtsi2sd %esi,%xmm1
mulsd  %xmm1,%xmm0
lea    (%rax,%rdx,8),%rsi
movsd  %xmm0,(%r9,%rsi,1)
add    $0x1,%rdx
cmp    %rdx,%r8
jne    11e0 <factorial+0xc0>
movslq %edi,%rax
lea    0x2e35(%rip),%rcx
movsd  (%rcx,%rax,8),%xmm0
pop    %rax
ret
lea    0xe08(%rip),%rdi
jmp    1222 <factorial+0x102>
lea    0xdde(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1040 <exit@plt>

LLVM IR (-O0):
@factorial.memory = internal global <{ double, [63 x double] }> <{ double 1.000000e+00, [63 x double] zeroinitializer }>
@factorial.have_entry = internal global i32 0
@.str = [17 x i8] c"Oops : val < 0.\0A\00"
@.str.1 = [34 x i8] c"Oops : val > ARRAY_LEN (memory).\0A\00"
define double @factorial(i32 %0) {
%2 = alloca double
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @exit(i32 1)
unreachable
10: ; preds = %1
%11 = load i32, i32* %3
%12 = icmp sgt i32 %11, 64
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %10
%17 = load i32, i32* %3
%18 = load i32, i32* @factorial.have_entry
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %25
20: ; preds = %16
%21 = load i32, i32* %3
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [64 x double], [64 x double]* bitcast (<{ double, [63 x double] }>* @factorial.memory to [64 x double]*), i64 0, i64 %22
%24 = load double, double* %23
store double %24, double* %2
br label %52
25: ; preds = %16
%26 = load i32, i32* @factorial.have_entry
%27 = add nsw i32 %26, 1
store i32 %27, i32* %4
br label %28
28: ; preds = %44, %25
%29 = load i32, i32* %4
%30 = load i32, i32* %3
%31 = icmp sle i32 %29, %30
br i1 %31, label %32, label %47
32: ; preds = %28
%33 = load i32, i32* %4
%34 = sitofp i32 %33 to double
%35 = load i32, i32* %4
%36 = sub nsw i32 %35, 1
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds [64 x double], [64 x double]* bitcast (<{ double, [63 x double] }>* @factorial.memory to [64 x double]*), i64 0, i64 %37
%39 = load double, double* %38
%40 = fmul double %34, %39
%41 = load i32, i32* %4
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds [64 x double], [64 x double]* bitcast (<{ double, [63 x double] }>* @factorial.memory to [64 x double]*), i64 0, i64 %42
store double %40, double* %43
br label %44
44: ; preds = %32
%45 = load i32, i32* %4
%46 = add nsw i32 %45, 1
store i32 %46, i32* %4
br label %28
47: ; preds = %28
%48 = load i32, i32* %3
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds [64 x double], [64 x double]* bitcast (<{ double, [63 x double] }>* @factorial.memory to [64 x double]*), i64 0, i64 %49
%51 = load double, double* %50
store double %51, double* %2
br label %52
52: ; preds = %47, %20
%53 = load double, double* %2
ret double %53
}

LLVM IR (-O2):
@factorial.memory = internal unnamed_addr global <{ double, [63 x double] }> <{ double 1.000000e+00, [63 x double] zeroinitializer }>
@str = [33 x i8] c"Oops : val > ARRAY_LEN (memory).\00"
@str.2 = [16 x i8] c"Oops : val < 0.\00"
define double @factorial(i32 %0) {
%2 = icmp slt i32 %0, 0
br i1 %2, label %3, label %6
3: ; preds = %1
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.2, i64 0, i64 0))
%5 = tail call i32 @exit(i32 1)
unreachable
6: ; preds = %1
%7 = icmp ugt i32 %0, 64
br i1 %7, label %20, label %8
8: ; preds = %6
%9 = icmp eq i32 %0, 0
br i1 %9, label %64, label %10
10: ; preds = %8
%11 = add nuw nsw i32 %0, 1
%12 = zext i32 %11 to i64
%13 = load double, double* getelementptr inbounds (<{ double, [63 x double] }>, <{ double, [63 x double] }>* @factorial.memory, i64 0, i32 0)
%14 = add nsw i64 %12, -1
%15 = add nsw i64 %12, -2
%16 = and i64 %14, 3
%17 = icmp ult i64 %15, 3
br i1 %17, label %49, label %18
18: ; preds = %10
%19 = and i64 %14, -4
br label %23
20: ; preds = %6
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str, i64 0, i64 0))
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %23, %18
%24 = phi double [ %13, %18 ], [ %44, %23 ]
%25 = phi i64 [ 1, %18 ], [ %46, %23 ]
%26 = phi i64 [ 0, %18 ], [ %47, %23 ]
%27 = trunc i64 %25 to i32
%28 = sitofp i32 %27 to double
%29 = fmul double %24, %28
%30 = getelementptr inbounds [64 x double], [64 x double]* bitcast (<{ double, [63 x double] }>* @factorial.memory to [64 x double]*), i64 0, i64 %25
store double %29, double* %30
%31 = add nuw nsw i64 %25, 1
%32 = trunc i64 %31 to i32
%33 = sitofp i32 %32 to double
%34 = fmul double %29, %33
%35 = getelementptr inbounds [64 x double], [64 x double]* bitcast (<{ double, [63 x double] }>* @factorial.memory to [64 x double]*), i64 0, i64 %31
store double %34, double* %35
%36 = add nuw nsw i64 %25, 2
%37 = trunc i64 %36 to i32
%38 = sitofp i32 %37 to double
%39 = fmul double %34, %38
%40 = getelementptr inbounds [64 x double], [64 x double]* bitcast (<{ double, [63 x double] }>* @factorial.memory to [64 x double]*), i64 0, i64 %36
store double %39, double* %40
%41 = add nuw nsw i64 %25, 3
%42 = trunc i64 %41 to i32
%43 = sitofp i32 %42 to double
%44 = fmul double %39, %43
%45 = getelementptr inbounds [64 x double], [64 x double]* bitcast (<{ double, [63 x double] }>* @factorial.memory to [64 x double]*), i64 0, i64 %41
store double %44, double* %45
%46 = add nuw nsw i64 %25, 4
%47 = add i64 %26, 4
%48 = icmp eq i64 %47, %19
br i1 %48, label %49, label %23
49: ; preds = %23, %10
%50 = phi double [ %13, %10 ], [ %44, %23 ]
%51 = phi i64 [ 1, %10 ], [ %46, %23 ]
%52 = icmp eq i64 %16, 0
br i1 %52, label %64, label %53
53: ; preds = %49, %53
%54 = phi double [ %59, %53 ], [ %50, %49 ]
%55 = phi i64 [ %61, %53 ], [ %51, %49 ]
%56 = phi i64 [ %62, %53 ], [ 0, %49 ]
%57 = trunc i64 %55 to i32
%58 = sitofp i32 %57 to double
%59 = fmul double %54, %58
%60 = getelementptr inbounds [64 x double], [64 x double]* bitcast (<{ double, [63 x double] }>* @factorial.memory to [64 x double]*), i64 0, i64 %55
store double %59, double* %60
%61 = add nuw nsw i64 %55, 1
%62 = add i64 %56, 1
%63 = icmp eq i64 %62, %16
br i1 %63, label %64, label %53
64: ; preds = %49, %53, %8
%65 = sext i32 %0 to i64
%66 = getelementptr inbounds [64 x double], [64 x double]* bitcast (<{ double, [63 x double] }>* @factorial.memory to [64 x double]*), i64 0, i64 %65
%67 = load double, double* %66
ret double %67
}