#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 */
int /*<<< orphan*/ close(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
scalar_t__ pipe(int *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main() {
  int fildes[2];
  pid_t pid;
  int i, j;
  char buf[256];
  if (pipe(fildes) < 0) {
    fprintf(stderr, "pipe error!\n");
    return 0;
  }
  if ((pid = fork()) < 0) {
    fprintf(stderr, "fork error!\n");
    return 0;
  }
  if (pid == 0) {
    close(fildes[1]);
    memset(buf, 0, sizeof(buf));
    j = read(fildes[0], buf, sizeof(buf));
    fprintf(stderr, "[child] buf=[%s] len [%d]\n", buf, j);
    return 0;
  }
  close(fildes[0]);
  write(fildes[1], "hello!", strlen("hello!"));
  write(fildes[1], "world!", strlen("world!"));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0x0,%rax
jge    11c8 <main+0x48>
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
lea    0xe4b(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12cb <main+0x14b>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    1200 <main+0x80>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
lea    0xe20(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12cb <main+0x14b>
cmpq   $0x0,-0x18(%rbp)
jne    126a <main+0xea>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
lea    -0x120(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1050 <memset@plt>
mov    -0xc(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0x100,%edx
call   1080 <read@plt>
mov    %eax,-0x20(%rbp)
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
lea    -0x120(%rbp),%rdx
mov    -0x20(%rbp),%ecx
lea    0xdc3(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12cb <main+0x14b>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x128(%rbp)
lea    0xdb3(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x128(%rbp),%edi
mov    %eax,%edx
lea    0xd9f(%rip),%rsi
call   1030 <write@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x124(%rbp)
lea    0xd91(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x124(%rbp),%edi
mov    %eax,%edx
lea    0xd7d(%rip),%rsi
call   1030 <write@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x118,%rsp
lea    0x8(%rsp),%rdi
call   1070 <pipe@plt>
test   %rax,%rax
js     11f0 <main+0x70>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     1202 <main+0x82>
je     121b <main+0x9b>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
mov    0xc(%rsp),%ebx
lea    0xe7a(%rip),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    0xc(%rsp),%ebx
lea    0xe62(%rip),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
jmp    1265 <main+0xe5>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
lea    0xe00(%rip),%rsi
jmp    1212 <main+0x92>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
lea    0xdfb(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
jmp    1265 <main+0xe5>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1050 <memset@plt>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x100,%edx
call   1080 <read@plt>
mov    0x2d90(%rip),%rcx
mov    (%rcx),%edi
lea    0xdc1(%rip),%rsi
mov    %rbx,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1090 <fprintf@plt>
xor    %eax,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [13 x i8] c"pipe error!\0A\00"
@.str.1 = [13 x i8] c"fork error!\0A\00"
@.str.2 = [27 x i8] c"[child] buf=[%s] len [%d]\0A\00"
@.str.3 = [7 x i8] c"hello!\00"
@.str.4 = [7 x i8] c"world!\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
%6 = alloca [256 x i8]
store i32 0, i32* %1
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%8 = call i64 @pipe(i32* %7)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = load i32, i32* @stderr
%12 = call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %1
br label %48
13: ; preds = %0
%14 = call i64 (...) @fork()
store i64 %14, i64* %3
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = load i32, i32* @stderr
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %1
br label %48
19: ; preds = %13
%20 = load i64, i64* %3
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %36
22: ; preds = %19
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%24 = load i32, i32* %23
%25 = call i32 @close(i32 %24)
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%27 = call i32 @memset(i8* %26, i32 0, i32 256)
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%29 = load i32, i32* %28
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%31 = call i32 @read(i32 %29, i8* %30, i32 256)
store i32 %31, i32* %5
%32 = load i32, i32* @stderr
%33 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%34 = load i32, i32* %5
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %33, i32 %34)
store i32 0, i32* %1
br label %48
36: ; preds = %19
%37 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%38 = load i32, i32* %37
%39 = call i32 @close(i32 %38)
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%41 = load i32, i32* %40
%42 = call i32 @strlen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%43 = call i32 @write(i32 %41, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %42)
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%45 = load i32, i32* %44
%46 = call i32 @strlen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%47 = call i32 @write(i32 %45, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i32 %46)
store i32 0, i32* %1
br label %48
48: ; preds = %36, %22, %16, %10
%49 = load i32, i32* %1
ret i32 %49
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [13 x i8] c"pipe error!\0A\00"
@.str.1 = [13 x i8] c"fork error!\0A\00"
@.str.2 = [27 x i8] c"[child] buf=[%s] len [%d]\0A\00"
@.str.3 = [7 x i8] c"hello!\00"
@.str.4 = [7 x i8] c"world!\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [256 x i8]
%3 = bitcast [2 x i32]* %1 to i8*
%4 = getelementptr inbounds [256 x i8], [256 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 %8, label %11
8: ; preds = %0
%9 = load i32, i32* @stderr
%10 = call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %38
11: ; preds = %0
%12 = call i64 (...) @fork()
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* @stderr
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %38
17: ; preds = %11
%18 = icmp eq i64 %12, 0
br i1 %18, label %19, label %28
19: ; preds = %17
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%21 = load i32, i32* %20
%22 = call i32 @close(i32 %21)
%23 = call i32 @memset(i8* %4, i32 0, i32 256)
%24 = load i32, i32* %5
%25 = call i32 @read(i32 %24, i8* %4, i32 256)
%26 = load i32, i32* @stderr
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %4, i32 %25)
br label %38
28: ; preds = %17
%29 = load i32, i32* %5
%30 = call i32 @close(i32 %29)
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%32 = load i32, i32* %31
%33 = call i32 @strlen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%34 = call i32 @write(i32 %32, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %33)
%35 = load i32, i32* %31
%36 = call i32 @strlen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%37 = call i32 @write(i32 %35, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i32 %36)
br label %38
38: ; preds = %28, %19, %14, %8
ret i32 0
}