#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 *, char *);
int read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int fd[2];
  pipe(fd);
  if (fork()) {
    close(fd[0]);
    while (1) {
      write(fd[1], "hello", 5);
      sleep(1);
    }
  } else {
    char buf[20];
    int r;
    close(fd[1]);
    while (1) {
      r = read(fd[0], buf, 20);
      buf[r] = 0;
      printf("%s\n", buf);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
call   1060 <pipe@plt>
mov    $0x0,%al
call   1090 <fork@plt>
cmp    $0x0,%rax
je     11c4 <main+0x54>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%edi
lea    0xe55(%rip),%rsi
mov    $0x5,%edx
call   1030 <write@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
jmp    11a1 <main+0x31>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x14,%edx
call   1070 <read@plt>
mov    %eax,-0x24(%rbp)
movslq -0x24(%rbp),%rax
movb   $0x0,-0x20(%rbp,%rax,1)
lea    -0x20(%rbp),%rsi
lea    0xe12(%rip),%rdi
call   1040 <printf@plt>
jmp    11cc <main+0x5c>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x28,%rsp
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
je     11bd <main+0x4d>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
lea    0xe63(%rip),%rbx
nopl   (%rax)
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    $0x5,%edx
call   1030 <write@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
jmp    11a0 <main+0x30>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
lea    0x10(%rsp),%rbx
lea    0xe34(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x14,%edx
call   1070 <read@plt>
cltq
movb   $0x0,0x10(%rsp,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
jmp    11e0 <main+0x70>

LLVM IR (-O0):
@.str = [6 x i8] c"hello\00"
@.str.1 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca [20 x i8]
%4 = alloca i32
store i32 0, i32* %1
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%6 = call i32 @pipe(i32* %5)
%7 = call i64 (...) @fork()
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %18
9: ; preds = %0
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%11 = load i32, i32* %10
%12 = call i32 @close(i32 %11)
br label %13
13: ; preds = %9, %13
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%15 = load i32, i32* %14
%16 = call i32 @write(i32 %15, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 5)
%17 = call i32 @sleep(i32 1)
br label %13
18: ; preds = %0
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%20 = load i32, i32* %19
%21 = call i32 @close(i32 %20)
br label %22
22: ; preds = %18, %22
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%24 = load i32, i32* %23
%25 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%26 = call i32 @read(i32 %24, i8* %25, i32 20)
store i32 %26, i32* %4
%27 = load i32, i32* %4
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 %28
store i8 0, i8* %29
%30 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%31 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %30)
br label %22
}

LLVM IR (-O2):
@.str = [6 x i8] c"hello\00"
@.str.1 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [20 x 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 = call i64 (...) @fork()
%7 = icmp eq i64 %6, 0
br i1 %7, label %16, label %8
8: ; preds = %0
%9 = load i32, i32* %4
%10 = call i32 @close(i32 %9)
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
br label %12
12: ; preds = %12, %8
%13 = load i32, i32* %11
%14 = call i32 @write(i32 %13, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 5)
%15 = call i32 @sleep(i32 1)
br label %12
16: ; preds = %0
%17 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%19 = load i32, i32* %18
%20 = call i32 @close(i32 %19)
br label %21
21: ; preds = %21, %16
%22 = load i32, i32* %4
%23 = call i32 @read(i32 %22, i8* %17, i32 20)
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 %24
store i8 0, i8* %25
%26 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %17)
br label %21
}