#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*/ EXIT_FAILURE;
int *calloc(int, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ free(int *);
int /*<<< orphan*/ perror(char *);
int *realloc(int *, int);
int main(int argc, char *argv[]) {
  int *arr, *new_arr;
  arr = calloc(50, sizeof(int));
  if (arr == NULL) {
    perror("Error on calloc");
    exit(EXIT_FAILURE);
  } else {
    new_arr = realloc(arr, sizeof(int) * 100);
    if (new_arr == NULL) {
      perror("Error on realloc");
      free(arr);
    } else {
      free(new_arr);
    }
  }
  exit(EXIT_FAILURE);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x32,%edi
mov    $0x4,%esi
call   1040 <calloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    119e <main+0x4e>
lea    0xe75(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    $0x190,%esi
call   1050 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d5 <main+0x85>
lea    0xe4e(%rip),%rdi
call   1060 <perror@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
jmp    11de <main+0x8e>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
jmp    11e3 <main+0x93>
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    $0x32,%edi
mov    $0x4,%esi
call   1040 <calloc@plt>
test   %rax,%rax
jne    1173 <main+0x23>
lea    0xe94(%rip),%rdi
call   1060 <perror@plt>
jmp    119c <main+0x4c>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x190,%esi
call   1050 <realloc@plt>
test   %rax,%rax
jne    11aa <main+0x5a>
lea    0xe81(%rip),%rdi
call   1060 <perror@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    %rax,%rbx
jmp    1194 <main+0x44>

LLVM IR (-O0):
@.str = [16 x i8] c"Error on calloc\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [17 x i8] c"Error on realloc\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i32* @calloc(i32 50, i32 4)
store i32* %8, i32** %6
%9 = load i32*, i32** %6
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %2
%16 = load i32*, i32** %6
%17 = call i32* @realloc(i32* %16, i32 400)
store i32* %17, i32** %7
%18 = load i32*, i32** %7
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %24
20: ; preds = %15
%21 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32*, i32** %6
%23 = call i32 @free(i32* %22)
br label %27
24: ; preds = %15
%25 = load i32*, i32** %7
%26 = call i32 @free(i32* %25)
br label %27
27: ; preds = %24, %20
br label %28
28: ; preds = %27
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
}

LLVM IR (-O2):
@.str = [16 x i8] c"Error on calloc\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [17 x i8] c"Error on realloc\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32* @calloc(i32 50, i32 4)
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %2
%10 = tail call i32* @realloc(i32* %3, i32 400)
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %9, %12
%15 = phi i32* [ %3, %12 ], [ %10, %9 ]
%16 = tail call i32 @free(i32* %15)
%17 = load i32, i32* @EXIT_FAILURE
%18 = tail call i32 @exit(i32 %17)
unreachable
}