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


x86 assembly (clang, -O0):
<test2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movq   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x64,%esi
call   1060 <realloc@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    118e <test2+0x4e>
lea    0xe85(%rip),%rdi
call   1040 <printf@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1197 <test2+0x57>
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     11dd <test2+0x9d>
lea    0xe5d(%rip),%rdi
call   1040 <printf@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test2>:
push   %r14
push   %rbx
push   %rax
xor    %r14d,%r14d
xor    %edi,%edi
mov    $0x64,%esi
call   1060 <realloc@plt>
test   %rax,%rax
je     1162 <test2+0x22>
mov    %rax,%rdi
call   1030 <free@plt>
jmp    1174 <test2+0x34>
lea    0xe97(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%r14d
mov    $0x64,%edi
call   1050 <malloc@plt>
mov    %rax,%rdi
xor    %esi,%esi
call   1060 <realloc@plt>
test   %rax,%rax
je     11a8 <test2+0x68>
mov    %rax,%rbx
lea    0xe8b(%rip),%rdi
call   1040 <puts@plt>
add    $0x1,%r14d
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
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 i32 @test2() {
%1 = alloca i8*
%2 = alloca i32
store i8* null, i8** %1
store i32 0, i32* %2
%3 = load i8*, i8** %1
%4 = call i8* @realloc(i8* %3, i32 100)
store i8* %4, i8** %1
%5 = load i8*, i8** %1
%6 = icmp ne i8* %5, null
br i1 %6, label %11, label %7
7: ; preds = %0
%8 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* %2
%10 = add nsw i32 %9, 1
store i32 %10, i32* %2
br label %14
11: ; preds = %0
%12 = load i8*, i8** %1
%13 = call i32 @free(i8* %12)
br label %14
14: ; preds = %11, %7
%15 = call i8* @malloc(i32 100)
store i8* %15, i8** %1
%16 = load i8*, i8** %1
%17 = call i8* @realloc(i8* %16, i32 0)
store i8* %17, i8** %1
%18 = load i8*, i8** %1
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %26
20: ; preds = %14
%21 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32, i32* %2
%23 = add nsw i32 %22, 1
store i32 %23, i32* %2
%24 = load i8*, i8** %1
%25 = call i32 @free(i8* %24)
br label %26
26: ; preds = %20, %14
%27 = load i32, i32* %2
ret i32 %27
}

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 i32 @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 = phi i32 [ 0, %5 ], [ 1, %3 ]
%9 = tail call dereferenceable_or_null(100) i8* @malloc(i32 100)
%10 = tail call i8* @realloc(i8* %9, i32 0)
%11 = icmp eq i8* %10, null
br i1 %11, label %16, label %12
12: ; preds = %7
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.2, i64 0, i64 0))
%14 = add nuw nsw i32 %8, 1
%15 = tail call i32 @free(i8* %10)
br label %16
16: ; preds = %12, %7
%17 = phi i32 [ %14, %12 ], [ %8, %7 ]
ret i32 %17
}