#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 int ssize_t;
typedef scalar_t__ pid_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ calloc(int, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ free(char *);
int getpid();
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *, int);
size_t read(int, char *, int);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int write(int, char *, int);
int main() {
  int fds[2] = {0};
  pipe(fds);
  char buf[(4096 << 4)] = {0};
  pid_t pid = fork();
  if (pid < 0) {
    perror("fork");
    goto err;
  }
  if (pid == 0) {
    printf("pid: %d child\n", getpid());
    memset(buf, 0, sizeof(buf));
    close(fds[1]);
    size_t n;
    while ((n = read(fds[0], buf, sizeof(buf))) != 0) {
      printf("read: %d byte\n", n);
    }
    close(fds[0]);
    exit(0);
  } else {
    printf("pid: %d parent\n", getpid());
    close(fds[0]);
    char *wbuf = (char *)calloc((4096 << 4), 4);
    ssize_t n = 0;
    n = write(fds[1], wbuf, (4096 << 4) << 2);
    printf("write %d byte\n", n);
    close(fds[1]);
    free(wbuf);
  }
  wait(NULL);
  return 0;
err:
  return -1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10030,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1070 <memset@plt>
lea    -0xc(%rbp),%rdi
call   1090 <pipe@plt>
lea    -0x10010(%rbp),%rdi
xor    %esi,%esi
mov    $0x10000,%edx
call   1070 <memset@plt>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %rax,-0x10018(%rbp)
cmpq   $0x0,-0x10018(%rbp)
jge    123b <main+0x6b>
lea    0xdcf(%rip),%rdi
call   10c0 <perror@plt>
jmp    1359 <main+0x189>
cmpq   $0x0,-0x10018(%rbp)
jne    12c7 <main+0xf7>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,%esi
lea    0xdac(%rip),%rdi
call   1060 <printf@plt>
lea    -0x10010(%rbp),%rdi
xor    %esi,%esi
mov    $0x10000,%edx
call   1070 <memset@plt>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
mov    -0xc(%rbp),%edi
lea    -0x10010(%rbp),%rsi
mov    $0x10000,%edx
call   10a0 <read@plt>
mov    %rax,-0x10020(%rbp)
cmp    $0x0,%rax
je     12b8 <main+0xe8>
mov    -0x10020(%rbp),%rax
mov    %eax,%esi
lea    0xd66(%rip),%rdi
call   1060 <printf@plt>
jmp    1279 <main+0xa9>
mov    -0xc(%rbp),%edi
call   1080 <close@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,%esi
lea    0xd4c(%rip),%rdi
call   1060 <printf@plt>
mov    -0xc(%rbp),%edi
call   1080 <close@plt>
mov    $0x10000,%edi
mov    $0x4,%esi
call   10b0 <calloc@plt>
mov    %rax,-0x10028(%rbp)
movl   $0x0,-0x1002c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10028(%rbp),%rsi
mov    $0x40000,%edx
call   1040 <write@plt>
mov    %eax,-0x1002c(%rbp)
mov    -0x1002c(%rbp),%esi
lea    0xd08(%rip),%rdi
call   1060 <printf@plt>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
mov    -0x10028(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10e0 <wait@plt>
movl   $0x0,-0x4(%rbp)
jmp    1360 <main+0x190>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x10018,%rsp
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rdi
call   1090 <pipe@plt>
lea    0x10(%rsp),%rdi
mov    $0x10000,%edx
xor    %esi,%esi
call   1070 <memset@plt>
xor    %eax,%eax
call   10f0 <fork@plt>
test   %rax,%rax
js     1292 <main+0xc2>
mov    %rax,%rbx
xor    %r14d,%r14d
xor    %eax,%eax
call   1050 <getpid@plt>
test   %rbx,%rbx
je     12a6 <main+0xd6>
lea    0xdf8(%rip),%rdi
mov    %eax,%esi
call   1060 <printf@plt>
mov    0x8(%rsp),%edi
call   1080 <close@plt>
mov    $0x10000,%edi
mov    $0x4,%esi
call   10b0 <calloc@plt>
mov    %rax,%rbx
mov    0xc(%rsp),%edi
mov    %rax,%rsi
mov    $0x40000,%edx
call   1040 <write@plt>
lea    0xdce(%rip),%rdi
mov    %eax,%esi
call   1060 <printf@plt>
mov    0xc(%rsp),%edi
call   1080 <close@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %edi,%edi
call   10e0 <wait@plt>
mov    %r14d,%eax
add    $0x10018,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd67(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%r14d
jmp    1284 <main+0xb4>
lea    0xd58(%rip),%rdi
mov    %eax,%esi
call   1060 <printf@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x10000,%edx
call   1070 <memset@plt>
mov    0xc(%rsp),%edi
call   1080 <close@plt>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x10000,%edx
call   10a0 <read@plt>
test   %rax,%rax
je     1320 <main+0x150>
lea    0xd26(%rip),%r14
lea    0x10(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r14,%rdi
mov    %eax,%esi
call   1060 <printf@plt>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x10000,%edx
call   10a0 <read@plt>
test   %rax,%rax
jne    1300 <main+0x130>
mov    0x8(%rsp),%edi
call   1080 <close@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@.str.1 = [15 x i8] c"pid: %d child\0A\00"
@.str.2 = [15 x i8] c"read: %d byte\0A\00"
@.str.3 = [16 x i8] c"pid: %d parent\0A\00"
@.str.4 = [15 x i8] c"write %d byte\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca [65536 x i8]
%4 = alloca i64
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i32
store i32 0, i32* %1
%8 = bitcast [2 x i32]* %2 to i8*
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%10 = call i32 @pipe(i32* %9)
%11 = bitcast [65536 x i8]* %3 to i8*
%12 = call i64 (...) @fork()
store i64 %12, i64* %4
%13 = load i64, i64* %4
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %64
17: ; preds = %0
%18 = load i64, i64* %4
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %43
20: ; preds = %17
%21 = call i32 (...) @getpid()
%22 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = getelementptr inbounds [65536 x i8], [65536 x i8]* %3, i64 0, i64 0
%24 = call i32 @memset(i8* %23, i32 0, i32 65536)
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%26 = load i32, i32* %25
%27 = call i32 @close(i32 %26)
br label %28
28: ; preds = %34, %20
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%30 = load i32, i32* %29
%31 = getelementptr inbounds [65536 x i8], [65536 x i8]* %3, i64 0, i64 0
%32 = call i64 @read(i32 %30, i8* %31, i32 65536)
store i64 %32, i64* %5
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %28
%35 = load i64, i64* %5
%36 = trunc i64 %35 to i32
%37 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %36)
br label %28
38: ; preds = %28
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%40 = load i32, i32* %39
%41 = call i32 @close(i32 %40)
%42 = call i32 @exit(i32 0)
unreachable
43: ; preds = %17
%44 = call i32 (...) @getpid()
%45 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %44)
%46 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%47 = load i32, i32* %46
%48 = call i32 @close(i32 %47)
%49 = call i64 @calloc(i32 65536, i32 4)
%50 = inttoptr i64 %49 to i8*
store i8* %50, i8** %6
store i32 0, i32* %7
%51 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%52 = load i32, i32* %51
%53 = load i8*, i8** %6
%54 = call i32 @write(i32 %52, i8* %53, i32 262144)
store i32 %54, i32* %7
%55 = load i32, i32* %7
%56 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %55)
%57 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%58 = load i32, i32* %57
%59 = call i32 @close(i32 %58)
%60 = load i8*, i8** %6
%61 = call i32 @free(i8* %60)
br label %62
62: ; preds = %43
%63 = call i32 @wait(i32* null)
store i32 0, i32* %1
br label %65
64: ; preds = %15
store i32 -1, i32* %1
br label %65
65: ; preds = %64, %62
%66 = load i32, i32* %1
ret i32 %66
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@.str.1 = [15 x i8] c"pid: %d child\0A\00"
@.str.2 = [15 x i8] c"read: %d byte\0A\00"
@.str.3 = [16 x i8] c"pid: %d parent\0A\00"
@.str.4 = [15 x i8] c"write %d byte\0A\00"
define i32 @main() {
%1 = alloca i64
%2 = bitcast i64* %1 to [2 x i32]*
%3 = alloca [65536 x i8]
%4 = bitcast i64* %1 to i8*
store i64 0, i64* %1
%5 = bitcast i64* %1 to i32*
%6 = call i32 @pipe(i32* %5)
%7 = getelementptr inbounds [65536 x i8], [65536 x i8]* %3, i64 0, i64 0
%8 = call i64 (...) @fork()
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %49
12: ; preds = %0
%13 = icmp eq i64 %8, 0
%14 = call i32 (...) @getpid()
br i1 %13, label %15, label %35
15: ; preds = %12
%16 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%17 = call i32 @memset(i8* %7, i32 0, i32 65536)
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%19 = load i32, i32* %18
%20 = call i32 @close(i32 %19)
%21 = load i32, i32* %5
%22 = call i64 @read(i32 %21, i8* %7, i32 65536)
%23 = icmp eq i64 %22, 0
br i1 %23, label %31, label %24
24: ; preds = %15, %24
%25 = phi i64 [ %29, %24 ], [ %22, %15 ]
%26 = trunc i64 %25 to i32
%27 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %26)
%28 = load i32, i32* %5
%29 = call i64 @read(i32 %28, i8* %7, i32 65536)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %24
31: ; preds = %24, %15
%32 = load i32, i32* %5
%33 = call i32 @close(i32 %32)
%34 = call i32 @exit(i32 0)
unreachable
35: ; preds = %12
%36 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %14)
%37 = load i32, i32* %5
%38 = call i32 @close(i32 %37)
%39 = call i64 @calloc(i32 65536, i32 4)
%40 = inttoptr i64 %39 to i8*
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%42 = load i32, i32* %41
%43 = call i32 @write(i32 %42, i8* %40, i32 262144)
%44 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %43)
%45 = load i32, i32* %41
%46 = call i32 @close(i32 %45)
%47 = call i32 @free(i8* %40)
%48 = call i32 @wait(i32* null)
br label %49
49: ; preds = %10, %35
%50 = phi i32 [ -1, %10 ], [ 0, %35 ]
ret i32 %50
}