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

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
scalar_t__ getpid();
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  pid_t child_id;
  pid_t my_id;
  int pipe_desc[2];
  char *string;
  char read_buffer[4096];
  int err;
  err = pipe(pipe_desc);
  if (err < 0) {
    printf("error creating pipe\n");
    exit(1);
  }
  child_id = fork();
  if (child_id != 0) {
    my_id = getpid();
    close(pipe_desc[0]);
    string = "a string made by the parent\n";
    printf("pid: %d -- writing %s to pipe_desc[1]\n", (int)my_id, string);
    write(pipe_desc[1], string, strlen(string));
    string = "and another string";
    printf("pid: %d -- writing %s to pipe_desc[1]\n", (int)my_id, string);
    write(pipe_desc[1], string, strlen(string));
  } else {
    my_id = getpid();
    close(pipe_desc[1]);
    memset(read_buffer, 0, sizeof(read_buffer));
    while (read(pipe_desc[0], read_buffer, sizeof(read_buffer))) {
      printf("pid: %d -- received %s from parent\n", (int)my_id, read_buffer);
      memset(read_buffer, 0, sizeof(read_buffer));
    }
    printf("pid: %d -- child detects write end closed\n", (int)my_id);
    close(pipe_desc[0]);
  }
  printf("pid: %d -- I am exiting\n", my_id);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1060,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x28(%rbp),%rdi
