#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 BUFSIZ;
int STDOUT_FILENO;
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ exit(int);
int fork();
int getpid();
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int /*<<< orphan*/ printf(char *, int);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ wait(int *);
scalar_t__ write(int, char *, int /*<<< orphan*/);
int main(void) {
  int pipefd[2];
  pid_t pid;
  char buf[BUFSIZ];
  if (pipe(pipefd) == -1) {
    perror("pipe()");
    exit(-1);
  }
  pid = fork();
  if (-1 == pid) {
    perror("fork()");
    exit(-1);
  } else if (pid == 0) {
    printf("Child pid is: %d\n", getpid());
    if (read(pipefd[0], buf, BUFSIZ) < 0) {
      perror("child read()");
      exit(-1);
    }
    write(STDOUT_FILENO, buf, strlen(buf));
    bzero(buf, BUFSIZ);
    snprintf(buf, BUFSIZ, "Message from child: My pid is: %d\n", getpid());
    if (write(pipefd[1], buf, strlen(buf)) < 0) {
      perror("write()");
      exit(-1);
    }
  } else {
    printf("Parent pid is: %d\n", getpid());
    snprintf(buf, BUFSIZ, "Message from parent: My pid is %d\n", getpid());
    if (write(pipefd[1], buf, strlen(buf)) < 0) {
      perror("write()");
      exit(-1);
    }
    int status;
    wait(&status);
    bzero(buf, BUFSIZ);
    if (read(pipefd[0], buf, BUFSIZ) < 0) {
      perror("read()");
      exit(-1);
    }
    write(STDOUT_FILENO, buf, strlen(buf));
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e02(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
lea    -0xc(%rbp),%rdi
call   1080 <pipe@plt>
cmp    $0xffffffff,%eax
jne    1223 <main+0x63>
lea    0xdec(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %eax,-0x10(%rbp)
mov    $0xffffffff,%eax
cmp    -0x10(%rbp),%eax
jne    1251 <main+0x91>
lea    0xdc5(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
cmpl   $0x0,-0x10(%rbp)
jne    1344 <main+0x184>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xda3(%rip),%rdi
call   1060 <printf@plt>
mov    -0x30(%rbp),%rsi
mov    -0xc(%rbp),%edi
mov    0x2d5a(%rip),%rax
mov    (%rax),%edx
call   1090 <read@plt>
cmp    $0x0,%rax
jge    12a5 <main+0xe5>
lea    0xd8a(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
mov    -0x30(%rbp),%rdi
mov    0x2d40(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
call   1050 <strlen@plt>
mov    -0x3c(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x30(%rbp),%rdi
mov    0x2d05(%rip),%rax
mov    (%rax),%esi
call   10b0 <bzero@plt>
mov    0x2cf7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x38(%rbp),%esi
mov    -0x30(%rbp),%rdi
mov    %eax,%ecx
lea    0xd30(%rip),%rdx
call   1070 <snprintf@plt>
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%eax
mov    %eax,-0x34(%rbp)
call   1050 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    133f <main+0x17f>
lea    0xd20(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
jmp    1431 <main+0x271>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xd04(%rip),%rdi
call   1060 <printf@plt>
mov    0x2c78(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x44(%rbp),%esi
mov    -0x30(%rbp),%rdi
mov    %eax,%ecx
lea    0xcef(%rip),%rdx
call   1070 <snprintf@plt>
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%eax
mov    %eax,-0x40(%rbp)
call   1050 <strlen@plt>
mov    -0x40(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    13be <main+0x1fe>
lea    0xca1(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
lea    -0x24(%rbp),%rdi
call   10d0 <wait@plt>
mov    -0x30(%rbp),%rdi
mov    0x2c06(%rip),%rax
mov    (%rax),%esi
call   10b0 <bzero@plt>
mov    -0x30(%rbp),%rsi
mov    -0xc(%rbp),%edi
mov    0x2bf1(%rip),%rax
mov    (%rax),%edx
call   1090 <read@plt>
cmp    $0x0,%rax
jge    140e <main+0x24e>
lea    0xc27(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
mov    -0x30(%rbp),%rdi
mov    0x2bd7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
call   1050 <strlen@plt>
mov    -0x48(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
jmp    1436 <main+0x276>
movl   $0x0,-0x4(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsp,%r15
mov    0x2dff(%rip),%r12
mov    (%r12),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    -0x28(%rbp),%rdi
call   1080 <pipe@plt>
cmp    $0xffffffff,%eax
je     1367 <main+0x1a7>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
je     12c1 <main+0x101>
cmp    $0xffffffff,%eax
je     1379 <main+0x1b9>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe32(%rip),%rdi
mov    %eax,%esi
call   1060 <printf@plt>
mov    (%r12),%r14d
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe2c(%rip),%rdx
mov    %rbx,%rdi
mov    %r14d,%esi
mov    %eax,%ecx
call   1070 <snprintf@plt>
mov    -0x24(%rbp),%r14d
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %rax,%rax
js     1370 <main+0x1b0>
lea    -0x2c(%rbp),%rdi
call   10d0 <wait@plt>
mov    (%r12),%r14d
mov    %rbx,%rdi
mov    %r14d,%esi
call   10b0 <bzero@plt>
mov    -0x28(%rbp),%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1090 <read@plt>
test   %rax,%rax
js     1382 <main+0x1c2>
mov    0x2d4c(%rip),%rax
mov    (%rax),%r14d
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
jmp    1355 <main+0x195>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xd3f(%rip),%rdi
mov    %eax,%esi
call   1060 <printf@plt>
mov    -0x28(%rbp),%edi
mov    (%r12),%edx
mov    %rbx,%rsi
call   1090 <read@plt>
test   %rax,%rax
js     138b <main+0x1cb>
mov    0x2cfb(%rip),%rax
mov    (%rax),%r14d
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    (%r12),%r14d
mov    %rbx,%rdi
mov    %r14d,%esi
call   10b0 <bzero@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xd03(%rip),%rdx
mov    %rbx,%rdi
mov    %r14d,%esi
mov    %eax,%ecx
call   1070 <snprintf@plt>
mov    -0x24(%rbp),%r14d
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %rax,%rax
js     1370 <main+0x1b0>
mov    %r15,%rsp
xor    %eax,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc92(%rip),%rdi
jmp    1392 <main+0x1d2>
lea    0xcd9(%rip),%rdi
jmp    1392 <main+0x1d2>
lea    0xc87(%rip),%rdi
jmp    1392 <main+0x1d2>
lea    0xc9d(%rip),%rdi
jmp    1392 <main+0x1d2>
lea    0xc8e(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@BUFSIZ = global i32 0
@.str = [7 x i8] c"pipe()\00"
@.str.1 = [7 x i8] c"fork()\00"
@.str.2 = [18 x i8] c"Child pid is: %d\0A\00"
@.str.3 = [13 x i8] c"child read()\00"
@STDOUT_FILENO = global i32 0
@.str.4 = [35 x i8] c"Message from child: My pid is: %d\0A\00"
@.str.5 = [8 x i8] c"write()\00"
@.str.6 = [19 x i8] c"Parent pid is: %d\0A\00"
@.str.7 = [35 x i8] c"Message from parent: My pid is %d\0A\00"
@.str.8 = [7 x i8] c"read()\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
store i32 0, i32* %1
%7 = load i32, i32* @BUFSIZ
%8 = zext i32 %7 to i64
%9 = call i8* @llvm.stacksave()
store i8* %9, i8** %4
%10 = alloca i8, i64 %8
store i64 %8, i64* %5
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%12 = call i32 @pipe(i32* %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 -1)
unreachable
17: ; preds = %0
%18 = call i32 (...) @fork()
store i32 %18, i32* %3
%19 = load i32, i32* %3
%20 = icmp eq i32 -1, %19
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @exit(i32 -1)
unreachable
24: ; preds = %17
%25 = load i32, i32* %3
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %56
27: ; preds = %24
%28 = call i32 (...) @getpid()
%29 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %28)
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%31 = load i32, i32* %30
%32 = load i32, i32* @BUFSIZ
%33 = call i64 @read(i32 %31, i8* %10, i32 %32)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %27
%36 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%37 = call i32 @exit(i32 -1)
unreachable
38: ; preds = %27
%39 = load i32, i32* @STDOUT_FILENO
%40 = call i32 @strlen(i8* %10)
%41 = call i64 @write(i32 %39, i8* %10, i32 %40)
%42 = load i32, i32* @BUFSIZ
%43 = call i32 @bzero(i8* %10, i32 %42)
%44 = load i32, i32* @BUFSIZ
%45 = call i32 (...) @getpid()
%46 = call i32 @snprintf(i8* %10, i32 %44, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i32 %45)
%47 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%48 = load i32, i32* %47
%49 = call i32 @strlen(i8* %10)
%50 = call i64 @write(i32 %48, i8* %10, i32 %49)
%51 = icmp slt i64 %50, 0
br i1 %51, label %52, label %55
52: ; preds = %38
%53 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
%54 = call i32 @exit(i32 -1)
unreachable
55: ; preds = %38
br label %86
56: ; preds = %24
%57 = call i32 (...) @getpid()
%58 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i32 %57)
%59 = load i32, i32* @BUFSIZ
%60 = call i32 (...) @getpid()
%61 = call i32 @snprintf(i8* %10, i32 %59, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.7, i64 0, i64 0), i32 %60)
%62 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%63 = load i32, i32* %62
%64 = call i32 @strlen(i8* %10)
%65 = call i64 @write(i32 %63, i8* %10, i32 %64)
%66 = icmp slt i64 %65, 0
br i1 %66, label %67, label %70
67: ; preds = %56
%68 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
%69 = call i32 @exit(i32 -1)
unreachable
70: ; preds = %56
%71 = call i32 @wait(i32* %6)
%72 = load i32, i32* @BUFSIZ
%73 = call i32 @bzero(i8* %10, i32 %72)
%74 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%75 = load i32, i32* %74
%76 = load i32, i32* @BUFSIZ
%77 = call i64 @read(i32 %75, i8* %10, i32 %76)
%78 = icmp slt i64 %77, 0
br i1 %78, label %79, label %82
79: ; preds = %70
%80 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0))
%81 = call i32 @exit(i32 -1)
unreachable
82: ; preds = %70
%83 = load i32, i32* @STDOUT_FILENO
%84 = call i32 @strlen(i8* %10)
%85 = call i64 @write(i32 %83, i8* %10, i32 %84)
br label %86
86: ; preds = %82, %55
br label %87
87: ; preds = %86
store i32 0, i32* %1
%88 = load i8*, i8** %4
%89 = load i32, i32* %1
ret i32 %89
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@.str = [7 x i8] c"pipe()\00"
@.str.1 = [7 x i8] c"fork()\00"
@.str.2 = [18 x i8] c"Child pid is: %d\0A\00"
@.str.3 = [13 x i8] c"child read()\00"
@STDOUT_FILENO = global i32 0
@.str.4 = [35 x i8] c"Message from child: My pid is: %d\0A\00"
@.str.5 = [8 x i8] c"write()\00"
@.str.6 = [19 x i8] c"Parent pid is: %d\0A\00"
@.str.7 = [35 x i8] c"Message from parent: My pid is %d\0A\00"
@.str.8 = [7 x i8] c"read()\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca i32
%3 = bitcast [2 x i32]* %1 to i8*
%4 = load i32, i32* @BUFSIZ
%5 = zext i32 %4 to i64
%6 = tail call i8* @llvm.stacksave()
%7 = alloca i8, i64 %5
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%9 = call i32 @pipe(i32* %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 -1)
unreachable
14: ; preds = %0
%15 = call i32 (...) @fork()
switch i32 %15, label %45 [
i32 -1, label %16
i32 0, label %19
]
16: ; preds = %14
%17 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @exit(i32 -1)
unreachable
19: ; preds = %14
%20 = call i32 (...) @getpid()
%21 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %20)
%22 = load i32, i32* %8
%23 = load i32, i32* @BUFSIZ
%24 = call i64 @read(i32 %22, i8* %7, i32 %23)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %19
%27 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%28 = call i32 @exit(i32 -1)
unreachable
29: ; preds = %19
%30 = load i32, i32* @STDOUT_FILENO
%31 = call i32 @strlen(i8* %7)
%32 = call i64 @write(i32 %30, i8* %7, i32 %31)
%33 = load i32, i32* @BUFSIZ
%34 = call i32 @bzero(i8* %7, i32 %33)
%35 = call i32 (...) @getpid()
%36 = call i32 @snprintf(i8* %7, i32 %33, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i32 %35)
%37 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%38 = load i32, i32* %37
%39 = call i32 @strlen(i8* %7)
%40 = call i64 @write(i32 %38, i8* %7, i32 %39)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %74
42: ; preds = %29
%43 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
%44 = call i32 @exit(i32 -1)
unreachable
45: ; preds = %14
%46 = call i32 (...) @getpid()
%47 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i32 %46)
%48 = load i32, i32* @BUFSIZ
%49 = call i32 (...) @getpid()
%50 = call i32 @snprintf(i8* %7, i32 %48, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.7, i64 0, i64 0), i32 %49)
%51 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%52 = load i32, i32* %51
%53 = call i32 @strlen(i8* %7)
%54 = call i64 @write(i32 %52, i8* %7, i32 %53)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %45
%57 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
%58 = call i32 @exit(i32 -1)
unreachable
59: ; preds = %45
%60 = bitcast i32* %2 to i8*
%61 = call i32 @wait(i32* %2)
%62 = load i32, i32* @BUFSIZ
%63 = call i32 @bzero(i8* %7, i32 %62)
%64 = load i32, i32* %8
%65 = call i64 @read(i32 %64, i8* %7, i32 %62)
%66 = icmp slt i64 %65, 0
br i1 %66, label %67, label %70
67: ; preds = %59
%68 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0))
%69 = call i32 @exit(i32 -1)
unreachable
70: ; preds = %59
%71 = load i32, i32* @STDOUT_FILENO
%72 = call i32 @strlen(i8* %7)
%73 = call i64 @write(i32 %71, i8* %7, i32 %72)
br label %74
74: ; preds = %70, %29
ret i32 0
}