#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 num) {
  int fact = 1;
  int esfact = 0;
  int i = 1;
  while (fact <= num) {
    fact = (fact * i);
    if (fact == num) {
      esfact = 1;
      printf("El numero %d es factorial de %d \n", num, i);
      break;
    } else {
      i++;
    }
  }
  if (esfact == 0) {
    i = (i - 2);
    printf("El numero %d no es el resultado de ningun factorial \n", num);
    printf("El factorial menor mas cercano a %d es %d! \n", num, i);
  }
}


x86 assembly (clang, -O0):
<factorial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x1,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jg     1180 <factorial+0x70>
mov    -0xc(%rbp),%eax
imul   -0x14(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jne    1172 <factorial+0x62>
movl   $0x1,-0x10(%rbp)
mov    -0x8(%rbp),%esi
mov    -0x14(%rbp),%edx
lea    0xe9a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1180 <factorial+0x70>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1130 <factorial+0x20>
cmpl   $0x0,-0x10(%rbp)
jne    11b8 <factorial+0xa8>
mov    -0x14(%rbp),%eax
sub    $0x2,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe85(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%esi
mov    -0x14(%rbp),%edx
lea    0xea7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<factorial>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
test   %edi,%edi
jle    1143 <factorial+0x33>
mov    $0x1,%eax
mov    $0xffffffff,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    0x2(%rbx),%edx
imul   %edx,%eax
cmp    %ebp,%eax
je     1165 <factorial+0x55>
add    $0x1,%ebx
cmp    %ebp,%eax
jle    1130 <factorial+0x20>
jmp    1148 <factorial+0x38>
mov    $0xffffffff,%ebx
lea    0xed3(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xef9(%rip),%rdi
mov    %ebp,%esi
mov    %ebx,%edx
jmp    116e <factorial+0x5e>
lea    0xe94(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1030 <printf@plt>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [34 x i8] c"El numero %d es factorial de %d \0A\00"
@.str.1 = [54 x i8] c"El numero %d no es el resultado de ningun factorial \0A\00"
@.str.2 = [45 x i8] c"El factorial menor mas cercano a %d es %d! \0A\00"
define i32 @factorial(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i32 1, i32* %4
store i32 0, i32* %5
store i32 1, i32* %6
br label %7
7: ; preds = %25, %1
%8 = load i32, i32* %4
%9 = load i32, i32* %3
%10 = icmp sle i32 %8, %9
br i1 %10, label %11, label %26
11: ; preds = %7
%12 = load i32, i32* %4
%13 = load i32, i32* %6
%14 = mul nsw i32 %12, %13
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = load i32, i32* %3
%17 = icmp eq i32 %15, %16
br i1 %17, label %18, label %22
18: ; preds = %11
store i32 1, i32* %5
%19 = load i32, i32* %3
%20 = load i32, i32* %6
%21 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %19, i32 %20)
br label %26
22: ; preds = %11
%23 = load i32, i32* %6
%24 = add nsw i32 %23, 1
store i32 %24, i32* %6
br label %25
25: ; preds = %22
br label %7
26: ; preds = %18, %7
%27 = load i32, i32* %5
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %37
29: ; preds = %26
%30 = load i32, i32* %6
%31 = sub nsw i32 %30, 2
store i32 %31, i32* %6
%32 = load i32, i32* %3
%33 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0), i32 %32)
%34 = load i32, i32* %3
%35 = load i32, i32* %6
%36 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i32 %34, i32 %35)
br label %37
37: ; preds = %29, %26
%38 = load i32, i32* %2
ret i32 %38
}

LLVM IR (-O2):
@.str = [34 x i8] c"El numero %d es factorial de %d \0A\00"
@.str.1 = [54 x i8] c"El numero %d no es el resultado de ningun factorial \0A\00"
@.str.2 = [45 x i8] c"El factorial menor mas cercano a %d es %d! \0A\00"
define i32 @factorial(i32 %0) {
%2 = icmp slt i32 %0, 1
br i1 %2, label %15, label %3
3: ; preds = %1, %10
%4 = phi i32 [ %11, %10 ], [ 1, %1 ]
%5 = phi i32 [ %6, %10 ], [ 1, %1 ]
%6 = mul nsw i32 %4, %5
%7 = icmp eq i32 %6, %0
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %4)
br label %19
10: ; preds = %3
%11 = add nuw nsw i32 %4, 1
%12 = icmp sgt i32 %6, %0
br i1 %12, label %13, label %3
13: ; preds = %10
%14 = add nsw i32 %4, -1
br label %15
15: ; preds = %13, %1
%16 = phi i32 [ -1, %1 ], [ %14, %13 ]
%17 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%18 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i32 %0, i32 %16)
br label %19
19: ; preds = %8, %15
ret i32 undef
}