#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 */
scalar_t__ calloc(int, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ free(int *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
void main() {
  int *array = 0, num = 5, i;
  int *carray;
  printf("please input the number of element: %d\n", num);
  array = (int *)malloc(sizeof(int) * num);
  carray = (int *)calloc(sizeof(int), num + 1);
  if (array == 0) {
    printf("out of memory, press any key to quit...\n");
    exit(0);
  }
  printf("malloc array init: %d %o 0x%x\n", array[0], array[1], array[num]);
  printf("calloc array init: %d %o 0x%x\n", carray[0], carray[1], carray[2]);
  printf("please input %d elements:\n", num);
  for (i = 0; i < num; i++) {
    array[i] = num + i;
  }
  printf("%d elements are:\n", num);
  for (i = 0; i < num; i++) {
    printf("%d,", array[i]);
  }
  printf("\b \n");
  free(array);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movq   $0x0,-0x8(%rbp)
movl   $0x5,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xe8f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movslq -0xc(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x8(%rbp)
mov    -0xc(%rbp),%esi
add    $0x1,%esi
mov    $0x4,%edi
call   1050 <calloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11bf <main+0x6f>
lea    0xe77(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%edx
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    (%rax,%rcx,4),%ecx
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edx
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%ecx
lea    0xe70(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%esi
lea    0xe7e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    124a <main+0xfa>
mov    -0xc(%rbp),%edx
add    -0x10(%rbp),%edx
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    121f <main+0xcf>
mov    -0xc(%rbp),%esi
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1295 <main+0x145>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1262 <main+0x112>
lea    0xe24(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
lea    0xea5(%rip),%rdi
mov    $0x5,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x14,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    $0x4,%edi
mov    $0x6,%esi
call   1060 <calloc@plt>
test   %rbx,%rbx
je     1267 <main+0x107>
mov    %rax,%r14
mov    (%rbx),%esi
mov    0x4(%rbx),%edx
mov    0x14(%rbx),%ecx
lea    0xe8a(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%r14),%esi
mov    0x4(%r14),%edx
mov    0x8(%r14),%ecx
lea    0xe90(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xea1(%rip),%rdi
mov    $0x5,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movaps 0xe18(%rip),%xmm0
movups %xmm0,(%rbx)
movl   $0x9,0x10(%rbx)
lea    0xe98(%rip),%rdi
mov    $0x5,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%rbx),%esi
lea    0xe95(%rip),%r14
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x4(%rbx),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rbx),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0xc(%rbx),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x10(%rbx),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe54(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <free@plt>
lea    0xe3c(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [40 x i8] c"please input the number of element: %d\0A\00"
@.str.1 = [41 x i8] c"out of memory, press any key to quit...\0A\00"
@.str.2 = [31 x i8] c"malloc array init: %d %o 0x%x\0A\00"
@.str.3 = [31 x i8] c"calloc array init: %d %o 0x%x\0A\00"
@.str.4 = [27 x i8] c"please input %d elements:\0A\00"
@.str.5 = [18 x i8] c"%d elements are:\0A\00"
@.str.6 = [4 x i8] c"%d,\00"
@.str.7 = [4 x i8] c"\08 \0A\00"
define void @main() {
%1 = alloca i32*
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
store i32* null, i32** %1
store i32 5, i32* %2
%5 = load i32, i32* %2
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = load i32, i32* %2
%8 = sext i32 %7 to i64
%9 = mul i64 4, %8
%10 = trunc i64 %9 to i32
%11 = call i64 @malloc(i32 %10)
%12 = inttoptr i64 %11 to i32*
store i32* %12, i32** %1
%13 = load i32, i32* %2
%14 = add nsw i32 %13, 1
%15 = call i64 @calloc(i32 4, i32 %14)
%16 = inttoptr i64 %15 to i32*
store i32* %16, i32** %4
%17 = load i32*, i32** %1
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %22
19: ; preds = %0
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @exit(i32 0)
unreachable
22: ; preds = %0
%23 = load i32*, i32** %1
%24 = getelementptr inbounds i32, i32* %23, i64 0
%25 = load i32, i32* %24
%26 = load i32*, i32** %1
%27 = getelementptr inbounds i32, i32* %26, i64 1
%28 = load i32, i32* %27
%29 = load i32*, i32** %1
%30 = load i32, i32* %2
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i32, i32* %29, i64 %31
%33 = load i32, i32* %32
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %25, i32 %28, i32 %33)
%35 = load i32*, i32** %4
%36 = getelementptr inbounds i32, i32* %35, i64 0
%37 = load i32, i32* %36
%38 = load i32*, i32** %4
%39 = getelementptr inbounds i32, i32* %38, i64 1
%40 = load i32, i32* %39
%41 = load i32*, i32** %4
%42 = getelementptr inbounds i32, i32* %41, i64 2
%43 = load i32, i32* %42
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %37, i32 %40, i32 %43)
%45 = load i32, i32* %2
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i32 %45)
store i32 0, i32* %3
br label %47
47: ; preds = %59, %22
%48 = load i32, i32* %3
%49 = load i32, i32* %2
%50 = icmp slt i32 %48, %49
br i1 %50, label %51, label %62
51: ; preds = %47
%52 = load i32, i32* %2
%53 = load i32, i32* %3
%54 = add nsw i32 %52, %53
%55 = load i32*, i32** %1
%56 = load i32, i32* %3
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i32, i32* %55, i64 %57
store i32 %54, i32* %58
br label %59
59: ; preds = %51
%60 = load i32, i32* %3
%61 = add nsw i32 %60, 1
store i32 %61, i32* %3
br label %47
62: ; preds = %47
%63 = load i32, i32* %2
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i32 %63)
store i32 0, i32* %3
br label %65
65: ; preds = %76, %62
%66 = load i32, i32* %3
%67 = load i32, i32* %2
%68 = icmp slt i32 %66, %67
br i1 %68, label %69, label %79
69: ; preds = %65
%70 = load i32*, i32** %1
%71 = load i32, i32* %3
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds i32, i32* %70, i64 %72
%74 = load i32, i32* %73
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 %74)
br label %76
76: ; preds = %69
%77 = load i32, i32* %3
%78 = add nsw i32 %77, 1
store i32 %78, i32* %3
br label %65
79: ; preds = %65
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0))
%81 = load i32*, i32** %1
%82 = call i32 @free(i32* %81)
ret void
}

LLVM IR (-O2):
@.str = [40 x i8] c"please input the number of element: %d\0A\00"
@.str.2 = [31 x i8] c"malloc array init: %d %o 0x%x\0A\00"
@.str.3 = [31 x i8] c"calloc array init: %d %o 0x%x\0A\00"
@.str.4 = [27 x i8] c"please input %d elements:\0A\00"
@.str.5 = [18 x i8] c"%d elements are:\0A\00"
@.str.6 = [4 x i8] c"%d,\00"
@str = [3 x i8] c"\08 \00"
@str.8 = [40 x i8] c"out of memory, press any key to quit...\00"
define void @main() {
%1 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 5)
%2 = tail call i64 @malloc(i32 20)
%3 = inttoptr i64 %2 to i32*
%4 = tail call i64 @calloc(i32 4, i32 6)
%5 = inttoptr i64 %4 to i32*
%6 = icmp eq i64 %2, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str.8, i64 0, i64 0))
%9 = tail call i32 @exit(i32 0)
unreachable
10: ; preds = %0
%11 = load i32, i32* %3
%12 = getelementptr inbounds i32, i32* %3, i64 1
%13 = load i32, i32* %12
%14 = getelementptr inbounds i32, i32* %3, i64 5
%15 = load i32, i32* %14
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %11, i32 %13, i32 %15)
%17 = load i32, i32* %5
%18 = getelementptr inbounds i32, i32* %5, i64 1
%19 = load i32, i32* %18
%20 = getelementptr inbounds i32, i32* %5, i64 2
%21 = load i32, i32* %20
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %17, i32 %19, i32 %21)
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i32 5)
%24 = getelementptr inbounds i32, i32* %3, i64 2
%25 = getelementptr inbounds i32, i32* %3, i64 3
%26 = inttoptr i64 %2 to <4 x i32>*
store <4 x i32> <i32 5, i32 6, i32 7, i32 8>, <4 x i32>* %26
%27 = getelementptr inbounds i32, i32* %3, i64 4
store i32 9, i32* %27
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i32 5)
%29 = load i32, i32* %3
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 %29)
%31 = load i32, i32* %12
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 %31)
%33 = load i32, i32* %24
%34 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 %33)
%35 = load i32, i32* %25
%36 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 %35)
%37 = load i32, i32* %27
%38 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 %37)
%39 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @str, i64 0, i64 0))
%40 = tail call i32 @free(i32* %3)
ret void
}