#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;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ fork();
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *, scalar_t__, char *);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main(void) {
  pid_t childpid;
  int fd[2];
  char buf[10] = "Bye";
  if (pipe(fd) < 0) {
    return 1;
  }
  childpid = fork();
  if (childpid > 0) {
    write(fd[1], "Hello", sizeof("Hello"));
  } else if (childpid == 0) {
    read(fd[0], buf, sizeof(buf));
  }
  printf("CHILD PID = %d , buf = %s \n", childpid, buf);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe9a(%rip),%rax
mov    %rax,-0x22(%rbp)
mov    0xe97(%rip),%ax
mov    %ax,-0x1a(%rbp)
lea    -0x18(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0x0,%rax
jge    1194 <main+0x44>
movl   $0x1,-0x4(%rbp)
jmp    11ff <main+0xaf>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jle    11c3 <main+0x73>
mov    -0x14(%rbp),%edi
lea    0xe56(%rip),%rsi
mov    $0x6,%edx
call   1030 <write@plt>
jmp    11e4 <main+0x94>
cmpq   $0x0,-0x10(%rbp)
jne    11df <main+0x8f>
mov    -0x18(%rbp),%edi
lea    -0x22(%rbp),%rsi
mov    $0xa,%edx
call   1060 <read@plt>
jmp    11e4 <main+0x94>
mov    -0x10(%rbp),%rsi
lea    -0x22(%rbp),%rdx
lea    0xe1d(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
movw   $0x0,0x10(%rsp)
movq   $0x657942,0x8(%rsp)
lea    0x18(%rsp),%rdi
call   1050 <pipe@plt>
test   %rax,%rax
js     119a <main+0x4a>
xor    %eax,%eax
call   1070 <fork@plt>
mov    %rax,%rbx
test   %rax,%rax
jle    11a1 <main+0x51>
mov    0x1c(%rsp),%edi
lea    0xe7c(%rip),%rsi
mov    $0x6,%edx
call   1030 <write@plt>
jmp    11b6 <main+0x66>
mov    $0x1,%eax
jmp    11cc <main+0x7c>
jne    11b6 <main+0x66>
mov    0x18(%rsp),%edi
lea    0x8(%rsp),%rsi
mov    $0xa,%edx
call   1060 <read@plt>
lea    0xe53(%rip),%rdi
lea    0x8(%rsp),%rdx
mov    %rbx,%rsi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
ret

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

LLVM IR (-O2):
@__const.main.buf = [10 x i8] c"Bye\00\00\00\00\00\00\00"
@.str = [6 x i8] c"Hello\00"
@.str.1 = [28 x i8] c"CHILD PID = %d , buf = %s \0A\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [10 x i8]
%3 = bitcast [2 x i32]* %1 to i8*
%4 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%6 = call i64 @pipe(i32* %5)
%7 = icmp slt i64 %6, 0
br i1 %7, label %22, label %8
8: ; preds = %0
%9 = call i64 (...) @fork()
%10 = icmp sgt i64 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %8
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%13 = load i32, i32* %12
%14 = call i32 @write(i32 %13, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 6)
br label %20
15: ; preds = %8
%16 = icmp eq i64 %9, 0
br i1 %16, label %17, label %20
17: ; preds = %15
%18 = load i32, i32* %5
%19 = call i32 @read(i32 %18, i8* %4, i32 10)
br label %20
20: ; preds = %15, %17, %11
%21 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i64 %9, i8* %4)
br label %22
22: ; preds = %0, %20
%23 = phi i32 [ 0, %20 ], [ 1, %0 ]
ret i32 %23
}