#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*/ exit(int);
int fork();
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int strlen(char *);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int pipefd[2];
  char buf[1024];
  int len, read_len;
  if (pipe(pipefd) == -1) {
    perror("pipe() failed");
    exit(1);
  }
  switch (fork()) {
  case -1:
    perror("fork() failed");
    exit(1);
  case 0:
    len = strlen("I want a cookie\n");
    while (1) {
      if (write(pipefd[1], "I want a cookie\n", len) != len) {
        perror("write() failed");
        exit(1);
      }
      sleep(5);
    }
  default:
    len = strlen("testing...\n");
    while (1) {
      if (write(pipefd[1], "testing...\n", len) != len) {
        perror("write() failed");
        exit(1);
      }
      sleep(1);
      read_len = read(pipefd[0], buf, 1024);
      if (read_len <= 0)
        break;
      write(1, buf, read_len);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11c1 <main+0x41>
lea    0xe4e(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x42c(%rbp)
sub    $0xffffffff,%eax
je     11f1 <main+0x71>
jmp    11de <main+0x5e>
mov    -0x42c(%rbp),%eax
test   %eax,%eax
je     1207 <main+0x87>
jmp    125f <main+0xdf>
lea    0xe16(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xe0e(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x424(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x424(%rbp),%edx
lea    0xdf3(%rip),%rsi
call   1030 <write@plt>
cmp    -0x424(%rbp),%eax
je     1250 <main+0xd0>
lea    0xdec(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    $0x5,%edi
call   1090 <sleep@plt>
jmp    1219 <main+0x99>
lea    0xdd6(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x424(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x424(%rbp),%edx
lea    0xdbb(%rip),%rsi
call   1030 <write@plt>
cmp    -0x424(%rbp),%eax
je     12a8 <main+0x128>
lea    0xd94(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    -0x18(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,-0x428(%rbp)
cmpl   $0x0,-0x428(%rbp)
jg     12de <main+0x15e>
jmp    12fa <main+0x17a>
lea    -0x420(%rbp),%rsi
mov    -0x428(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
jmp    1271 <main+0xf1>
jmp    12ff <main+0x17f>
xor    %eax,%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x410,%rsp
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
je     1255 <main+0xd5>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     125e <main+0xde>
cmp    $0xffffffff,%eax
je     12ae <main+0x12e>
lea    0xe7e(%rip),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    0xc(%rsp),%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    %ebx,%eax
jne    1230 <main+0xb0>
lea    0x10(%rsp),%r14
lea    0xe56(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    0x8(%rsp),%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    1246 <main+0xc6>
mov    $0x1,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    0xc(%rsp),%edi
mov    %r15,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
cmp    %ebx,%eax
je     11f0 <main+0x70>
lea    0xdf6(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
xor    %eax,%eax
add    $0x410,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xda4(%rip),%rdi
jmp    1237 <main+0xb7>
lea    0xdb7(%rip),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    0xc(%rsp),%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    %ebx,%eax
jne    1230 <main+0xb0>
lea    0xd94(%rip),%r14
nopl   0x0(%rax,%rax,1)
mov    $0x5,%edi
call   1090 <sleep@plt>
mov    0xc(%rsp),%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
cmp    %ebx,%eax
je     1290 <main+0x110>
jmp    1230 <main+0xb0>
lea    0xd59(%rip),%rdi
jmp    1237 <main+0xb7>

LLVM IR (-O0):
@.str = [14 x i8] c"pipe() failed\00"
@.str.1 = [14 x i8] c"fork() failed\00"
@.str.2 = [17 x i8] c"I want a cookie\0A\00"
@.str.3 = [15 x i8] c"write() failed\00"
@.str.4 = [12 x i8] c"testing...\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [2 x i32]
%7 = alloca [1024 x i8]
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%11 = call i32 @pipe(i32* %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = call i32 (...) @fork()
switch i32 %17, label %35 [
i32 -1, label %18
i32 0, label %21
]
18: ; preds = %16
%19 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %16
%22 = call i32 @strlen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
store i32 %22, i32* %8
br label %23
23: ; preds = %21, %33
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%25 = load i32, i32* %24
%26 = load i32, i32* %8
%27 = call i32 @write(i32 %25, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %26)
%28 = load i32, i32* %8
%29 = icmp ne i32 %27, %28
br i1 %29, label %30, label %33
30: ; preds = %23
%31 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %23
%34 = call i32 @sleep(i32 5)
br label %23
35: ; preds = %16
%36 = call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
store i32 %36, i32* %8
br label %37
37: ; preds = %35, %56
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%39 = load i32, i32* %38
%40 = load i32, i32* %8
%41 = call i32 @write(i32 %39, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 %40)
%42 = load i32, i32* %8
%43 = icmp ne i32 %41, %42
br i1 %43, label %44, label %47
44: ; preds = %37
%45 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %37
%48 = call i32 @sleep(i32 1)
%49 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%50 = load i32, i32* %49
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%52 = call i32 @read(i32 %50, i8* %51, i32 1024)
store i32 %52, i32* %9
%53 = load i32, i32* %9
%54 = icmp sle i32 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %47
br label %60
56: ; preds = %47
%57 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%58 = load i32, i32* %9
%59 = call i32 @write(i32 1, i8* %57, i32 %58)
br label %37
60: ; preds = %55
br label %61
61: ; preds = %60
ret i32 0
}

LLVM IR (-O2):
@.str = [14 x i8] c"pipe() failed\00"
@.str.1 = [14 x i8] c"fork() failed\00"
@.str.2 = [17 x i8] c"I want a cookie\0A\00"
@.str.3 = [15 x i8] c"write() failed\00"
@.str.4 = [12 x i8] c"testing...\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [2 x i32]
%4 = alloca [1024 x i8]
%5 = bitcast [2 x i32]* %3 to i8*
%6 = getelementptr inbounds [1024 x i8], [1024 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 eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = call i32 (...) @fork()
switch i32 %14, label %32 [
i32 -1, label %15
i32 0, label %18
]
15: ; preds = %13
%16 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %13
%19 = call i32 @strlen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%21 = load i32, i32* %20
%22 = call i32 @write(i32 %21, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %19)
%23 = icmp eq i32 %22, %19
br i1 %23, label %27, label %24
24: ; preds = %27, %18
%25 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %18, %27
%28 = call i32 @sleep(i32 5)
%29 = load i32, i32* %20
%30 = call i32 @write(i32 %29, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %19)
%31 = icmp eq i32 %30, %19
br i1 %31, label %27, label %24
32: ; preds = %13
%33 = call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%35 = load i32, i32* %34
%36 = call i32 @write(i32 %35, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 %33)
%37 = icmp eq i32 %36, %33
br i1 %37, label %41, label %38
38: ; preds = %46, %32
%39 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %32, %46
%42 = call i32 @sleep(i32 1)
%43 = load i32, i32* %7
%44 = call i32 @read(i32 %43, i8* %6, i32 1024)
%45 = icmp slt i32 %44, 1
br i1 %45, label %51, label %46
46: ; preds = %41
%47 = call i32 @write(i32 1, i8* %6, i32 %44)
%48 = load i32, i32* %34
%49 = call i32 @write(i32 %48, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 %33)
%50 = icmp eq i32 %49, %33
br i1 %50, label %41, label %38
51: ; preds = %41
ret i32 0
}