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

/* Variables and functions */
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int wait();
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int fds[2];
  char buf[128];
  int n, pid;
  pipe(fds);
  if ((pid = fork()) > 0) {
    write(fds[1], "pipe test", 9);
    pid = wait();
    printf("child is done\n");
  } else {
    n = read(fds[0], buf, sizeof(buf));
    printf("%s\n", buf);
    exit(0);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
lea    -0x8(%rbp),%rdi
call   1050 <pipe@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x98(%rbp)
cmp    $0x0,%eax
jle    11ce <main+0x5e>
mov    -0x4(%rbp),%edi
lea    0xe5c(%rip),%rsi
mov    $0x9,%edx
call   1030 <write@plt>
mov    $0x0,%al
call   1080 <wait@plt>
mov    %eax,-0x98(%rbp)
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1204 <main+0x94>
mov    -0x8(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x80,%edx
call   1060 <read@plt>
mov    %eax,-0x94(%rbp)
lea    -0x90(%rbp),%rsi
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
xor    %eax,%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x90,%rsp
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
jle    11c0 <main+0x50>
mov    0xc(%rsp),%edi
lea    0xe68(%rip),%rsi
mov    $0x9,%edx
call   1040 <write@plt>
xor    %eax,%eax
call   1080 <wait@plt>
lea    0xe5a(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x90,%rsp
pop    %rbx
ret
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rbx
mov    %rbx,%rsi
mov    $0x80,%edx
call   1060 <read@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"pipe test\00"
@.str.1 = [15 x i8] c"child is done\0A\00"
@.str.2 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [128 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%6 = call i32 @pipe(i32* %5)
%7 = call i32 (...) @fork()
store i32 %7, i32* %4
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %15
9: ; preds = %0
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%11 = load i32, i32* %10
%12 = call i32 @write(i32 %11, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 9)
%13 = call i32 (...) @wait()
store i32 %13, i32* %4
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %23
15: ; preds = %0
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%17 = load i32, i32* %16
%18 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%19 = call i32 @read(i32 %17, i8* %18, i32 128)
store i32 %19, i32* %3
%20 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %20)
%22 = call i32 @exit(i32 0)
unreachable
23: ; preds = %9
ret i32 0
}

LLVM IR (-O2):
@.str = [10 x i8] c"pipe test\00"
@str = [14 x i8] c"child is done\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [128 x i8]
%3 = bitcast [2 x i32]* %1 to i8*
%4 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%6 = call i32 @pipe(i32* %5)
%7 = call i32 (...) @fork()
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %15
9: ; preds = %0
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%11 = load i32, i32* %10
%12 = call i32 @write(i32 %11, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 9)
%13 = call i32 (...) @wait()
%14 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
ret i32 0
15: ; preds = %0
%16 = load i32, i32* %5
%17 = call i32 @read(i32 %16, i8* %4, i32 128)
%18 = call i32 @puts(i8* %4)
%19 = call i32 @exit(i32 0)
unreachable
}