#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;
void *calloc(int, size_t);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int);
void *realloc(void *, int);
int /*<<< orphan*/ stderr;
void *reallocate(void *base, int n, size_t sz, char *src_file, int line,
                 bool clear) {
  base = clear ? calloc(n, sz) : realloc(base, n * sz);
  if ((base == NULL) && ((n * sz) > 0)) {
    fprintf(stderr, "%s:%d - ERROR: Failed to allocate memory\n", src_file,
            line);
    exit(EXIT_FAILURE);
  }
  return base;
}


x86 assembly (clang, -O0):
<reallocate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    %r9d,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     117e <reallocate+0x3e>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1030 <calloc@plt>
mov    %rax,-0x30(%rbp)
jmp    1196 <reallocate+0x56>
mov    -0x8(%rbp),%rdi
movslq -0xc(%rbp),%rax
imul   -0x18(%rbp),%rax
mov    %eax,%esi
call   1050 <realloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11e6 <reallocate+0xa6>
movslq -0xc(%rbp),%rax
imul   -0x18(%rbp),%rax
cmp    $0x0,%rax
jbe    11e6 <reallocate+0xa6>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
mov    -0x24(%rbp),%ecx
lea    0xe2d(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reallocate>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %r8d,%r14d
mov    %rcx,%r15
mov    %rdx,%rbp
mov    %esi,%ebx
test   %r9d,%r9d
je     1168 <reallocate+0x28>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1030 <calloc@plt>
test   %rax,%rax
jne    1183 <reallocate+0x43>
jmp    1177 <reallocate+0x37>
mov    %ebp,%esi
imul   %ebx,%esi
call   1050 <realloc@plt>
test   %rax,%rax
jne    1183 <reallocate+0x43>
movslq %ebx,%rcx
imul   %rbp,%rcx
test   %rcx,%rcx
jne    118e <reallocate+0x4e>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
lea    0xe62(%rip),%rsi
mov    %r15,%rdx
mov    %r14d,%ecx
call   1040 <fprintf@plt>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [42 x i8] c"%s:%d - ERROR: Failed to allocate memory\0A\00"
@EXIT_FAILURE = global i32 0
define i8* @reallocate(i8* %0, i32 %1, i64 %2, i8* %3, i32 %4, i32 %5) {
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %7
store i32 %1, i32* %8
store i64 %2, i64* %9
store i8* %3, i8** %10
store i32 %4, i32* %11
store i32 %5, i32* %12
%13 = load i32, i32* %12
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %6
%16 = load i32, i32* %8
%17 = load i64, i64* %9
%18 = call i8* @calloc(i32 %16, i64 %17)
br label %27
19: ; preds = %6
%20 = load i8*, i8** %7
%21 = load i32, i32* %8
%22 = sext i32 %21 to i64
%23 = load i64, i64* %9
%24 = mul i64 %22, %23
%25 = trunc i64 %24 to i32
%26 = call i8* @realloc(i8* %20, i32 %25)
br label %27
27: ; preds = %19, %15
%28 = phi i8* [ %18, %15 ], [ %26, %19 ]
store i8* %28, i8** %7
%29 = load i8*, i8** %7
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %44
31: ; preds = %27
%32 = load i32, i32* %8
%33 = sext i32 %32 to i64
%34 = load i64, i64* %9
%35 = mul i64 %33, %34
%36 = icmp ugt i64 %35, 0
br i1 %36, label %37, label %44
37: ; preds = %31
%38 = load i32, i32* @stderr
%39 = load i8*, i8** %10
%40 = load i32, i32* %11
%41 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %39, i32 %40)
%42 = load i32, i32* @EXIT_FAILURE
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %31, %27
%45 = load i8*, i8** %7
ret i8* %45
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [42 x i8] c"%s:%d - ERROR: Failed to allocate memory\0A\00"
@EXIT_FAILURE = global i32 0
define i8* @reallocate(i8* %0, i32 %1, i64 %2, i8* %3, i32 %4, i32 %5) {
%7 = icmp eq i32 %5, 0
br i1 %7, label %10, label %8
8: ; preds = %6
%9 = tail call i8* @calloc(i32 %1, i64 %2)
br label %14
10: ; preds = %6
%11 = trunc i64 %2 to i32
%12 = mul i32 %11, %1
%13 = tail call i8* @realloc(i8* %0, i32 %12)
br label %14
14: ; preds = %10, %8
%15 = phi i8* [ %9, %8 ], [ %13, %10 ]
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %26
17: ; preds = %14
%18 = sext i32 %1 to i64
%19 = mul i64 %18, %2
%20 = icmp eq i64 %19, 0
br i1 %20, label %26, label %21
21: ; preds = %17
%22 = load i32, i32* @stderr
%23 = tail call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %3, i32 %4)
%24 = load i32, i32* @EXIT_FAILURE
%25 = tail call i32 @exit(i32 %24)
unreachable
26: ; preds = %17, %14
ret i8* %15
}