#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 /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int strlen(char *);
int write(int, char *, int);
int main() {
  int pipefd[2];
  int len;
  char buf[BUFSIZ];
  int read_len;
  if (pipe(pipefd) == -1) {
    {
      perror("can`t get a pipe");
      exit(1);
    };
  }
  switch (fork()) {
  case -1: {
    perror("can`t 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("write");
        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;
      printf("%s\n", buf);
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%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,-0x20(%rbp)
lea    -0xc(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11f3 <main+0x63>
lea    0xe1c(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x34(%rbp)
sub    $0xffffffff,%eax
je     121d <main+0x8d>
jmp    120d <main+0x7d>
mov    -0x34(%rbp),%eax
test   %eax,%eax
je     1233 <main+0xa3>
jmp    1287 <main+0xf7>
lea    0xded(%rip),%rdi
call   1080 <perror@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
lea    0xde2(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x10(%rbp)
jmp    1247 <main+0xb7>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%edx
lea    0xdc8(%rip),%rsi
call   1030 <write@plt>
cmp    -0x10(%rbp),%eax
je     1278 <main+0xe8>
lea    0xdc4(%rip),%rdi
call   1080 <perror@plt>
mov    $0x3,%edi
call   1090 <exit@plt>
mov    $0x5,%edi
call   10a0 <sleep@plt>
jmp    1242 <main+0xb2>
lea    0xda5(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x10(%rbp)
jmp    129b <main+0x10b>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%edx
lea    0xd8b(%rip),%rsi
call   1030 <write@plt>
cmp    -0x10(%rbp),%eax
je     12cc <main+0x13c>
lea    0xd70(%rip),%rdi
call   1080 <perror@plt>
mov    $0x4,%edi
call   1090 <exit@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    -0x30(%rbp),%rsi
mov    -0xc(%rbp),%edi
mov    0x2cfc(%rip),%rax
mov    (%rax),%edx
call   1070 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jg     12fd <main+0x16d>
jmp    1312 <main+0x182>
mov    -0x30(%rbp),%rsi
lea    0xd36(%rip),%rdi
call   1050 <printf@plt>
jmp    1296 <main+0x106>
jmp    1317 <main+0x187>
mov    -0x18(%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
sub    $0x18,%rsp
mov    %rsp,%rbx
mov    0x2e35(%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   1060 <pipe@plt>
cmp    $0xffffffff,%eax
je     1284 <main+0xf4>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     129a <main+0x10a>
mov    %rbx,-0x38(%rbp)
cmp    $0xffffffff,%eax
je     1301 <main+0x171>
lea    0xe3e(%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    1259 <main+0xc9>
lea    0xe27(%rip),%r15
lea    0xe15(%rip),%r12
xchg   %ax,%ax
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    -0x30(%rbp),%edi
mov    0x0(%r13),%edx
mov    %r14,%rsi
call   1070 <read@plt>
test   %eax,%eax
jle    126f <main+0xdf>
mov    %r15,%rdi
mov    %r14,%rsi
call   1050 <printf@plt>
mov    -0x2c(%rbp),%edi
mov    %r12,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
cmp    %ebx,%eax
je     1220 <main+0x90>
lea    0xdcd(%rip),%rdi
call   1080 <perror@plt>
mov    $0x4,%edi
call   1090 <exit@plt>
mov    -0x38(%rbp),%rsp
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd75(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xd7b(%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    12eb <main+0x15b>
lea    0xd59(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    $0x5,%edi
call   10a0 <sleep@plt>
mov    -0x2c(%rbp),%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
cmp    %ebx,%eax
je     12d0 <main+0x140>
lea    0xd3b(%rip),%rdi
call   1080 <perror@plt>
mov    $0x3,%edi
call   1090 <exit@plt>
lea    0xd09(%rip),%rdi
call   1080 <perror@plt>
mov    $0x2,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@BUFSIZ = global i32 0
@.str = [17 x i8] c"can`t get a pipe\00"
@.str.1 = [11 x i8] c"can`t fork\00"
@.str.2 = [17 x i8] c"I want a cookie\0A\00"
@.str.3 = [6 x i8] c"write\00"
@.str.4 = [11 x i8] c"testing..\0A\00"
@.str.5 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
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** %4
%10 = alloca i8, i64 %8
store i64 %8, i64* %5
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %2, 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 ([17 x i8], [17 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 ([11 x i8], [11 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* %3
br label %24
24: ; preds = %35, %22
br label %25
25: ; preds = %24
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%27 = load i32, i32* %26
%28 = load i32, i32* %3
%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* %3
%31 = icmp ne i32 %29, %30
br i1 %31, label %32, label %35
32: ; preds = %25
%33 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 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 ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
store i32 %38, i32* %3
br label %39
39: ; preds = %59, %37
br label %40
40: ; preds = %39
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%42 = load i32, i32* %41
%43 = load i32, i32* %3
%44 = call i32 @write(i32 %42, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %43)
%45 = load i32, i32* %3
%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.3, 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]* %2, 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* %6
%56 = load i32, i32* %6
%57 = icmp sle i32 %56, 0
br i1 %57, label %58, label %59
58: ; preds = %50
br label %61
59: ; preds = %50
%60 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %10)
br label %39
61: ; preds = %58
br label %62
62: ; preds = %61
%63 = load i8*, i8** %4
%64 = load i32, i32* %1
ret i32 %64
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@.str = [17 x i8] c"can`t get a pipe\00"
@.str.1 = [11 x i8] c"can`t fork\00"
@.str.2 = [17 x i8] c"I want a cookie\0A\00"
@.str.3 = [6 x i8] c"write\00"
@.str.4 = [11 x i8] c"testing..\0A\00"
@.str.5 = [4 x i8] c"%s\0A\00"
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 ([17 x i8], [17 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 ([11 x i8], [11 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 ([6 x i8], [6 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 ([11 x i8], [11 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 ([11 x i8], [11 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.3, 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 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %6)
%49 = load i32, i32* %34
%50 = call i32 @write(i32 %49, i8* getelementptr inbounds ([11 x i8], [11 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
ret i32 0
}