#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 */
long double CalculateFactorial(long double);
char EOF;
int EXIT_SUCCESS;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char getchar();
int /*<<< orphan*/ isspace(char);
int /*<<< orphan*/ printf(char *, long double, long double);
int /*<<< orphan*/ puts(char *);
int scanf(char *, long double *);
int /*<<< orphan*/ stderr;
int main(void) {
  long double number = 0.0L;
  long double fact = 0.0L;
  int scanfState = 0;
  char c = '\0';
  int isNumberValid = 0;
  puts("Please enter a positive number to calculate its factorial: ");
  while (!isNumberValid) {
    isNumberValid = 1;
    if (scanf(" %Lf", &number) != 1) {
      isNumberValid = 0;
    }
    if (number < 0.0L) {
      isNumberValid = 0;
    }
    while ((c = getchar()) != '\n' && c != EOF) {
      if (!isspace(c)) {
        isNumberValid = 0;
      }
    }
    if (!isNumberValid) {
      if (number < 0.0) {
        fprintf(stderr, "Cannot calculate a factorial of a negative number."
                        " Please try again.\n");
      } else {
        fprintf(stderr, "Input invalid. Please try again.\n");
      }
    }
  }
  fact = CalculateFactorial(number);
  printf("Factorial of %g: %g\n", number, fact);
  getchar();
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
fldz
fld    %st(0)
fstpt  -0x20(%rbp)
fstpt  -0x30(%rbp)
movl   $0x0,-0x34(%rbp)
movb   $0x0,-0x35(%rbp)
movl   $0x0,-0x3c(%rbp)
lea    0xe5e(%rip),%rdi
call   1030 <puts@plt>
cmpl   $0x0,-0x3c(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11bd <main+0x4d>
jmp    12ba <main+0x14a>
movl   $0x1,-0x3c(%rbp)
lea    0xe71(%rip),%rdi
lea    -0x20(%rbp),%rsi
call   1090 <scanf@plt>
cmp    $0x1,%eax
je     11e4 <main+0x74>
movl   $0x0,-0x3c(%rbp)
fldt   -0x20(%rbp)
fldz
fucomip %st(1),%st
fstp   %st(0)
jbe    11ff <main+0x8f>
jmp    11f8 <main+0x88>
movl   $0x0,-0x3c(%rbp)
jmp    1204 <main+0x94>
mov    $0x0,%al
call   1070 <getchar@plt>
mov    %al,-0x35(%rbp)
movsbl %al,%ecx
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x3d(%rbp)
je     1235 <main+0xc5>
movsbl -0x35(%rbp),%eax
mov    0x2dbe(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
setne  %al
mov    %al,-0x3d(%rbp)
mov    -0x3d(%rbp),%al
test   $0x1,%al
jne    1245 <main+0xd5>
jmp    1263 <main+0xf3>
movsbl -0x35(%rbp),%edi
call   1060 <isspace@plt>
cmp    $0x0,%eax
jne    125e <main+0xee>
movl   $0x0,-0x3c(%rbp)
jmp    1204 <main+0x94>
cmpl   $0x0,-0x3c(%rbp)
jne    12b5 <main+0x145>
fldt   -0x20(%rbp)
fldz
fucomip %st(1),%st
fstp   %st(0)
jbe    129b <main+0x12b>
jmp    1281 <main+0x111>
mov    0x2d50(%rip),%rax
mov    (%rax),%edi
lea    0xdb0(%rip),%rsi
call   1080 <fprintf@plt>
jmp    12b0 <main+0x140>
mov    0x2d36(%rip),%rax
mov    (%rax),%edi
lea    0xddc(%rip),%rsi
call   1080 <fprintf@plt>
jmp    12b5 <main+0x145>
jmp    11a7 <main+0x37>
fldt   -0x20(%rbp)
mov    %rsp,%rax
fstpt  (%rax)
call   1040 <CalculateFactorial@plt>
fstpt  -0x30(%rbp)
fldt   -0x20(%rbp)
fldt   -0x30(%rbp)
mov    %rsp,%rax
fstpt  0x10(%rax)
fstpt  (%rax)
lea    0xdca(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   1070 <getchar@plt>
mov    0x2cde(%rip),%rax
mov    (%rax),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
fldz
fld    %st(0)
fstpt  0x3c(%rsp)
fstpt  0x20(%rsp)
lea    0xe6f(%rip),%rdi
call   1030 <puts@plt>
lea    0xe9f(%rip),%r14
lea    0x20(%rsp),%r15
mov    0x2e3f(%rip),%rbx
lea    0xe91(%rip),%r13
lea    0xed0(%rip),%rbp
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %r15,%rsi
call   1090 <scanf@plt>
cmp    $0x1,%eax
sete   %al
fldt   0x20(%rsp)
fldt   0x3c(%rsp)
fucomip %st(1),%st
fstp   %st(0)
setbe  %cl
and    %al,%cl
movzbl %cl,%r12d
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   1070 <getchar@plt>
cmp    $0xa,%al
je     1210 <main+0xa0>
cmp    (%rbx),%al
je     1210 <main+0xa0>
movsbl %al,%edi
call   1060 <isspace@plt>
test   %eax,%eax
cmove  %eax,%r12d
jmp    11f0 <main+0x80>
nop
fldt   0x20(%rsp)
test   %r12d,%r12d
jne    1238 <main+0xc8>
fldt   0x3c(%rsp)
fucomip %st(1),%st
fstp   %st(0)
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
mov    %rbp,%rsi
cmova  %r13,%rsi
call   1080 <fprintf@plt>
jmp    11c0 <main+0x50>
fstpt  (%rsp)
call   1040 <CalculateFactorial@plt>
fldt   0x20(%rsp)
fxch   %st(1)
fstpt  0x10(%rsp)
fstpt  (%rsp)
lea    0xe55(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
call   1070 <getchar@plt>
mov    0x2d69(%rip),%rax
mov    (%rax),%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [60 x i8] c"Please enter a positive number to calculate its factorial: \00"
@.str.1 = [5 x i8] c" %Lf\00"
@EOF = global i8 0
@stderr = global i32 0
@.str.2 = [70 x i8] c"Cannot calculate a factorial of a negative number. Please try again.\0A\00"
@.str.3 = [34 x i8] c"Input invalid. Please try again.\0A\00"
@.str.4 = [21 x i8] c"Factorial of %g: %g\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca x86_fp80
%3 = alloca x86_fp80
%4 = alloca i32
%5 = alloca i8
%6 = alloca i32
store i32 0, i32* %1
store x86_fp80 0xK00000000000000000000, x86_fp80* %2
store x86_fp80 0xK00000000000000000000, x86_fp80* %3
store i32 0, i32* %4
store i8 0, i8* %5
store i32 0, i32* %6
%7 = call i32 @puts(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %52, %0
%9 = load i32, i32* %6
%10 = icmp ne i32 %9, 0
%11 = xor i1 %10, true
br i1 %11, label %12, label %53
12: ; preds = %8
store i32 1, i32* %6
%13 = call i32 @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), x86_fp80* %2)
%14 = icmp ne i32 %13, 1
br i1 %14, label %15, label %16
15: ; preds = %12
store i32 0, i32* %6
br label %16
16: ; preds = %15, %12
%17 = load x86_fp80, x86_fp80* %2
%18 = fcmp olt x86_fp80 %17, 0xK00000000000000000000
br i1 %18, label %19, label %20
19: ; preds = %16
store i32 0, i32* %6
br label %20
20: ; preds = %19, %16
br label %21
21: ; preds = %38, %20
%22 = call signext i8 (...) @getchar()
store i8 %22, i8* %5
%23 = sext i8 %22 to i32
%24 = icmp ne i32 %23, 10
br i1 %24, label %25, label %31
25: ; preds = %21
%26 = load i8, i8* %5
%27 = sext i8 %26 to i32
%28 = load i8, i8* @EOF
%29 = sext i8 %28 to i32
%30 = icmp ne i32 %27, %29
br label %31
31: ; preds = %25, %21
%32 = phi i1 [ false, %21 ], [ %30, %25 ]
br i1 %32, label %33, label %39
33: ; preds = %31
%34 = load i8, i8* %5
%35 = call i32 @isspace(i8 signext %34)
%36 = icmp ne i32 %35, 0
br i1 %36, label %38, label %37
37: ; preds = %33
store i32 0, i32* %6
br label %38
38: ; preds = %37, %33
br label %21
39: ; preds = %31
%40 = load i32, i32* %6
%41 = icmp ne i32 %40, 0
br i1 %41, label %52, label %42
42: ; preds = %39
%43 = load x86_fp80, x86_fp80* %2
%44 = fcmp olt x86_fp80 %43, 0xK00000000000000000000
br i1 %44, label %45, label %48
45: ; preds = %42
%46 = load i32, i32* @stderr
%47 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.2, i64 0, i64 0))
br label %51
48: ; preds = %42
%49 = load i32, i32* @stderr
%50 = call i32 @fprintf(i32 %49, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
br label %51
51: ; preds = %48, %45
br label %52
52: ; preds = %51, %39
br label %8
53: ; preds = %8
%54 = load x86_fp80, x86_fp80* %2
%55 = call x86_fp80 @CalculateFactorial(x86_fp80 %54)
store x86_fp80 %55, x86_fp80* %3
%56 = load x86_fp80, x86_fp80* %2
%57 = load x86_fp80, x86_fp80* %3
%58 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), x86_fp80 %56, x86_fp80 %57)
%59 = call signext i8 (...) @getchar()
%60 = load i32, i32* @EXIT_SUCCESS
ret i32 %60
}

