#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 EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ free(int *);
int *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int sum(int *, int);
int main(int argc, char *argv[]) {
  int *arr;
  int iter;
  int length = 12;
  int total;
  arr = malloc(length * sizeof(int));
  if (arr == NULL) {
    printf("malloc fails.\n");
    return EXIT_FAILURE;
  }
  for (iter = 0; iter < length; iter++) {
    arr[iter] = iter;
  }
  total = sum(arr, length);
  printf("Total is %d.\n", total);
  free(arr);
  return EXIT_SUCCESS;
}


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)
movl   $0xc,-0x20(%rbp)
movslq -0x20(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    119a <main+0x5a>
lea    0xe7e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e50(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11fe <main+0xbe>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    11c9 <main+0x89>
mov    -0x1c(%rbp),%edx
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11a1 <main+0x61>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1050 <sum@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    0x2ddf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    $0x30,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     11b1 <main+0x61>
mov    %rax,%rbx
movaps 0xe96(%rip),%xmm0
movups %xmm0,(%rax)
movaps 0xe9c(%rip),%xmm0
movups %xmm0,0x10(%rax)
movaps 0xea1(%rip),%xmm0
movups %xmm0,0x20(%rax)
mov    %rax,%rdi
mov    $0xc,%esi
call   1060 <sum@plt>
lea    0xe99(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x2e29(%rip),%rax
jmp    11c4 <main+0x74>
lea    0xe86(%rip),%rdi
call   1040 <puts@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [15 x i8] c"malloc fails.\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [14 x i8] c"Total is %d.\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 12, i32* %8
%10 = load i32, i32* %8
%11 = sext i32 %10 to i64
%12 = mul i64 %11, 4
%13 = trunc i64 %12 to i32
%14 = call i32* @malloc(i32 %13)
store i32* %14, i32** %6
%15 = load i32*, i32** %6
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
store i32 %19, i32* %3
br label %43
20: ; preds = %2
store i32 0, i32* %7
br label %21
21: ; preds = %31, %20
%22 = load i32, i32* %7
%23 = load i32, i32* %8
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %34
25: ; preds = %21
%26 = load i32, i32* %7
%27 = load i32*, i32** %6
%28 = load i32, i32* %7
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i32, i32* %27, i64 %29
store i32 %26, i32* %30
br label %31
31: ; preds = %25
%32 = load i32, i32* %7
%33 = add nsw i32 %32, 1
store i32 %33, i32* %7
br label %21
34: ; preds = %21
%35 = load i32*, i32** %6
%36 = load i32, i32* %8
%37 = call i32 @sum(i32* %35, i32 %36)
store i32 %37, i32* %9
%38 = load i32, i32* %9
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %38)
%40 = load i32*, i32** %6
%41 = call i32 @free(i32* %40)
%42 = load i32, i32* @EXIT_SUCCESS
store i32 %42, i32* %3
br label %43
43: ; preds = %34, %17
%44 = load i32, i32* %3
ret i32 %44
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@.str.1 = [14 x i8] c"Total is %d.\0A\00"
@EXIT_SUCCESS = global i32 0
@str = [14 x i8] c"malloc fails.\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32* @malloc(i32 48)
%4 = icmp eq i32* %3, null
br i1 %4, label %14, label %5
5: ; preds = %2
%6 = bitcast i32* %3 to <4 x i32>*
store <4 x i32> <i32 0, i32 1, i32 2, i32 3>, <4 x i32>* %6
%7 = getelementptr inbounds i32, i32* %3, i64 4
%8 = bitcast i32* %7 to <4 x i32>*
store <4 x i32> <i32 4, i32 5, i32 6, i32 7>, <4 x i32>* %8
%9 = getelementptr inbounds i32, i32* %3, i64 8
%10 = bitcast i32* %9 to <4 x i32>*
store <4 x i32> <i32 8, i32 9, i32 10, i32 11>, <4 x i32>* %10
%11 = tail call i32 @sum(i32* %3, i32 12)
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = tail call i32 @free(i32* %3)
br label %16
14: ; preds = %2
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
br label %16
16: ; preds = %5, %14
%17 = phi i32* [ @EXIT_FAILURE, %14 ], [ @EXIT_SUCCESS, %5 ]
%18 = load i32, i32* %17
ret i32 %18
}