#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*/ dup2(int, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int getpid();
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *, int);
int read(int /*<<< orphan*/, 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]);
    dup2(p[0], 0);
    nbc = read(0, buffer, 10);
    write(1, buffer, nbc);
    exit(0);
  } else {
    close(p[0]);
    dup2(p[1], 1);
    printf("%d\n", getpid());
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    -0x8(%rbp),%rdi
call   1080 <pipe@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
cmp    $0x0,%rax
jne    11ef <main+0x5f>
mov    -0x4(%rbp),%edi
call   1070 <close@plt>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1050 <dup2@plt>
lea    -0x12(%rbp),%rsi
xor    %edi,%edi
mov    $0xa,%edx
call   1090 <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   10a0 <exit@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
mov    -0x4(%rbp),%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xdec(%rip),%rdi
call   1060 <printf@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   1080 <pipe@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
je     11df <main+0x4f>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
mov    0x10(%rsp),%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe30(%rip),%rdi
mov    %eax,%esi
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
ret
mov    0x10(%rsp),%edi
call   1070 <close@plt>
mov    0xc(%rsp),%edi
xor    %esi,%esi
call   1050 <dup2@plt>
lea    0x16(%rsp),%rbx
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0xa,%edx
call   1090 <read@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
xor    %edi,%edi
call   10a0 <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 %21
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 = call i32 @dup2(i32 %13, i32 0)
%15 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%16 = call i32 @read(i32 0, i8* %15, i32 10)
store i32 %16, i32* %3
%17 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%18 = load i32, i32* %3
%19 = call i32 @write(i32 1, i8* %17, i32 %18)
%20 = call i32 @exit(i32 0)
unreachable
21: ; preds = %0
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%23 = load i32, i32* %22
%24 = call i32 @close(i32 %23)
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%26 = load i32, i32* %25
%27 = call i32 @dup2(i32 %26, i32 1)
%28 = call i32 (...) @getpid()
%29 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %28)
br label %30
30: ; preds = %21
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 %18
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 @dup2(i32 %13, i32 0)
%15 = call i32 @read(i32 0, i8* %4, i32 10)
%16 = call i32 @write(i32 1, i8* %4, i32 %15)
%17 = call i32 @exit(i32 0)
unreachable
18: ; preds = %0
%19 = load i32, i32* %5
%20 = call i32 @close(i32 %19)
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%22 = load i32, i32* %21
%23 = call i32 @dup2(i32 %22, i32 1)
%24 = call i32 (...) @getpid()
%25 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %24)
ret i32 0
}