#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*/ free(void *);
void *malloc(int);
int /*<<< orphan*/ printf(char *);
void *realloc(void *, int);
void test2(void) {
  void *ptr = NULL;
  ptr = realloc(ptr, 100);
  if (!ptr) {
    printf("couldn't realloc() a NULL pointer\n");
  } else {
    free(ptr);
  }
  ptr = malloc(100);
  ptr = realloc(ptr, 0);
  if (ptr) {
    printf("realloc(,0) failed\n");
    free(ptr);
  }
}


x86 assembly (clang, -O0):
<test2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x64,%esi
call   1060 <realloc@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    117e <test2+0x3e>
lea    0xe8c(%rip),%rdi
call   1040 <printf@plt>
jmp    1187 <test2+0x47>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
mov    $0x64,%edi
call   1050 <malloc@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
call   1060 <realloc@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11c4 <test2+0x84>
lea    0xe6d(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test2>:
push   %rbx
xor    %edi,%edi
mov    $0x64,%esi
call   1060 <realloc@plt>
test   %rax,%rax
je     115c <test2+0x1c>
mov    %rax,%rdi
call   1030 <free@plt>
jmp    1168 <test2+0x28>
lea    0xe9d(%rip),%rdi
call   1040 <puts@plt>
mov    $0x64,%edi
call   1050 <malloc@plt>
mov    %rax,%rdi
xor    %esi,%esi
call   1060 <realloc@plt>
test   %rax,%rax
je     1199 <test2+0x59>
mov    %rax,%rbx
lea    0xe97(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
pop    %rbx
jmp    1030 <free@plt>
pop    %rbx
ret

LLVM IR (-O0):
@.str = [35 x i8] c"couldn't realloc() a NULL pointer\0A\00"
@.str.1 = [20 x i8] c"realloc(,0) failed\0A\00"
define void @test2() {
%1 = alloca i8*
store i8* null, i8** %1
%2 = load i8*, i8** %1
%3 = call i8* @realloc(i8* %2, i32 100)
store i8* %3, i8** %1
%4 = load i8*, i8** %1
%5 = icmp ne i8* %4, null
br i1 %5, label %8, label %6
6: ; preds = %0
%7 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
br label %11
8: ; preds = %0
%9 = load i8*, i8** %1
%10 = call i32 @free(i8* %9)
br label %11
11: ; preds = %8, %6
%12 = call i8* @malloc(i32 100)
store i8* %12, i8** %1
%13 = load i8*, i8** %1
%14 = call i8* @realloc(i8* %13, i32 0)
store i8* %14, i8** %1
%15 = load i8*, i8** %1
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %21
17: ; preds = %11
%18 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%19 = load i8*, i8** %1
%20 = call i32 @free(i8* %19)
br label %21
21: ; preds = %17, %11
ret void
}

LLVM IR (-O2):
@str = [34 x i8] c"couldn't realloc() a NULL pointer\00"
@str.2 = [19 x i8] c"realloc(,0) failed\00"
define void @test2() {
%1 = tail call i8* @realloc(i8* null, i32 100)
%2 = icmp eq i8* %1, null
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
br label %7
5: ; preds = %0
%6 = tail call i32 @free(i8* %1)
br label %7
7: ; preds = %5, %3
%8 = tail call dereferenceable_or_null(100) i8* @malloc(i32 100)
%9 = tail call i8* @realloc(i8* %8, i32 0)
%10 = icmp eq i8* %9, null
br i1 %10, label %14, label %11
11: ; preds = %7
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.2, i64 0, i64 0))
%13 = tail call i32 @free(i8* %9)
br label %14
14: ; preds = %11, %7
ret void
}