#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*/ fact(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ rfact(int);
int scanf(char *, int *);
int main(void) {
  int num;
  printf("This program calculates factorials.\n");
  printf("Enter a value in the range 0-12 (q to quit):\n");
  while (scanf("%d", &num) == 1) {
    if (num < 0) {
      printf("No negative numbers, please.\n");
    } else if (num > 12) {
      printf("Keep input under13.\n");
    } else {
      printf("loop: %d factorial = %ld\n", num, fact(num));
      printf("recursion: %d factorial = %ld\n", num, rfact(num));
    }
    printf("Enter a value in the range 0-12 (q to quit):\n");
  }
  printf("Bye.\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeaa(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xec1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xee1(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1060 <scanf@plt>
cmp    $0x1,%eax
jne    1218 <main+0xd8>
cmpl   $0x0,-0x8(%rbp)
jge    11a1 <main+0x61>
lea    0xec1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1205 <main+0xc5>
cmpl   $0xc,-0x8(%rbp)
jle    11be <main+0x7e>
lea    0xec2(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1200 <main+0xc0>
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <fact@plt>
mov    -0x10(%rbp),%esi
mov    %eax,%edx
lea    0xeb1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <rfact@plt>
mov    -0xc(%rbp),%esi
mov    %eax,%edx
lea    0xeaa(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1205 <main+0xc5>
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    116b <main+0x2b>
lea    0xea3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xeda(%rip),%rdi
call   1030 <puts@plt>
lea    0xef7(%rip),%rdi
call   1030 <puts@plt>
lea    0xe86(%rip),%rdi
lea    0x4(%rsp),%rsi
call   1070 <scanf@plt>
cmp    $0x1,%eax
jne    1224 <main+0xd4>
lea    0xf12(%rip),%r14
lea    0xeca(%rip),%r15
lea    0xe5e(%rip),%r12
lea    0x4(%rsp),%rbx
lea    0xe6f(%rip),%r13
jmp    11d0 <main+0x80>
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %r15,%rdi
call   1030 <puts@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
call   1070 <scanf@plt>
cmp    $0x1,%eax
jne    1224 <main+0xd4>
mov    0x4(%rsp),%ebp
test   %ebp,%ebp
js     11b0 <main+0x60>
cmp    $0xd,%ebp
jb     11f0 <main+0xa0>
lea    0xeae(%rip),%rdi
jmp    11b3 <main+0x63>
cs nopw 0x0(%rax,%rax,1)
mov    %ebp,%edi
call   1060 <fact@plt>
lea    0xe05(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x4(%rsp),%ebp
mov    %ebp,%edi
call   1050 <rfact@plt>
mov    %r13,%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11b8 <main+0x68>
lea    0xe35(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [37 x i8] c"This program calculates factorials.\0A\00"
@.str.1 = [46 x i8] c"Enter a value in the range 0-12 (q to quit):\0A\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [30 x i8] c"No negative numbers, please.\0A\00"
@.str.4 = [21 x i8] c"Keep input under13.\0A\00"
@.str.5 = [26 x i8] c"loop: %d factorial = %ld\0A\00"
@.str.6 = [31 x i8] c"recursion: %d factorial = %ld\0A\00"
@.str.7 = [6 x i8] c"Bye.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%4 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0))
br label %5
5: ; preds = %28, %0
%6 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %2)
%7 = icmp eq i32 %6, 1
br i1 %7, label %8, label %30
8: ; preds = %5
%9 = load i32, i32* %2
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
br label %28
13: ; preds = %8
%14 = load i32, i32* %2
%15 = icmp sgt i32 %14, 12
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %27
18: ; preds = %13
%19 = load i32, i32* %2
%20 = load i32, i32* %2
%21 = call i32 @fact(i32 %20)
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i32 %19, i32 %21)
%23 = load i32, i32* %2
%24 = load i32, i32* %2
%25 = call i32 @rfact(i32 %24)
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), i32 %23, i32 %25)
br label %27
27: ; preds = %18, %16
br label %28
28: ; preds = %27, %11
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0))
br label %5
30: ; preds = %5
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str.2 = [3 x i8] c"%d\00"
@.str.5 = [26 x i8] c"loop: %d factorial = %ld\0A\00"
@.str.6 = [31 x i8] c"recursion: %d factorial = %ld\0A\00"
@str = [36 x i8] c"This program calculates factorials.\00"
@str.9 = [5 x i8] c"Bye.\00"
@str.10 = [45 x i8] c"Enter a value in the range 0-12 (q to quit):\00"
@str.11 = [20 x i8] c"Keep input under13.\00"
@str.12 = [29 x i8] c"No negative numbers, please.\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str, i64 0, i64 0))
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @str.10, i64 0, i64 0))
%5 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %1)
%6 = icmp eq i32 %5, 1
br i1 %6, label %7, label %26
7: ; preds = %0, %22
%8 = load i32, i32* %1
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.12, i64 0, i64 0))
br label %22
12: ; preds = %7
%13 = icmp ugt i32 %8, 12
br i1 %13, label %14, label %16
14: ; preds = %12
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.11, i64 0, i64 0))
br label %22
16: ; preds = %12
%17 = call i32 @fact(i32 %8)
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i32 %8, i32 %17)
%19 = load i32, i32* %1
%20 = call i32 @rfact(i32 %19)
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), i32 %19, i32 %20)
br label %22
22: ; preds = %14, %16, %10
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @str.10, i64 0, i64 0))
%24 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %1)
%25 = icmp eq i32 %24, 1
br i1 %25, label %7, label %26
26: ; preds = %22, %0
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str.9, i64 0, i64 0))
ret i32 0
}