#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 EXIT_SUCCESS;
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int getpid();
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ read(int, int *, int);
int /*<<< orphan*/ write(int, int *, int);
int main(int argc, char *argv[]) {
  pid_t pid1, pid2;
  int pd[2];
  pipe(pd);
  switch (pid1 = fork()) {
  case -1:
    err(1, "fork");
  case 0:
    if (read(pd[0], &pid2, sizeof(pid_t)) > 0) {
      printf("My sibling has the following PID: %d\n", pid2);
      exit(0);
    }
  }
  switch (pid2 = fork()) {
  case -1:
    err(1, "fork");
  case 0:
    pid2 = getpid();
    printf("I'm: %d. Let's send my PID through a pipe.\n", pid2);
    write(pd[1], &pid2, sizeof(pid_t));
  }
  return (EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
call   1060 <pipe@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,-0x14(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x24(%rbp)
sub    $0xffffffff,%eax
je     11cc <main+0x4c>
jmp    11bc <main+0x3c>
mov    -0x24(%rbp),%eax
test   %eax,%eax
je     11dd <main+0x5d>
jmp    1212 <main+0x92>
mov    $0x1,%edi
lea    0xe28(%rip),%rsi
call   1080 <err@plt>
mov    -0x20(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1070 <read@plt>
cmp    $0x0,%eax
jle    120d <main+0x8d>
mov    -0x18(%rbp),%esi
lea    0xe04(%rip),%rdi
call   1050 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>
jmp    1212 <main+0x92>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,-0x18(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x28(%rbp)
sub    $0xffffffff,%eax
je     123f <main+0xbf>
jmp    122f <main+0xaf>
mov    -0x28(%rbp),%eax
test   %eax,%eax
je     1250 <main+0xd0>
jmp    127a <main+0xfa>
mov    $0x1,%edi
lea    0xdb5(%rip),%rsi
call   1080 <err@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xdc7(%rip),%rdi
call   1050 <printf@plt>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    0x2d5f(%rip),%rax
mov    (%rax),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
sub    $0x18,%rsp
lea    0x10(%rsp),%rdi
call   1060 <pipe@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     11af <main+0x2f>
cmp    $0xffffffff,%eax
jne    11c6 <main+0x46>
lea    0xe5b(%rip),%rsi
mov    $0x1,%edi
call   1080 <err@plt>
mov    0x10(%rsp),%edi
lea    0xc(%rsp),%rsi
mov    $0x4,%edx
call   1070 <read@plt>
test   %eax,%eax
jg     1221 <main+0xa1>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     11e7 <main+0x67>
cmp    $0xffffffff,%eax
jne    1213 <main+0x93>
lea    0xe23(%rip),%rsi
mov    $0x1,%edi
call   1080 <err@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,0xc(%rsp)
lea    0xe32(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    0x14(%rsp),%edi
lea    0xc(%rsp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%eax
add    $0x18,%rsp
ret
mov    0xc(%rsp),%esi
lea    0xdd9(%rip),%rdi
call   1050 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@.str.1 = [38 x i8] c"My sibling has the following PID: %d\0A\00"
@.str.2 = [44 x i8] c"I'm: %d. Let's send my PID through a pipe.\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [2 x i32]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%10 = call i32 @pipe(i32* %9)
%11 = call i32 (...) @fork()
store i32 %11, i32* %6
switch i32 %11, label %24 [
i32 -1, label %12
i32 0, label %14
]
12: ; preds = %2
%13 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %2, %12
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%16 = load i32, i32* %15
%17 = call i32 @read(i32 %16, i32* %7, i32 4)
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %14
%20 = load i32, i32* %7
%21 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %20)
%22 = call i32 @exit(i32 0)
unreachable
23: ; preds = %14
br label %24
24: ; preds = %23, %2
%25 = call i32 (...) @fork()
store i32 %25, i32* %7
switch i32 %25, label %35 [
i32 -1, label %26
i32 0, label %28
]
26: ; preds = %24
%27 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %28
28: ; preds = %24, %26
%29 = call i32 (...) @getpid()
store i32 %29, i32* %7
%30 = load i32, i32* %7
%31 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32 %30)
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%33 = load i32, i32* %32
%34 = call i32 @write(i32 %33, i32* %7, i32 4)
br label %35
35: ; preds = %28, %24
%36 = load i32, i32* @EXIT_SUCCESS
ret i32 %36
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@.str.1 = [38 x i8] c"My sibling has the following PID: %d\0A\00"
@.str.2 = [44 x i8] c"I'm: %d. Let's send my PID through a pipe.\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca [2 x i32]
%5 = bitcast i32* %3 to i8*
%6 = bitcast [2 x i32]* %4 to i8*
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%8 = call i32 @pipe(i32* %7)
%9 = call i32 (...) @fork()
switch i32 %9, label %20 [
i32 -1, label %10
i32 0, label %12
]
10: ; preds = %2
%11 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %2, %10
%13 = load i32, i32* %7
%14 = call i32 @read(i32 %13, i32* %3, i32 4)
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %12
%17 = load i32, i32* %3
%18 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%19 = call i32 @exit(i32 0)
unreachable
20: ; preds = %12, %2
%21 = call i32 (...) @fork()
switch i32 %21, label %30 [
i32 -1, label %22
i32 0, label %24
]
22: ; preds = %20
%23 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %24
24: ; preds = %20, %22
%25 = call i32 (...) @getpid()
store i32 %25, i32* %3
%26 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32 %25)
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%28 = load i32, i32* %27
%29 = call i32 @write(i32 %28, i32* %3, i32 4)
br label %30
30: ; preds = %24, %20
%31 = load i32, i32* @EXIT_SUCCESS
ret i32 %31
}