#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);
scalar_t__ fork();
int getpid();
int getppid();
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *, int, ...);
int read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int strlen(char *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char **argv) {
  int n, fd[2];
  pid_t pid;
  char line[64];
  if (pipe(fd) < 0) {
    perror("pipe error");
    exit(1);
  }
  if ((pid = fork()) < 0) {
    perror("fork error");
    exit(1);
  } else if (pid > 0) {
    close(fd[0]);
    printf("P=> Parent process with pid %d (and its ppid %d).\n", getpid(),
           getppid());
    printf("P=> Sending a message to the child process (pid %d):\n", pid);
    snprintf(line, 64, "Hello child!  I'm your parent pid %d!\n", getpid());
    write(fd[1], line, strlen(line));
    close(fd[1]);
  } else {
    close(fd[1]);
    printf("C=> Child process with pid %d (and its ppid %d).\n", getpid(),
           getppid());
    printf("C=> Reading a message from the parent (pid %d):\n", getppid());
    n = read(fd[0], line, 64);
    close(fd[1]);
    write(STDOUT_FILENO, line, n);
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x1c(%rbp),%rdi
call   1090 <pipe@plt>
cmp    $0x0,%rax
jge    1202 <main+0x42>
lea    0xe0d(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jge    122d <main+0x6d>
lea    0xded(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
cmpq   $0x0,-0x28(%rbp)
jle    12dc <main+0x11c>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x8c(%rbp)
mov    $0x0,%al
call   10c0 <getppid@plt>
mov    -0x8c(%rbp),%esi
mov    %eax,%edx
lea    0xdb3(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x28(%rbp),%rax
mov    %eax,%esi
lea    0xdd2(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x70(%rbp),%rax
mov    %rax,-0x88(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x88(%rbp),%rdi
mov    %eax,%ecx
mov    $0x40,%esi
lea    0xdda(%rip),%rdx
call   1070 <snprintf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x7c(%rbp)
lea    -0x70(%rbp),%rax
mov    %rax,-0x78(%rbp)
lea    -0x70(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x7c(%rbp),%edi
mov    -0x78(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x18(%rbp),%edi
call   1080 <close@plt>
jmp    1356 <main+0x196>
mov    -0x18(%rbp),%edi
call   1080 <close@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x90(%rbp)
mov    $0x0,%al
call   10c0 <getppid@plt>
mov    -0x90(%rbp),%esi
mov    %eax,%edx
lea    0xd9f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   10c0 <getppid@plt>
mov    %eax,%esi
lea    0xdba(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x1c(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0x40,%edx
call   10a0 <read@plt>
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
call   1080 <close@plt>
mov    0x2ca8(%rip),%rax
mov    (%rax),%edi
lea    -0x70(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
jmp    135b <main+0x19b>
xor    %edi,%edi
call   10d0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x58,%rsp
lea    0x8(%rsp),%rdi
call   1090 <pipe@plt>
test   %rax,%rax
jns    11de <main+0x1e>
lea    0xe24(%rip),%rdi
jmp    11f1 <main+0x31>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %rax,%rax
jns    1200 <main+0x40>
lea    0xe1a(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
jne    127f <main+0xbf>
mov    0xc(%rsp),%edi
call   1080 <close@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   10c0 <getppid@plt>
lea    0xe84(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   10c0 <getppid@plt>
lea    0xe9d(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rbx
mov    %rbx,%rsi
mov    $0x40,%edx
call   10a0 <read@plt>
mov    %eax,%ebp
mov    0xc(%rsp),%edi
call   1080 <close@plt>
mov    0x2d84(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
mov    %rax,%rbx
mov    0x8(%rsp),%edi
call   1080 <close@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   10c0 <getppid@plt>
lea    0xd74(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xd95(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xdb4(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x40,%esi
mov    %eax,%ecx
call   1070 <snprintf@plt>
mov    0xc(%rsp),%ebp
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    0xc(%rsp),%edi
call   1080 <close@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"pipe error\00"
@.str.1 = [11 x i8] c"fork error\00"
@.str.2 = [51 x i8] c"P=> Parent process with pid %d (and its ppid %d).\0A\00"
@.str.3 = [54 x i8] c"P=> Sending a message to the child process (pid %d):\0A\00"
@.str.4 = [39 x i8] c"Hello child! I'm your parent pid %d!\0A\00"
@.str.5 = [50 x i8] c"C=> Child process with pid %d (and its ppid %d).\0A\00"
@.str.6 = [49 x i8] c"C=> Reading a message from the parent (pid %d):\0A\00"
@STDOUT_FILENO = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [2 x i32]
%8 = alloca i64
%9 = alloca [64 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%11 = call i64 @pipe(i32* %10)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = call i64 (...) @fork()
store i64 %17, i64* %8
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %16
%23 = load i64, i64* %8
%24 = icmp sgt i64 %23, 0
br i1 %24, label %25, label %47
25: ; preds = %22
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%27 = load i32, i32* %26
%28 = call i32 @close(i32 %27)
%29 = call i32 (...) @getpid()
%30 = call i32 (...) @getppid()
%31 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i32 %29, i32 %30)
%32 = load i64, i64* %8
%33 = trunc i64 %32 to i32
%34 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.3, i64 0, i64 0), i32 %33)
%35 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%36 = call i32 (...) @getpid()
%37 = call i32 @snprintf(i8* %35, i32 64, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i32 %36)
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%39 = load i32, i32* %38
%40 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%41 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%42 = call i32 @strlen(i8* %41)
%43 = call i32 @write(i32 %39, i8* %40, i32 %42)
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%45 = load i32, i32* %44
%46 = call i32 @close(i32 %45)
br label %67
47: ; preds = %22
%48 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%49 = load i32, i32* %48
%50 = call i32 @close(i32 %49)
%51 = call i32 (...) @getpid()
%52 = call i32 (...) @getppid()
%53 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.5, i64 0, i64 0), i32 %51, i32 %52)
%54 = call i32 (...) @getppid()
%55 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.6, i64 0, i64 0), i32 %54)
%56 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%57 = load i32, i32* %56
%58 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%59 = call i32 @read(i32 %57, i8* %58, i32 64)
store i32 %59, i32* %6
%60 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%61 = load i32, i32* %60
%62 = call i32 @close(i32 %61)
%63 = load i32, i32* @STDOUT_FILENO
%64 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%65 = load i32, i32* %6
%66 = call i32 @write(i32 %63, i8* %64, i32 %65)
br label %67
67: ; preds = %47, %25
br label %68
68: ; preds = %67
%69 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [11 x i8] c"pipe error\00"
@.str.1 = [11 x i8] c"fork error\00"
@.str.2 = [51 x i8] c"P=> Parent process with pid %d (and its ppid %d).\0A\00"
@.str.3 = [54 x i8] c"P=> Sending a message to the child process (pid %d):\0A\00"
@.str.4 = [39 x i8] c"Hello child! I'm your parent pid %d!\0A\00"
@.str.5 = [50 x i8] c"C=> Child process with pid %d (and its ppid %d).\0A\00"
@.str.6 = [49 x i8] c"C=> Reading a message from the parent (pid %d):\0A\00"
@STDOUT_FILENO = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [2 x i32]
%4 = alloca [64 x i8]
%5 = bitcast [2 x i32]* %3 to i8*
%6 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%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 %13
10: ; preds = %2
%11 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = call i64 (...) @fork()
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %13
%20 = icmp eq i64 %14, 0
br i1 %20, label %37, label %21
21: ; preds = %19
%22 = load i32, i32* %7
%23 = call i32 @close(i32 %22)
%24 = call i32 (...) @getpid()
%25 = call i32 (...) @getppid()
%26 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i32 %24, i32 %25)
%27 = trunc i64 %14 to i32
%28 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.3, i64 0, i64 0), i32 %27)
%29 = call i32 (...) @getpid()
%30 = call i32 @snprintf(i8* %6, i32 64, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i32 %29)
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%32 = load i32, i32* %31
%33 = call i32 @strlen(i8* %6)
%34 = call i32 @write(i32 %32, i8* %6, i32 %33)
%35 = load i32, i32* %31
%36 = call i32 @close(i32 %35)
br label %52
37: ; preds = %19
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%39 = load i32, i32* %38
%40 = call i32 @close(i32 %39)
%41 = call i32 (...) @getpid()
%42 = call i32 (...) @getppid()
%43 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.5, i64 0, i64 0), i32 %41, i32 %42)
%44 = call i32 (...) @getppid()
%45 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.6, i64 0, i64 0), i32 %44)
%46 = load i32, i32* %7
%47 = call i32 @read(i32 %46, i8* %6, i32 64)
%48 = load i32, i32* %38
%49 = call i32 @close(i32 %48)
%50 = load i32, i32* @STDOUT_FILENO
%51 = call i32 @write(i32 %50, i8* %6, i32 %47)
br label %52
52: ; preds = %21, %37
%53 = call i32 @exit(i32 0)
unreachable
}