#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*/ assert(int);
int /*<<< orphan*/ error(int, char *);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, size_t);
void *realloc(void *, size_t);
void *safe_realloc(void *mem, size_t old_size, size_t new_size) {
  assert(new_size > 0);
  assert(new_size > old_size);
  mem = realloc(mem, new_size);
  if (!mem)
    error(2, "Failed to allocate more memory");
  memset(mem + old_size, 0, new_size - old_size);
  return mem;
}


x86 assembly (clang, -O0):
<safe_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
seta   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
seta   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <realloc@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11a8 <safe_realloc+0x68>
mov    $0x2,%edi
lea    0xe5d(%rip),%rsi
call   1060 <error@plt>
mov    -0x8(%rbp),%rdi
add    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
sub    -0x10(%rbp),%rdx
xor    %esi,%esi
call   1030 <memset@plt>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_realloc>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r12
xor    %edi,%edi
test   %rdx,%rdx
setne  %dil
call   1040 <assert@plt>
xor    %edi,%edi
mov    %rbx,%r15
sub    %r14,%r15
seta   %dil
call   1040 <assert@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
call   1050 <realloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1194 <safe_realloc+0x54>
lea    0xe76(%rip),%rsi
mov    $0x2,%edi
call   1060 <error@plt>
mov    %rbx,%rdi
add    %r14,%rdi
xor    %esi,%esi
mov    %r15,%rdx
call   1030 <memset@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [31 x i8] c"Failed to allocate more memory\00"
define i8* @safe_realloc(i8* %0, i64 %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
store i64 %2, i64* %6
%7 = load i64, i64* %6
%8 = icmp ugt i64 %7, 0
%9 = zext i1 %8 to i32
%10 = call i32 @assert(i32 %9)
%11 = load i64, i64* %6
%12 = load i64, i64* %5
%13 = icmp ugt i64 %11, %12
%14 = zext i1 %13 to i32
%15 = call i32 @assert(i32 %14)
%16 = load i8*, i8** %4
%17 = load i64, i64* %6
%18 = call i8* @realloc(i8* %16, i64 %17)
store i8* %18, i8** %4
%19 = load i8*, i8** %4
%20 = icmp ne i8* %19, null
br i1 %20, label %23, label %21
21: ; preds = %3
%22 = call i32 @error(i32 2, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %21, %3
%24 = load i8*, i8** %4
%25 = load i64, i64* %5
%26 = getelementptr i8, i8* %24, i64 %25
%27 = load i64, i64* %6
%28 = load i64, i64* %5
%29 = sub i64 %27, %28
%30 = call i32 @memset(i8* %26, i32 0, i64 %29)
%31 = load i8*, i8** %4
ret i8* %31
}

LLVM IR (-O2):
@.str = [31 x i8] c"Failed to allocate more memory\00"
define i8* @safe_realloc(i8* nocapture %0, i64 %1, i64 %2) {
%4 = icmp ne i64 %2, 0
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = icmp ugt i64 %2, %1
%8 = zext i1 %7 to i32
%9 = tail call i32 @assert(i32 %8)
%10 = tail call i8* @realloc(i8* %0, i64 %2)
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %14
12: ; preds = %3
%13 = tail call i32 @error(i32 2, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %3
%15 = getelementptr i8, i8* %10, i64 %1
%16 = sub i64 %2, %1
%17 = tail call i32 @memset(i8* %15, i32 0, i64 %16)
ret i8* %10
}