#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];
  int len;
  char buf[256];
  int read_len;
  if (pipe(pipefd) == -1) {
    {
      perror("cann't get a pip");
      exit(1);
    };
  }
  switch (fork()) {
  case -1: {
    perror("cann't fork");
    exit(2);
  }; break;
  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, 256);
      if (read_len < 0) {
        break;
      }
      write(1, buf, read_len);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%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,-0x128(%rbp)
sub    $0xffffffff,%eax
je     11f1 <main+0x71>
jmp    11de <main+0x5e>
mov    -0x128(%rbp),%eax
test   %eax,%eax
je     1207 <main+0x87>
jmp    1256 <main+0xd6>
lea    0xe19(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
lea    0xe0f(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x1c(%rbp),%edx
lea    0xdfa(%rip),%rsi
call   1030 <write@plt>
cmp    -0x1c(%rbp),%eax
je     1247 <main+0xc7>
lea    0xdf6(%rip),%rdi
call   1070 <perror@plt>
mov    $0x3,%edi
call   1080 <exit@plt>
mov    $0x5,%edi
call   1090 <sleep@plt>
jmp    1216 <main+0x96>
lea    0xdd7(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x1c(%rbp),%edx
lea    0xdc2(%rip),%rsi
call   1030 <write@plt>
cmp    -0x1c(%rbp),%eax
je     1296 <main+0x116>
lea    0xda7(%rip),%rdi
call   1070 <perror@plt>
mov    $0x4,%edi
call   1080 <exit@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    -0x18(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0x100,%edx
call   1060 <read@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0x0,-0x124(%rbp)
jge    12cc <main+0x14c>
jmp    12e8 <main+0x168>
lea    -0x120(%rbp),%rsi
mov    -0x124(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
jmp    1265 <main+0xe5>
jmp    12ed <main+0x16d>
xor    %eax,%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x110,%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     126b <main+0xeb>
cmp    $0xffffffff,%eax
je     12d2 <main+0x152>
lea    0xe76(%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    0xe4e(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    0x8(%rsp),%edi
mov    %r14,%rsi
mov    $0x100,%edx
call   1060 <read@plt>
test   %eax,%eax
js     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    0xdf7(%rip),%rdi
call   1070 <perror@plt>
mov    $0x4,%edi
call   1080 <exit@plt>
xor    %eax,%eax
add    $0x110,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xda4(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xdab(%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    12bc <main+0x13c>
lea    0xd88(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nop
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     12a0 <main+0x120>
lea    0xd6b(%rip),%rdi
call   1070 <perror@plt>
mov    $0x3,%edi
call   1080 <exit@plt>
lea    0xd38(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"cann't get a pip\00"
@.str.1 = [12 x i8] c"cann'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 = [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 i32
%8 = alloca [256 x i8]
%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 ([17 x i8], [17 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 ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 2)
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* %7
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* %7
%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* %7
%29 = icmp ne i32 %27, %28
br i1 %29, label %30, label %33
30: ; preds = %23
%31 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%32 = call i32 @exit(i32 3)
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* %7
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* %7
%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* %7
%43 = icmp ne i32 %41, %42
br i1 %43, label %44, label %47
44: ; preds = %37
%45 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%46 = call i32 @exit(i32 4)
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 [256 x i8], [256 x i8]* %8, i64 0, i64 0
%52 = call i32 @read(i32 %50, i8* %51, i32 256)
store i32 %52, i32* %9
%53 = load i32, i32* %9
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %47
br label %60
56: ; preds = %47
%57 = getelementptr inbounds [256 x i8], [256 x i8]* %8, 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 = [17 x i8] c"cann't get a pip\00"
@.str.1 = [12 x i8] c"cann'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 = [12 x i8] c"testing.. \0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [2 x i32]
%4 = alloca [256 x i8]
%5 = bitcast [2 x i32]* %3 to i8*
%6 = getelementptr inbounds [256 x i8], [256 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 ([17 x i8], [17 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 ([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]* %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 ([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 ([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 ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%40 = call i32 @exit(i32 4)
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 256)
%45 = icmp slt i32 %44, 0
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
}