#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);
int factorial(int n) {
  int ans;
  printf("n = %d\n", n);
  if (n == 0) {
    ans = 1;
    printf("ans (base case) = %d\n", ans);
  } else {
    ans = factorial(n - 1) * n;
    printf("ans  = %d\n", ans);
  }
  return ans;
}


x86 assembly (clang, -O0):
<factorial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%esi
lea    0xecb(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x0,-0x4(%rbp)
jne    115f <factorial+0x3f>
movl   $0x1,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xeb3(%rip),%rdi
call   1040 <printf@plt>
jmp    1180 <factorial+0x60>
mov    -0x4(%rbp),%edi
sub    $0x1,%edi
call   1030 <factorial@plt>
imul   -0x4(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xea3(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<factorial>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
lea    0xed4(%rip),%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
test   %ebx,%ebx
je     114f <factorial+0x2f>
lea    -0x1(%rbx),%edi
call   1030 <factorial@plt>
mov    %eax,%ebp
imul   %ebx,%ebp
lea    0xed3(%rip),%rdi
mov    %ebp,%esi
jmp    1160 <factorial+0x40>
lea    0xeb2(%rip),%rdi
mov    $0x1,%ebp
mov    $0x1,%esi
call   1040 <printf@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [8 x i8] c"n = %d\0A\00"
@.str.1 = [22 x i8] c"ans (base case) = %d\0A\00"
@.str.2 = [11 x i8] c"ans = %d\0A\00"
define i32 @factorial(i32 %0) {
%2 = tail call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %0)
%3 = icmp eq i32 %0, 0
br i1 %3, label %4, label %8
4: ; preds = %1
%5 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 1)
br label %6
6: ; preds = %4, %8
%7 = phi i32 [ %11, %8 ], [ 1, %4 ]
ret i32 %7
8: ; preds = %1
%9 = add nsw i32 %0, -1
%10 = tail call i32 @factorial(i32 %9)
%11 = mul nsw i32 %10, %0
%12 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %11)
br label %6
}