#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 pid_t;

/* Variables and functions */
int /*<<< orphan*/ SYS_getpid;
int /*<<< orphan*/ close(int);
int fork();
int getpid();
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ read(int, char *, int);
scalar_t__ strlen(char *);
int /*<<< orphan*/ syscall(int /*<<< orphan*/);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ write(int, char *, scalar_t__);
int main() {
  int pipefd[2];
  pid_t pid;
  printf("prefork %ld\n", syscall(SYS_getpid));
  pipe(pipefd);
  pid = fork();
  printf("forked pid = %d\n", pid);
  if (pid == 0) {
    char c;
    printf("child reading %d\n", getpid());
    close(pipefd[1]);
    while (read(pipefd[0], &c, 1) > 0 && c)
      printf("READ %d %c\n", c, c);
    printf("child done\n");
    close(pipefd[0]);
    printf("child done2\n");
  } else {
    char *foo = "foo\n";
    printf("parent writing %d\n", getpid());
    close(pipefd[0]);
    write(pipefd[1], foo, strlen(foo) + 1);
    close(pipefd[1]);
    printf("parent waiting\n");
    wait(NULL);
    printf("parent done\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
call   10a0 <syscall@plt>
mov    %eax,%esi
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0xc(%rbp),%rdi
call   1080 <pipe@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%esi
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
cmpl   $0x0,-0x10(%rbp)
jne    129e <main+0xfe>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xe13(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
mov    -0xc(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1090 <read@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x21(%rbp)
jle    124a <main+0xaa>
movsbl -0x11(%rbp),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    125a <main+0xba>
jmp    1275 <main+0xd5>
movsbl -0x11(%rbp),%esi
movsbl -0x11(%rbp),%edx
lea    0xdc7(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    121a <main+0x7a>
lea    0xdc0(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
lea    0xdb6(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    131f <main+0x17f>
lea    0xdb0(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xda1(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
lea    0xd6c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10b0 <wait@plt>
lea    0xd65(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
call   10a0 <syscall@plt>
lea    0xe44(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0x10(%rsp),%rdi
call   1080 <pipe@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,%ebx
lea    0xe2e(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
test   %ebx,%ebx
je     1249 <main+0xa9>
call   1050 <getpid@plt>
lea    0xe47(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x10(%rsp),%edi
call   1070 <close@plt>
mov    0x14(%rsp),%edi
lea    0xe25(%rip),%rsi
mov    $0x5,%edx
call   1040 <write@plt>
mov    0x14(%rsp),%edi
call   1070 <close@plt>
lea    0xe23(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   10b0 <wait@plt>
lea    0xe1f(%rip),%rdi
jmp    12ea <main+0x14a>
call   1050 <getpid@plt>
lea    0xdc9(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x14(%rsp),%edi
call   1070 <close@plt>
mov    0x10(%rsp),%edi
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1090 <read@plt>
test   %rax,%rax
jle    12ce <main+0x12e>
mov    0xf(%rsp),%al
test   %al,%al
je     12ce <main+0x12e>
lea    0xda2(%rip),%r14
lea    0xf(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsbl %al,%edx
mov    %r14,%rdi
mov    %edx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x10(%rsp),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1090 <read@plt>
test   %rax,%rax
jle    12ce <main+0x12e>
movzbl 0xf(%rsp),%eax
test   %al,%al
jne    12a0 <main+0x100>
lea    0xd9a(%rip),%rdi
call   1030 <puts@plt>
mov    0x10(%rsp),%edi
call   1070 <close@plt>
lea    0xd90(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [13 x i8] c"prefork %ld\0A\00"
@SYS_getpid = global i32 0
@.str.1 = [17 x i8] c"forked pid = %d\0A\00"
@.str.2 = [18 x i8] c"child reading %d\0A\00"
@.str.3 = [12 x i8] c"READ %d %c\0A\00"
@.str.4 = [12 x i8] c"child done\0A\00"
@.str.5 = [13 x i8] c"child done2\0A\00"
@.str.6 = [5 x i8] c"foo\0A\00"
@.str.7 = [19 x i8] c"parent writing %d\0A\00"
@.str.8 = [16 x i8] c"parent waiting\0A\00"
@.str.9 = [13 x i8] c"parent done\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i32
%4 = alloca i8
%5 = alloca i8*
store i32 0, i32* %1
%6 = load i32, i32* @SYS_getpid
%7 = call i32 @syscall(i32 %6)
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%10 = call i32 @pipe(i32* %9)
%11 = call i32 (...) @fork()
store i32 %11, i32* %3
%12 = load i32, i32* %3
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = load i32, i32* %3
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %45
16: ; preds = %0
%17 = call i32 (...) @getpid()
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %17)
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%20 = load i32, i32* %19
%21 = call i32 @close(i32 %20)
br label %22
22: ; preds = %33, %16
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%24 = load i32, i32* %23
%25 = call i64 @read(i32 %24, i8* %4, i32 1)
%26 = icmp sgt i64 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %22
%28 = load i8, i8* %4
%29 = sext i8 %28 to i32
%30 = icmp ne i32 %29, 0
br label %31
31: ; preds = %27, %22
%32 = phi i1 [ false, %22 ], [ %30, %27 ]
br i1 %32, label %33, label %39
33: ; preds = %31
%34 = load i8, i8* %4
%35 = sext i8 %34 to i32
%36 = load i8, i8* %4
%37 = sext i8 %36 to i32
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %35, i32 %37)
br label %22
39: ; preds = %31
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%42 = load i32, i32* %41
%43 = call i32 @close(i32 %42)
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
br label %64
45: ; preds = %0
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i8** %5
%46 = call i32 (...) @getpid()
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0), i32 %46)
%48 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%49 = load i32, i32* %48
%50 = call i32 @close(i32 %49)
%51 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%52 = load i32, i32* %51
%53 = load i8*, i8** %5
%54 = load i8*, i8** %5
%55 = call i64 @strlen(i8* %54)
%56 = add nsw i64 %55, 1
%57 = call i32 @write(i32 %52, i8* %53, i64 %56)
%58 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%59 = load i32, i32* %58
%60 = call i32 @close(i32 %59)
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.8, i64 0, i64 0))
%62 = call i32 @wait(i32* null)
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.9, i64 0, i64 0))
br label %64
64: ; preds = %45, %39
ret i32 0
}

LLVM IR (-O2):
@.str = [13 x i8] c"prefork %ld\0A\00"
@SYS_getpid = global i32 0
@.str.1 = [17 x i8] c"forked pid = %d\0A\00"
@.str.2 = [18 x i8] c"child reading %d\0A\00"
@.str.3 = [12 x i8] c"READ %d %c\0A\00"
@.str.6 = [5 x i8] c"foo\0A\00"
@.str.7 = [19 x i8] c"parent writing %d\0A\00"
@str = [15 x i8] c"parent waiting\00"
@str.10 = [12 x i8] c"parent done\00"
@str.11 = [11 x i8] c"child done\00"
@str.12 = [12 x i8] c"child done2\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca i8
%3 = bitcast [2 x i32]* %1 to i8*
%4 = load i32, i32* @SYS_getpid
%5 = tail call i32 @syscall(i32 %4)
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%8 = call i32 @pipe(i32* %7)
%9 = call i32 (...) @fork()
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%11 = icmp eq i32 %9, 0
br i1 %11, label %12, label %39
12: ; preds = %0
%13 = call i32 (...) @getpid()
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %13)
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%16 = load i32, i32* %15
%17 = call i32 @close(i32 %16)
%18 = load i32, i32* %7
%19 = call i64 @read(i32 %18, i8* %2, i32 1)
%20 = icmp sgt i64 %19, 0
%21 = load i8, i8* %2
%22 = icmp ne i8 %21, 0
%23 = select i1 %20, i1 %22, i1 false
br i1 %23, label %24, label %34
24: ; preds = %12, %24
%25 = phi i8 [ %31, %24 ], [ %21, %12 ]
%26 = sext i8 %25 to i32
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %26, i32 %26)
%28 = load i32, i32* %7
%29 = call i64 @read(i32 %28, i8* %2, i32 1)
%30 = icmp sgt i64 %29, 0
%31 = load i8, i8* %2
%32 = icmp ne i8 %31, 0
%33 = select i1 %30, i1 %32, i1 false
br i1 %33, label %24, label %34
34: ; preds = %24, %12
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.11, i64 0, i64 0))
%36 = load i32, i32* %7
%37 = call i32 @close(i32 %36)
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.12, i64 0, i64 0))
br label %52
39: ; preds = %0
%40 = call i32 (...) @getpid()
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0), i32 %40)
%42 = load i32, i32* %7
%43 = call i32 @close(i32 %42)
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%45 = load i32, i32* %44
%46 = call i32 @write(i32 %45, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i64 5)
%47 = load i32, i32* %44
%48 = call i32 @close(i32 %47)
%49 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%50 = call i32 @wait(i32* null)
%51 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.10, i64 0, i64 0))
br label %52
52: ; preds = %39, %34
ret i32 0
}