#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*/ add(char **, int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ mul(char *, int, char *, char);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int strlen(char *);
void factorial_calc(char *factorial, int factorial_deg) {
  char factor_n[((2 + 4U - 1) & ~(4U - 1)) + 1] = {'0'};
  char carry1[50] = {'0'};
  char carry2[50] = {'0'};
  char *carry_over[2] = {carry1, carry2};
  for (int i = 1; i <= factorial_deg; i++) {
    snprintf(factor_n, ((2 + 4U - 1) & ~(4U - 1)), "%d", i);
    for (int idx = 0, j = strlen(factor_n) - 1; j >= 0; j--, idx++)
      mul(factorial, idx, carry_over[j], factor_n[j] - '0');
    add(carry_over, 2);
    memcpy(factorial, carry_over[1], 50);
    memset(carry_over[0], '0', 50);
    memset(carry_over[1], '0', 50);
  }
  factorial[50 - 1] = '\0';
}


x86 assembly (clang, -O0):
<factorial_calc>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0xe88(%rip),%eax
mov    %eax,-0x11(%rbp)
mov    0xe83(%rip),%al
mov    %al,-0xd(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x32,%edx
call   1060 <memset@plt>
movb   $0x30,-0x50(%rbp)
lea    -0x90(%rbp),%rdi
xor    %esi,%esi
mov    $0x32,%edx
call   1060 <memset@plt>
movb   $0x30,-0x90(%rbp)
lea    -0x50(%rbp),%rax
mov    %rax,-0xa0(%rbp)
lea    -0x90(%rbp),%rax
mov    %rax,-0x98(%rbp)
movl   $0x1,-0xa4(%rbp)
mov    -0xa4(%rbp),%eax
cmp    -0xc(%rbp),%eax
jg     12e1 <factorial_calc+0x181>
lea    -0x11(%rbp),%rdi
mov    -0xa4(%rbp),%ecx
mov    $0x4,%esi
lea    0xe0b(%rip),%rdx
call   1050 <snprintf@plt>
movl   $0x0,-0xa8(%rbp)
lea    -0x11(%rbp),%rdi
call   1040 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0xac(%rbp)
cmpl   $0x0,-0xac(%rbp)
jl     127b <factorial_calc+0x11b>
mov    -0x8(%rbp),%rdi
mov    -0xa8(%rbp),%esi
movslq -0xac(%rbp),%rax
mov    -0xa0(%rbp,%rax,8),%rdx
ff
movslq -0xac(%rbp),%rax
movsbl -0x11(%rbp,%rax,1),%eax
sub    $0x30,%eax
movsbl %al,%ecx
call   1080 <mul@plt>
mov    -0xac(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xac(%rbp)
mov    -0xa8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xa8(%rbp)
jmp    121b <factorial_calc+0xbb>
lea    -0xa0(%rbp),%rdi
mov    $0x2,%esi
call   1030 <add@plt>
mov    -0x8(%rbp),%rdi
mov    -0x98(%rbp),%rsi
mov    $0x32,%edx
call   1070 <memcpy@plt>
mov    -0xa0(%rbp),%rdi
mov    $0x30,%esi
mov    $0x32,%edx
call   1060 <memset@plt>
mov    -0x98(%rbp),%rdi
mov    $0x30,%esi
mov    $0x32,%edx
call   1060 <memset@plt>
mov    -0xa4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xa4(%rbp)
jmp    11d5 <factorial_calc+0x75>
mov    -0x8(%rbp),%rax
movb   $0x0,0x31(%rax)
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<factorial_calc>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
mov    %rdi,%rbx
movb   $0x0,0xc(%rsp)
movl   $0x30,0x8(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movw   $0x0,0x90(%rsp)
movb   $0x30,0x60(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movw   $0x0,0x50(%rsp)
movb   $0x30,0x20(%rsp)
lea    0x60(%rsp),%rax
mov    %rax,0x10(%rsp)
lea    0x20(%rsp),%rax
mov    %rax,0x18(%rsp)
test   %esi,%esi
jle    12a0 <factorial_calc+0x140>
mov    %esi,%r15d
mov    $0x1,%ebp
lea    0x8(%rsp),%r12
jmp    1243 <factorial_calc+0xe3>
nopl   0x0(%rax,%rax,1)
lea    0x10(%rsp),%rdi
mov    $0x2,%esi
call   1030 <add@plt>
mov    0x18(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x32,%edx
call   1070 <memcpy@plt>
mov    0x10(%rsp),%rdi
mov    $0x30,%esi
mov    $0x32,%edx
call   1060 <memset@plt>
mov    0x18(%rsp),%rdi
mov    $0x30,%esi
mov    $0x32,%edx
call   1060 <memset@plt>
lea    0x1(%rbp),%eax
cmp    %r15d,%ebp
mov    %eax,%ebp
je     12a0 <factorial_calc+0x140>
mov    %r12,%rdi
mov    $0x4,%esi
lea    0xdb3(%rip),%rdx
mov    %ebp,%ecx
call   1050 <snprintf@plt>
mov    %r12,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
jle    11f0 <factorial_calc+0x90>
mov    %eax,%r13d
add    $0xffffffffffffffff,%r13
xor    %r14d,%r14d
nop
mov    %r13d,%eax
mov    0x10(%rsp,%rax,8),%rdx
movzbl 0x8(%rsp,%rax,1),%eax
add    $0xd0,%al
movsbl %al,%ecx
mov    %rbx,%rdi
mov    %r14d,%esi
call   1080 <mul@plt>
add    $0x1,%r14d
add    $0xffffffffffffffff,%r13
cmp    $0xffffffff,%r13d
jne    1270 <factorial_calc+0x110>
jmp    11f0 <factorial_calc+0x90>
movb   $0x0,0x31(%rbx)
add    $0x98,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.factorial_calc.factor_n = [5 x i8] c"0\00\00\00\00"
@.str = [3 x i8] c"%d\00"
define void @factorial_calc(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [5 x i8]
%6 = alloca [50 x i8]
%7 = alloca [50 x i8]
%8 = alloca [2 x i8*]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%12 = bitcast [5 x i8]* %5 to i8*
%13 = bitcast [50 x i8]* %6 to i8*
%14 = bitcast i8* %13 to <{ i8, [49 x i8] }>*
%15 = getelementptr inbounds <{ i8, [49 x i8] }>, <{ i8, [49 x i8] }>* %14, i32 0, i32 0
store i8 48, i8* %15
%16 = bitcast [50 x i8]* %7 to i8*
%17 = bitcast i8* %16 to <{ i8, [49 x i8] }>*
%18 = getelementptr inbounds <{ i8, [49 x i8] }>, <{ i8, [49 x i8] }>* %17, i32 0, i32 0
store i8 48, i8* %18
%19 = getelementptr inbounds [2 x i8*], [2 x i8*]* %8, i64 0, i64 0
%20 = getelementptr inbounds [50 x i8], [50 x i8]* %6, i64 0, i64 0
store i8* %20, i8** %19
%21 = getelementptr inbounds i8*, i8** %19, i64 1
%22 = getelementptr inbounds [50 x i8], [50 x i8]* %7, i64 0, i64 0
store i8* %22, i8** %21
store i32 1, i32* %9
br label %23
23: ; preds = %70, %2
%24 = load i32, i32* %9
%25 = load i32, i32* %4
%26 = icmp sle i32 %24, %25
br i1 %26, label %27, label %73
27: ; preds = %23
%28 = getelementptr inbounds [5 x i8], [5 x i8]* %5, i64 0, i64 0
%29 = load i32, i32* %9
%30 = call i32 @snprintf(i8* %28, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %29)
store i32 0, i32* %10
%31 = getelementptr inbounds [5 x i8], [5 x i8]* %5, i64 0, i64 0
%32 = call i32 @strlen(i8* %31)
%33 = sub nsw i32 %32, 1
store i32 %33, i32* %11
br label %34
34: ; preds = %52, %27
%35 = load i32, i32* %11
%36 = icmp sge i32 %35, 0
br i1 %36, label %37, label %57
37: ; preds = %34
%38 = load i8*, i8** %3
%39 = load i32, i32* %10
%40 = load i32, i32* %11
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds [2 x i8*], [2 x i8*]* %8, i64 0, i64 %41
%43 = load i8*, i8** %42
%44 = load i32, i32* %11
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds [5 x i8], [5 x i8]* %5, i64 0, i64 %45
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = sub nsw i32 %48, 48
%50 = trunc i32 %49 to i8
%51 = call i32 @mul(i8* %38, i32 %39, i8* %43, i8 signext %50)
br label %52
52: ; preds = %37
%53 = load i32, i32* %11
%54 = add nsw i32 %53, -1
store i32 %54, i32* %11
%55 = load i32, i32* %10
%56 = add nsw i32 %55, 1
store i32 %56, i32* %10
br label %34
57: ; preds = %34
%58 = getelementptr inbounds [2 x i8*], [2 x i8*]* %8, i64 0, i64 0
%59 = call i32 @add(i8** %58, i32 2)
%60 = load i8*, i8** %3
%61 = getelementptr inbounds [2 x i8*], [2 x i8*]* %8, i64 0, i64 1
%62 = load i8*, i8** %61
%63 = call i32 @memcpy(i8* %60, i8* %62, i32 50)
%64 = getelementptr inbounds [2 x i8*], [2 x i8*]* %8, i64 0, i64 0
%65 = load i8*, i8** %64
%66 = call i32 @memset(i8* %65, i32 48, i32 50)
%67 = getelementptr inbounds [2 x i8*], [2 x i8*]* %8, i64 0, i64 1
%68 = load i8*, i8** %67
%69 = call i32 @memset(i8* %68, i32 48, i32 50)
br label %70
70: ; preds = %57
%71 = load i32, i32* %9
%72 = add nsw i32 %71, 1
store i32 %72, i32* %9
br label %23
73: ; preds = %23
%74 = load i8*, i8** %3
%75 = getelementptr inbounds i8, i8* %74, i64 49
store i8 0, i8* %75
ret void
}

LLVM IR (-O2):
@__const.factorial_calc.factor_n = [5 x i8] c"0\00\00\00\00"
@.str = [3 x i8] c"%d\00"
define void @factorial_calc(i8* %0, i32 %1) {
%3 = alloca [5 x i8]
%4 = alloca [50 x i8]
%5 = alloca [50 x i8]
%6 = alloca [2 x i8*]
%7 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [50 x i8], [50 x i8]* %4, i64 0, i64 0
store i8 48, i8* %8
%9 = getelementptr inbounds [50 x i8], [50 x i8]* %5, i64 0, i64 0
store i8 48, i8* %9
%10 = bitcast [2 x i8*]* %6 to i8*
%11 = getelementptr inbounds [2 x i8*], [2 x i8*]* %6, i64 0, i64 0
store i8* %8, i8** %11
%12 = getelementptr inbounds [2 x i8*], [2 x i8*]* %6, i64 0, i64 1
store i8* %9, i8** %12
%13 = icmp slt i32 %1, 1
br i1 %13, label %14, label %16
14: ; preds = %23, %2
%15 = getelementptr inbounds i8, i8* %0, i64 49
store i8 0, i8* %15
ret void
16: ; preds = %2, %23
%17 = phi i32 [ %31, %23 ], [ 1, %2 ]
%18 = call i32 @snprintf(i8* %7, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = call i32 @strlen(i8* %7)
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = zext i32 %19 to i64
br label %33
23: ; preds = %33, %16
%24 = call i32 @add(i8** %11, i32 2)
%25 = load i8*, i8** %12
%26 = call i32 @memcpy(i8* %0, i8* %25, i32 50)
%27 = load i8*, i8** %11
%28 = call i32 @memset(i8* %27, i32 48, i32 50)
%29 = load i8*, i8** %12
%30 = call i32 @memset(i8* %29, i32 48, i32 50)
%31 = add nuw i32 %17, 1
%32 = icmp eq i32 %17, %1
br i1 %32, label %14, label %16
33: ; preds = %21, %33
%34 = phi i64 [ %22, %21 ], [ %36, %33 ]
%35 = phi i32 [ 0, %21 ], [ %44, %33 ]
%36 = add nsw i64 %34, -1
%37 = and i64 %36, 4294967295
%38 = getelementptr inbounds [2 x i8*], [2 x i8*]* %6, i64 0, i64 %37
%39 = load i8*, i8** %38
%40 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 %37
%41 = load i8, i8* %40
%42 = add i8 %41, -48
%43 = call i32 @mul(i8* %0, i32 %35, i8* %39, i8 signext %42)
%44 = add nuw nsw i32 %35, 1
%45 = icmp eq i32 %44, %19
br i1 %45, label %23, label %33
}