#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*/ close(int);
int fork();
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
size_t read(int, char *, int);
size_t write(int, char *, int);
int main() {
  int fd1[2], fd2[2];
  size_t size;
  char string1[] = "Hello, World?";
  char string2[] = "hello, world!";
  char st1[50], st2[50];
  int sz = 14;
  if (pipe(fd1) != 0)
    printf("Bad1\n");
  if (pipe(fd2) != 0)
    printf("Bad2\n");
  int id = fork();
  if (id < 0)
    printf("Bad");
  if (id > 0) {
    size = read(fd1[0], st1, sz);
    printf("Parent read - |%s|\n", st1);
    size = write(fd2[1], string2, sz);
  } else {
    size = write(fd1[1], string1, sz);
    size = read(fd2[0], st2, sz);
    printf("Child read - |%s|\n", st2);
  }
  close(fd1[0]);
  close(fd1[1]);
  close(fd2[0]);
  close(fd2[1]);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe87(%rip),%rax
mov    %rax,-0x2e(%rbp)
mov    0xe85(%rip),%eax
mov    %eax,-0x26(%rbp)
mov    0xe7f(%rip),%ax
mov    %ax,-0x22(%rbp)
mov    0xe76(%rip),%rax
mov    %rax,-0x3c(%rbp)
mov    0xe74(%rip),%eax
mov    %eax,-0x34(%rbp)
mov    0xe6e(%rip),%ax
mov    %ax,-0x30(%rbp)
movl   $0xe,-0xb4(%rbp)
lea    -0xc(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0x0,%rax
je     11db <main+0x7b>
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x14(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0x0,%rax
je     11fc <main+0x9c>
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0xb8(%rbp)
cmpl   $0x0,-0xb8(%rbp)
jge    1224 <main+0xc4>
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0xb8(%rbp)
jle    1274 <main+0x114>
mov    -0xc(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    -0xb4(%rbp),%edx
call   1070 <read@plt>
mov    %rax,-0x20(%rbp)
lea    -0x70(%rbp),%rsi
lea    0xdda(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%edi
lea    -0x3c(%rbp),%rsi
mov    -0xb4(%rbp),%edx
call   1030 <write@plt>
mov    %rax,-0x20(%rbp)
jmp    12b8 <main+0x158>
mov    -0x8(%rbp),%edi
lea    -0x2e(%rbp),%rsi
mov    -0xb4(%rbp),%edx
call   1030 <write@plt>
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0xb0(%rbp),%rsi
mov    -0xb4(%rbp),%edx
call   1070 <read@plt>
mov    %rax,-0x20(%rbp)
lea    -0xb0(%rbp),%rsi
lea    0xd8f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x70,%rsp
movabs $0x3f646c726f5720,%rax
mov    %rax,0x26(%rsp)
movabs $0x57202c6f6c6c6548,%rax
2c 20 57
mov    %rax,0x20(%rsp)
movabs $0x21646c726f7720,%rax
mov    %rax,0x16(%rsp)
movabs $0x77202c6f6c6c6568,%rax
2c 20 77
mov    %rax,0x10(%rsp)
lea    0x8(%rsp),%rdi
call   1070 <pipe@plt>
test   %rax,%rax
je     11cc <main+0x5c>
lea    0xe85(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rdi
call   1070 <pipe@plt>
test   %rax,%rax
je     11e5 <main+0x75>
lea    0xe67(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
js     122e <main+0xbe>
je     123c <main+0xcc>
mov    0x8(%rsp),%edi
lea    0x30(%rsp),%rbx
mov    %rbx,%rsi
mov    $0xe,%edx
call   1080 <read@plt>
lea    0xe11(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x4(%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0xe,%edx
call   1040 <write@plt>
jmp    1275 <main+0x105>
lea    0xde7(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0xc(%rsp),%edi
lea    0x20(%rsp),%rsi
mov    $0xe,%edx
call   1040 <write@plt>
mov    (%rsp),%edi
lea    0x30(%rsp),%rbx
mov    %rbx,%rsi
mov    $0xe,%edx
call   1080 <read@plt>
lea    0xdc9(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
mov    (%rsp),%edi
call   1060 <close@plt>
mov    0x4(%rsp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@__const.main.string1 = [14 x i8] c"Hello, World?\00"
@__const.main.string2 = [14 x i8] c"hello, world!\00"
@.str = [6 x i8] c"Bad1\0A\00"
@.str.1 = [6 x i8] c"Bad2\0A\00"
@.str.2 = [4 x i8] c"Bad\00"
@.str.3 = [20 x i8] c"Parent read - |%s|\0A\00"
@.str.4 = [19 x i8] c"Child read - |%s|\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca [2 x i32]
%4 = alloca i64
%5 = alloca [14 x i8]
%6 = alloca [14 x i8]
%7 = alloca [50 x i8]
%8 = alloca [50 x i8]
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %1
%11 = bitcast [14 x i8]* %5 to i8*
%12 = bitcast [14 x i8]* %6 to i8*
store i32 14, i32* %9
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%14 = call i64 @pipe(i32* %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %0
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %0
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%20 = call i64 @pipe(i32* %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %22, %18
%25 = call i32 (...) @fork()
store i32 %25, i32* %10
%26 = load i32, i32* %10
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
br label %30
30: ; preds = %28, %24
%31 = load i32, i32* %10
%32 = icmp sgt i32 %31, 0
br i1 %32, label %33, label %46
33: ; preds = %30
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%35 = load i32, i32* %34
%36 = getelementptr inbounds [50 x i8], [50 x i8]* %7, i64 0, i64 0
%37 = load i32, i32* %9
%38 = call i64 @read(i32 %35, i8* %36, i32 %37)
store i64 %38, i64* %4
%39 = getelementptr inbounds [50 x i8], [50 x i8]* %7, i64 0, i64 0
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %39)
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%42 = load i32, i32* %41
%43 = getelementptr inbounds [14 x i8], [14 x i8]* %6, i64 0, i64 0
%44 = load i32, i32* %9
%45 = call i64 @write(i32 %42, i8* %43, i32 %44)
store i64 %45, i64* %4
br label %59
46: ; preds = %30
%47 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%48 = load i32, i32* %47
%49 = getelementptr inbounds [14 x i8], [14 x i8]* %5, i64 0, i64 0
%50 = load i32, i32* %9
%51 = call i64 @write(i32 %48, i8* %49, i32 %50)
store i64 %51, i64* %4
%52 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%53 = load i32, i32* %52
%54 = getelementptr inbounds [50 x i8], [50 x i8]* %8, i64 0, i64 0
%55 = load i32, i32* %9
%56 = call i64 @read(i32 %53, i8* %54, i32 %55)
store i64 %56, i64* %4
%57 = getelementptr inbounds [50 x i8], [50 x i8]* %8, i64 0, i64 0
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %57)
br label %59
59: ; preds = %46, %33
%60 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%61 = load i32, i32* %60
%62 = call i32 @close(i32 %61)
%63 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%64 = load i32, i32* %63
%65 = call i32 @close(i32 %64)
%66 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%67 = load i32, i32* %66
%68 = call i32 @close(i32 %67)
%69 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%70 = load i32, i32* %69
%71 = call i32 @close(i32 %70)
ret i32 0
}

LLVM IR (-O2):
@__const.main.string1 = [14 x i8] c"Hello, World?\00"
@__const.main.string2 = [14 x i8] c"hello, world!\00"
@.str.2 = [4 x i8] c"Bad\00"
@.str.3 = [20 x i8] c"Parent read - |%s|\0A\00"
@.str.4 = [19 x i8] c"Child read - |%s|\0A\00"
@str = [5 x i8] c"Bad2\00"
@str.5 = [5 x i8] c"Bad1\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [2 x i32]
%3 = alloca [14 x i8]
%4 = alloca [14 x i8]
%5 = alloca [50 x i8]
%6 = alloca [50 x i8]
%7 = bitcast [2 x i32]* %1 to i8*
%8 = bitcast [2 x i32]* %2 to i8*
%9 = getelementptr inbounds [14 x i8], [14 x i8]* %3, i64 0, i64 0
%10 = getelementptr inbounds [14 x i8], [14 x i8]* %4, i64 0, i64 0
%11 = getelementptr inbounds [50 x i8], [50 x i8]* %5, i64 0, i64 0
%12 = getelementptr inbounds [50 x i8], [50 x i8]* %6, i64 0, i64 0
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%14 = call i64 @pipe(i32* %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %0
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str.5, i64 0, i64 0))
br label %18
18: ; preds = %16, %0
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%20 = call i64 @pipe(i32* %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %18
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
br label %24
24: ; preds = %22, %18
%25 = call i32 (...) @fork()
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
br label %38
29: ; preds = %24
%30 = icmp eq i32 %25, 0
br i1 %30, label %38, label %31
31: ; preds = %29
%32 = load i32, i32* %13
%33 = call i64 @read(i32 %32, i8* %11, i32 14)
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %11)
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%36 = load i32, i32* %35
%37 = call i64 @write(i32 %36, i8* %10, i32 14)
br label %45
38: ; preds = %27, %29
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%40 = load i32, i32* %39
%41 = call i64 @write(i32 %40, i8* %9, i32 14)
%42 = load i32, i32* %19
%43 = call i64 @read(i32 %42, i8* %12, i32 14)
%44 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %12)
br label %45
45: ; preds = %38, %31
%46 = load i32, i32* %13
%47 = call i32 @close(i32 %46)
%48 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%49 = load i32, i32* %48
%50 = call i32 @close(i32 %49)
%51 = load i32, i32* %19
%52 = call i32 @close(i32 %51)
%53 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%54 = load i32, i32* %53
%55 = call i32 @close(i32 %54)
ret i32 0
}