#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 int /*<<< orphan*/ read_buf;
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int fd[2] = {};
  int ret = 0;
  ret = pipe(fd);
  if (ret < 0) {
    perror("pipe");
  }
  pid_t pid;
  pid = fork();
  if (pid < 0) {
    perror("fork");
  }
  if (pid == 0) {
    char buf[100] = "hello world\n";
    write(fd[1], buf, strlen(buf));
    exit(0);
  } else {
    char read_buf[100] = "";
    wait(NULL);
    read(fd[0], read_buf, sizeof(read_buf));
    printf("read_buf is %s\n", read_buf);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x110,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
movl   $0x0,-0x1c(%rbp)
lea    -0x18(%rbp),%rdi
call   1070 <pipe@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1202 <main+0x52>
lea    0xe03(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    1224 <main+0x74>
lea    0xde6(%rip),%rdi
call   10a0 <perror@plt>
cmpq   $0x0,-0x28(%rbp)
jne    1285 <main+0xd5>
lea    -0x90(%rbp),%rdi
lea    0xde3(%rip),%rsi
mov    $0x64,%edx
call   1090 <memcpy@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x10c(%rbp)
lea    -0x90(%rbp),%rax
mov    %rax,-0x108(%rbp)
lea    -0x90(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x10c(%rbp),%edi
mov    -0x108(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
lea    -0x100(%rbp),%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1060 <memset@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10c0 <wait@plt>
mov    -0x18(%rbp),%edi
lea    -0x100(%rbp),%rsi
mov    $0x64,%edx
call   1080 <read@plt>
lea    -0x100(%rbp),%rsi
lea    0xd47(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x110,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x78,%rsp
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
test   %eax,%eax
js     121b <main+0x8b>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     1229 <main+0x99>
je     1237 <main+0xa7>
xorps  %xmm0,%xmm0
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movl   $0x0,0x70(%rsp)
xor    %edi,%edi
call   10a0 <wait@plt>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rbx
mov    %rbx,%rsi
mov    $0x64,%edx
call   1070 <read@plt>
lea    0xe01(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x78,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdde(%rip),%rdi
call   1080 <perror@plt>
jmp    11ae <main+0x1e>
lea    0xdd5(%rip),%rdi
call   1080 <perror@plt>
jmp    11bc <main+0x2c>
movaps 0xe32(%rip),%xmm0
movaps %xmm0,0x60(%rsp)
movaps 0xe16(%rip),%xmm0
movaps %xmm0,0x50(%rsp)
movaps 0xdfa(%rip),%xmm0
movaps %xmm0,0x40(%rsp)
movaps 0xdde(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movaps 0xdc2(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xda6(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movl   $0x0,0x70(%rsp)
mov    0xc(%rsp),%ebx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"pipe\00"
@.str.1 = [5 x i8] c"fork\00"
@__const.main.buf = [100 x i8] c"hello world\0A\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str.2 = [16 x i8] c"read_buf is %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [2 x i32]
%7 = alloca i32
%8 = alloca i64
%9 = alloca [100 x i8]
%10 = alloca [100 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = bitcast [2 x i32]* %6 to i8*
store i32 0, i32* %7
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%13 = call i32 @pipe(i32* %12)
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %2
%19 = call i64 (...) @fork()
store i64 %19, i64* %8
%20 = load i64, i64* %8
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %22, %18
%25 = load i64, i64* %8
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %36
27: ; preds = %24
%28 = bitcast [100 x i8]* %9 to i8*
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%30 = load i32, i32* %29
%31 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%32 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%33 = call i32 @strlen(i8* %32)
%34 = call i32 @write(i32 %30, i8* %31, i32 %33)
%35 = call i32 @exit(i32 0)
unreachable
36: ; preds = %24
%37 = bitcast [100 x i8]* %10 to i8*
%38 = call i32 @wait(i32* null)
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%40 = load i32, i32* %39
%41 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%42 = call i32 @read(i32 %40, i8* %41, i32 100)
%43 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%44 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %43)
br label %45
45: ; preds = %36
ret i32 0
}

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