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

/* Variables and functions */
int STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main(void) {
  int n;
  int fd[2];
  pid_t pid;
  char line[4096] = {0};
  if (pipe(fd) < 0) {
    printf("pipe error\n");
    return -1;
  }
  if ((pid = fork()) < 0) {
    printf("fork error");
    return -1;
  } else if (pid > 0) {
    close(fd[0]);
    write(fd[1], "pipe from parent\n", 17);
  } else {
    close(fd[1]);
    n = read(fd[0], line, 4096);
    write(STDOUT_FILENO, line, n);
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x1020(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
lea    -0x10(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0x0,%rax
jge    11d0 <main+0x50>
lea    0xe41(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126c <main+0xec>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    11fd <main+0x7d>
lea    0xe20(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126c <main+0xec>
cmpq   $0x0,-0x18(%rbp)
jle    1229 <main+0xa9>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
mov    -0xc(%rbp),%edi
lea    0xdfd(%rip),%rsi
mov    $0x11,%edx
call   1030 <write@plt>
jmp    1260 <main+0xe0>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1080 <read@plt>
mov    %eax,-0x8(%rbp)
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x8(%rbp),%edx
call   1030 <write@plt>
jmp    1265 <main+0xe5>
xor    %edi,%edi
call   1090 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x1010,%rsp
lea    0x10(%rsp),%rdi
mov    $0x1000,%edx
xor    %esi,%esi
call   1060 <memset@plt>
lea    0x8(%rsp),%rdi
call   1080 <pipe@plt>
test   %rax,%rax
js     11d2 <main+0x42>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
jns    11ec <main+0x5c>
lea    0xe35(%rip),%rdi
call   1050 <printf@plt>
jmp    11de <main+0x4e>
lea    0xe44(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x1010,%rsp
pop    %rbx
ret
jne    121d <main+0x8d>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rbx
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1090 <read@plt>
mov    0x2ddc(%rip),%rcx
mov    (%rcx),%edi
mov    %rbx,%rsi
mov    %eax,%edx
jmp    1236 <main+0xa6>
mov    0x8(%rsp),%edi
call   1070 <close@plt>
mov    0xc(%rsp),%edi
lea    0xdda(%rip),%rsi
mov    $0x11,%edx
call   1040 <write@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

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

LLVM IR (-O2):
@.str.1 = [11 x i8] c"fork error\00"
@.str.2 = [18 x i8] c"pipe from parent\0A\00"
@STDOUT_FILENO = global i32 0
@str = [11 x i8] c"pipe error\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [4096 x i8]
%3 = bitcast [2 x i32]* %1 to i8*
%4 = getelementptr inbounds [4096 x i8], [4096 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 %10
8: ; preds = %0
%9 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
br label %33
10: ; preds = %0
%11 = call i64 (...) @fork()
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %33
15: ; preds = %10
%16 = icmp eq i64 %11, 0
br i1 %16, label %23, label %17
17: ; preds = %15
%18 = load i32, i32* %5
%19 = call i32 @close(i32 %18)
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%21 = load i32, i32* %20
%22 = call i32 @write(i32 %21, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 17)
br label %31
23: ; preds = %15
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%25 = load i32, i32* %24
%26 = call i32 @close(i32 %25)
%27 = load i32, i32* %5
%28 = call i32 @read(i32 %27, i8* %4, i32 4096)
%29 = load i32, i32* @STDOUT_FILENO
%30 = call i32 @write(i32 %29, i8* %4, i32 %28)
br label %31
31: ; preds = %17, %23
%32 = call i32 @exit(i32 0)
unreachable
33: ; preds = %13, %8
ret i32 -1
}