#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 *, ...);
void *realloc(void *, size_t);
int /*<<< orphan*/ stderr;
void *xmalloc(size_t);
extern void *xrealloc(void *ptr, size_t size) {
  void *dummy;
  if (ptr == NULL) {
    dummy = xmalloc(size);
    return dummy;
  }
  dummy = realloc(ptr, size);
  if (dummy == NULL) {
    fprintf(stderr, "Could not re-allocate ");
    if (size < 1024) {
      fprintf(stderr, "%ib\n", (int)size);
    } else if (size < 1048576) {
      fprintf(stderr, "%fkb\n", size / 1024.);
    } else {
      fprintf(stderr, "%fMb\n", size / 1048576.);
    }
    fprintf(stderr, "Exiting... :-(\n");
    exit(EXIT_FAILURE);
  }
  return dummy;
}


x86 assembly (clang, -O0):
<xrealloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1175 <xrealloc+0x35>
mov    -0x18(%rbp),%rdi
call   1030 <xmalloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12b1 <xrealloc+0x171>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    12a9 <xrealloc+0x169>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
lea    0xe8f(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
cmpq   $0x400,-0x18(%rbp)
jae    11d8 <xrealloc+0x98>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rax
mov    %eax,%edx
lea    0xe7b(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    1284 <xrealloc+0x144>
cmpq   $0x100000,-0x18(%rbp)
jae    1235 <xrealloc+0xf5>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
movaps 0xe1a(%rip),%xmm1
movq   -0x18(%rbp),%xmm0
punpckldq %xmm1,%xmm0
movapd 0xe19(%rip),%xmm1
subpd  %xmm1,%xmm0
movaps %xmm0,%xmm1
unpckhpd %xmm0,%xmm0
addsd  %xmm1,%xmm0
movsd  0xdea(%rip),%xmm1
divsd  %xmm1,%xmm0
lea    0xe23(%rip),%rsi
mov    $0x1,%al
call   1040 <fprintf@plt>
jmp    127f <xrealloc+0x13f>
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
movaps 0xdcb(%rip),%xmm1
movq   -0x18(%rbp),%xmm0
punpckldq %xmm1,%xmm0
movapd 0xdca(%rip),%xmm1
subpd  %xmm1,%xmm0
movaps %xmm0,%xmm1
unpckhpd %xmm0,%xmm0
addsd  %xmm1,%xmm0
movsd  0xd93(%rip),%xmm1
divsd  %xmm1,%xmm0
lea    0xdda(%rip),%rsi
mov    $0x1,%al
call   1040 <fprintf@plt>
jmp    1284 <xrealloc+0x144>
mov    0x2d55(%rip),%rax
mov    (%rax),%edi
lea    0xdc4(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2d36(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xrealloc>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
test   %rdi,%rdi
je     1161 <xrealloc+0x21>
mov    %rbx,%rsi
call   1050 <realloc@plt>
test   %rax,%rax
je     1170 <xrealloc+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <xmalloc@plt>
mov    0x2e69(%rip),%r14
mov    (%r14),%edi
lea    0xeaf(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
cmp    $0x3ff,%rbx
ja     11a6 <xrealloc+0x66>
mov    (%r14),%edi
lea    0xeac(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    11fa <xrealloc+0xba>
movq   %rbx,%xmm1
punpckldq 0xe4d(%rip),%xmm1
subpd  0xe55(%rip),%xmm1
mov    (%r14),%edi
movapd %xmm1,%xmm0
unpckhpd %xmm1,%xmm0
addsd  %xmm1,%xmm0
cmp    $0xfffff,%rbx
ja     11e4 <xrealloc+0xa4>
mulsd  0xe4d(%rip),%xmm0
lea    0xe6a(%rip),%rsi
jmp    11f3 <xrealloc+0xb3>
mulsd  0xe34(%rip),%xmm0
lea    0xe5f(%rip),%rsi
mov    $0x1,%al
call   1040 <fprintf@plt>
mov    (%r14),%edi
lea    0xe54(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [23 x i8] c"Could not re-allocate \00"
@.str.1 = [5 x i8] c"%ib\0A\00"
@.str.2 = [6 x i8] c"%fkb\0A\00"
@.str.3 = [6 x i8] c"%fMb\0A\00"
@.str.4 = [16 x i8] c"Exiting... :-(\0A\00"
@EXIT_FAILURE = global i32 0
define i8* @xrealloc(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
%7 = load i8*, i8** %4
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %13
9: ; preds = %2
%10 = load i64, i64* %5
%11 = call i8* @xmalloc(i64 %10)
store i8* %11, i8** %6
%12 = load i8*, i8** %6
store i8* %12, i8** %3
br label %52
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = load i64, i64* %5
%16 = call i8* @realloc(i8* %14, i64 %15)
store i8* %16, i8** %6
%17 = load i8*, i8** %6
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %50
19: ; preds = %13
%20 = load i32, i32* @stderr
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%22 = load i64, i64* %5
%23 = icmp ult i64 %22, 1024
br i1 %23, label %24, label %29
24: ; preds = %19
%25 = load i32, i32* @stderr
%26 = load i64, i64* %5
%27 = trunc i64 %26 to i32
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %27)
br label %45
29: ; preds = %19
%30 = load i64, i64* %5
%31 = icmp ult i64 %30, 1048576
br i1 %31, label %32, label %38
32: ; preds = %29
%33 = load i32, i32* @stderr
%34 = load i64, i64* %5
%35 = uitofp i64 %34 to double
%36 = fdiv double %35, 1.024000e+03
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), double %36)
br label %44
38: ; preds = %29
%39 = load i32, i32* @stderr
%40 = load i64, i64* %5
%41 = uitofp i64 %40 to double
%42 = fdiv double %41, 0x4130000000000000
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), double %42)
br label %44
44: ; preds = %38, %32
br label %45
45: ; preds = %44, %24
%46 = load i32, i32* @stderr
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%48 = load i32, i32* @EXIT_FAILURE
%49 = call i32 @exit(i32 %48)
unreachable
50: ; preds = %13
%51 = load i8*, i8** %6
store i8* %51, i8** %3
br label %52
52: ; preds = %50, %9
%53 = load i8*, i8** %3
ret i8* %53
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [23 x i8] c"Could not re-allocate \00"
@.str.1 = [5 x i8] c"%ib\0A\00"
@.str.2 = [6 x i8] c"%fkb\0A\00"
@.str.3 = [6 x i8] c"%fMb\0A\00"
@.str.4 = [16 x i8] c"Exiting... :-(\0A\00"
@EXIT_FAILURE = global i32 0
define i8* @xrealloc(i8* %0, i64 %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i8* @xmalloc(i64 %1)
br label %32
6: ; preds = %2
%7 = tail call i8* @realloc(i8* %0, i64 %1)
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %32
9: ; preds = %6
%10 = load i32, i32* @stderr
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%12 = icmp ult i64 %1, 1024
br i1 %12, label %13, label %17
13: ; preds = %9
%14 = load i32, i32* @stderr
%15 = trunc i64 %1 to i32
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %15)
br label %27
17: ; preds = %9
%18 = icmp ult i64 %1, 1048576
%19 = load i32, i32* @stderr
%20 = uitofp i64 %1 to double
br i1 %18, label %21, label %24
21: ; preds = %17
%22 = fmul double %20, 0x3F50000000000000
%23 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), double %22)
br label %27
24: ; preds = %17
%25 = fmul double %20, 0x3EB0000000000000
%26 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), double %25)
br label %27
27: ; preds = %21, %24, %13
%28 = load i32, i32* @stderr
%29 = tail call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%30 = load i32, i32* @EXIT_FAILURE
%31 = tail call i32 @exit(i32 %30)
unreachable
32: ; preds = %6, %4
%33 = phi i8* [ %5, %4 ], [ %7, %6 ]
ret i8* %33
}