#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 */
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ memset(char *, char, int);
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ wait(int *);
int write(int, char *, int);
int main() {
  int rc, status, filedes[2];
  pid_t pid;
  char buffer[1024];
  if (pipe(filedes) != 0)
    exit(EXIT_FAILURE);
  memset(buffer, 0, 1024);
  pid = fork();
  if (pid > 0) {
    memset(buffer, 'X', 1024);
    close(filedes[0]);
    rc = write(filedes[1], buffer, 1024);
    printf("\nParent wrote %d bytes\n", rc);
    (void)wait(&status);
    printf("\nParent now exiting\n");
    exit(EXIT_SUCCESS);
  }
  if (pid == 0) {
    close(filedes[1]);
    rc = read(filedes[0], buffer, 1024);
    printf("\n      Child read %d bytes  \n", rc);
    write(STDOUT_FILENO, buffer, 1024);
    exit(EXIT_SUCCESS);
  }
  printf("fork failed");
  exit(EXIT_FAILURE);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x14(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0x0,%rax
je     11c3 <main+0x33>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jle    1257 <main+0xc7>
lea    -0x420(%rbp),%rdi
mov    $0x58,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1030 <write@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xdd5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0xc(%rbp),%rdi
call   10a0 <wait@plt>
lea    0xdd6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
jne    12ba <main+0x12a>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1080 <read@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xda2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1030 <write@plt>
mov    0x2d1d(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
lea    0xd8a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d09(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x410,%rsp
lea    0x4(%rsp),%rdi
call   1080 <pipe@plt>
test   %rax,%rax
jne    1294 <main+0xf4>
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
jle    1231 <main+0x91>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x58,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    0x4(%rsp),%edi
call   1070 <close@plt>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1040 <write@plt>
lea    0xdf0(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xc(%rsp),%rdi
call   10b0 <wait@plt>
lea    0xe18(%rip),%rdi
call   1030 <puts@plt>
jmp    1278 <main+0xd8>
jne    1286 <main+0xe6>
mov    0x8(%rsp),%edi
call   1070 <close@plt>
mov    0x4(%rsp),%edi
lea    0x10(%rsp),%rbx
mov    %rbx,%rsi
mov    $0x400,%edx
call   1090 <read@plt>
lea    0xdbf(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1040 <write@plt>
mov    0x2d51(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0xda9(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d3d(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@.str = [24 x i8] c"\0AParent wrote %d bytes\0A\00"
@.str.1 = [21 x i8] c"\0AParent now exiting\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [30 x i8] c"\0A Child read %d bytes \0A\00"
@STDOUT_FILENO = global i32 0
@.str.3 = [12 x i8] c"fork failed\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [2 x i32]
%5 = alloca i64
%6 = alloca [1024 x i8]
store i32 0, i32* %1
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%8 = call i64 @pipe(i32* %7)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = load i32, i32* @EXIT_FAILURE
%12 = call i32 @exit(i32 %11)
unreachable
13: ; preds = %0
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%15 = call i32 @memset(i8* %14, i8 signext 0, i32 1024)
%16 = call i64 (...) @fork()
store i64 %16, i64* %5
%17 = load i64, i64* %5
%18 = icmp sgt i64 %17, 0
br i1 %18, label %19, label %35
19: ; preds = %13
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%21 = call i32 @memset(i8* %20, i8 signext 88, i32 1024)
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%23 = load i32, i32* %22
%24 = call i32 @close(i32 %23)
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%26 = load i32, i32* %25
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%28 = call i32 @write(i32 %26, i8* %27, i32 1024)
store i32 %28, i32* %2
%29 = load i32, i32* %2
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %29)
%31 = call i32 @wait(i32* %3)
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%33 = load i32, i32* @EXIT_SUCCESS
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %13
%36 = load i64, i64* %5
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %53
38: ; preds = %35
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%40 = load i32, i32* %39
%41 = call i32 @close(i32 %40)
%42 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%43 = load i32, i32* %42
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%45 = call i32 @read(i32 %43, i8* %44, i32 1024)
store i32 %45, i32* %2
%46 = load i32, i32* %2
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 %46)
%48 = load i32, i32* @STDOUT_FILENO
%49 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%50 = call i32 @write(i32 %48, i8* %49, i32 1024)
%51 = load i32, i32* @EXIT_SUCCESS
%52 = call i32 @exit(i32 %51)
unreachable
53: ; preds = %35
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%55 = load i32, i32* @EXIT_FAILURE
%56 = call i32 @exit(i32 %55)
unreachable
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@.str = [24 x i8] c"\0AParent wrote %d bytes\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [30 x i8] c"\0A Child read %d bytes \0A\00"
@STDOUT_FILENO = global i32 0
@.str.3 = [12 x i8] c"fork failed\00"
@str = [20 x i8] c"\0AParent now exiting\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca [1024 x i8]
%4 = bitcast i32* %1 to i8*
%5 = bitcast [2 x i32]* %2 to i8*
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%8 = call i64 @pipe(i32* %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %13, label %10
10: ; preds = %0
%11 = load i32, i32* @EXIT_FAILURE
%12 = call i32 @exit(i32 %11)
unreachable
13: ; preds = %0
%14 = call i32 @memset(i8* %6, i8 signext 0, i32 1024)
%15 = call i64 (...) @fork()
%16 = icmp sgt i64 %15, 0
br i1 %16, label %17, label %29
17: ; preds = %13
%18 = call i32 @memset(i8* %6, i8 signext 88, i32 1024)
%19 = load i32, i32* %7
%20 = call i32 @close(i32 %19)
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%22 = load i32, i32* %21
%23 = call i32 @write(i32 %22, i8* %6, i32 1024)
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %23)
%25 = call i32 @wait(i32* %1)
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
%27 = load i32, i32* @EXIT_SUCCESS
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %13
%30 = icmp eq i64 %15, 0
br i1 %30, label %31, label %42
31: ; preds = %29
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%33 = load i32, i32* %32
%34 = call i32 @close(i32 %33)
%35 = load i32, i32* %7
%36 = call i32 @read(i32 %35, i8* %6, i32 1024)
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 %36)
%38 = load i32, i32* @STDOUT_FILENO
%39 = call i32 @write(i32 %38, i8* %6, i32 1024)
%40 = load i32, i32* @EXIT_SUCCESS
%41 = call i32 @exit(i32 %40)
unreachable
42: ; preds = %29
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%44 = load i32, i32* @EXIT_FAILURE
%45 = call i32 @exit(i32 %44)
unreachable
}