#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 atoi(char *);
double factorial(int);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  if (argc < 2) {
    printf("%s\n%s\n", "Usage: ./program_name number_of_digits",
           "You must input a valid number");
    return -1;
  }
  int size = atoi(argv[1]);
  if (size < 1) {
    printf("Input a valid number between 1 and %d\n", 34);
    return -1;
  } else if (size > 34) {
    size = 34;
  }
  double decimal = 0.0;
  double e = 0.0;
  int i;
  for (i = 0; i < size; i++) {
    decimal = 1.0 / factorial(i);
    e += decimal;
  }
  printf("The mathematical constant e with %d decimals is %.*f\n", size, size,
         e);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1178 <main+0x48>
lea    0xeb1(%rip),%rdi
lea    0xeb1(%rip),%rsi
lea    0xed1(%rip),%rdx
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1243 <main+0x113>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <atoi@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jge    11b1 <main+0x81>
lea    0xebb(%rip),%rdi
mov    $0x22,%esi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1243 <main+0x113>
cmpl   $0x22,-0x14(%rbp)
jle    11c2 <main+0x92>
movl   $0x22,-0x14(%rbp)
jmp    11c7 <main+0x97>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1223 <main+0xf3>
mov    -0x2c(%rbp),%edi
call   1030 <factorial@plt>
movaps %xmm0,%xmm1
movsd  0xe03(%rip),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
addsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11de <main+0xae>
mov    -0x14(%rbp),%esi
mov    -0x14(%rbp),%edx
movsd  -0x28(%rbp),%xmm0
lea    0xe46(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
cmp    $0x1,%edi
jg     1156 <main+0x26>
lea    0xec9(%rip),%rdi
lea    0xec9(%rip),%rsi
lea    0xee9(%rip),%rdx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11d1 <main+0xa1>
mov    0x8(%rsi),%rdi
call   1050 <atoi@plt>
test   %eax,%eax
jle    11be <main+0x8e>
cmp    $0x22,%eax
mov    $0x22,%ebx
cmovb  %eax,%ebx
xorpd  %xmm0,%xmm0
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movsd  %xmm0,(%rsp)
mov    %ebp,%edi
call   1030 <factorial@plt>
movsd  0xe6c(%rip),%xmm1
divsd  %xmm0,%xmm1
movsd  (%rsp),%xmm0
addsd  %xmm1,%xmm0
add    $0x1,%ebp
cmp    %ebp,%ebx
jne    1180 <main+0x50>
lea    0xecc(%rip),%rdi
mov    %ebx,%esi
mov    %ebx,%edx
mov    $0x1,%al
call   1040 <printf@plt>
xor    %eax,%eax
jmp    11d6 <main+0xa6>
lea    0xe8f(%rip),%rdi
mov    $0x22,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"%s\0A%s\0A\00"
@.str.1 = [39 x i8] c"Usage: ./program_name number_of_digits\00"
@.str.2 = [30 x i8] c"You must input a valid number\00"
@.str.3 = [39 x i8] c"Input a valid number between 1 and %d\0A\00"
@.str.4 = [54 x i8] c"The mathematical constant e with %d decimals is %.*f\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca double
%8 = alloca double
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 2
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %48
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32 @atoi(i8* %17)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = icmp slt i32 %19, 1
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i32 34)
store i32 -1, i32* %3
br label %48
23: ; preds = %14
%24 = load i32, i32* %6
%25 = icmp sgt i32 %24, 34
br i1 %25, label %26, label %27
26: ; preds = %23
store i32 34, i32* %6
br label %27
27: ; preds = %26, %23
br label %28
28: ; preds = %27
store double 0.000000e+00, double* %7
store double 0.000000e+00, double* %8
store i32 0, i32* %9
br label %29
29: ; preds = %40, %28
%30 = load i32, i32* %9
%31 = load i32, i32* %6
%32 = icmp slt i32 %30, %31
br i1 %32, label %33, label %43
33: ; preds = %29
%34 = load i32, i32* %9
%35 = call double @factorial(i32 %34)
%36 = fdiv double 1.000000e+00, %35
store double %36, double* %7
%37 = load double, double* %7
%38 = load double, double* %8
%39 = fadd double %38, %37
store double %39, double* %8
br label %40
40: ; preds = %33
%41 = load i32, i32* %9
%42 = add nsw i32 %41, 1
store i32 %42, i32* %9
br label %29
43: ; preds = %29
%44 = load i32, i32* %6
%45 = load i32, i32* %6
%46 = load double, double* %8
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.4, i64 0, i64 0), i32 %44, i32 %45, double %46)
store i32 0, i32* %3
br label %48
48: ; preds = %43, %21, %12
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
@.str = [7 x i8] c"%s\0A%s\0A\00"
@.str.1 = [39 x i8] c"Usage: ./program_name number_of_digits\00"
@.str.2 = [30 x i8] c"You must input a valid number\00"
@.str.3 = [39 x i8] c"Input a valid number between 1 and %d\0A\00"
@.str.4 = [54 x i8] c"The mathematical constant e with %d decimals is %.*f\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %26
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32 @atoi(i8* %8)
%10 = icmp slt i32 %9, 1
br i1 %10, label %11, label %13
11: ; preds = %6
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i32 34)
br label %26
13: ; preds = %6
%14 = icmp ult i32 %9, 34
%15 = select i1 %14, i32 %9, i32 34
br label %16
16: ; preds = %13, %16
%17 = phi i32 [ 0, %13 ], [ %22, %16 ]
%18 = phi double [ 0.000000e+00, %13 ], [ %21, %16 ]
%19 = tail call double @factorial(i32 %17)
%20 = fdiv double 1.000000e+00, %19
%21 = fadd double %18, %20
%22 = add nuw nsw i32 %17, 1
%23 = icmp eq i32 %22, %15
br i1 %23, label %24, label %16
24: ; preds = %16
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @.str.4, i64 0, i64 0), i32 %15, i32 %15, double %21)
br label %26
26: ; preds = %11, %24, %4
%27 = phi i32 [ -1, %4 ], [ -1, %11 ], [ 0, %24 ]
ret i32 %27
}