#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(char *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, char *, char *);
char *realloc(char *, int);
int main(void) {
  char *x = NULL, *y = NULL;
  printf("before malloc(): x = %p, y = %p\n", x, y);
  x = malloc(123 * sizeof(char));
  y = realloc(x, 456);
  printf("after realloc(): x = %p, y = %p\n", x, y);
  if (y == NULL)
    free(x);
  free(y);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe92(%rip),%rdi
call   1040 <printf@plt>
mov    $0x7b,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x1c8,%esi
call   1060 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe7f(%rip),%rdi
call   1040 <printf@plt>
cmpq   $0x0,-0x18(%rbp)
jne    11bb <main+0x7b>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
lea    0xeb5(%rip),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1040 <printf@plt>
mov    $0x7b,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x1c8,%esi
call   1060 <realloc@plt>
mov    %rax,%rbx
lea    0xea9(%rip),%rdi
mov    %r14,%rsi
mov    %rax,%rdx
call   1040 <printf@plt>
test   %rbx,%rbx
jne    1190 <main+0x50>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [33 x i8] c"before malloc(): x = %p, y = %p\0A\00"
@.str.1 = [33 x i8] c"after realloc(): x = %p, y = %p\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
store i32 0, i32* %1
store i8* null, i8** %2
store i8* null, i8** %3
%4 = load i8*, i8** %2
%5 = load i8*, i8** %3
%6 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8* %4, i8* %5)
%7 = call i8* @malloc(i32 123)
store i8* %7, i8** %2
%8 = load i8*, i8** %2
%9 = call i8* @realloc(i8* %8, i32 456)
store i8* %9, i8** %3
%10 = load i8*, i8** %2
%11 = load i8*, i8** %3
%12 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %10, i8* %11)
%13 = load i8*, i8** %3
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %18
15: ; preds = %0
%16 = load i8*, i8** %2
%17 = call i32 @free(i8* %16)
br label %18
18: ; preds = %15, %0
%19 = load i8*, i8** %3
%20 = call i32 @free(i8* %19)
ret i32 0
}

LLVM IR (-O2):
@.str = [33 x i8] c"before malloc(): x = %p, y = %p\0A\00"
@.str.1 = [33 x i8] c"after realloc(): x = %p, y = %p\0A\00"
define i32 @main() {
%1 = tail call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8* null, i8* null)
%2 = tail call dereferenceable_or_null(123) i8* @malloc(i32 123)
%3 = tail call i8* @realloc(i8* %2, i32 456)
%4 = tail call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %2, i8* %3)
%5 = icmp eq i8* %3, null
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @free(i8* %2)
br label %8
8: ; preds = %6, %0
%9 = tail call i32 @free(i8* %3)
ret i32 0
}