#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 *alloc();
int /*<<< orphan*/ free(unsigned int *);
unsigned int *factorial(unsigned int n) {
  unsigned int *p, *q;
  if (n == 0) {
    p = alloc();
    if (!p)
      return 0;
    *p = 1;
    *(p + 1) = 0;
    return p;
  }
  q = factorial(n - 1);
  if (!q)
    return 0;
  p = alloc();
  if (!p) {
    while (q) {
      unsigned int *k = (unsigned int *)*(q + 1);
      free(q);
      q = k;
    }
    return 0;
  }
  *p = n * *q;
  *(p + 1) = (unsigned int)q;
  return p;
}


x86 assembly (clang, -O0):
<factorial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    118a <factorial+0x5a>
mov    $0x0,%al
call   1050 <alloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1168 <factorial+0x38>
movq   $0x0,-0x8(%rbp)
jmp    122a <factorial+0xfa>
mov    -0x18(%rbp),%rax
movl   $0x1,(%rax)
mov    -0x18(%rbp),%rax
movl   $0x0,0x4(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    122a <factorial+0xfa>
mov    -0xc(%rbp),%edi
sub    $0x1,%edi
call   1040 <factorial@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b1 <factorial+0x81>
movq   $0x0,-0x8(%rbp)
jmp    122a <factorial+0xfa>
mov    $0x0,%al
call   1050 <alloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1205 <factorial+0xd5>
jmp    11cc <factorial+0x9c>
cmpq   $0x0,-0x20(%rbp)
je     11f8 <factorial+0xc8>
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%eax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    11cc <factorial+0x9c>
movq   $0x0,-0x8(%rbp)
jmp    122a <factorial+0xfa>
mov    -0xc(%rbp),%ecx
mov    -0x20(%rbp),%rax
imul   (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<factorial>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %edi,%edi
je     1169 <factorial+0x39>
mov    %edi,%r15d
lea    -0x1(%r15),%edi
call   1040 <factorial@plt>
test   %rax,%rax
je     1187 <factorial+0x57>
mov    %rax,%r12
xor    %eax,%eax
call   1050 <alloc@plt>
test   %rax,%rax
je     118c <factorial+0x5c>
mov    %rax,%r14
imul   (%r12),%r15d
mov    %r15d,(%rax)
jmp    1181 <factorial+0x51>
xor    %r12d,%r12d
xor    %eax,%eax
call   1050 <alloc@plt>
test   %rax,%rax
je     1187 <factorial+0x57>
mov    %rax,%r14
movl   $0x1,(%rax)
mov    %r12d,0x4(%r14)
jmp    11a5 <factorial+0x75>
xor    %r14d,%r14d
jmp    11a5 <factorial+0x75>
xor    %r14d,%r14d
nop
mov    0x4(%r12),%ebx
mov    %r12,%rdi
call   1030 <free@plt>
mov    %rbx,%r12
test   %rbx,%rbx
jne    1190 <factorial+0x60>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32* @factorial(i32 %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32*
store i32 %0, i32* %3
%7 = load i32, i32* %3
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %19
9: ; preds = %1
%10 = call i32* (...) @alloc()
store i32* %10, i32** %4
%11 = load i32*, i32** %4
%12 = icmp ne i32* %11, null
br i1 %12, label %14, label %13
13: ; preds = %9
store i32* null, i32** %2
br label %55
14: ; preds = %9
%15 = load i32*, i32** %4
store i32 1, i32* %15
%16 = load i32*, i32** %4
%17 = getelementptr inbounds i32, i32* %16, i64 1
store i32 0, i32* %17
%18 = load i32*, i32** %4
store i32* %18, i32** %2
br label %55
19: ; preds = %1
%20 = load i32, i32* %3
%21 = sub i32 %20, 1
%22 = call i32* @factorial(i32 %21)
store i32* %22, i32** %5
%23 = load i32*, i32** %5
%24 = icmp ne i32* %23, null
br i1 %24, label %26, label %25
25: ; preds = %19
store i32* null, i32** %2
br label %55
26: ; preds = %19
%27 = call i32* (...) @alloc()
store i32* %27, i32** %4
%28 = load i32*, i32** %4
%29 = icmp ne i32* %28, null
br i1 %29, label %44, label %30
30: ; preds = %26
br label %31
31: ; preds = %34, %30
%32 = load i32*, i32** %5
%33 = icmp ne i32* %32, null
br i1 %33, label %34, label %43
34: ; preds = %31
%35 = load i32*, i32** %5
%36 = getelementptr inbounds i32, i32* %35, i64 1
%37 = load i32, i32* %36
%38 = zext i32 %37 to i64
%39 = inttoptr i64 %38 to i32*
store i32* %39, i32** %6
%40 = load i32*, i32** %5
%41 = call i32 @free(i32* %40)
%42 = load i32*, i32** %6
store i32* %42, i32** %5
br label %31
43: ; preds = %31
store i32* null, i32** %2
br label %55
44: ; preds = %26
%45 = load i32, i32* %3
%46 = load i32*, i32** %5
%47 = load i32, i32* %46
%48 = mul i32 %45, %47
%49 = load i32*, i32** %4
store i32 %48, i32* %49
%50 = load i32*, i32** %5
%51 = ptrtoint i32* %50 to i32
%52 = load i32*, i32** %4
%53 = getelementptr inbounds i32, i32* %52, i64 1
store i32 %51, i32* %53
%54 = load i32*, i32** %4
store i32* %54, i32** %2
br label %55
55: ; preds = %44, %43, %25, %14, %13
%56 = load i32*, i32** %2
ret i32* %56
}

LLVM IR (-O2):
define i32* @factorial(i32 %0) {
%2 = icmp eq i32 %0, 0
br i1 %2, label %3, label %7
3: ; preds = %1
%4 = tail call i32* (...) @alloc()
%5 = icmp eq i32* %4, null
br i1 %5, label %31, label %6
6: ; preds = %3
store i32 1, i32* %4
br label %27
7: ; preds = %1
%8 = add i32 %0, -1
%9 = tail call i32* @factorial(i32 %8)
%10 = icmp eq i32* %9, null
br i1 %10, label %31, label %11
11: ; preds = %7
%12 = tail call i32* (...) @alloc()
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %22
14: ; preds = %11, %14
%15 = phi i32* [ %19, %14 ], [ %9, %11 ]
%16 = getelementptr inbounds i32, i32* %15, i64 1
%17 = load i32, i32* %16
%18 = zext i32 %17 to i64
%19 = inttoptr i64 %18 to i32*
%20 = tail call i32 @free(i32* %15)
%21 = icmp eq i32 %17, 0
br i1 %21, label %31, label %14
22: ; preds = %11
%23 = load i32, i32* %9
%24 = mul i32 %23, %0
store i32 %24, i32* %12
%25 = ptrtoint i32* %9 to i64
%26 = trunc i64 %25 to i32
br label %27
27: ; preds = %6, %22
%28 = phi i32* [ %12, %22 ], [ %4, %6 ]
%29 = phi i32 [ %26, %22 ], [ 0, %6 ]
%30 = getelementptr inbounds i32, i32* %28, i64 1
store i32 %29, i32* %30
br label %31
31: ; preds = %14, %27, %7, %3
%32 = phi i32* [ null, %3 ], [ null, %7 ], [ %28, %27 ], [ null, %14 ]
ret i32* %32
}