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

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dprintf(int, char *, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ pipe(int *);
int read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int p[2];
  char buffer[10];
  int nbc;
  pipe(p);
  if (fork() == 0) {
    close(p[1]);
    nbc = read(p[0], buffer, 10);
    write(1, buffer, nbc);
    exit(0);
  } else {
    close(p[0]);
    dprintf(p[1], "%d\n", getpid());
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    -0x8(%rbp),%rdi
call   1070 <pipe@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
cmp    $0x0,%rax
jne    11d6 <main+0x56>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%edi
lea    -0x12(%rbp),%rsi
mov    $0xa,%edx
call   1080 <read@plt>
mov    %eax,-0x18(%rbp)
lea    -0x12(%rbp),%rsi
mov    -0x18(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x1c(%rbp),%edi
mov    %eax,%edx
lea    0xe09(%rip),%rsi
call   1060 <dprintf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
lea    0xc(%rsp),%rdi
call   1070 <pipe@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     11c7 <main+0x47>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
mov    0x10(%rsp),%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe4a(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
call   1060 <dprintf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
ret
mov    0x10(%rsp),%edi
call   1050 <close@plt>
mov    0xc(%rsp),%edi
lea    0x16(%rsp),%rbx
mov    %rbx,%rsi
mov    $0xa,%edx
call   1080 <read@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [10 x i8]
%3 = alloca i32
%4 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%5 = call i32 @pipe(i32* %4)
%6 = call i64 (...) @fork()
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %20
8: ; preds = %0
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%10 = load i32, i32* %9
%11 = call i32 @close(i32 %10)
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%13 = load i32, i32* %12
%14 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%15 = call i32 @read(i32 %13, i8* %14, i32 10)
store i32 %15, i32* %3
%16 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%17 = load i32, i32* %3
%18 = call i32 @write(i32 1, i8* %16, i32 %17)
%19 = call i32 @exit(i32 0)
unreachable
20: ; preds = %0
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%22 = load i32, i32* %21
%23 = call i32 @close(i32 %22)
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%25 = load i32, i32* %24
%26 = call i32 (...) @getpid()
%27 = call i32 @dprintf(i32 %25, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %26)
br label %28
28: ; preds = %20
ret i32 0
}

LLVM IR (-O2):
@.str = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [10 x i8]
%3 = bitcast [2 x i32]* %1 to i8*
%4 = getelementptr inbounds [10 x i8], [10 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 i64 (...) @fork()
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %17
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 @close(i32 %11)
%13 = load i32, i32* %5
%14 = call i32 @read(i32 %13, i8* %4, i32 10)
%15 = call i32 @write(i32 1, i8* %4, i32 %14)
%16 = call i32 @exit(i32 0)
unreachable
17: ; preds = %0
%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 (...) @getpid()
%23 = call i32 @dprintf(i32 %21, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %22)
ret i32 0
}