#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 *realloc(char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char **argv) {
  char *x = 0, *y;
  for (;;) {
    if ((y = realloc(x, 1 << 19)) == 0) {
      write(1, "X\n", 2);
      free(x);
      return 0;
    }
    if ((x = realloc(y, 1 << 19)) == 0) {
      write(1, "Y\n", 2);
      free(y);
      return 0;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x80000,%esi
call   1050 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1195 <main+0x65>
mov    $0x1,%edi
lea    0xe8a(%rip),%rsi
mov    $0x2,%edx
call   1040 <write@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    11e1 <main+0xb1>
mov    -0x20(%rbp),%rdi
mov    $0x80000,%esi
call   1050 <realloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11dc <main+0xac>
mov    $0x1,%edi
lea    0xe46(%rip),%rsi
mov    $0x2,%edx
call   1040 <write@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    11e1 <main+0xb1>
jmp    114e <main+0x1e>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
xor    %r14d,%r14d
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    $0x80000,%esi
call   1050 <realloc@plt>
test   %rax,%rax
je     1185 <main+0x55>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x80000,%esi
call   1050 <realloc@plt>
mov    %rax,%r14
test   %rax,%rax
jne    1140 <main+0x10>
lea    0xe92(%rip),%rsi
mov    $0x1,%edi
mov    $0x2,%edx
call   1040 <write@plt>
mov    %rbx,%rdi
jmp    119e <main+0x6e>
lea    0xe74(%rip),%rsi
mov    $0x1,%edi
mov    $0x2,%edx
call   1040 <write@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [3 x i8] c"X\0A\00"
@.str.1 = [3 x i8] c"Y\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %6
br label %8
8: ; preds = %24, %2
%9 = load i8*, i8** %6
%10 = call i8* @realloc(i8* %9, i32 524288)
store i8* %10, i8** %7
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %16
12: ; preds = %8
%13 = call i32 @write(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%14 = load i8*, i8** %6
%15 = call i32 @free(i8* %14)
store i32 0, i32* %3
br label %25
16: ; preds = %8
%17 = load i8*, i8** %7
%18 = call i8* @realloc(i8* %17, i32 524288)
store i8* %18, i8** %6
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %24
20: ; preds = %16
%21 = call i32 @write(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2)
%22 = load i8*, i8** %7
%23 = call i32 @free(i8* %22)
store i32 0, i32* %3
br label %25
24: ; preds = %16
br label %8
25: ; preds = %20, %12
%26 = load i32, i32* %3
ret i32 %26
}

LLVM IR (-O2):
@.str = [3 x i8] c"X\0A\00"
@.str.1 = [3 x i8] c"Y\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
br label %3
3: ; preds = %10, %2
%4 = phi i8* [ null, %2 ], [ %11, %10 ]
%5 = tail call i8* @realloc(i8* %4, i32 524288)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %10
7: ; preds = %3
%8 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%9 = tail call i32 @free(i8* %4)
br label %16
10: ; preds = %3
%11 = tail call i8* @realloc(i8* %5, i32 524288)
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %3
13: ; preds = %10
%14 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2)
%15 = tail call i32 @free(i8* %5)
br label %16
16: ; preds = %13, %7
ret i32 0
}