#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 uint32_t;
typedef int int32_t;

/* Variables and functions */
int /*<<< orphan*/ warning(char *);
double fact(int32_t x) {
  const static double table[] = {1,
                                 1,
                                 2,
                                 6,
                                 24,
                                 120,
                                 720,
                                 5040,
                                 40320,
                                 362880,
                                 3628800,
                                 39916800,
                                 479001600,
                                 6227020800,
                                 87178291200,
                                 1307674368000,
                                 20922789888000,
                                 355687428096000,
                                 6402373705728000,
                                 121645100408832000,
                                 2432902008176640000};
  double ans;
  uint32_t i;
  if (x < 0) {
    warning("fact = negativ szam faktorialisa.");
    return 0;
  }
  if (x <= 20)
    return table[x];
  else {
    ans = table[20];
    for (i = 21; i <= x; i++)
      ans *= i;
    return ans;
  }
}


x86 assembly (clang, -O0):
<fact>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    113e <fact+0x2e>
lea    0xf7c(%rip),%rdi
call   1030 <warning@plt>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11a9 <fact+0x99>
cmpl   $0x14,-0xc(%rbp)
jg     1162 <fact+0x52>
movslq -0xc(%rbp),%rcx
lea    0xead(%rip),%rax
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11a9 <fact+0x99>
movsd  0xf36(%rip),%xmm0
movsd  %xmm0,-0x18(%rbp)
movl   $0x15,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jg     119f <fact+0x8f>
cvtsi2sdl -0x1c(%rbp),%xmm0
mulsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1176 <fact+0x66>
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fact>:
push   %rax
test   %edi,%edi
js     112a <fact+0x1a>
cmp    $0x15,%edi
jae    113b <fact+0x2b>
mov    %edi,%eax
lea    0xeed(%rip),%rcx
movsd  (%rcx,%rax,8),%xmm0
pop    %rax
ret
lea    0xf87(%rip),%rdi
call   1030 <warning@plt>
xorps  %xmm0,%xmm0
pop    %rax
ret
lea    -0x14(%rdi),%ecx
add    $0xffffffeb,%edi
mov    %ecx,%eax
and    $0x7,%eax
cmp    $0x7,%edi
jae    115d <fact+0x4d>
movsd  0xead(%rip),%xmm0
mov    $0x15,%edx
jmp    11e9 <fact+0xd9>
and    $0xfffffff8,%ecx
movsd  0xe98(%rip),%xmm0
mov    $0x15,%edx
nopl   (%rax)
mov    %edx,%esi
xorps  %xmm1,%xmm1
cvtsi2sd %edx,%xmm1
mulsd  %xmm0,%xmm1
lea    0x1(%rsi),%edx
xorps  %xmm0,%xmm0
cvtsi2sd %edx,%xmm0
mulsd  %xmm1,%xmm0
lea    0x2(%rsi),%edx
xorps  %xmm1,%xmm1
cvtsi2sd %edx,%xmm1
mulsd  %xmm0,%xmm1
lea    0x3(%rsi),%edx
xorps  %xmm0,%xmm0
cvtsi2sd %edx,%xmm0
mulsd  %xmm1,%xmm0
lea    0x4(%rsi),%edx
xorps  %xmm1,%xmm1
cvtsi2sd %edx,%xmm1
mulsd  %xmm0,%xmm1
lea    0x5(%rsi),%edx
xorps  %xmm0,%xmm0
cvtsi2sd %edx,%xmm0
mulsd  %xmm1,%xmm0
lea    0x6(%rsi),%edx
xorps  %xmm1,%xmm1
cvtsi2sd %edx,%xmm1
mulsd  %xmm0,%xmm1
lea    0x7(%rsi),%edx
xorps  %xmm0,%xmm0
cvtsi2sd %edx,%xmm0
mulsd  %xmm1,%xmm0
lea    0x8(%rsi),%edx
add    $0xfffffff3,%esi
cmp    %ecx,%esi
jne    1170 <fact+0x60>
test   %eax,%eax
je     1203 <fact+0xf3>
nopl   (%rax)
xorps  %xmm1,%xmm1
cvtsi2sd %edx,%xmm1
mulsd  %xmm1,%xmm0
add    $0x1,%edx
add    $0xffffffff,%eax
jne    11f0 <fact+0xe0>
pop    %rax
ret

