#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 /*<<< orphan*/ abort();
int /*<<< orphan*/ assert(int);
double *calloc(int, int);
scalar_t__ isinf(double);
int inefficient_factorial(int new_n, double **fac, int increment) {
  int i;
  double *f;
  assert(new_n >= 0);
  assert(1 == increment || 2 == increment);
  *fac = calloc(new_n + 1, sizeof(double));
  f = *fac;
  if (0 < new_n)
    f[0] = 1.0;
  if (1 < new_n)
    f[1] = 1.0;
  for (i = 2; i <= new_n; i++) {
    f[i] = f[i - increment] * i;
    if (isinf(f[i]))
      abort();
  }
  return new_n;
}


x86 assembly (clang, -O0):
<inefficient_factorial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x0,-0x4(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    $0x1,%al
mov    $0x1,%ecx
cmp    -0x14(%rbp),%ecx
mov    %al,-0x21(%rbp)
je     1184 <inefficient_factorial+0x44>
mov    $0x2,%eax
cmp    -0x14(%rbp),%eax
sete   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x4(%rbp),%edi
add    $0x1,%edi
mov    $0x8,%esi
call   1040 <calloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmp    -0x4(%rbp),%eax
jge    11d1 <inefficient_factorial+0x91>
mov    -0x20(%rbp),%rax
movsd  0xe33(%rip),%xmm0
movsd  %xmm0,(%rax)
mov    $0x1,%eax
cmp    -0x4(%rbp),%eax
jge    11f0 <inefficient_factorial+0xb0>
mov    -0x20(%rbp),%rax
movsd  0xe15(%rip),%xmm0
movsd  %xmm0,0x8(%rax)
movl   $0x2,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x4(%rbp),%eax
jg     1261 <inefficient_factorial+0x121>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%ecx
sub    -0x14(%rbp),%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
cvtsi2sdl -0x18(%rbp),%xmm1
mulsd  %xmm1,%xmm0
mov    -0x20(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x20(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
call   1060 <isinf@plt>
cmp    $0x0,%rax
je     124e <inefficient_factorial+0x10e>
mov    $0x0,%al
call   1030 <abort@plt>
jmp    1253 <inefficient_factorial+0x113>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11f7 <inefficient_factorial+0xb7>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<inefficient_factorial>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r12
mov    %edi,%r14d
not    %edi
shr    $0x1f,%edi
call   1050 <assert@plt>
lea    -0x1(%r15),%eax
xor    %edi,%edi
cmp    $0x2,%eax
setb   %dil
call   1050 <assert@plt>
lea    0x1(%r14),%ebx
mov    %ebx,%edi
mov    $0x8,%esi
call   1040 <calloc@plt>
mov    %rax,(%r12)
test   %r14d,%r14d
jle    11e6 <inefficient_factorial+0xa6>
mov    %rax,%r13
movabs $0x3ff0000000000000,%rax
mov    %rax,0x0(%r13)
cmp    $0x1,%r14d
je     11e6 <inefficient_factorial+0xa6>
mov    %rax,0x8(%r13)
movslq %r15d,%rax
mov    %ebx,%r15d
shl    $0x3,%rax
mov    %r13,%rbx
sub    %rax,%rbx
mov    $0x2,%ebp
nopl   0x0(%rax)
xorps  %xmm0,%xmm0
cvtsi2sd %ebp,%xmm0
mulsd  (%rbx,%rbp,8),%xmm0
movsd  %xmm0,0x0(%r13,%rbp,8)
call   1060 <isinf@plt>
test   %rax,%rax
jne    11f8 <inefficient_factorial+0xb8>
add    $0x1,%rbp
cmp    %rbp,%r15
jne    11c0 <inefficient_factorial+0x80>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
define i32 @inefficient_factorial(i32 %0, double** %1, i32 %2) {
%4 = alloca i32
%5 = alloca double**
%6 = alloca i32
%7 = alloca i32
%8 = alloca double*
store i32 %0, i32* %4
store double** %1, double*** %5
store i32 %2, i32* %6
%9 = load i32, i32* %4
%10 = icmp sge i32 %9, 0
%11 = zext i1 %10 to i32
%12 = call i32 @assert(i32 %11)
%13 = load i32, i32* %6
%14 = icmp eq i32 1, %13
br i1 %14, label %18, label %15
15: ; preds = %3
%16 = load i32, i32* %6
%17 = icmp eq i32 2, %16
br label %18
18: ; preds = %15, %3
%19 = phi i1 [ true, %3 ], [ %17, %15 ]
%20 = zext i1 %19 to i32
%21 = call i32 @assert(i32 %20)
%22 = load i32, i32* %4
%23 = add nsw i32 %22, 1
%24 = call double* @calloc(i32 %23, i32 8)
%25 = load double**, double*** %5
store double* %24, double** %25
%26 = load double**, double*** %5
%27 = load double*, double** %26
store double* %27, double** %8
%28 = load i32, i32* %4
%29 = icmp slt i32 0, %28
br i1 %29, label %30, label %33
30: ; preds = %18
%31 = load double*, double** %8
%32 = getelementptr inbounds double, double* %31, i64 0
store double 1.000000e+00, double* %32
br label %33
33: ; preds = %30, %18
%34 = load i32, i32* %4
%35 = icmp slt i32 1, %34
br i1 %35, label %36, label %39
36: ; preds = %33
%37 = load double*, double** %8
%38 = getelementptr inbounds double, double* %37, i64 1
store double 1.000000e+00, double* %38
br label %39
39: ; preds = %36, %33
store i32 2, i32* %7
br label %40
40: ; preds = %69, %39
%41 = load i32, i32* %7
%42 = load i32, i32* %4
%43 = icmp sle i32 %41, %42
br i1 %43, label %44, label %72
44: ; preds = %40
%45 = load double*, double** %8
%46 = load i32, i32* %7
%47 = load i32, i32* %6
%48 = sub nsw i32 %46, %47
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds double, double* %45, i64 %49
%51 = load double, double* %50
%52 = load i32, i32* %7
%53 = sitofp i32 %52 to double
%54 = fmul double %51, %53
%55 = load double*, double** %8
%56 = load i32, i32* %7
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds double, double* %55, i64 %57
store double %54, double* %58
%59 = load double*, double** %8
%60 = load i32, i32* %7
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds double, double* %59, i64 %61
%63 = load double, double* %62
%64 = call i64 @isinf(double %63)
%65 = icmp ne i64 %64, 0
br i1 %65, label %66, label %68
66: ; preds = %44
%67 = call i32 (...) @abort()
unreachable
68: ; preds = %44
br label %69
69: ; preds = %68
%70 = load i32, i32* %7
%71 = add nsw i32 %70, 1
store i32 %71, i32* %7
br label %40
72: ; preds = %40
%73 = load i32, i32* %4
ret i32 %73
}

LLVM IR (-O2):
define i32 @inefficient_factorial(i32 returned %0, double** nocapture writeonly %1, i32 %2) {
%4 = xor i32 %0, -1
%5 = lshr i32 %4, 31
%6 = tail call i32 @assert(i32 %5)
%7 = add i32 %2, -1
%8 = icmp ult i32 %7, 2
%9 = zext i1 %8 to i32
%10 = tail call i32 @assert(i32 %9)
%11 = add i32 %0, 1
%12 = tail call double* @calloc(i32 %11, i32 8)
store double* %12, double** %1
%13 = icmp sgt i32 %0, 0
br i1 %13, label %14, label %36
14: ; preds = %3
store double 1.000000e+00, double* %12
%15 = icmp eq i32 %0, 1
br i1 %15, label %36, label %16
16: ; preds = %14
%17 = getelementptr inbounds double, double* %12, i64 1
store double 1.000000e+00, double* %17
%18 = sext i32 %2 to i64
%19 = zext i32 %11 to i64
br label %23
20: ; preds = %23
%21 = add nuw nsw i64 %24, 1
%22 = icmp eq i64 %21, %19
br i1 %22, label %36, label %23
23: ; preds = %16, %20
%24 = phi i64 [ 2, %16 ], [ %21, %20 ]
%25 = sub nsw i64 %24, %18
%26 = getelementptr inbounds double, double* %12, i64 %25
%27 = load double, double* %26
%28 = trunc i64 %24 to i32
%29 = sitofp i32 %28 to double
%30 = fmul double %27, %29
%31 = getelementptr inbounds double, double* %12, i64 %24
store double %30, double* %31
%32 = tail call i64 @isinf(double %30)
%33 = icmp eq i64 %32, 0
br i1 %33, label %20, label %34
34: ; preds = %23
%35 = tail call i32 (...) @abort()
unreachable
36: ; preds = %20, %14, %3
ret i32 %0
}