#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 scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int /*<<< orphan*/ printf(char *, int);
int read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int write(int, char *, int);
int main(void) {
  int i = 0;
  int fdArray[2];
  char chr;
  pid_t childPid = -1;
  int retval;
  if (pipe(fdArray) == -1) {
    perror("pipe creation");
    exit(EXIT_FAILURE);
  }
  childPid = fork();
  if (childPid < 0) {
    perror("fork");
    exit(EXIT_FAILURE);
  } else if (childPid == 0) {
    close(fdArray[0]);
    for (i = 0; i < 10; i++) {
      chr = '0' + i;
      if (write(fdArray[1], &chr, 1) == -1) {
        perror("write");
        exit(EXIT_FAILURE);
      }
      sleep(1);
    }
    close(fdArray[1]);
  } else {
    close(fdArray[1]);
    while ((retval = read(fdArray[0], &chr, 1)) != 0) {
      write(STDOUT_FILENO, &chr, 1);
    }
    printf("Parent received EOF: %d\n", retval);
    close(fdArray[0]);
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movq   $0xffffffffffffffff,-0x20(%rbp)
ff
lea    -0x10(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11da <main+0x4a>
lea    0xe39(%rip),%rdi
call   1080 <perror@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    120a <main+0x7a>
lea    0xe17(%rip),%rdi
call   1080 <perror@plt>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
jne    1290 <main+0x100>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0xa,-0x8(%rbp)
jge    1283 <main+0xf3>
mov    -0x8(%rbp),%eax
add    $0x30,%eax
mov    %al,-0x11(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    126b <main+0xdb>
lea    0xdbb(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1224 <main+0x94>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
jmp    12e8 <main+0x158>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
je     12d1 <main+0x141>
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    1298 <main+0x108>
mov    -0x24(%rbp),%esi
lea    0xd3e(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
jmp    12ed <main+0x15d>
mov    0x2cdc(%rip),%rax
mov    (%rax),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
lea    0x10(%rsp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
je     13fa <main+0x26a>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     1403 <main+0x273>
je     122c <main+0x9c>
mov    0x14(%rsp),%edi
call   1050 <close@plt>
mov    0x10(%rsp),%edi
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
test   %eax,%eax
je     1215 <main+0x85>
mov    0x2e0d(%rip),%r14
lea    0xf(%rsp),%rbx
nopl   0x0(%rax,%rax,1)
mov    (%r14),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    0x10(%rsp),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
test   %eax,%eax
jne    11f0 <main+0x60>
lea    0xdfd(%rip),%rdi
xor    %esi,%esi
call   1040 <printf@plt>
mov    0x10(%rsp),%edi
jmp    13db <main+0x24b>
mov    0x10(%rsp),%edi
call   1050 <close@plt>
movb   $0x30,0xf(%rsp)
mov    0x14(%rsp),%edi
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     13f1 <main+0x261>
mov    $0x1,%edi
call   10a0 <sleep@plt>
movb   $0x31,0xf(%rsp)
mov    0x14(%rsp),%edi
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     13f1 <main+0x261>
mov    $0x1,%edi
call   10a0 <sleep@plt>
movb   $0x32,0xf(%rsp)
mov    0x14(%rsp),%edi
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     13f1 <main+0x261>
mov    $0x1,%edi
call   10a0 <sleep@plt>
movb   $0x33,0xf(%rsp)
mov    0x14(%rsp),%edi
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     13f1 <main+0x261>
mov    $0x1,%edi
call   10a0 <sleep@plt>
movb   $0x34,0xf(%rsp)
mov    0x14(%rsp),%edi
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     13f1 <main+0x261>
mov    $0x1,%edi
call   10a0 <sleep@plt>
movb   $0x35,0xf(%rsp)
mov    0x14(%rsp),%edi
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     13f1 <main+0x261>
mov    $0x1,%edi
call   10a0 <sleep@plt>
movb   $0x36,0xf(%rsp)
mov    0x14(%rsp),%edi
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     13f1 <main+0x261>
mov    $0x1,%edi
call   10a0 <sleep@plt>
movb   $0x37,0xf(%rsp)
mov    0x14(%rsp),%edi
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     13f1 <main+0x261>
mov    $0x1,%edi
call   10a0 <sleep@plt>
movb   $0x38,0xf(%rsp)
mov    0x14(%rsp),%edi
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     13f1 <main+0x261>
mov    $0x1,%edi
call   10a0 <sleep@plt>
movb   $0x39,0xf(%rsp)
mov    0x14(%rsp),%edi
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     13f1 <main+0x261>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    0x14(%rsp),%edi
call   1050 <close@plt>
mov    0x2be9(%rip),%rax
mov    (%rax),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xc1b(%rip),%rdi
jmp    140a <main+0x27a>
lea    0xbff(%rip),%rdi
jmp    140a <main+0x27a>
lea    0xc04(%rip),%rdi
call   1080 <perror@plt>
mov    0x2bc2(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"pipe creation\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [6 x i8] c"write\00"
@STDOUT_FILENO = global i32 0
@.str.3 = [25 x i8] c"Parent received EOF: %d\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [2 x i32]
%4 = alloca i8
%5 = alloca i64
%6 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %2
store i64 -1, i64* %5
%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 %14
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %0
%15 = call i64 (...) @fork()
store i64 %15, i64* %5
%16 = load i64, i64* %5
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %14
%19 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%20 = load i32, i32* @EXIT_FAILURE
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %14
%23 = load i64, i64* %5
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %53
25: ; preds = %22
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%27 = load i32, i32* %26
%28 = call i32 @close(i32 %27)
store i32 0, i32* %2
br label %29
29: ; preds = %46, %25
%30 = load i32, i32* %2
%31 = icmp slt i32 %30, 10
br i1 %31, label %32, label %49
32: ; preds = %29
%33 = load i32, i32* %2
%34 = add nsw i32 48, %33
%35 = trunc i32 %34 to i8
store i8 %35, i8* %4
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%37 = load i32, i32* %36
%38 = call i32 @write(i32 %37, i8* %4, i32 1)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %44
40: ; preds = %32
%41 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%42 = load i32, i32* @EXIT_FAILURE
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %32
%45 = call i32 @sleep(i32 1)
br label %46
46: ; preds = %44
%47 = load i32, i32* %2
%48 = add nsw i32 %47, 1
store i32 %48, i32* %2
br label %29
49: ; preds = %29
%50 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%51 = load i32, i32* %50
%52 = call i32 @close(i32 %51)
br label %71
53: ; preds = %22
%54 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%55 = load i32, i32* %54
%56 = call i32 @close(i32 %55)
br label %57
57: ; preds = %62, %53
%58 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%59 = load i32, i32* %58
%60 = call i32 @read(i32 %59, i8* %4, i32 1)
store i32 %60, i32* %6
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %65
62: ; preds = %57
%63 = load i32, i32* @STDOUT_FILENO
%64 = call i32 @write(i32 %63, i8* %4, i32 1)
br label %57
65: ; preds = %57
%66 = load i32, i32* %6
%67 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %66)
%68 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%69 = load i32, i32* %68
%70 = call i32 @close(i32 %69)
br label %71
71: ; preds = %65, %49
br label %72
72: ; preds = %71
%73 = load i32, i32* @EXIT_SUCCESS
ret i32 %73
}

LLVM IR (-O2):
@.str = [14 x i8] c"pipe creation\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [6 x i8] c"write\00"
@STDOUT_FILENO = global i32 0
@.str.3 = [25 x i8] c"Parent received EOF: %d\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca i8
%3 = bitcast [2 x i32]* %1 to i8*
%4 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%5 = call i32 @pipe(i32* %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %11
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = call i32 @exit(i32 %9)
unreachable
11: ; preds = %0
%12 = call i64 (...) @fork()
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %11
%19 = icmp eq i64 %12, 0
br i1 %19, label %20, label %79
20: ; preds = %18
%21 = load i32, i32* %4
%22 = call i32 @close(i32 %21)
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
store i8 48, i8* %2
%24 = load i32, i32* %23
%25 = call i32 @write(i32 %24, i8* %2, i32 1)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %31
27: ; preds = %71, %66, %61, %56, %51, %46, %41, %36, %31, %20
%28 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %20
%32 = call i32 @sleep(i32 1)
store i8 49, i8* %2
%33 = load i32, i32* %23
%34 = call i32 @write(i32 %33, i8* %2, i32 1)
%35 = icmp eq i32 %34, -1
br i1 %35, label %27, label %36
36: ; preds = %31
%37 = call i32 @sleep(i32 1)
store i8 50, i8* %2
%38 = load i32, i32* %23
%39 = call i32 @write(i32 %38, i8* %2, i32 1)
%40 = icmp eq i32 %39, -1
br i1 %40, label %27, label %41
41: ; preds = %36
%42 = call i32 @sleep(i32 1)
store i8 51, i8* %2
%43 = load i32, i32* %23
%44 = call i32 @write(i32 %43, i8* %2, i32 1)
%45 = icmp eq i32 %44, -1
br i1 %45, label %27, label %46
46: ; preds = %41
%47 = call i32 @sleep(i32 1)
store i8 52, i8* %2
%48 = load i32, i32* %23
%49 = call i32 @write(i32 %48, i8* %2, i32 1)
%50 = icmp eq i32 %49, -1
br i1 %50, label %27, label %51
51: ; preds = %46
%52 = call i32 @sleep(i32 1)
store i8 53, i8* %2
%53 = load i32, i32* %23
%54 = call i32 @write(i32 %53, i8* %2, i32 1)
%55 = icmp eq i32 %54, -1
br i1 %55, label %27, label %56
56: ; preds = %51
%57 = call i32 @sleep(i32 1)
store i8 54, i8* %2
%58 = load i32, i32* %23
%59 = call i32 @write(i32 %58, i8* %2, i32 1)
%60 = icmp eq i32 %59, -1
br i1 %60, label %27, label %61
61: ; preds = %56
%62 = call i32 @sleep(i32 1)
store i8 55, i8* %2
%63 = load i32, i32* %23
%64 = call i32 @write(i32 %63, i8* %2, i32 1)
%65 = icmp eq i32 %64, -1
br i1 %65, label %27, label %66
66: ; preds = %61
%67 = call i32 @sleep(i32 1)
store i8 56, i8* %2
%68 = load i32, i32* %23
%69 = call i32 @write(i32 %68, i8* %2, i32 1)
%70 = icmp eq i32 %69, -1
br i1 %70, label %27, label %71
71: ; preds = %66
%72 = call i32 @sleep(i32 1)
store i8 57, i8* %2
%73 = load i32, i32* %23
%74 = call i32 @write(i32 %73, i8* %2, i32 1)
%75 = icmp eq i32 %74, -1
br i1 %75, label %27, label %76
76: ; preds = %71
%77 = call i32 @sleep(i32 1)
%78 = load i32, i32* %23
br label %95
79: ; preds = %18
%80 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%81 = load i32, i32* %80
%82 = call i32 @close(i32 %81)
%83 = load i32, i32* %4
%84 = call i32 @read(i32 %83, i8* %2, i32 1)
%85 = icmp eq i32 %84, 0
br i1 %85, label %92, label %86
86: ; preds = %79, %86
%87 = load i32, i32* @STDOUT_FILENO
%88 = call i32 @write(i32 %87, i8* %2, i32 1)
%89 = load i32, i32* %4
%90 = call i32 @read(i32 %89, i8* %2, i32 1)
%91 = icmp eq i32 %90, 0
br i1 %91, label %92, label %86
92: ; preds = %86, %79
%93 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 0)
%94 = load i32, i32* %4
br label %95
95: ; preds = %76, %92
%96 = phi i32 [ %78, %76 ], [ %94, %92 ]
%97 = call i32 @close(i32 %96)
%98 = load i32, i32* @EXIT_SUCCESS
ret i32 %98
}