#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*/ close(int);
scalar_t__ fork();
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ sscanf(char *, char *, int *);
int /*<<< orphan*/ write(int, char *, int);
main() {
  int fd[2];
  int n = 0;
  int i;
  char line[80];
  pipe(fd);
  if (fork() == 0) {
    close(fd[0]);
    for (i = 0; i < 10; i++) {
      sprintf(line, "%d", n);
      write(fd[1], line, 80);
      printf("Child writes: %d\n", n);
      n++;
      sleep(2);
    }
  } else {
    close(fd[1]);
    for (i = 0; i < 10; i++) {
      printf("\t\t\t Parent trying to read pipe\n");
      read(fd[0], line, 80);
      sscanf(line, "%d", &n);
      printf("\t\t\t Parent reads: %d\n", n);
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x10(%rbp)
lea    -0xc(%rbp),%rdi
call   1060 <pipe@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
cmp    $0x0,%rax
jne    1234 <main+0xa4>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0xa,-0x14(%rbp)
jge    122f <main+0x9f>
lea    -0x70(%rbp),%rdi
mov    -0x10(%rbp),%edx
lea    0xe19(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x8(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0x50,%edx
call   1030 <write@plt>
mov    -0x10(%rbp),%esi
lea    0xdfc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    $0x2,%edi
call   10a0 <sleep@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11cf <main+0x3f>
jmp    12a4 <main+0x114>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0xa,-0x14(%rbp)
jge    129f <main+0x10f>
lea    0xdc1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0x50,%edx
call   1070 <read@plt>
lea    -0x70(%rbp),%rdi
lea    0xd89(%rip),%rsi
lea    -0x10(%rbp),%rdx
call   1080 <sscanf@plt>
mov    -0x10(%rbp),%esi
lea    0xdab(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1243 <main+0xb3>
jmp    12a4 <main+0x114>
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
movl   $0x0,0x4(%rsp)
lea    0x8(%rsp),%rdi
call   1070 <pipe@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
je     123c <main+0x9c>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
mov    $0xa,%ebp
lea    0xe4a(%rip),%r14
lea    0x10(%rsp),%rbx
lea    0xe13(%rip),%r15
lea    0x4(%rsp),%r12
lea    0xe1c(%rip),%r13
nopl   0x0(%rax)
mov    %r14,%rdi
call   1030 <puts@plt>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x50,%edx
call   1080 <read@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1090 <sscanf@plt>
mov    0x4(%rsp),%esi
mov    %r13,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0xffffffff,%ebp
jne    1200 <main+0x60>
jmp    129c <main+0xfc>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
lea    0xdb4(%rip),%r14
lea    0x10(%rsp),%rbx
lea    0xdab(%rip),%r15
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   10a0 <sprintf@plt>
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    $0x50,%edx
call   1040 <write@plt>
mov    %r15,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%ebp
mov    $0x2,%edi
call   10b0 <sleep@plt>
cmp    $0xa,%ebp
jne    1260 <main+0xc0>
xor    %eax,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [18 x i8] c"Child writes: %d\0A\00"
@.str.2 = [32 x i8] c"\09\09\09 Parent trying to read pipe\0A\00"
@.str.3 = [22 x i8] c"\09\09\09 Parent reads: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i32
%4 = alloca i32
%5 = alloca [80 x i8]
store i32 0, i32* %1
store i32 0, i32* %3
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%7 = call i32 @pipe(i32* %6)
%8 = call i64 (...) @fork()
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %34
10: ; preds = %0
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%12 = load i32, i32* %11
%13 = call i32 @close(i32 %12)
store i32 0, i32* %4
br label %14
14: ; preds = %30, %10
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 10
br i1 %16, label %17, label %33
17: ; preds = %14
%18 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%19 = load i32, i32* %3
%20 = call i32 @sprintf(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%22 = load i32, i32* %21
%23 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%24 = call i32 @write(i32 %22, i8* %23, i32 80)
%25 = load i32, i32* %3
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %25)
%27 = load i32, i32* %3
%28 = add nsw i32 %27, 1
store i32 %28, i32* %3
%29 = call i32 @sleep(i32 2)
br label %30
30: ; preds = %17
%31 = load i32, i32* %4
%32 = add nsw i32 %31, 1
store i32 %32, i32* %4
br label %14
33: ; preds = %14
br label %55
34: ; preds = %0
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%36 = load i32, i32* %35
%37 = call i32 @close(i32 %36)
store i32 0, i32* %4
br label %38
38: ; preds = %51, %34
%39 = load i32, i32* %4
%40 = icmp slt i32 %39, 10
br i1 %40, label %41, label %54
41: ; preds = %38
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%44 = load i32, i32* %43
%45 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%46 = call i32 @read(i32 %44, i8* %45, i32 80)
%47 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%48 = call i32 @sscanf(i8* %47, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %3)
%49 = load i32, i32* %3
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %49)
br label %51
51: ; preds = %41
%52 = load i32, i32* %4
%53 = add nsw i32 %52, 1
store i32 %53, i32* %4
br label %38
54: ; preds = %38
br label %55
55: ; preds = %54, %33
%56 = load i32, i32* %1
ret i32 %56
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [18 x i8] c"Child writes: %d\0A\00"
@.str.3 = [22 x i8] c"\09\09\09 Parent reads: %d\0A\00"
@str = [31 x i8] c"\09\09\09 Parent trying to read pipe\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca i32
%3 = alloca [80 x i8]
%4 = bitcast [2 x i32]* %1 to i8*
%5 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%6 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%8 = call i32 @pipe(i32* %7)
%9 = call i64 (...) @fork()
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %26
11: ; preds = %0
%12 = load i32, i32* %7
%13 = call i32 @close(i32 %12)
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
br label %15
15: ; preds = %11, %15
%16 = phi i32 [ 0, %11 ], [ %22, %15 ]
%17 = phi i32 [ 0, %11 ], [ %24, %15 ]
%18 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %16)
%19 = load i32, i32* %14
%20 = call i32 @write(i32 %19, i8* %6, i32 80)
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%22 = add nuw nsw i32 %16, 1
%23 = call i32 @sleep(i32 2)
%24 = add nuw nsw i32 %17, 1
%25 = icmp eq i32 %24, 10
br i1 %25, label %40, label %15
26: ; preds = %0
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%28 = load i32, i32* %27
%29 = call i32 @close(i32 %28)
br label %30
30: ; preds = %26, %30
%31 = phi i32 [ 0, %26 ], [ %38, %30 ]
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
%33 = load i32, i32* %7
%34 = call i32 @read(i32 %33, i8* %6, i32 80)
%35 = call i32 @sscanf(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %2)
%36 = load i32, i32* %2
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %36)
%38 = add nuw nsw i32 %31, 1
%39 = icmp eq i32 %38, 10
br i1 %39, label %40, label %30
40: ; preds = %30, %15
ret i32 0
}