#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 BUFSIZ;
int EXIT_SUCCESS;
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(void) {
  int len, read_len, pipefd[2];
  char buf[BUFSIZ];
  if (pipe(pipefd) == -1) {
    perror("cannot get a pipe");
    exit(1);
  };
  switch (fork()) {
  case -1: {
    perror("cannot fork");
    exit(2);
  };
  case 0:
    len = strlen("I want a cookie\n");
    while (1) {
      if (write(pipefd[1], "I want a cookie\n", len) != len) {
        perror("cannot");
        exit(3);
      };
      sleep(5);
    }
  default:
    len = strlen("testing...\n");
    while (1) {
      if (write(pipefd[1], "testing...\n", len) != len) {
        perror("write");
        exit(4);
      };
      sleep(1);
      read_len = read(pipefd[0], buf, BUFSIZ);
      if (read_len <= 0)
        break;
      write(1, buf, read_len);
    }
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
lea    -0x14(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11e3 <main+0x63>
lea    0xe2c(%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,-0x34(%rbp)
sub    $0xffffffff,%eax
je     120d <main+0x8d>
jmp    11fd <main+0x7d>
mov    -0x34(%rbp),%eax
test   %eax,%eax
je     1223 <main+0xa3>
jmp    1277 <main+0xf7>
lea    0xdfe(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
lea    0xdf4(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x8(%rbp)
jmp    1237 <main+0xb7>
mov    -0x10(%rbp),%edi
mov    -0x8(%rbp),%edx
lea    0xdda(%rip),%rsi
call   1030 <write@plt>
cmp    -0x8(%rbp),%eax
je     1268 <main+0xe8>
lea    0xdd6(%rip),%rdi
call   1070 <perror@plt>
mov    $0x3,%edi
call   1080 <exit@plt>
mov    $0x5,%edi
call   1090 <sleep@plt>
jmp    1232 <main+0xb2>
lea    0xdb8(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x8(%rbp)
jmp    128b <main+0x10b>
mov    -0x10(%rbp),%edi
mov    -0x8(%rbp),%edx
lea    0xd9e(%rip),%rsi
call   1030 <write@plt>
cmp    -0x8(%rbp),%eax
je     12bc <main+0x13c>
lea    0xd95(%rip),%rdi
call   1070 <perror@plt>
mov    $0x4,%edi
call   1080 <exit@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    -0x30(%rbp),%rsi
mov    -0x14(%rbp),%edi
mov    0x2d0c(%rip),%rax
mov    (%rax),%edx
call   1060 <read@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jg     12ed <main+0x16d>
jmp    1303 <main+0x183>
mov    -0x30(%rbp),%rsi
mov    -0xc(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
jmp    1286 <main+0x106>
jmp    1308 <main+0x188>
mov    0x2cc9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsp,%r12
mov    0x2e48(%rip),%r13
mov    0x0(%r13),%eax
mov    %rsp,%r14
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
lea    -0x30(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
je     126e <main+0xee>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     1284 <main+0x104>
cmp    $0xffffffff,%eax
je     12e1 <main+0x161>
lea    0xe58(%rip),%r15
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    -0x2c(%rbp),%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    %ebx,%eax
jne    123d <main+0xbd>
lea    0xe36(%rip),%r15
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    -0x30(%rbp),%edi
mov    0x0(%r13),%edx
mov    %r14,%rsi
call   1060 <read@plt>
test   %eax,%eax
jle    1253 <main+0xd3>
mov    $0x1,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x2c(%rbp),%edi
mov    %r15,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
cmp    %ebx,%eax
je     1200 <main+0x80>
lea    0xdfe(%rip),%rdi
call   1070 <perror@plt>
mov    $0x4,%edi
call   1080 <exit@plt>
mov    0x2d7e(%rip),%rax
mov    (%rax),%eax
mov    %r12,%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd8b(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xd93(%rip),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    -0x2c(%rbp),%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    %ebx,%eax
jne    12cb <main+0x14b>
lea    0xd71(%rip),%r14
nopl   (%rax)
mov    $0x5,%edi
call   1090 <sleep@plt>
mov    -0x2c(%rbp),%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
cmp    %ebx,%eax
je     12b0 <main+0x130>
lea    0xd5d(%rip),%rdi
call   1070 <perror@plt>
mov    $0x3,%edi
call   1080 <exit@plt>
lea    0xd2a(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>

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

LLVM IR (-O2):
@BUFSIZ = global i32 0
@.str = [18 x i8] c"cannot get a pipe\00"
@.str.1 = [12 x i8] c"cannot fork\00"
@.str.2 = [17 x i8] c"I want a cookie\0A\00"
@.str.3 = [7 x i8] c"cannot\00"
@.str.4 = [12 x i8] c"testing...\0A\00"
@.str.5 = [6 x i8] c"write\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca [2 x i32]
%2 = bitcast [2 x i32]* %1 to i8*
%3 = load i32, i32* @BUFSIZ
%4 = zext i32 %3 to i64
%5 = tail call i8* @llvm.stacksave()
%6 = alloca i8, i64 %4
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %1, 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 = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%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 ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @exit(i32 2)
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]* %1, 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 ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%26 = call i32 @exit(i32 3)
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]* %1, 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 = %47, %32
%39 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%40 = call i32 @exit(i32 4)
unreachable
41: ; preds = %32, %47
%42 = call i32 @sleep(i32 1)
%43 = load i32, i32* %7
%44 = load i32, i32* @BUFSIZ
%45 = call i32 @read(i32 %43, i8* %6, i32 %44)
%46 = icmp slt i32 %45, 1
br i1 %46, label %52, label %47
47: ; preds = %41
%48 = call i32 @write(i32 1, i8* %6, i32 %45)
%49 = load i32, i32* %34
%50 = call i32 @write(i32 %49, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 %33)
%51 = icmp eq i32 %50, %33
br i1 %51, label %41, label %38
52: ; preds = %41
%53 = load i32, i32* @EXIT_SUCCESS
ret i32 %53
}