#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 /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, 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 = realloc(base, n * sz);
  if (base == NULL) {
    fprintf(stderr, "%s:%d - ERROR: Failed to allocate memory\n", src_file,
            line);
    exit(EXIT_FAILURE);
  }
  if (clear)
    memset(base, 0, n * sz);
  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)
mov    -0x8(%rbp),%rdi
movslq -0xc(%rbp),%rax
imul   -0x18(%rbp),%rax
mov    %eax,%esi
call   1050 <realloc@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11ac <reallocate+0x6c>
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
mov    -0x24(%rbp),%ecx
lea    0xe67(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
cmpl   $0x0,-0x28(%rbp)
je     11cc <reallocate+0x8c>
mov    -0x8(%rbp),%rdi
movslq -0xc(%rbp),%rax
imul   -0x18(%rbp),%rax
mov    %eax,%edx
xor    %esi,%esi
call   1030 <memset@plt>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reallocate>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %r9d,%r12d
mov    %r8d,%r14d
mov    %rcx,%r15
mov    %esi,%ebp
imul   %edx,%ebp
mov    %ebp,%esi
call   1050 <realloc@plt>
test   %rax,%rax
je     1182 <reallocate+0x42>
mov    %rax,%rbx
test   %r12d,%r12d
je     1176 <reallocate+0x36>
mov    %rbx,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1030 <memset@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
lea    0xe6e(%rip),%rsi
mov    %r15,%rdx
mov    %r14d,%ecx
call   1040 <fprintf@plt>
mov    0x2e34(%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 i8*, i8** %7
%14 = load i32, i32* %8
%15 = sext i32 %14 to i64
%16 = load i64, i64* %9
%17 = mul i64 %15, %16
%18 = trunc i64 %17 to i32
%19 = call i8* @realloc(i8* %13, i32 %18)
store i8* %19, i8** %7
%20 = load i8*, i8** %7
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %29
22: ; preds = %6
%23 = load i32, i32* @stderr
%24 = load i8*, i8** %10
%25 = load i32, i32* %11
%26 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %24, i32 %25)
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %6
%30 = load i32, i32* %12
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %40
32: ; preds = %29
%33 = load i8*, i8** %7
%34 = load i32, i32* %8
%35 = sext i32 %34 to i64
%36 = load i64, i64* %9
%37 = mul i64 %35, %36
%38 = trunc i64 %37 to i32
%39 = call i32 @memset(i8* %33, i32 0, i32 %38)
br label %40
40: ; preds = %32, %29
%41 = load i8*, i8** %7
ret i8* %41
}

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 = trunc i64 %2 to i32
%8 = mul i32 %7, %1
%9 = tail call i8* @realloc(i8* %0, i32 %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %16
11: ; preds = %6
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %3, i32 %4)
%14 = load i32, i32* @EXIT_FAILURE
%15 = tail call i32 @exit(i32 %14)
unreachable
16: ; preds = %6
%17 = icmp eq i32 %5, 0
br i1 %17, label %20, label %18
18: ; preds = %16
%19 = tail call i32 @memset(i8* %9, i32 0, i32 %8)
br label %20
20: ; preds = %18, %16
ret i8* %9
}