call   1090 <pipe@plt>
mov    %eax,-0x1034(%rbp)
cmpl   $0x0,-0x1034(%rbp)
jge    11ed <main+0x4d>
lea    0xe24(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12c3 <main+0x123>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%edi
call   1080 <close@plt>
lea    0xdf8(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    %eax,%esi
mov    -0x30(%rbp),%rdx
lea    0xe00(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x1054(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x1050(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x1054(%rbp),%edi
mov    -0x1050(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
lea    0xde8(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    %eax,%esi
mov    -0x30(%rbp),%rdx
lea    0xdac(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x1044(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x1040(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x1044(%rbp),%edi
mov    -0x1040(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
jmp    1356 <main+0x1b6>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
lea    -0x1030(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1070 <memset@plt>
mov    -0x28(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    $0x1000,%edx
call   10a0 <read@plt>
cmp    $0x0,%rax
je     133a <main+0x19a>
mov    -0x20(%rbp),%rax
mov    %eax,%esi
lea    -0x1030(%rbp),%rdx
lea    0xd51(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x1030(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1070 <memset@plt>
jmp    12e9 <main+0x149>
mov    -0x20(%rbp),%rax
mov    %eax,%esi
lea    0xd49(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x28(%rbp),%edi
call   1080 <close@plt>
mov    -0x20(%rbp),%rsi
lea    0xd5a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1018,%rsp
lea    0x8(%rsp),%rdi
call   10a0 <pipe@plt>
test   %eax,%eax
jns    11e1 <main+0x31>
lea    0xeed(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %rax,%r14
test   %rbx,%rbx
jne    1291 <main+0xe1>
mov    0xc(%rsp),%edi
call   1090 <close@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1080 <memset@plt>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   10b0 <read@plt>
test   %rax,%rax
je     1275 <main+0xc5>
lea    0xe1f(%rip),%r15
lea    0x10(%rsp),%rbx
nopl   (%rax)
mov    %r15,%rdi
mov    %r14d,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1080 <memset@plt>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   10b0 <read@plt>
test   %rax,%rax
jne    1240 <main+0x90>
lea    0xdff(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x8(%rsp),%edi
call   1090 <close@plt>
jmp    12ff <main+0x14f>
mov    0x8(%rsp),%edi
call   1090 <close@plt>
lea    0xd7c(%rip),%r15
lea    0xd58(%rip),%rbx
mov    %r15,%rdi
mov    %r14d,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <printf@plt>
mov    0xc(%rsp),%ebp
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
lea    0xd6d(%rip),%rbx
mov    %r15,%rdi
mov    %r14d,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <printf@plt>
mov    0xc(%rsp),%ebp
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
lea    0xda0(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [21 x i8] c"error creating pipe\0A\00"
@.str.1 = [29 x i8] c"a string made by the parent\0A\00"
@.str.2 = [39 x i8] c"pid: %d -- writing %s to pipe_desc[1]\0A\00"
@.str.3 = [19 x i8] c"and another string\00"
@.str.4 = [36 x i8] c"pid: %d -- received %s from parent\0A\00"
@.str.5 = [43 x i8] c"pid: %d -- child detects write end closed\0A\00"
@.str.6 = [25 x i8] c"pid: %d -- I am exiting\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
%8 = alloca [2 x i32]
%9 = alloca i8*
%10 = alloca [4096 x i8]
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%13 = call i32 @pipe(i32* %12)
store i32 %13, i32* %11
%14 = load i32, i32* %11
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = call i64 (...) @fork()
store i64 %20, i64* %6
%21 = load i64, i64* %6
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %48
23: ; preds = %19
%24 = call i64 (...) @getpid()
store i64 %24, i64* %7
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%26 = load i32, i32* %25
%27 = call i32 @close(i32 %26)
store i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8** %9
%28 = load i64, i64* %7
%29 = trunc i64 %28 to i32
%30 = load i8*, i8** %9
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 %29, i8* %30)
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%33 = load i32, i32* %32
%34 = load i8*, i8** %9
%35 = load i8*, i8** %9
%36 = call i32 @strlen(i8* %35)
%37 = call i32 @write(i32 %33, i8* %34, i32 %36)
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8** %9
%38 = load i64, i64* %7
%39 = trunc i64 %38 to i32
%40 = load i8*, i8** %9
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 %39, i8* %40)
%42 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%43 = load i32, i32* %42
%44 = load i8*, i8** %9
%45 = load i8*, i8** %9
%46 = call i32 @strlen(i8* %45)
%47 = call i32 @write(i32 %43, i8* %44, i32 %46)
br label %75
48: ; preds = %19
%49 = call i64 (...) @getpid()
store i64 %49, i64* %7
%50 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%51 = load i32, i32* %50
%52 = call i32 @close(i32 %51)
%53 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%54 = call i32 @memset(i8* %53, i32 0, i32 4096)
br label %55
55: ; preds = %61, %48
%56 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%57 = load i32, i32* %56
%58 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%59 = call i64 @read(i32 %57, i8* %58, i32 4096)
%60 = icmp ne i64 %59, 0
br i1 %60, label %61, label %68
61: ; preds = %55
%62 = load i64, i64* %7
%63 = trunc i64 %62 to i32
%64 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i32 %63, i8* %64)
%66 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%67 = call i32 @memset(i8* %66, i32 0, i32 4096)
br label %55
68: ; preds = %55
%69 = load i64, i64* %7
%70 = trunc i64 %69 to i32
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.5, i64 0, i64 0), i32 %70)
%72 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%73 = load i32, i32* %72
%74 = call i32 @close(i32 %73)
br label %75
75: ; preds = %68, %23
%76 = load i64, i64* %7
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0), i64 %76)
%78 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str.1 = [29 x i8] c"a string made by the parent\0A\00"
@.str.2 = [39 x i8] c"pid: %d -- writing %s to pipe_desc[1]\0A\00"
@.str.3 = [19 x i8] c"and another string\00"
@.str.4 = [36 x i8] c"pid: %d -- received %s from parent\0A\00"
@.str.5 = [43 x i8] c"pid: %d -- child detects write end closed\0A\00"
@.str.6 = [25 x i8] c"pid: %d -- I am exiting\0A\00"
@str = [20 x i8] c"error creating pipe\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [2 x i32]
%4 = alloca [4096 x i8]
%5 = bitcast [2 x i32]* %3 to i8*
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%8 = call i32 @pipe(i32* %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = call i64 (...) @fork()
%15 = icmp eq i64 %14, 0
%16 = call i64 (...) @getpid()
br i1 %15, label %30, label %17
17: ; preds = %13
%18 = load i32, i32* %7
%19 = call i32 @close(i32 %18)
%20 = trunc i64 %16 to i32
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 %20, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%23 = load i32, i32* %22
%24 = call i32 @strlen(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @write(i32 %23, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %24)
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 %20, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%27 = load i32, i32* %22
%28 = call i32 @strlen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%29 = call i32 @write(i32 %27, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %28)
br label %49
30: ; preds = %13
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%32 = load i32, i32* %31
%33 = call i32 @close(i32 %32)
%34 = call i32 @memset(i8* %6, i32 0, i32 4096)
%35 = load i32, i32* %7
%36 = call i64 @read(i32 %35, i8* %6, i32 4096)
%37 = icmp eq i64 %36, 0
%38 = trunc i64 %16 to i32
br i1 %37, label %45, label %39
39: ; preds = %30, %39
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i32 %38, i8* %6)
%41 = call i32 @memset(i8* %6, i32 0, i32 4096)
%42 = load i32, i32* %7
%43 = call i64 @read(i32 %42, i8* %6, i32 4096)
%44 = icmp eq i64 %43, 0
br i1 %44, label %45, label %39
45: ; preds = %39, %30
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.5, i64 0, i64 0), i32 %38)
%47 = load i32, i32* %7
%48 = call i32 @close(i32 %47)
br label %49
49: ; preds = %45, %17
%50 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0), i64 %16)
%51 = call i32 @exit(i32 0)
unreachable
}