#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 *calloc(int, int);
int /*<<< orphan*/ print_array(int *, int);
int /*<<< orphan*/ printf(char *);
int *realloc(int *, int);
int main(void) {
  int *a, *b;
  a = calloc(10, sizeof(int));
  if (!a) {
    printf("Calloc cannot allocate memory.\n");
    return 1;
  }
  int i;
  for (i = 0; i < 10; i++) {
    a[i] = i;
  }
  printf("Array a:\n");
  print_array(a, 10);
  b = realloc(a, 20);
  if (!b) {
    printf("Could not expand the memory block.\n");
    return 2;
  }
  printf("Array b:\n");
  print_array(b, 20);
  printf("Both a and b point to same memory location.\n");
  b[4] = 12345;
  print_array(a, 10);
  print_array(b, 20);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0xa,%edi
mov    $0x4,%esi
call   1050 <calloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1185 <main+0x45>
lea    0xe8c(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1255 <main+0x115>
movl   $0x0,-0x1c(%rbp)
cmpl   $0xa,-0x1c(%rbp)
jge    11b2 <main+0x72>
mov    -0x1c(%rbp),%edx
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    118c <main+0x4c>
lea    0xe67(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    $0xa,%esi
call   1030 <print_array@plt>
mov    -0x10(%rbp),%rdi
mov    $0x14,%esi
call   1060 <realloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1201 <main+0xc1>
lea    0xe3a(%rip),%rdi
call   1040 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1255 <main+0x115>
lea    0xe46(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    $0x14,%esi
call   1030 <print_array@plt>
lea    0xe36(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rax
movl   $0x3039,0x10(%rax)
mov    -0x10(%rbp),%rdi
mov    $0xa,%esi
call   1030 <print_array@plt>
mov    -0x18(%rbp),%rdi
mov    $0x14,%esi
call   1030 <print_array@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    $0xa,%edi
mov    $0x4,%esi
call   1050 <calloc@plt>
test   %rax,%rax
je     11fb <main+0xbb>
mov    %rax,%rbx
movaps 0xe9a(%rip),%xmm0
movups %xmm0,(%rax)
movaps 0xea0(%rip),%xmm0
movups %xmm0,0x10(%rax)
movabs $0x900000008,%rax
mov    %rax,0x20(%rbx)
lea    0xeb6(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
mov    $0xa,%esi
call   1040 <print_array@plt>
mov    %rbx,%rdi
mov    $0x14,%esi
call   1060 <realloc@plt>
test   %rax,%rax
je     120e <main+0xce>
mov    %rax,%r14
lea    0xeb4(%rip),%rdi
call   1030 <puts@plt>
mov    %r14,%rdi
mov    $0x14,%esi
call   1040 <print_array@plt>
lea    0xea4(%rip),%rdi
call   1030 <puts@plt>
movl   $0x3039,0x10(%r14)
mov    %rbx,%rdi
mov    $0xa,%esi
call   1040 <print_array@plt>
mov    %r14,%rdi
mov    $0x14,%esi
call   1040 <print_array@plt>
xor    %eax,%eax
jmp    121f <main+0xdf>
lea    0xe1e(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
jmp    121f <main+0xdf>
lea    0xe33(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [32 x i8] c"Calloc cannot allocate memory.\0A\00"
@.str.1 = [10 x i8] c"Array a:\0A\00"
@.str.2 = [36 x i8] c"Could not expand the memory block.\0A\00"
@.str.3 = [10 x i8] c"Array b:\0A\00"
@.str.4 = [45 x i8] c"Both a and b point to same memory location.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32*
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32* @calloc(i32 10, i32 4)
store i32* %5, i32** %2
%6 = load i32*, i32** %2
%7 = icmp ne i32* %6, null
br i1 %7, label %10, label %8
8: ; preds = %0
%9 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %44
10: ; preds = %0
store i32 0, i32* %4
br label %11
11: ; preds = %20, %10
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 10
br i1 %13, label %14, label %23
14: ; preds = %11
%15 = load i32, i32* %4
%16 = load i32*, i32** %2
%17 = load i32, i32* %4
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i32, i32* %16, i64 %18
store i32 %15, i32* %19
br label %20
20: ; preds = %14
%21 = load i32, i32* %4
%22 = add nsw i32 %21, 1
store i32 %22, i32* %4
br label %11
23: ; preds = %11
%24 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%25 = load i32*, i32** %2
%26 = call i32 @print_array(i32* %25, i32 10)
%27 = load i32*, i32** %2
%28 = call i32* @realloc(i32* %27, i32 20)
store i32* %28, i32** %3
%29 = load i32*, i32** %3
%30 = icmp ne i32* %29, null
br i1 %30, label %33, label %31
31: ; preds = %23
%32 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %1
br label %44
33: ; preds = %23
%34 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%35 = load i32*, i32** %3
%36 = call i32 @print_array(i32* %35, i32 20)
%37 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.4, i64 0, i64 0))
%38 = load i32*, i32** %3
%39 = getelementptr inbounds i32, i32* %38, i64 4
store i32 12345, i32* %39
%40 = load i32*, i32** %2
%41 = call i32 @print_array(i32* %40, i32 10)
%42 = load i32*, i32** %3
%43 = call i32 @print_array(i32* %42, i32 20)
store i32 0, i32* %1
br label %44
44: ; preds = %33, %31, %8
%45 = load i32, i32* %1
ret i32 %45
}

LLVM IR (-O2):
@str = [31 x i8] c"Calloc cannot allocate memory.\00"
@str.5 = [9 x i8] c"Array a:\00"
@str.6 = [35 x i8] c"Could not expand the memory block.\00"
@str.7 = [9 x i8] c"Array b:\00"
@str.8 = [44 x i8] c"Both a and b point to same memory location.\00"
define i32 @main() {
%1 = tail call i32* @calloc(i32 10, i32 4)
%2 = icmp eq i32* %1, null
br i1 %2, label %13, label %3
3: ; preds = %0
%4 = bitcast i32* %1 to <4 x i32>*
store <4 x i32> <i32 0, i32 1, i32 2, i32 3>, <4 x i32>* %4
%5 = getelementptr inbounds i32, i32* %1, i64 4
%6 = bitcast i32* %5 to <4 x i32>*
store <4 x i32> <i32 4, i32 5, i32 6, i32 7>, <4 x i32>* %6
%7 = getelementptr inbounds i32, i32* %1, i64 8
store i32 8, i32* %7
%8 = getelementptr inbounds i32, i32* %1, i64 9
store i32 9, i32* %8
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.5, i64 0, i64 0))
%10 = tail call i32 @print_array(i32* %1, i32 10)
%11 = tail call i32* @realloc(i32* %1, i32 20)
%12 = icmp eq i32* %11, null
br i1 %12, label %15, label %17
13: ; preds = %0
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
br label %24
15: ; preds = %3
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.6, i64 0, i64 0))
br label %24
17: ; preds = %3
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.7, i64 0, i64 0))
%19 = tail call i32 @print_array(i32* %11, i32 20)
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str.8, i64 0, i64 0))
%21 = getelementptr inbounds i32, i32* %11, i64 4
store i32 12345, i32* %21
%22 = tail call i32 @print_array(i32* %1, i32 10)
%23 = tail call i32 @print_array(i32* %11, i32 20)
br label %24
24: ; preds = %15, %17, %13
%25 = phi i32 [ 1, %13 ], [ 0, %17 ], [ 2, %15 ]
ret i32 %25
}