#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*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
void *realloc(void *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int realloc_memory(void **p, int newsize, const char *func) {
  void *np;
  int i = 0;
  int n = 16;
  for (i = 0; i < 8; i++) {
    if (newsize <= n) {
      newsize = n;
      break;
    }
    n = n << 1;
  }
  newsize = newsize;
  np = realloc(*p, newsize);
  if (!np) {
    fprintf(stderr,
            "ERR: "
            "%s: realloc failed: %s\n",
            func, strerror(errno));
    return -1;
  }
  *p = np;
  return 0;
}


x86 assembly (clang, -O0):
<realloc_memory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x10,-0x30(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpl   $0x8,-0x2c(%rbp)
jge    1190 <realloc_memory+0x60>
mov    -0x14(%rbp),%eax
cmp    -0x30(%rbp),%eax
jg     1179 <realloc_memory+0x49>
mov    -0x30(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    1190 <realloc_memory+0x60>
mov    -0x30(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1158 <realloc_memory+0x28>
mov    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x14(%rbp),%esi
call   1040 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11f8 <realloc_memory+0xc8>
mov    0x2e25(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
lea    0xe19(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120a <realloc_memory+0xda>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<realloc_memory>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rdi,%rbx
mov    $0x10,%eax
cmp    $0x11,%esi
jl     119a <realloc_memory+0x6a>
mov    $0x20,%eax
cmp    $0x21,%esi
jb     119a <realloc_memory+0x6a>
mov    $0x40,%eax
cmp    $0x41,%esi
jb     119a <realloc_memory+0x6a>
mov    $0x80,%eax
cmp    $0x81,%esi
jb     119a <realloc_memory+0x6a>
mov    $0x100,%eax
cmp    $0x101,%esi
jb     119a <realloc_memory+0x6a>
mov    $0x200,%eax
cmp    $0x201,%esi
jb     119a <realloc_memory+0x6a>
mov    $0x400,%eax
cmp    $0x401,%esi
jb     119a <realloc_memory+0x6a>
cmp    $0x801,%esi
mov    $0x800,%eax
cmovae %esi,%eax
mov    (%rbx),%rdi
mov    %eax,%esi
call   1040 <realloc@plt>
test   %rax,%rax
je     11b0 <realloc_memory+0x80>
mov    %rax,(%rbx)
xor    %eax,%eax
jmp    11e0 <realloc_memory+0xb0>
mov    0x2e29(%rip),%rax
mov    (%rax),%ebx
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe32(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1030 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [29 x i8] c"ERR: %s: realloc failed: %s\0A\00"
@errno = global i32 0
define i32 @realloc_memory(i8** %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i8** %0, i8*** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 0, i32* %9
store i32 16, i32* %10
store i32 0, i32* %9
br label %11
11: ; preds = %23, %3
%12 = load i32, i32* %9
%13 = icmp slt i32 %12, 8
br i1 %13, label %14, label %26
14: ; preds = %11
%15 = load i32, i32* %6
%16 = load i32, i32* %10
%17 = icmp sle i32 %15, %16
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = load i32, i32* %10
store i32 %19, i32* %6
br label %26
20: ; preds = %14
%21 = load i32, i32* %10
%22 = shl i32 %21, 1
store i32 %22, i32* %10
br label %23
23: ; preds = %20
%24 = load i32, i32* %9
%25 = add nsw i32 %24, 1
store i32 %25, i32* %9
br label %11
26: ; preds = %18, %11
%27 = load i32, i32* %6
store i32 %27, i32* %6
%28 = load i8**, i8*** %5
%29 = load i8*, i8** %28
%30 = load i32, i32* %6
%31 = call i8* @realloc(i8* %29, i32 %30)
store i8* %31, i8** %8
%32 = load i8*, i8** %8
%33 = icmp ne i8* %32, null
br i1 %33, label %40, label %34
34: ; preds = %26
%35 = load i32, i32* @stderr
%36 = load i8*, i8** %7
%37 = load i32, i32* @errno
%38 = call i8* @strerror(i32 %37)
%39 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %36, i8* %38)
store i32 -1, i32* %4
br label %43
40: ; preds = %26
%41 = load i8*, i8** %8
%42 = load i8**, i8*** %5
store i8* %41, i8** %42
store i32 0, i32* %4
br label %43
43: ; preds = %40, %34
%44 = load i32, i32* %4
ret i32 %44
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [29 x i8] c"ERR: %s: realloc failed: %s\0A\00"
@errno = global i32 0
define i32 @realloc_memory(i8** nocapture %0, i32 %1, i8* %2) {
%4 = icmp sgt i32 %1, 16
br i1 %4, label %5, label %20
5: ; preds = %3
%6 = icmp ugt i32 %1, 32
br i1 %6, label %7, label %20
7: ; preds = %5
%8 = icmp ugt i32 %1, 64
br i1 %8, label %9, label %20
9: ; preds = %7
%10 = icmp ugt i32 %1, 128
br i1 %10, label %11, label %20
11: ; preds = %9
%12 = icmp ugt i32 %1, 256
br i1 %12, label %13, label %20
13: ; preds = %11
%14 = icmp ugt i32 %1, 512
br i1 %14, label %15, label %20
15: ; preds = %13
%16 = icmp ugt i32 %1, 1024
br i1 %16, label %17, label %20
17: ; preds = %15
%18 = icmp ugt i32 %1, 2048
%19 = select i1 %18, i32 %1, i32 2048
br label %20
20: ; preds = %17, %15, %13, %11, %9, %7, %5, %3
%21 = phi i32 [ 16, %3 ], [ 32, %5 ], [ 64, %7 ], [ 128, %9 ], [ 256, %11 ], [ 512, %13 ], [ 1024, %15 ], [ %19, %17 ]
%22 = load i8*, i8** %0
%23 = tail call i8* @realloc(i8* %22, i32 %21)
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %30
25: ; preds = %20
%26 = load i32, i32* @stderr
%27 = load i32, i32* @errno
%28 = tail call i8* @strerror(i32 %27)
%29 = tail call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %2, i8* %28)
br label %31
30: ; preds = %20
store i8* %23, i8** %0
br label %31
31: ; preds = %30, %25
%32 = phi i32 [ 0, %30 ], [ -1, %25 ]
ret i32 %32
}