#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*/ printf(char *, ...);
int factorial(int n) {
  int i = 0;
  for (i = 0; i < n - 1; i++)
    printf(" ");
  printf("%d\n", n);
  if (1 == n) {
    return n;
  } else {
    return n * factorial(n - 1);
  }
}


x86 assembly (clang, -O0):
<factorial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    1166 <factorial+0x46>
lea    0xeaf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1139 <factorial+0x19>
mov    -0x8(%rbp),%esi
lea    0xe92(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%eax
cmp    -0x8(%rbp),%eax
jne    1190 <factorial+0x70>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ac <factorial+0x8c>
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
sub    $0x1,%edi
call   1030 <factorial@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%eax
imul   %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<factorial>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
mov    $0x1,%r15d
lea    0xeca(%rip),%r14
cmp    $0x2,%ebx
jl     1161 <factorial+0x41>
nopl   0x0(%rax,%rax,1)
mov    $0x1,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    $0x20,%edi
call   1030 <putchar@plt>
add    $0x1,%ebp
cmp    %ebp,%ebx
jne    1150 <factorial+0x30>
mov    %r14,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
cmp    $0x1,%ebx
je     1182 <factorial+0x62>
imul   %ebx,%r15d
lea    -0x1(%rbx),%eax
mov    %eax,%ebx
cmp    $0x2,%ebx
jge    1140 <factorial+0x20>
jmp    1161 <factorial+0x41>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c" \00"
@.str.1 = [4 x i8] c"%d\0A\00"
define i32 @factorial(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
store i32 0, i32* %4
store i32 0, i32* %4
br label %5
5: ; preds = %12, %1
%6 = load i32, i32* %4
%7 = load i32, i32* %3
%8 = sub nsw i32 %7, 1
%9 = icmp slt i32 %6, %8
br i1 %9, label %10, label %15
10: ; preds = %5
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10
%13 = load i32, i32* %4
%14 = add nsw i32 %13, 1
store i32 %14, i32* %4
br label %5
15: ; preds = %5
%16 = load i32, i32* %3
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%18 = load i32, i32* %3
%19 = icmp eq i32 1, %18
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = load i32, i32* %3
store i32 %21, i32* %2
br label %28
22: ; preds = %15
%23 = load i32, i32* %3
%24 = load i32, i32* %3
%25 = sub nsw i32 %24, 1
%26 = call i32 @factorial(i32 %25)
%27 = mul nsw i32 %23, %26
store i32 %27, i32* %2
br label %28
28: ; preds = %22, %20
%29 = load i32, i32* %2
ret i32 %29
}

LLVM IR (-O2):
@.str.1 = [4 x i8] c"%d\0A\00"
define i32 @factorial(i32 %0) {
br label %2
2: ; preds = %17, %1
%3 = phi i32 [ %6, %17 ], [ %0, %1 ]
%4 = phi i32 [ %18, %17 ], [ 1, %1 ]
%5 = phi i32 [ %7, %17 ], [ %0, %1 ]
%6 = add i32 %3, -1
%7 = add nsw i32 %5, -1
%8 = icmp sgt i32 %5, 1
br i1 %8, label %9, label %14
9: ; preds = %2, %9
%10 = phi i32 [ %12, %9 ], [ 0, %2 ]
%11 = tail call i32 @putchar(i32 32)
%12 = add nuw nsw i32 %10, 1
%13 = icmp eq i32 %12, %6
br i1 %13, label %14, label %9
14: ; preds = %9, %2
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%16 = icmp eq i32 %5, 1
br i1 %16, label %19, label %17
17: ; preds = %14
%18 = mul nsw i32 %5, %4
br label %2
19: ; preds = %14
ret i32 %4
}