#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 /*<<< orphan*/ read_buffer;
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
scalar_t__ getpid();
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ wait(int *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  pid_t child_id;
  pid_t my_id;
  int pipe_desc[2];
  char *string;
  char read_buffer[4096];
  int child_status;
  int err;
  err = pipe(pipe_desc);
  if (err < 0) {
    printf("error creating pipe\n");
    exit(1);
  }
  child_id = fork();
  if (child_id != 0) {
    my_id = getpid();
    close(pipe_desc[0]);
    string = "a string made by the parent\n";
    printf("pid: %d -- writing %s to pipe_desc[1]\n", (int)my_id, string);
    write(pipe_desc[1], string, strlen(string));
    close(pipe_desc[1]);
    wait(&child_status);
  } else {
    my_id = getpid();
    close(pipe_desc[1]);
    memset(read_buffer, 0, sizeof(read_buffer));
    read(pipe_desc[0], read_buffer, sizeof(read_buffer));
    printf("pid: %d -- received %s from parent\n", (int)my_id, read_buffer);
    close(pipe_desc[0]);
  }
  printf("pid: %d -- I am exiting\n", my_id);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1050,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x28(%rbp),%rdi
call   1090 <pipe@plt>
mov    %eax,-0x1038(%rbp)
cmpl   $0x0,-0x1038(%rbp)
jge    11fd <main+0x4d>
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1293 <main+0xe3>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%edi
call   1080 <close@plt>
lea    0xde8(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    %eax,%esi
mov    -0x30(%rbp),%rdx
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x1044(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x1040(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x1044(%rbp),%edi
mov    -0x1040(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
lea    -0x1034(%rbp),%rdi
call   10c0 <wait@plt>
jmp    12f0 <main+0x140>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
lea    -0x1030(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1070 <memset@plt>
mov    -0x28(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    $0x1000,%edx
call   10a0 <read@plt>
mov    -0x20(%rbp),%rax
mov    %eax,%esi
lea    -0x1030(%rbp),%rdx
lea    0xd78(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x28(%rbp),%edi
call   1080 <close@plt>
mov    -0x20(%rbp),%rsi
lea    0xd82(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x1010,%rsp
lea    0x8(%rsp),%rdi
call   10a0 <pipe@plt>
test   %eax,%eax
jns    11ef <main+0x2f>
lea    0xea1(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %rax,%r14
test   %rbx,%rbx
jne    1255 <main+0x95>
mov    0xc(%rsp),%edi
call   1090 <close@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1080 <memset@plt>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   10b0 <read@plt>
lea    0xe07(%rip),%rdi
mov    %r14d,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x8(%rsp),%edi
call   1090 <close@plt>
jmp    12a4 <main+0xe4>
mov    0x8(%rsp),%edi
call   1090 <close@plt>
lea    0xdb8(%rip),%rdi
lea    0xd94(%rip),%rbx
mov    %r14d,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <printf@plt>
mov    0xc(%rsp),%ebp
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    0xc(%rsp),%edi
call   1090 <close@plt>
lea    0x10(%rsp),%rdi
call   10d0 <wait@plt>
lea    0xdbd(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [21 x i8] c"error creating pipe\0A\00"
@.str.1 = [29 x i8] c"a string made by the parent\0A\00"
@.str.2 = [39 x i8] c"pid: %d -- writing %s to pipe_desc[1]\0A\00"
@.str.3 = [36 x i8] c"pid: %d -- received %s from parent\0A\00"
@.str.4 = [25 x i8] c"pid: %d -- I am exiting\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
%8 = alloca [2 x i32]
%9 = alloca i8*
%10 = alloca [4096 x i8]
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%14 = call i32 @pipe(i32* %13)
store i32 %14, i32* %12
%15 = load i32, i32* %12
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = call i64 (...) @fork()
store i64 %21, i64* %6
%22 = load i64, i64* %6
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %43
24: ; preds = %20
%25 = call i64 (...) @getpid()
store i64 %25, i64* %7
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%27 = load i32, i32* %26
%28 = call i32 @close(i32 %27)
store i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8** %9
%29 = load i64, i64* %7
%30 = trunc i64 %29 to i32
%31 = load i8*, i8** %9
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 %30, i8* %31)
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%34 = load i32, i32* %33
%35 = load i8*, i8** %9
%36 = load i8*, i8** %9
%37 = call i32 @strlen(i8* %36)
%38 = call i32 @write(i32 %34, i8* %35, i32 %37)
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%40 = load i32, i32* %39
%41 = call i32 @close(i32 %40)
%42 = call i32 @wait(i32* %11)
br label %61
43: ; preds = %20
%44 = call i64 (...) @getpid()
store i64 %44, i64* %7
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%46 = load i32, i32* %45
%47 = call i32 @close(i32 %46)
%48 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%49 = call i32 @memset(i8* %48, i32 0, i32 4096)
%50 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%51 = load i32, i32* %50
%52 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%53 = call i32 @read(i32 %51, i8* %52, i32 4096)
%54 = load i64, i64* %7
%55 = trunc i64 %54 to i32
%56 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i32 %55, i8* %56)
%58 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%59 = load i32, i32* %58
%60 = call i32 @close(i32 %59)
br label %61
61: ; preds = %43, %24
%62 = load i64, i64* %7
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i64 %62)
%64 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str.1 = [29 x i8] c"a string made by the parent\0A\00"
@.str.2 = [39 x i8] c"pid: %d -- writing %s to pipe_desc[1]\0A\00"
@.str.3 = [36 x i8] c"pid: %d -- received %s from parent\0A\00"
@.str.4 = [25 x i8] c"pid: %d -- I am exiting\0A\00"
@str = [20 x i8] c"error creating pipe\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [2 x i32]
%4 = alloca [4096 x i8]
%5 = alloca i32
%6 = bitcast [2 x i32]* %3 to i8*
%7 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%8 = bitcast i32* %5 to i8*
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%10 = call i32 @pipe(i32* %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = call i64 (...) @fork()
%17 = icmp eq i64 %16, 0
%18 = call i64 (...) @getpid()
br i1 %17, label %31, label %19
19: ; preds = %15
%20 = load i32, i32* %9
%21 = call i32 @close(i32 %20)
%22 = trunc i64 %18 to i32
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 %22, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%25 = load i32, i32* %24
%26 = call i32 @strlen(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @write(i32 %25, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %26)
%28 = load i32, i32* %24
%29 = call i32 @close(i32 %28)
%30 = call i32 @wait(i32* %5)
br label %42
31: ; preds = %15
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%33 = load i32, i32* %32
%34 = call i32 @close(i32 %33)
%35 = call i32 @memset(i8* %7, i32 0, i32 4096)
%36 = load i32, i32* %9
%37 = call i32 @read(i32 %36, i8* %7, i32 4096)
%38 = trunc i64 %18 to i32
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i32 %38, i8* %7)
%40 = load i32, i32* %9
%41 = call i32 @close(i32 %40)
br label %42
42: ; preds = %31, %19
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i64 %18)
%44 = call i32 @exit(i32 0)
unreachable
}