#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 */
char *calloc(int, int);
int /*<<< orphan*/ free(char *);
int mul_int_string(int, char *, char **);
char *realloc(char *, scalar_t__);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
int factorial(int num, char **factorial_ptr) {
  int status = 0;
  char *tmp = calloc(2, sizeof(char));
  int i;
  for (i = 1; i < num; ++i) {
    status = mul_int_string(i + 1, *factorial_ptr, &tmp);
    *factorial_ptr = realloc(*factorial_ptr, strlen(tmp) + 1);
    strcpy(*factorial_ptr, tmp);
  }
  free(tmp);
  return (status);
}


x86 assembly (clang, -O0):
<factorial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    $0x2,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1201 <factorial+0xa1>
mov    -0x24(%rbp),%edi
add    $0x1,%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    -0x20(%rbp),%rdx
call   1080 <mul_int_string@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x30(%rbp),%rdi
mov    %rax,%rsi
add    $0x1,%rsi
call   1070 <realloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1190 <factorial+0x30>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<factorial>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %edi,%r14d
mov    $0x2,%ebx
mov    $0x2,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,(%rsp)
cmp    $0x2,%r14d
jl     11f0 <factorial+0x90>
neg    %r14d
mov    %rsp,%r12
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%r15),%rsi
mov    %ebx,%edi
mov    %r12,%rdx
call   1080 <mul_int_string@plt>
mov    %eax,%r13d
mov    (%r15),%rbp
mov    (%rsp),%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rsi
mov    %rbp,%rdi
call   1070 <realloc@plt>
mov    %rax,(%r15)
mov    (%rsp),%rsi
mov    %rax,%rdi
call   1040 <strcpy@plt>
lea    (%r14,%rbx,1),%eax
add    $0x1,%eax
mov    %ebx,%ecx
add    $0x1,%ecx
mov    %ecx,%ebx
cmp    $0x1,%eax
jne    11a0 <factorial+0x40>
mov    (%rsp),%rax
jmp    11f3 <factorial+0x93>
xor    %r13d,%r13d
mov    %rax,%rdi
call   1030 <free@plt>
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @factorial(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
store i32 0, i32* %5
%8 = call i8* @calloc(i32 2, i32 1)
store i8* %8, i8** %6
store i32 1, i32* %7
br label %9
9: ; preds = %30, %2
%10 = load i32, i32* %7
%11 = load i32, i32* %3
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %33
13: ; preds = %9
%14 = load i32, i32* %7
%15 = add nsw i32 %14, 1
%16 = load i8**, i8*** %4
%17 = load i8*, i8** %16
%18 = call i32 @mul_int_string(i32 %15, i8* %17, i8** %6)
store i32 %18, i32* %5
%19 = load i8**, i8*** %4
%20 = load i8*, i8** %19
%21 = load i8*, i8** %6
%22 = call i64 @strlen(i8* %21)
%23 = add nsw i64 %22, 1
%24 = call i8* @realloc(i8* %20, i64 %23)
%25 = load i8**, i8*** %4
store i8* %24, i8** %25
%26 = load i8**, i8*** %4
%27 = load i8*, i8** %26
%28 = load i8*, i8** %6
%29 = call i32 @strcpy(i8* %27, i8* %28)
br label %30
30: ; preds = %13
%31 = load i32, i32* %7
%32 = add nsw i32 %31, 1
store i32 %32, i32* %7
br label %9
33: ; preds = %9
%34 = load i8*, i8** %6
%35 = call i32 @free(i8* %34)
%36 = load i32, i32* %5
ret i32 %36
}

LLVM IR (-O2):
define i32 @factorial(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = tail call dereferenceable_or_null(2) i8* @calloc(i32 2, i32 1)
store i8* %5, i8** %3
%6 = icmp sgt i32 %0, 1
br i1 %6, label %7, label %22
7: ; preds = %2, %7
%8 = phi i32 [ %9, %7 ], [ 1, %2 ]
%9 = add nuw nsw i32 %8, 1
%10 = load i8*, i8** %1
%11 = call i32 @mul_int_string(i32 %9, i8* %10, i8** %3)
%12 = load i8*, i8** %1
%13 = load i8*, i8** %3
%14 = call i64 @strlen(i8* dereferenceable(1) %13)
%15 = add nsw i64 %14, 1
%16 = call i8* @realloc(i8* %12, i64 %15)
store i8* %16, i8** %1
%17 = load i8*, i8** %3
%18 = call i32 @strcpy(i8* %16, i8* %17)
%19 = icmp eq i32 %9, %0
br i1 %19, label %20, label %7
20: ; preds = %7
%21 = load i8*, i8** %3
br label %22
22: ; preds = %20, %2
%23 = phi i8* [ %5, %2 ], [ %21, %20 ]
%24 = phi i32 [ 0, %2 ], [ %11, %20 ]
%25 = call i32 @free(i8* %23)
ret i32 %24
}