#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*/ printf(char *, ...);
void *realloc(void *, long);
void *memory_resize(void *ptr, long nbytes, const char *file, int line) {
  ;
  do {
    if ((nbytes) <= 0) {
      printf("Error: try to allocate negative-sized memory block!\n");
      printf("  raised in %s at %s:%d\n", "memory_resize", file, line);
      exit(EXIT_FAILURE);
    }
  } while (0);
  ptr = realloc(ptr, nbytes);
  do {
    if ((ptr) == NULL) {
      printf("Error: failed to allocate memory!\n");
      printf("  raised in %s at %s:%d\n", "memory_resize", file, line);
      exit(EXIT_FAILURE);
    }
  } while (0);
  return ptr;
}


x86 assembly (clang, -O0):
<memory_resize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
jg     118a <memory_resize+0x5a>
lea    0xea7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
lea    0xec7(%rip),%rdi
lea    0xed9(%rip),%rsi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
jmp    118f <memory_resize+0x5f>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <realloc@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11e3 <memory_resize+0xb3>
lea    0xeaa(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
lea    0xe6e(%rip),%rdi
lea    0xe80(%rip),%rsi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
jmp    11e8 <memory_resize+0xb8>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<memory_resize>:
push   %rbp
push   %rbx
push   %rax
mov    %ecx,%ebp
mov    %rdx,%rbx
test   %rsi,%rsi
jle    115e <memory_resize+0x1e>
call   1050 <realloc@plt>
test   %rax,%rax
je     1167 <memory_resize+0x27>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xee4(%rip),%rdi
jmp    116e <memory_resize+0x2e>
lea    0xeb9(%rip),%rdi
call   1030 <puts@plt>
lea    0xe86(%rip),%rdi
lea    0xe98(%rip),%rsi
mov    %rbx,%rdx
mov    %ebp,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [53 x i8] c"Error: try to allocate negative-sized memory block!\0A\00"
@.str.1 = [25 x i8] c" raised in %s at %s:%d\0A\00"
@.str.2 = [14 x i8] c"memory_resize\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [35 x i8] c"Error: failed to allocate memory!\0A\00"
define i8* @memory_resize(i8* %0, i64 %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
br label %9
9: ; preds = %4
%10 = load i64, i64* %6
%11 = icmp sle i64 %10, 0
br i1 %11, label %12, label %19
12: ; preds = %9
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0))
%14 = load i8*, i8** %7
%15 = load i32, i32* %8
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %14, i32 %15)
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %9
br label %20
20: ; preds = %19
%21 = load i8*, i8** %5
%22 = load i64, i64* %6
%23 = call i8* @realloc(i8* %21, i64 %22)
store i8* %23, i8** %5
br label %24
24: ; preds = %20
%25 = load i8*, i8** %5
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %34
27: ; preds = %24
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
%29 = load i8*, i8** %7
%30 = load i32, i32* %8
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %29, i32 %30)
%32 = load i32, i32* @EXIT_FAILURE
%33 = call i32 @exit(i32 %32)
unreachable
34: ; preds = %24
br label %35
35: ; preds = %34
%36 = load i8*, i8** %5
ret i8* %36
}

LLVM IR (-O2):
@.str.1 = [25 x i8] c" raised in %s at %s:%d\0A\00"
@.str.2 = [14 x i8] c"memory_resize\00"
@EXIT_FAILURE = global i32 0
@str = [34 x i8] c"Error: failed to allocate memory!\00"
@str.4 = [52 x i8] c"Error: try to allocate negative-sized memory block!\00"
define noalias i8* @memory_resize(i8* nocapture %0, i64 %1, i8* %2, i32 %3) {
%5 = icmp slt i64 %1, 1
br i1 %5, label %6, label %11
6: ; preds = %4
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @str.4, i64 0, i64 0))
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %2, i32 %3)
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %4
%12 = tail call i8* @realloc(i8* %0, i64 %1)
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %19
14: ; preds = %11
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %2, i32 %3)
%17 = load i32, i32* @EXIT_FAILURE
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %11
ret i8* %12
}