#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 */
char *malloc(scalar_t__);
int msize(char *);
int /*<<< orphan*/ printf(char *);
char *realloc(char *, int);
scalar_t__ strcmp(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char const *);
scalar_t__ strlen(char const *);
int test_realloc(void) {
  const char *value = "hello, world!";
  char *block;
  printf("\trealloc");
  block = malloc(strlen(value) + 1);
  strcpy(block, value);
  block = realloc(block, 100);
  if (block == NULL)
    return 1;
  else
    printf(".");
  if (msize(block) < 100)
    return 1;
  else
    printf(".");
  if (strcmp(block, value))
    return 1;
  else
    printf(".");
  printf(" passed.\n");
  return 0;
}


x86 assembly (clang, -O0):
<test_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xe81(%rip),%rax
mov    %rax,-0x10(%rbp)
lea    0xe84(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rdi
mov    $0x64,%esi
call   1080 <realloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11de <test_realloc+0x6e>
movl   $0x1,-0x4(%rbp)
jmp    1256 <test_realloc+0xe6>
lea    0xe32(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1090 <msize@plt>
cmp    $0x64,%eax
jge    1208 <test_realloc+0x98>
movl   $0x1,-0x4(%rbp)
jmp    1256 <test_realloc+0xe6>
lea    0xe08(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     1237 <test_realloc+0xc7>
movl   $0x1,-0x4(%rbp)
jmp    1256 <test_realloc+0xe6>
lea    0xdd9(%rip),%rdi
call   1050 <printf@plt>
lea    0xdcf(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_realloc>:
push   %rbp
push   %rbx
push   %rax
lea    0xe84(%rip),%rdi
call   1060 <printf@plt>
mov    $0xe,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
lea    0xe5d(%rip),%rsi
mov    %rax,%rdi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
mov    $0x64,%esi
call   1090 <realloc@plt>
mov    $0x1,%ebp
test   %rax,%rax
je     1212 <test_realloc+0x92>
mov    %rax,%rbx
mov    $0x2e,%edi
call   1030 <putchar@plt>
mov    %rbx,%rdi
call   10a0 <msize@plt>
cmp    $0x64,%eax
jl     1212 <test_realloc+0x92>
mov    $0x2e,%edi
call   1030 <putchar@plt>
lea    0xe13(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strcmp@plt>
test   %rax,%rax
jne    1212 <test_realloc+0x92>
mov    $0x2e,%edi
call   1030 <putchar@plt>
lea    0xe0c(%rip),%rdi
call   1050 <puts@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"hello, world!\00"
@.str.1 = [9 x i8] c"\09realloc\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [10 x i8] c" passed.\0A\00"
define i32 @test_realloc() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8** %2
%4 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%5 = load i8*, i8** %2
%6 = call i64 @strlen(i8* %5)
%7 = add nsw i64 %6, 1
%8 = call i8* @malloc(i64 %7)
store i8* %8, i8** %3
%9 = load i8*, i8** %3
%10 = load i8*, i8** %2
%11 = call i32 @strcpy(i8* %9, i8* %10)
%12 = load i8*, i8** %3
%13 = call i8* @realloc(i8* %12, i32 100)
store i8* %13, i8** %3
%14 = load i8*, i8** %3
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %17
16: ; preds = %0
store i32 1, i32* %1
br label %36
17: ; preds = %0
%18 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %19
19: ; preds = %17
%20 = load i8*, i8** %3
%21 = call i32 @msize(i8* %20)
%22 = icmp slt i32 %21, 100
br i1 %22, label %23, label %24
23: ; preds = %19
store i32 1, i32* %1
br label %36
24: ; preds = %19
%25 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %26
26: ; preds = %24
%27 = load i8*, i8** %3
%28 = load i8*, i8** %2
%29 = call i64 @strcmp(i8* %27, i8* %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %26
store i32 1, i32* %1
br label %36
32: ; preds = %26
%33 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %34
34: ; preds = %32
%35 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %1
br label %36
36: ; preds = %34, %31, %23, %16
%37 = load i32, i32* %1
ret i32 %37
}

LLVM IR (-O2):
@.str = [14 x i8] c"hello, world!\00"
@.str.1 = [9 x i8] c"\09realloc\00"
@str = [9 x i8] c" passed.\00"
define i32 @test_realloc() {
%1 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%2 = tail call dereferenceable_or_null(14) i8* @malloc(i64 14)
%3 = tail call i32 @strcpy(i8* %2, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%4 = tail call i8* @realloc(i8* %2, i32 100)
%5 = icmp eq i8* %4, null
br i1 %5, label %17, label %6
6: ; preds = %0
%7 = tail call i32 @putchar(i32 46)
%8 = tail call i32 @msize(i8* %4)
%9 = icmp slt i32 %8, 100
br i1 %9, label %17, label %10
10: ; preds = %6
%11 = tail call i32 @putchar(i32 46)
%12 = tail call i64 @strcmp(i8* %4, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = tail call i32 @putchar(i32 46)
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
br label %17
17: ; preds = %10, %6, %0, %14
%18 = phi i32 [ 0, %14 ], [ 1, %0 ], [ 1, %6 ], [ 1, %10 ]
ret i32 %18
}