#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 */
struct timeval {
  int dummy;
};
typedef int /*<<< orphan*/ buff;

/* Variables and functions */
int errno;
scalar_t__ fork();
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int pipe(int *);
int /*<<< orphan*/ printf(char *, int);
int read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int write(int, char *, int);
int main() {
  int pipe_fds[2];
  int ret;
  char buff[4096], ch = 'a';
  struct timeval t1, t2;
  ret = pipe(pipe_fds);
  if (ret < 0) {
    printf("pipe creation failed with error %d\n", errno);
    return ret;
  }
  if (fork()) {
    while (1) {
      sleep(1);
      ret = read(pipe_fds[0], buff, sizeof(buff));
      if (ret < 0) {
        printf("Read from pipe failed with error %d\n", errno);
        return ret;
      }
      printf("Read %d bytes from pipe\n", ret);
    }
  } else {
    while (1) {
      memset(buff, ch++, sizeof(buff));
      if ((ret = write(pipe_fds[1], buff, sizeof(buff))) < 0) {
        printf("Write to pipe failed with error %d\n", errno);
        return ret;
      }
      printf("Wrote %d bytes to pipe\n", ret);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
movl   $0x0,-0x4(%rbp)
movb   $0x61,-0x1011(%rbp)
lea    -0xc(%rbp),%rdi
call   1060 <pipe@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11bf <main+0x4f>
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
lea    0xe51(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12b2 <main+0x142>
mov    $0x0,%al
call   1090 <fork@plt>
cmp    $0x0,%rax
je     1234 <main+0xc4>
jmp    11d5 <main+0x65>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    -0xc(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1070 <read@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1220 <main+0xb0>
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
lea    0xe14(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12b2 <main+0x142>
mov    -0x10(%rbp),%esi
lea    0xe1f(%rip),%rdi
call   1040 <printf@plt>
jmp    11d5 <main+0x65>
jmp    1239 <main+0xc9>
lea    -0x1010(%rbp),%rdi
mov    -0x1011(%rbp),%al
mov    %al,%cl
add    $0x1,%cl
mov    %cl,-0x1011(%rbp)
movsbl %al,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1030 <write@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    129e <main+0x12e>
mov    0x2d5b(%rip),%rax
mov    (%rax),%esi
lea    0xdd4(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12b2 <main+0x142>
mov    -0x10(%rbp),%esi
lea    0xdde(%rip),%rdi
call   1040 <printf@plt>
jmp    1239 <main+0xc9>
mov    -0x4(%rbp),%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1010,%rsp
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
test   %eax,%eax
js     1210 <main+0xa0>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
je     121b <main+0xab>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0x1000,%edx
call   1070 <read@plt>
mov    %eax,%ebp
lea    0xe5d(%rip),%r14
test   %eax,%eax
js     1296 <main+0x126>
lea    0xe73(%rip),%r15
lea    0x10(%rsp),%rbx
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1070 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11e0 <main+0x70>
jmp    1296 <main+0x126>
mov    %eax,%ebp
lea    0xde7(%rip),%r14
jmp    1296 <main+0x126>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x61,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1030 <write@plt>
mov    %eax,%ebp
lea    0xe16(%rip),%r14
test   %eax,%eax
js     1296 <main+0x126>
mov    $0x62,%bl
lea    0xe2d(%rip),%r15
lea    0x10(%rsp),%r12
xchg   %ax,%ax
mov    %r15,%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
movsbl %bl,%ebx
mov    %r12,%rdi
mov    %ebx,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
mov    0xc(%rsp),%edi
mov    %r12,%rsi
mov    $0x1000,%edx
call   1030 <write@plt>
mov    %eax,%ebp
add    $0x1,%bl
test   %eax,%eax
jns    1260 <main+0xf0>
mov    0x2d43(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1040 <printf@plt>
mov    %ebp,%eax
add    $0x1010,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32 }
@.str = [36 x i8] c"pipe creation failed with error %d\0A\00"
@errno = global i32 0
@.str.1 = [37 x i8] c"Read from pipe failed with error %d\0A\00"
@.str.2 = [25 x i8] c"Read %d bytes from pipe\0A\00"
@.str.3 = [36 x i8] c"Write to pipe failed with error %d\0A\00"
@.str.4 = [24 x i8] c"Wrote %d bytes to pipe\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i32
%4 = alloca [4096 x i8]
%5 = alloca i8
%6 = alloca %struct.timeval
%7 = alloca %struct.timeval
store i32 0, i32* %1
store i8 97, i8* %5
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%9 = call i32 @pipe(i32* %8)
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %0
%13 = load i32, i32* @errno
%14 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = load i32, i32* %3
store i32 %15, i32* %1
br label %54
16: ; preds = %0
%17 = call i64 (...) @fork()
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %35
19: ; preds = %16
br label %20
20: ; preds = %19, %32
%21 = call i32 @sleep(i32 1)
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%23 = load i32, i32* %22
%24 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%25 = call i32 @read(i32 %23, i8* %24, i32 4096)
store i32 %25, i32* %3
%26 = load i32, i32* %3
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %20
%29 = load i32, i32* @errno
%30 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %29)
%31 = load i32, i32* %3
store i32 %31, i32* %1
br label %54
32: ; preds = %20
%33 = load i32, i32* %3
%34 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %33)
br label %20
35: ; preds = %16
br label %36
36: ; preds = %35, %51
%37 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%38 = load i8, i8* %5
%39 = add i8 %38, 1
store i8 %39, i8* %5
%40 = sext i8 %38 to i32
%41 = call i32 @memset(i8* %37, i32 %40, i32 4096)
%42 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%43 = load i32, i32* %42
%44 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%45 = call i32 @write(i32 %43, i8* %44, i32 4096)
store i32 %45, i32* %3
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %36
%48 = load i32, i32* @errno
%49 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i32 %48)
%50 = load i32, i32* %3
store i32 %50, i32* %1
br label %54
51: ; preds = %36
%52 = load i32, i32* %3
%53 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i32 %52)
br label %36
54: ; preds = %47, %28, %12
%55 = load i32, i32* %1
ret i32 %55
}

LLVM IR (-O2):
@.str = [36 x i8] c"pipe creation failed with error %d\0A\00"
@errno = global i32 0
@.str.1 = [37 x i8] c"Read from pipe failed with error %d\0A\00"
@.str.2 = [25 x i8] c"Read %d bytes from pipe\0A\00"
@.str.3 = [36 x i8] c"Write to pipe failed with error %d\0A\00"
@.str.4 = [24 x i8] c"Wrote %d bytes to pipe\0A\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [4096 x i8]
%3 = bitcast [2 x i32]* %1 to i8*
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%6 = call i32 @pipe(i32* %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %39, label %8
8: ; preds = %0
%9 = call i64 (...) @fork()
%10 = icmp eq i64 %9, 0
br i1 %10, label %16, label %11
11: ; preds = %8
%12 = call i32 @sleep(i32 1)
%13 = load i32, i32* %5
%14 = call i32 @read(i32 %13, i8* %4, i32 4096)
%15 = icmp slt i32 %14, 0
br i1 %15, label %39, label %22
16: ; preds = %8
%17 = call i32 @memset(i8* %4, i32 97, i32 4096)
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%19 = load i32, i32* %18
%20 = call i32 @write(i32 %19, i8* %4, i32 4096)
%21 = icmp slt i32 %20, 0
br i1 %21, label %39, label %29
22: ; preds = %11, %22
%23 = phi i32 [ %27, %22 ], [ %14, %11 ]
%24 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %23)
%25 = call i32 @sleep(i32 1)
%26 = load i32, i32* %5
%27 = call i32 @read(i32 %26, i8* %4, i32 4096)
%28 = icmp slt i32 %27, 0
br i1 %28, label %39, label %22
29: ; preds = %16, %29
%30 = phi i32 [ %37, %29 ], [ %20, %16 ]
%31 = phi i8 [ %32, %29 ], [ 97, %16 ]
%32 = add i8 %31, 1
%33 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i32 %30)
%34 = sext i8 %32 to i32
%35 = call i32 @memset(i8* %4, i32 %34, i32 4096)
%36 = load i32, i32* %18
%37 = call i32 @write(i32 %36, i8* %4, i32 4096)
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %29
39: ; preds = %22, %29, %16, %11, %0
%40 = phi i8* [ getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), %0 ], [ getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), %11 ], [ getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), %16 ], [ getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), %29 ], [ getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), %22 ]
%41 = phi i32 [ %6, %0 ], [ %14, %11 ], [ %20, %16 ], [ %37, %29 ], [ %27, %22 ]
%42 = load i32, i32* @errno
%43 = call i32 @printf(i8* %40, i32 %42)
ret i32 %41
}