#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;
typedef int /*<<< orphan*/ buf2;
typedef int /*<<< orphan*/ buf1;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  pid_t pid;
  int fd[2], n;
  char buf1[512], buf2[512];
  pipe(fd);
  pid = fork();
  if (pid < 0) {
    return 1;
  } else if (pid == 0) {
    close(fd[1]);
    while ((n = read(fd[0], buf2, sizeof(buf2))) > 0) {
      buf2[n] = 0;
      printf("read %d bytes from pipe:\n\t%s", n, buf2);
    }
    close(fd[0]);
  } else {
    close(fd[0]);
    strcpy(buf1, "This is a message from parent to child\n");
    int f = open("README.txt", O_RDONLY, 0);
    if (f < 0) {
      printf("Cannot open README.txt\n");
      close(fd[1]);
      exit(1);
    }
    while ((n = read(f, buf1, sizeof(buf1))) > 0) {
      write(fd[1], buf1, n);
    }
    close(fd[1]);
    close(f);
    wait(NULL);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x18(%rbp),%rdi
call   1070 <pipe@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11dd <main+0x3d>
movl   $0x1,-0x4(%rbp)
jmp    130b <main+0x16b>
cmpq   $0x0,-0x10(%rbp)
jne    1246 <main+0xa6>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x200,%edx
call   1080 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jle    1239 <main+0x99>
movslq -0x1c(%rbp),%rax
movb   $0x0,-0x420(%rbp,%rax,1)
mov    -0x1c(%rbp),%esi
lea    -0x420(%rbp),%rdx
lea    0xdd3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    11f0 <main+0x50>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
jmp    1306 <main+0x166>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
lea    -0x220(%rbp),%rdi
lea    0xdc1(%rip),%rsi
call   1030 <strcpy@plt>
mov    0x2d80(%rip),%rax
mov    (%rax),%esi
lea    0xdd4(%rip),%rdi
xor    %edx,%edx
call   1090 <open@plt>
mov    %eax,-0x424(%rbp)
cmpl   $0x0,-0x424(%rbp)
jge    12ab <main+0x10b>
lea    0xdbe(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    12b0 <main+0x110>
mov    -0x424(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0x200,%edx
call   1080 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jle    12ea <main+0x14a>
mov    -0x14(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <write@plt>
jmp    12b0 <main+0x110>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x424(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10b0 <wait@plt>
jmp    130b <main+0x16b>
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x210,%rsp
lea    0x8(%rsp),%rdi
call   1080 <pipe@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
js     126c <main+0xbc>
je     1273 <main+0xc3>
mov    0x8(%rsp),%edi
call   1070 <close@plt>
lea    0xe32(%rip),%rsi
lea    0x10(%rsp),%rdi
call   1030 <strcpy@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
lea    0xe4c(%rip),%rdi
xor    %edx,%edx
call   10a0 <open@plt>
test   %eax,%eax
js     12e4 <main+0x134>
mov    %eax,%ebp
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0x200,%edx
call   1090 <read@plt>
test   %eax,%eax
jle    1251 <main+0xa1>
lea    0x10(%rsp),%rbx
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1090 <read@plt>
test   %eax,%eax
jg     1230 <main+0x80>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %ebx,%ebx
xor    %edi,%edi
call   10c0 <wait@plt>
jmp    12d6 <main+0x126>
mov    $0x1,%ebx
jmp    12d6 <main+0x126>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0x200,%edx
call   1090 <read@plt>
test   %eax,%eax
jle    12cb <main+0x11b>
lea    0xd66(%rip),%r14
lea    0x10(%rsp),%rbx
nop
mov    %eax,%ecx
movb   $0x0,0x10(%rsp,%rcx,1)
mov    %r14,%rdi
mov    %eax,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1090 <read@plt>
test   %eax,%eax
jg     12a0 <main+0xf0>
mov    0x8(%rsp),%edi
call   1070 <close@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x210,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd5a(%rip),%rdi
call   1040 <puts@plt>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [29 x i8] c"read %d bytes from pipe:\0A\09%s\00"
@.str.1 = [40 x i8] c"This is a message from parent to child\0A\00"
@.str.2 = [11 x i8] c"README.txt\00"
@O_RDONLY = global i32 0
@.str.3 = [24 x i8] c"Cannot open README.txt\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca [2 x i32]
%4 = alloca i32
%5 = alloca [512 x i8]
%6 = alloca [512 x i8]
%7 = alloca i32
store i32 0, i32* %1
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%9 = call i32 @pipe(i32* %8)
%10 = call i64 (...) @fork()
store i64 %10, i64* %2
%11 = load i64, i64* %2
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %0
store i32 1, i32* %1
br label %74
14: ; preds = %0
%15 = load i64, i64* %2
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %38
17: ; preds = %14
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%19 = load i32, i32* %18
%20 = call i32 @close(i32 %19)
br label %21
21: ; preds = %27, %17
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%23 = load i32, i32* %22
%24 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%25 = call i32 @read(i32 %23, i8* %24, i32 512)
store i32 %25, i32* %4
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %34
27: ; preds = %21
%28 = load i32, i32* %4
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 %29
store i8 0, i8* %30
%31 = load i32, i32* %4
%32 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %31, i8* %32)
br label %21
34: ; preds = %21
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%36 = load i32, i32* %35
%37 = call i32 @close(i32 %36)
br label %73
38: ; preds = %14
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%40 = load i32, i32* %39
%41 = call i32 @close(i32 %40)
%42 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%43 = call i32 @strcpy(i8* %42, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
%44 = load i32, i32* @O_RDONLY
%45 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %44, i32 0)
store i32 %45, i32* %7
%46 = load i32, i32* %7
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %54
48: ; preds = %38
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%50 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%51 = load i32, i32* %50
%52 = call i32 @close(i32 %51)
%53 = call i32 @exit(i32 1)
unreachable
54: ; preds = %38
br label %55
55: ; preds = %60, %54
%56 = load i32, i32* %7
%57 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%58 = call i32 @read(i32 %56, i8* %57, i32 512)
store i32 %58, i32* %4
%59 = icmp sgt i32 %58, 0
br i1 %59, label %60, label %66
60: ; preds = %55
%61 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%62 = load i32, i32* %61
%63 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%64 = load i32, i32* %4
%65 = call i32 @write(i32 %62, i8* %63, i32 %64)
br label %55
66: ; preds = %55
%67 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%68 = load i32, i32* %67
%69 = call i32 @close(i32 %68)
%70 = load i32, i32* %7
%71 = call i32 @close(i32 %70)
%72 = call i32 @wait(i32* null)
br label %73
73: ; preds = %66, %34
br label %74
74: ; preds = %13, %73
%75 = load i32, i32* %1
ret i32 %75
}

LLVM IR (-O2):
@.str = [29 x i8] c"read %d bytes from pipe:\0A\09%s\00"
@.str.1 = [40 x i8] c"This is a message from parent to child\0A\00"
@.str.2 = [11 x i8] c"README.txt\00"
@O_RDONLY = global i32 0
@str = [23 x i8] c"Cannot open README.txt\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [512 x i8]
%3 = alloca [512 x i8]
%4 = bitcast [2 x i32]* %1 to i8*
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%8 = call i32 @pipe(i32* %7)
%9 = call i64 (...) @fork()
%10 = icmp slt i64 %9, 0
br i1 %10, label %61, label %11
11: ; preds = %0
%12 = icmp eq i64 %9, 0
br i1 %12, label %13, label %31
13: ; preds = %11
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%15 = load i32, i32* %14
%16 = call i32 @close(i32 %15)
%17 = load i32, i32* %7
%18 = call i32 @read(i32 %17, i8* %6, i32 512)
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %28
20: ; preds = %13, %20
%21 = phi i32 [ %26, %20 ], [ %18, %13 ]
%22 = zext i32 %21 to i64
%23 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 %22
store i8 0, i8* %23
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %21, i8* %6)
%25 = load i32, i32* %7
%26 = call i32 @read(i32 %25, i8* %6, i32 512)
%27 = icmp sgt i32 %26, 0
br i1 %27, label %20, label %28
28: ; preds = %20, %13
%29 = load i32, i32* %7
%30 = call i32 @close(i32 %29)
br label %61
31: ; preds = %11
%32 = load i32, i32* %7
%33 = call i32 @close(i32 %32)
%34 = call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
%35 = load i32, i32* @O_RDONLY
%36 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %35, i32 0)
%37 = icmp slt i32 %36, 0
br i1 %37, label %43, label %38
38: ; preds = %31
%39 = call i32 @read(i32 %36, i8* %5, i32 512)
%40 = icmp sgt i32 %39, 0
br i1 %40, label %41, label %55
41: ; preds = %38
%42 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
br label %49
43: ; preds = %31
%44 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%46 = load i32, i32* %45
%47 = call i32 @close(i32 %46)
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %41, %49
%50 = phi i32 [ %39, %41 ], [ %53, %49 ]
%51 = load i32, i32* %42
%52 = call i32 @write(i32 %51, i8* %5, i32 %50)
%53 = call i32 @read(i32 %36, i8* %5, i32 512)
%54 = icmp sgt i32 %53, 0
br i1 %54, label %49, label %55
55: ; preds = %49, %38
%56 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%57 = load i32, i32* %56
%58 = call i32 @close(i32 %57)
%59 = call i32 @close(i32 %36)
%60 = call i32 @wait(i32* null)
br label %61
61: ; preds = %55, %28, %0
%62 = phi i32 [ 1, %0 ], [ 0, %28 ], [ 0, %55 ]
ret i32 %62
}