#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 arc4random();
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *,
                           char *, size_t);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ printf(char *, char const *);
char *realloc(char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int _realloc(char const *progname) {
  size_t i;
  char *p = NULL;
  char *q;
  printf("%s: Testing realloc()\n", progname);
  for (i = 0; i < 65536; i++) {
    if ((q = realloc(p, arc4random() % 65536)) == NULL) {
      fprintf(stderr, "%s: %s: %s (%zu)\n", progname, "realloc",
              strerror(errno), i);
      free(p);
      return -1;
    }
    p = q;
  }
  free(p);
  return 0;
}


x86 assembly (clang, -O0):
<_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe81(%rip),%rdi
call   1050 <printf@plt>
movq   $0x0,-0x18(%rbp)
cmpq   $0x10000,-0x18(%rbp)
jae    123b <_realloc+0xdb>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   1040 <arc4random@plt>
mov    -0x30(%rbp),%rdi
mov    $0x10000,%ecx
cltd
idiv   %ecx
mov    %edx,%esi
call   1070 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1222 <_realloc+0xc2>
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %rax,%r8
mov    -0x18(%rbp),%r9
lea    0xe16(%rip),%rsi
lea    0xe21(%rip),%rcx
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    124b <_realloc+0xeb>
mov    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    118c <_realloc+0x2c>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_realloc>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xe8f(%rip),%rdi
mov    %r14,%rsi
call   1050 <printf@plt>
xor    %r15d,%r15d
xor    %ebx,%ebx
xchg   %ax,%ax
xor    %eax,%eax
call   1040 <arc4random@plt>
lea    0xffff(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
and    $0xffff0000,%ecx
sub    %ecx,%eax
mov    %r15,%rdi
mov    %eax,%esi
call   1070 <realloc@plt>
test   %rax,%rax
je     11c5 <_realloc+0x65>
add    $0x1,%rbx
mov    %rax,%r15
cmp    $0x10000,%rbx
jne    1180 <_realloc+0x20>
mov    %rax,%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    1207 <_realloc+0xa7>
mov    0x2e14(%rip),%rax
mov    (%rax),%ebp
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe34(%rip),%rsi
lea    0xe3f(%rip),%rcx
mov    %ebp,%edi
mov    %r14,%rdx
mov    %rax,%r8
mov    %rbx,%r9
call   1060 <fprintf@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [23 x i8] c"%s: Testing realloc()\0A\00"
@stderr = global i32 0
@.str.1 = [18 x i8] c"%s: %s: %s (%zu)\0A\00"
@.str.2 = [8 x i8] c"realloc\00"
@errno = global i32 0
define i32 @_realloc(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %3
store i8* null, i8** %5
%7 = load i8*, i8** %3
%8 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %7)
store i64 0, i64* %4
br label %9
9: ; preds = %29, %1
%10 = load i64, i64* %4
%11 = icmp ult i64 %10, 65536
br i1 %11, label %12, label %32
12: ; preds = %9
%13 = load i8*, i8** %5
%14 = call i32 (...) @arc4random()
%15 = srem i32 %14, 65536
%16 = call i8* @realloc(i8* %13, i32 %15)
store i8* %16, i8** %6
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %27
18: ; preds = %12
%19 = load i32, i32* @stderr
%20 = load i8*, i8** %3
%21 = load i32, i32* @errno
%22 = call i8* @strerror(i32 %21)
%23 = load i64, i64* %4
%24 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %20, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %22, i64 %23)
%25 = load i8*, i8** %5
%26 = call i32 @free(i8* %25)
store i32 -1, i32* %2
br label %35
27: ; preds = %12
%28 = load i8*, i8** %6
store i8* %28, i8** %5
br label %29
29: ; preds = %27
%30 = load i64, i64* %4
%31 = add i64 %30, 1
store i64 %31, i64* %4
br label %9
32: ; preds = %9
%33 = load i8*, i8** %5
%34 = call i32 @free(i8* %33)
store i32 0, i32* %2
br label %35
35: ; preds = %32, %18
%36 = load i32, i32* %2
ret i32 %36
}

LLVM IR (-O2):
@.str = [23 x i8] c"%s: Testing realloc()\0A\00"
@stderr = global i32 0
@.str.1 = [18 x i8] c"%s: %s: %s (%zu)\0A\00"
@.str.2 = [8 x i8] c"realloc\00"
@errno = global i32 0
define i32 @_realloc(i8* %0) {
%2 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %3
3: ; preds = %1, %16
%4 = phi i8* [ null, %1 ], [ %8, %16 ]
%5 = phi i64 [ 0, %1 ], [ %17, %16 ]
%6 = tail call i32 (...) @arc4random()
%7 = srem i32 %6, 65536
%8 = tail call i8* @realloc(i8* %4, i32 %7)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %16
10: ; preds = %3
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = tail call i8* @strerror(i32 %12)
%14 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %13, i64 %5)
%15 = tail call i32 @free(i8* %4)
br label %21
16: ; preds = %3
%17 = add nuw nsw i64 %5, 1
%18 = icmp eq i64 %17, 65536
br i1 %18, label %19, label %3
19: ; preds = %16
%20 = tail call i32 @free(i8* %8)
br label %21
21: ; preds = %19, %10
%22 = phi i32 [ -1, %10 ], [ 0, %19 ]
ret i32 %22
}