#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 atoi(char *);
scalar_t__ calloc(int, int);
int factorial(int, char **);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ printf(char *, ...);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int main(int argc, char **argv) {
  if (argc != 2) {
    printf("Need 1 arg\n");
    return (1);
  }
  int maxa = atoi(argv[1]);
  char *b = (char *)calloc(2, sizeof(char));
  strncpy(b, "1", 2);
  int status = 0;
  status = factorial(maxa, &b);
  printf("main AFTER maxa is %d and FACTORIAL is %s and STATUS is %d\n", maxa,
         b, status);
  int sum = 0;
  int i;
  for (i = 0; i < strlen(b); ++i)
    sum += *(b + i) - '0';
  printf("SUM is %d\n", sum);
  free(b);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11aa <main+0x3a>
lea    0xe69(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    127e <main+0x10e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    $0x2,%edi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe34(%rip),%rsi
mov    $0x2,%edx
call   1040 <strncpy@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1050 <factorial@plt>
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rdx
mov    -0x24(%rbp),%ecx
lea    0xe05(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%eax
cmp    %ecx,%eax
jge    125d <main+0xed>
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
sub    $0x30,%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    121e <main+0xae>
mov    -0x28(%rbp),%esi
lea    0xde3(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
cmp    $0x2,%edi
jne    124a <main+0xca>
mov    0x8(%rsi),%rdi
call   10a0 <atoi@plt>
mov    %eax,%ebx
mov    $0x2,%edi
mov    $0x1,%esi
call   1090 <calloc@plt>
mov    %rax,0x8(%rsp)
lea    0xe49(%rip),%rsi
mov    %rax,%rdi
mov    $0x2,%edx
call   1040 <strncpy@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
call   1060 <factorial@plt>
mov    0x8(%rsp),%rdx
lea    0xe26(%rip),%rdi
xor    %r14d,%r14d
mov    %ebx,%esi
mov    %eax,%ecx
xor    %eax,%eax
call   1080 <printf@plt>
mov    0x8(%rsp),%rdi
call   1070 <strlen@plt>
mov    $0x0,%ebx
test   %eax,%eax
jle    122e <main+0xae>
xor    %ebp,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x8(%rsp),%rdi
movsbl (%rdi,%rbp,1),%eax
add    %eax,%ebx
add    $0xffffffd0,%ebx
add    $0x1,%rbp
call   1070 <strlen@plt>
cltq
cmp    %rax,%rbp
jl     1210 <main+0x90>
lea    0xe09(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1080 <printf@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
jmp    125c <main+0xdc>
lea    0xdf8(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"Need 1 arg\0A\00"
@.str.1 = [2 x i8] c"1\00"
@.str.2 = [60 x i8] c"main AFTER maxa is %d and FACTORIAL is %s and STATUS is %d\0A\00"
@.str.3 = [11 x i8] c"SUM is %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %53
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i32 @atoi(i8* %18)
store i32 %19, i32* %6
%20 = call i64 @calloc(i32 2, i32 1)
%21 = inttoptr i64 %20 to i8*
store i8* %21, i8** %7
%22 = load i8*, i8** %7
%23 = call i32 @strncpy(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 2)
store i32 0, i32* %8
%24 = load i32, i32* %6
%25 = call i32 @factorial(i32 %24, i8** %7)
store i32 %25, i32* %8
%26 = load i32, i32* %6
%27 = load i8*, i8** %7
%28 = load i32, i32* %8
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.2, i64 0, i64 0), i32 %26, i8* %27, i32 %28)
store i32 0, i32* %9
store i32 0, i32* %10
br label %30
30: ; preds = %45, %15
%31 = load i32, i32* %10
%32 = load i8*, i8** %7
%33 = call i32 @strlen(i8* %32)
%34 = icmp slt i32 %31, %33
br i1 %34, label %35, label %48
35: ; preds = %30
%36 = load i8*, i8** %7
%37 = load i32, i32* %10
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = sub nsw i32 %41, 48
%43 = load i32, i32* %9
%44 = add nsw i32 %43, %42
store i32 %44, i32* %9
br label %45
45: ; preds = %35
%46 = load i32, i32* %10
%47 = add nsw i32 %46, 1
store i32 %47, i32* %10
br label %30
48: ; preds = %30
%49 = load i32, i32* %9
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %49)
%51 = load i8*, i8** %7
%52 = call i32 @free(i8* %51)
store i32 0, i32* %3
br label %53
53: ; preds = %48, %13
%54 = load i32, i32* %3
ret i32 %54
}

LLVM IR (-O2):
@.str.1 = [2 x i8] c"1\00"
@.str.2 = [60 x i8] c"main AFTER maxa is %d and FACTORIAL is %s and STATUS is %d\0A\00"
@.str.3 = [11 x i8] c"SUM is %d\0A\00"
@str = [11 x i8] c"Need 1 arg\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = icmp eq i32 %0, 2
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
br label %39
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32 @atoi(i8* %9)
%11 = bitcast i8** %3 to i8*
%12 = tail call i64 @calloc(i32 2, i32 1)
%13 = inttoptr i64 %12 to i8*
store i8* %13, i8** %3
%14 = tail call i32 @strncpy(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 2)
%15 = call i32 @factorial(i32 %10, i8** %3)
%16 = load i8*, i8** %3
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([60 x i8], [60 x i8]* @.str.2, i64 0, i64 0), i32 %10, i8* %16, i32 %15)
%18 = load i8*, i8** %3
%19 = call i32 @strlen(i8* %18)
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %34
21: ; preds = %7, %21
%22 = phi i64 [ %30, %21 ], [ 0, %7 ]
%23 = phi i32 [ %29, %21 ], [ 0, %7 ]
%24 = load i8*, i8** %3
%25 = getelementptr inbounds i8, i8* %24, i64 %22
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = add i32 %23, -48
%29 = add i32 %28, %27
%30 = add nuw nsw i64 %22, 1
%31 = call i32 @strlen(i8* %24)
%32 = sext i32 %31 to i64
%33 = icmp slt i64 %30, %32
br i1 %33, label %21, label %34
34: ; preds = %21, %7
%35 = phi i32 [ 0, %7 ], [ %29, %21 ]
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %35)
%37 = load i8*, i8** %3
%38 = call i32 @free(i8* %37)
br label %39
39: ; preds = %34, %5
%40 = phi i32 [ 1, %5 ], [ 0, %34 ]
ret i32 %40
}