LLVM IR (-O0):
@fact.table = internal constant [21 x double] [double 1.000000e+00, double 1.000000e+00, double 2.000000e+00, double 6.000000e+00, double 2.400000e+01, double 1.200000e+02, double 7.200000e+02, double 5.040000e+03, double 4.032000e+04, double 3.628800e+05, double 3.628800e+06, double 3.991680e+07, double 0x41BC8CFC00000000, double 0x41F7328CC0000000, double 0x42344C3B28000000, double 0x4273077775800000, double 0x42B3077775800000, double 0x42F437EEECD80000, double 0x4336BEECCA730000, double 0x437B02B930689000, double 0x43C0E1B3BE415A00]
@.str = [34 x i8] c"fact = negativ szam faktorialisa.\00"
define double @fact(i32 %0) {
%2 = alloca double
%3 = alloca i32
%4 = alloca double
%5 = alloca i32
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i32 @warning(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
store double 0.000000e+00, double* %2
br label %34
10: ; preds = %1
%11 = load i32, i32* %3
%12 = icmp sle i32 %11, 20
br i1 %12, label %13, label %18
13: ; preds = %10
%14 = load i32, i32* %3
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds [21 x double], [21 x double]* @fact.table, i64 0, i64 %15
%17 = load double, double* %16
store double %17, double* %2
br label %34
18: ; preds = %10
%19 = load double, double* getelementptr inbounds ([21 x double], [21 x double]* @fact.table, i64 0, i64 20)
store double %19, double* %4
store i32 21, i32* %5
br label %20
20: ; preds = %29, %18
%21 = load i32, i32* %5
%22 = load i32, i32* %3
%23 = icmp sle i32 %21, %22
br i1 %23, label %24, label %32
24: ; preds = %20
%25 = load i32, i32* %5
%26 = sitofp i32 %25 to double
%27 = load double, double* %4
%28 = fmul double %27, %26
store double %28, double* %4
br label %29
29: ; preds = %24
%30 = load i32, i32* %5
%31 = add nsw i32 %30, 1
store i32 %31, i32* %5
br label %20
32: ; preds = %20
%33 = load double, double* %4
store double %33, double* %2
br label %34
34: ; preds = %32, %13, %8
%35 = load double, double* %2
ret double %35
}

LLVM IR (-O2):
@fact.table = internal unnamed_addr constant [21 x double] [double 1.000000e+00, double 1.000000e+00, double 2.000000e+00, double 6.000000e+00, double 2.400000e+01, double 1.200000e+02, double 7.200000e+02, double 5.040000e+03, double 4.032000e+04, double 3.628800e+05, double 3.628800e+06, double 3.991680e+07, double 0x41BC8CFC00000000, double 0x41F7328CC0000000, double 0x42344C3B28000000, double 0x4273077775800000, double 0x42B3077775800000, double 0x42F437EEECD80000, double 0x4336BEECCA730000, double 0x437B02B930689000, double 0x43C0E1B3BE415A00]
@.str = [34 x i8] c"fact = negativ szam faktorialisa.\00"
define double @fact(i32 %0) {
%2 = icmp slt i32 %0, 0
br i1 %2, label %3, label %5
3: ; preds = %1
%4 = tail call i32 @warning(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
br label %62
5: ; preds = %1
%6 = icmp ult i32 %0, 21
br i1 %6, label %14, label %7
7: ; preds = %5
%8 = add i32 %0, -20
%9 = add i32 %0, -21
%10 = and i32 %8, 7
%11 = icmp ult i32 %9, 7
br i1 %11, label %48, label %12
12: ; preds = %7
%13 = and i32 %8, -8
br label %18
14: ; preds = %5
%15 = zext i32 %0 to i64
%16 = getelementptr inbounds [21 x double], [21 x double]* @fact.table, i64 0, i64 %15
%17 = load double, double* %16
br label %62
18: ; preds = %18, %12
%19 = phi i32 [ 21, %12 ], [ %45, %18 ]
%20 = phi double [ 0x43C0E1B3BE415A00, %12 ], [ %44, %18 ]
%21 = phi i32 [ 0, %12 ], [ %46, %18 ]
%22 = sitofp i32 %19 to double
%23 = fmul double %20, %22
%24 = add nuw nsw i32 %19, 1
%25 = sitofp i32 %24 to double
%26 = fmul double %23, %25
%27 = add nuw nsw i32 %19, 2
%28 = sitofp i32 %27 to double
%29 = fmul double %26, %28
%30 = add nuw i32 %19, 3
%31 = sitofp i32 %30 to double
%32 = fmul double %29, %31
%33 = add nuw i32 %19, 4
%34 = sitofp i32 %33 to double
%35 = fmul double %32, %34
%36 = add nuw i32 %19, 5
%37 = sitofp i32 %36 to double
%38 = fmul double %35, %37
%39 = add nuw i32 %19, 6
%40 = sitofp i32 %39 to double
%41 = fmul double %38, %40
%42 = add nuw i32 %19, 7
%43 = sitofp i32 %42 to double
%44 = fmul double %41, %43
%45 = add nuw i32 %19, 8
%46 = add i32 %21, 8
%47 = icmp eq i32 %46, %13
br i1 %47, label %48, label %18
48: ; preds = %18, %7
%49 = phi double [ undef, %7 ], [ %44, %18 ]
%50 = phi i32 [ 21, %7 ], [ %45, %18 ]
%51 = phi double [ 0x43C0E1B3BE415A00, %7 ], [ %44, %18 ]
%52 = icmp eq i32 %10, 0
br i1 %52, label %62, label %53
53: ; preds = %48, %53
%54 = phi i32 [ %59, %53 ], [ %50, %48 ]
%55 = phi double [ %58, %53 ], [ %51, %48 ]
%56 = phi i32 [ %60, %53 ], [ 0, %48 ]
%57 = sitofp i32 %54 to double
%58 = fmul double %55, %57
%59 = add nuw i32 %54, 1
%60 = add i32 %56, 1
%61 = icmp eq i32 %60, %10
br i1 %61, label %62, label %53
62: ; preds = %48, %53, %14, %3
%63 = phi double [ 0.000000e+00, %3 ], [ %17, %14 ], [ %49, %48 ], [ %58, %53 ]
ret double %63
}