#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 */
void *findRealPtr(char *);
int /*<<< orphan*/ free(void *);
void *malloc(size_t);
void *real_realloc(void *, int);
void *realloc(void *ptr, size_t size) {
  if (NULL == ptr)
    return malloc(size);
  if (size == 0) {
    free(ptr);
    return NULL;
  };
  void *realptr = findRealPtr((char *)ptr);
  if (realptr == ptr) {
    ;
    return real_realloc(ptr, size + 128);
  }
  void *newptr = real_realloc(realptr, 128 + size + 128);
  ;
  return newptr + 128;
}


x86 assembly (clang, -O0):
<realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
jne    116e <realloc+0x2e>
mov    -0x18(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x8(%rbp)
jmp    11f5 <realloc+0xb5>
cmpq   $0x0,-0x18(%rbp)
jne    118f <realloc+0x4f>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11f5 <realloc+0xb5>
mov    -0x10(%rbp),%rdi
call   1040 <findRealPtr@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    11c8 <realloc+0x88>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
add    $0x80,%rax
mov    %eax,%esi
call   1060 <real_realloc@plt>
mov    %rax,-0x8(%rbp)
jmp    11f5 <realloc+0xb5>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rax
add    $0x80,%rax
add    $0x80,%rax
mov    %eax,%esi
call   1060 <real_realloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    $0x80,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<realloc>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
test   %rdi,%rdi
je     1179 <realloc+0x39>
mov    %rdi,%rbx
test   %r14,%r14
je     1188 <realloc+0x48>
mov    %rbx,%rdi
call   1040 <findRealPtr@plt>
cmp    %rbx,%rax
je     119a <realloc+0x5a>
add    $0x100,%r14d
mov    %rax,%rdi
mov    %r14d,%esi
call   1060 <real_realloc@plt>
sub    $0xffffffffffffff80,%rax
jmp    1192 <realloc+0x52>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1050 <malloc@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
sub    $0xffffff80,%r14d
mov    %rbx,%rdi
mov    %r14d,%esi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1060 <real_realloc@plt>

LLVM IR (-O0):
define i8* @realloc(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
%8 = load i8*, i8** %4
%9 = icmp eq i8* null, %8
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i64, i64* %5
%12 = call i8* @malloc(i64 %11)
store i8* %12, i8** %3
br label %40
13: ; preds = %2
%14 = load i64, i64* %5
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = load i8*, i8** %4
%18 = call i32 @free(i8* %17)
store i8* null, i8** %3
br label %40
19: ; preds = %13
%20 = load i8*, i8** %4
%21 = call i8* @findRealPtr(i8* %20)
store i8* %21, i8** %6
%22 = load i8*, i8** %6
%23 = load i8*, i8** %4
%24 = icmp eq i8* %22, %23
br i1 %24, label %25, label %31
25: ; preds = %19
%26 = load i8*, i8** %4
%27 = load i64, i64* %5
%28 = add i64 %27, 128
%29 = trunc i64 %28 to i32
%30 = call i8* @real_realloc(i8* %26, i32 %29)
store i8* %30, i8** %3
br label %40
31: ; preds = %19
%32 = load i8*, i8** %6
%33 = load i64, i64* %5
%34 = add i64 128, %33
%35 = add i64 %34, 128
%36 = trunc i64 %35 to i32
%37 = call i8* @real_realloc(i8* %32, i32 %36)
store i8* %37, i8** %7
%38 = load i8*, i8** %7
%39 = getelementptr i8, i8* %38, i64 128
store i8* %39, i8** %3
br label %40
40: ; preds = %31, %25, %16, %10
%41 = load i8*, i8** %3
ret i8* %41
}

LLVM IR (-O2):
define i8* @realloc(i8* %0, i64 %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i8* @malloc(i64 %1)
br label %21
6: ; preds = %2
%7 = icmp eq i64 %1, 0
br i1 %7, label %8, label %10
8: ; preds = %6
%9 = tail call i32 @free(i8* %0)
br label %21
10: ; preds = %6
%11 = tail call i8* @findRealPtr(i8* %0)
%12 = icmp eq i8* %11, %0
%13 = trunc i64 %1 to i32
br i1 %12, label %14, label %17
14: ; preds = %10
%15 = add i32 %13, 128
%16 = tail call i8* @real_realloc(i8* %0, i32 %15)
br label %21
17: ; preds = %10
%18 = add i32 %13, 256
%19 = tail call i8* @real_realloc(i8* %11, i32 %18)
%20 = getelementptr i8, i8* %19, i64 128
br label %21
21: ; preds = %14, %17, %8, %4
%22 = phi i8* [ %5, %4 ], [ null, %8 ], [ %16, %14 ], [ %20, %17 ]
ret i8* %22
}