#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 pid_t;

/* Variables and functions */
int STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int errno;
int fork();
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int /*<<< orphan*/ printf(char *);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int strlen(char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ write(int, char *, int);
int test() {
  int pipefd[2] = {0};
  int i = pipe(pipefd);
  char buf;
  if (i == -1) {
    perror("pipe");
    return errno;
  }
  pid_t pid = fork();
  if (pid == -1) {
    perror("fork");
    return errno;
  }
  if (pid == 0) {
    printf("===============child================\n");
    close(pipefd[1]);
    while (read(pipefd[0], &buf, 1) > 0)
      write(STDOUT_FILENO, &buf, 1);
    write(STDOUT_FILENO, "\n", 1);
    close(pipefd[0]);
  } else {
    printf("-----------------parent---------------------");
    close(pipefd[0]);
    char *str = "hello from parent";
    write(pipefd[1], str, strlen(str));
    close(pipefd[1]);
    wait(NULL);
  }
  sleep(10);
  return 0;
}


x86 assembly (clang, -O0):
<test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    -0xc(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
lea    -0xc(%rbp),%rdi
call   1080 <pipe@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11fb <test+0x4b>
lea    0xe1b(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2de7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    130e <test+0x15e>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    122c <test+0x7c>
lea    0xdef(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2db6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    130e <test+0x15e>
cmpl   $0x0,-0x18(%rbp)
jne    12a8 <test+0xf8>
lea    0xdcd(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
mov    -0xc(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1090 <read@plt>
cmp    $0x0,%rax
jle    1281 <test+0xd1>
mov    0x2d84(%rip),%rax
mov    (%rax),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    124a <test+0x9a>
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
lea    0xd9d(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
jmp    12fd <test+0x14d>
lea    0xd81(%rip),%rdi
call   1050 <printf@plt>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
lea    0xd9a(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10c0 <wait@plt>
mov    $0xa,%edi
call   10b0 <sleep@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test>:
push   %r14
push   %rbx
sub    $0x18,%rsp
movq   $0x0,0x10(%rsp)
lea    0x10(%rsp),%rdi
call   1080 <pipe@plt>
cmp    $0xffffffff,%eax
je     12cd <test+0x11d>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %eax,%eax
je     1230 <test+0x80>
cmp    $0xffffffff,%eax
je     12d6 <test+0x126>
lea    0xe1e(%rip),%rdi
call   1060 <printf@plt>
mov    0x10(%rsp),%edi
call   1070 <close@plt>
mov    0x14(%rsp),%ebx
lea    0xe32(%rip),%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    0x14(%rsp),%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10c0 <wait@plt>
jmp    12b9 <test+0x109>
lea    0xe14(%rip),%rdi
call   1030 <puts@plt>
mov    0x14(%rsp),%edi
call   1070 <close@plt>
mov    0x10(%rsp),%edi
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1090 <read@plt>
test   %rax,%rax
jle    1296 <test+0xe6>
mov    0x2d8c(%rip),%r14
lea    0xf(%rsp),%rbx
nopl   0x0(%rax)
mov    (%r14),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    0x10(%rsp),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1090 <read@plt>
test   %rax,%rax
jg     1270 <test+0xc0>
mov    0x2d53(%rip),%rax
mov    (%rax),%edi
lea    0xd64(%rip),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    0x10(%rsp),%edi
call   1070 <close@plt>
mov    $0xa,%edi
call   10b0 <sleep@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd2c(%rip),%rdi
jmp    12dd <test+0x12d>
lea    0xd28(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2cef(%rip),%rax
mov    (%rax),%eax
jmp    12c5 <test+0x115>

LLVM IR (-O0):
@.str = [5 x i8] c"pipe\00"
@errno = global i32 0
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [38 x i8] c"===============child================\0A\00"
@STDOUT_FILENO = global i32 0
@.str.3 = [2 x i8] c"\0A\00"
@.str.4 = [45 x i8] c"-----------------parent---------------------\00"
@.str.5 = [18 x i8] c"hello from parent\00"
define i32 @test() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i32
%4 = alloca i8
%5 = alloca i32
%6 = alloca i8*
%7 = bitcast [2 x i32]* %2 to i8*
%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 eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @errno
store i32 %14, i32* %1
br label %61
15: ; preds = %0
%16 = call i32 (...) @fork()
store i32 %16, i32* %5
%17 = load i32, i32* %5
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%21 = load i32, i32* @errno
store i32 %21, i32* %1
br label %61
22: ; preds = %15
%23 = load i32, i32* %5
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %44
25: ; preds = %22
%26 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%28 = load i32, i32* %27
%29 = call i32 @close(i32 %28)
br label %30
30: ; preds = %35, %25
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%32 = load i32, i32* %31
%33 = call i64 @read(i32 %32, i8* %4, i32 1)
%34 = icmp sgt i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %30
%36 = load i32, i32* @STDOUT_FILENO
%37 = call i32 @write(i32 %36, i8* %4, i32 1)
br label %30
38: ; preds = %30
%39 = load i32, i32* @STDOUT_FILENO
%40 = call i32 @write(i32 %39, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%42 = load i32, i32* %41
%43 = call i32 @close(i32 %42)
br label %59
44: ; preds = %22
%45 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.4, i64 0, i64 0))
%46 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%47 = load i32, i32* %46
%48 = call i32 @close(i32 %47)
store i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i8** %6
%49 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%50 = load i32, i32* %49
%51 = load i8*, i8** %6
%52 = load i8*, i8** %6
%53 = call i32 @strlen(i8* %52)
%54 = call i32 @write(i32 %50, i8* %51, i32 %53)
%55 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%56 = load i32, i32* %55
%57 = call i32 @close(i32 %56)
%58 = call i32 @wait(i32* null)
br label %59
59: ; preds = %44, %38
%60 = call i32 @sleep(i32 10)
store i32 0, i32* %1
br label %61
61: ; preds = %59, %19, %12
%62 = load i32, i32* %1
ret i32 %62
}

LLVM IR (-O2):
@.str = [5 x i8] c"pipe\00"
@errno = global i32 0
@.str.1 = [5 x i8] c"fork\00"
@STDOUT_FILENO = global i32 0
@.str.3 = [2 x i8] c"\0A\00"
@.str.4 = [45 x i8] c"-----------------parent---------------------\00"
@.str.5 = [18 x i8] c"hello from parent\00"
@str = [37 x i8] c"===============child================\00"
define i32 @test() {
%1 = alloca i64
%2 = bitcast i64* %1 to [2 x i32]*
%3 = alloca i8
%4 = bitcast i64* %1 to i8*
store i64 0, i64* %1
%5 = bitcast i64* %1 to i32*
%6 = call i32 @pipe(i32* %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @errno
br label %48
11: ; preds = %0
%12 = call i32 (...) @fork()
switch i32 %12, label %35 [
i32 -1, label %13
i32 0, label %16
]
13: ; preds = %11
%14 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%15 = load i32, i32* @errno
br label %48
16: ; preds = %11
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str, i64 0, i64 0))
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%19 = load i32, i32* %18
%20 = call i32 @close(i32 %19)
%21 = load i32, i32* %5
%22 = call i64 @read(i32 %21, i8* %3, i32 1)
%23 = icmp sgt i64 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %16, %24
%25 = load i32, i32* @STDOUT_FILENO
%26 = call i32 @write(i32 %25, i8* %3, i32 1)
%27 = load i32, i32* %5
%28 = call i64 @read(i32 %27, i8* %3, i32 1)
%29 = icmp sgt i64 %28, 0
br i1 %29, label %24, label %30
30: ; preds = %24, %16
%31 = load i32, i32* @STDOUT_FILENO
%32 = call i32 @write(i32 %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
%33 = load i32, i32* %5
%34 = call i32 @close(i32 %33)
br label %46
35: ; preds = %11
%36 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.4, i64 0, i64 0))
%37 = load i32, i32* %5
%38 = call i32 @close(i32 %37)
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%40 = load i32, i32* %39
%41 = call i32 @strlen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
%42 = call i32 @write(i32 %40, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i32 %41)
%43 = load i32, i32* %39
%44 = call i32 @close(i32 %43)
%45 = call i32 @wait(i32* null)
br label %46
46: ; preds = %35, %30
%47 = call i32 @sleep(i32 10)
br label %48
48: ; preds = %13, %46, %8
%49 = phi i32 [ %10, %8 ], [ %15, %13 ], [ 0, %46 ]
ret i32 %49
}