#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 */
typedef int uint64_t;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
uint64_t factorial(uint64_t n) {
  uint64_t product = 1;
  if (n < 2) {
    return 1;
  }
  for (; n > 0; n--) {
    uint64_t prev = product;
    product *= n;
    if (product < prev) {
      fprintf(stderr, "Overflowed\n");
      return product;
    }
  }
  return product;
}


x86 assembly (clang, -O0):
<factorial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x1,-0xc(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1138 <factorial+0x28>
movl   $0x1,-0x4(%rbp)
jmp    119c <factorial+0x8c>
jmp    113d <factorial+0x2d>
cmpl   $0x0,-0x8(%rbp)
jle    1196 <factorial+0x86>
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
imul   -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    1183 <factorial+0x73>
mov    0x2e76(%rip),%rax
mov    (%rax),%edi
lea    0xe8d(%rip),%rsi
call   1030 <fprintf@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    119c <factorial+0x8c>
jmp    1188 <factorial+0x78>
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
jmp    113d <factorial+0x2d>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<factorial>:
push   %rbx
mov    $0x1,%ebx
cmp    $0x2,%edi
jl     114a <factorial+0x3a>
mov    $0x1,%ebx
mov    %ebx,%eax
imul   %edi,%ebx
cmp    %eax,%ebx
jl     1135 <factorial+0x25>
lea    -0x1(%rdi),%eax
cmp    $0x2,%edi
mov    %eax,%edi
jge    1120 <factorial+0x10>
jmp    114a <factorial+0x3a>
mov    0x2ea4(%rip),%rax
mov    (%rax),%edi
lea    0xebb(%rip),%rsi
call   1030 <fprintf@plt>
mov    %ebx,%eax
pop    %rbx
ret

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

LLVM IR (-O2):
@stderr = global i32 0
@.str = [12 x i8] c"Overflowed\0A\00"
define i32 @factorial(i32 %0) {
%2 = icmp slt i32 %0, 2
br i1 %2, label %14, label %6
3: ; preds = %6
%4 = add nsw i32 %8, -1
%5 = icmp sgt i32 %8, 1
br i1 %5, label %6, label %14
6: ; preds = %1, %3
%7 = phi i32 [ %9, %3 ], [ 1, %1 ]
%8 = phi i32 [ %4, %3 ], [ %0, %1 ]
%9 = mul nsw i32 %7, %8
%10 = icmp slt i32 %9, %7
br i1 %10, label %11, label %3
11: ; preds = %6
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %3, %11, %1
%15 = phi i32 [ 1, %1 ], [ %9, %11 ], [ %9, %3 ]
ret i32 %15
}