LLVM IR (-O2):
@.str = [60 x i8] c"Please enter a positive number to calculate its factorial: \00"
@.str.1 = [5 x i8] c" %Lf\00"
@EOF = global i8 0
@stderr = global i32 0
@.str.2 = [70 x i8] c"Cannot calculate a factorial of a negative number. Please try again.\0A\00"
@.str.3 = [34 x i8] c"Input invalid. Please try again.\0A\00"
@.str.4 = [21 x i8] c"Factorial of %g: %g\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca x86_fp80
%2 = bitcast x86_fp80* %1 to i8*
store x86_fp80 0xK00000000000000000000, x86_fp80* %1
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([60 x i8], [60 x i8]* @.str, i64 0, i64 0))
br label %4
4: ; preds = %31, %0
%5 = call i32 @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), x86_fp80* %1)
%6 = icmp eq i32 %5, 1
%7 = load x86_fp80, x86_fp80* %1
%8 = fcmp uge x86_fp80 %7, 0xK00000000000000000000
%9 = and i1 %6, %8
%10 = zext i1 %9 to i32
%11 = call signext i8 (...) @getchar()
%12 = icmp ne i8 %11, 10
%13 = load i8, i8* @EOF
%14 = icmp ne i8 %11, %13
%15 = select i1 %12, i1 %14, i1 false
br i1 %15, label %16, label %27
16: ; preds = %4, %16
%17 = phi i8 [ %22, %16 ], [ %11, %4 ]
%18 = phi i32 [ %21, %16 ], [ %10, %4 ]
%19 = call i32 @isspace(i8 signext %17)
%20 = icmp eq i32 %19, 0
%21 = select i1 %20, i32 0, i32 %18
%22 = call signext i8 (...) @getchar()
%23 = icmp ne i8 %22, 10
%24 = load i8, i8* @EOF
%25 = icmp ne i8 %22, %24
%26 = select i1 %23, i1 %25, i1 false
br i1 %26, label %16, label %27
27: ; preds = %16, %4
%28 = phi i32 [ %10, %4 ], [ %21, %16 ]
%29 = icmp eq i32 %28, 0
%30 = load x86_fp80, x86_fp80* %1
br i1 %29, label %31, label %36
31: ; preds = %27
%32 = fcmp olt x86_fp80 %30, 0xK00000000000000000000
%33 = load i32, i32* @stderr
%34 = select i1 %32, i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0)
%35 = call i32 @fprintf(i32 %33, i8* %34)
br label %4
36: ; preds = %27
%37 = call x86_fp80 @CalculateFactorial(x86_fp80 %30)
%38 = load x86_fp80, x86_fp80* %1
%39 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), x86_fp80 %38, x86_fp80 %37)
%40 = call signext i8 (...) @getchar()
%41 = load i32, i32* @EXIT_SUCCESS
ret i32 %41
}