#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 /*<<< orphan*/ child_code(int *);
int /*<<< orphan*/ error(char *);
int fork();
int /*<<< orphan*/ parent_code(int *);
int pipe(int *);
int /*<<< orphan*/ printf(char *, int, int);
int wait(int *);
int main(int argc, char *argv[]) {
  int pipe_to_child[2];
  if (pipe(pipe_to_child) == -1)
    error("Can't create the first pipe");
  pid_t child_pid = fork();
  if (child_pid == -1)
    error("Can't fork process");
  if (child_pid == 0) {
    child_code(pipe_to_child);
  } else {
    parent_code(pipe_to_child);
  }
  int status;
  pid_t pid = wait(&status);
  if (pid == -1) {
    error("Error waiting for child process");
  }
  printf("Child %d exited with code %d\n", pid, status);
}


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    -0x18(%rbp),%rdi
call   1040 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11a4 <main+0x34>
lea    0xe61(%rip),%rdi
call   1050 <error@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11c4 <main+0x54>
lea    0xe5d(%rip),%rdi
call   1050 <error@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    11dc <main+0x6c>
lea    -0x18(%rbp),%rdi
call   1070 <child_code@plt>
jmp    11e5 <main+0x75>
lea    -0x18(%rbp),%rdi
call   1060 <parent_code@plt>
lea    -0x20(%rbp),%rdi
call   1080 <wait@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1207 <main+0x97>
lea    0xe2d(%rip),%rdi
call   1050 <error@plt>
mov    -0x24(%rbp),%esi
mov    -0x20(%rbp),%edx
lea    0xe3b(%rip),%rdi
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
lea    0x8(%rsp),%rdi
call   1040 <pipe@plt>
cmp    $0xffffffff,%eax
jne    1190 <main+0x20>
lea    0xe75(%rip),%rdi
call   1050 <error@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
je     11b8 <main+0x48>
cmp    $0xffffffff,%eax
jne    11ac <main+0x3c>
lea    0xe75(%rip),%rdi
call   1050 <error@plt>
lea    0x8(%rsp),%rdi
call   1060 <parent_code@plt>
jmp    11c2 <main+0x52>
lea    0x8(%rsp),%rdi
call   1070 <child_code@plt>
lea    0x4(%rsp),%rdi
call   1080 <wait@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11df <main+0x6f>
lea    0xe55(%rip),%rdi
call   1050 <error@plt>
mov    0x4(%rsp),%edx
lea    0xe65(%rip),%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [28 x i8] c"Can't create the first pipe\00"
@.str.1 = [19 x i8] c"Can't fork process\00"
@.str.2 = [32 x i8] c"Error waiting for child process\00"
@.str.3 = [30 x i8] c"Child %d exited with code %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [2 x i32]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%11 = call i32 @pipe(i32* %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @error(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %2
%16 = call i32 (...) @fork()
store i32 %16, i32* %7
%17 = load i32, i32* %7
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = call i32 @error(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %19, %15
%22 = load i32, i32* %7
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%26 = call i32 @child_code(i32* %25)
br label %30
27: ; preds = %21
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%29 = call i32 @parent_code(i32* %28)
br label %30
30: ; preds = %27, %24
%31 = call i32 @wait(i32* %8)
store i32 %31, i32* %9
%32 = load i32, i32* %9
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %36
34: ; preds = %30
%35 = call i32 @error(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
br label %36
36: ; preds = %34, %30
%37 = load i32, i32* %9
%38 = load i32, i32* %8
%39 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %37, i32 %38)
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
@.str = [28 x i8] c"Can't create the first pipe\00"
@.str.1 = [19 x i8] c"Can't fork process\00"
@.str.2 = [32 x i8] c"Error waiting for child process\00"
@.str.3 = [30 x i8] c"Child %d exited with code %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [2 x i32]
%4 = alloca i32
%5 = bitcast [2 x i32]* %3 to i8*
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%7 = call i32 @pipe(i32* %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = call i32 @error(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %2
%12 = call i32 (...) @fork()
switch i32 %12, label %17 [
i32 -1, label %13
i32 0, label %15
]
13: ; preds = %11
%14 = call i32 @error(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %17
15: ; preds = %11
%16 = call i32 @child_code(i32* %6)
br label %19
17: ; preds = %13, %11
%18 = call i32 @parent_code(i32* %6)
br label %19
19: ; preds = %17, %15
%20 = bitcast i32* %4 to i8*
%21 = call i32 @wait(i32* %4)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 @error(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
br label %25
25: ; preds = %23, %19
%26 = load i32, i32* %4
%27 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %21, i32 %26)
ret i32 0
}