#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 */

/* Variables and functions */
int /*<<< orphan*/ child_process(int *, int *);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ parent_process(int *, int *);
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char **argv) {
  int n, i, pid, pipes[2][2];
  for (i = 0; i < 2; i++)
    if (pipe(pipes[i]) < 0) {
      printf("We had an error when we tried to create the pipes %d", i);
      perror("pipe");
      exit(0);
    }
  pid = fork();
  if (pid < 0) {
    printf("We had an erorr when we tried to fork");
    exit(1);
  } else if (pid == 0) {
    child_process(pipes[0], pipes[1]);
    exit(0);
  } else {
    parent_process(pipes[0], pipes[1]);
  }
  return 0;
}


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)
movl   $0x0,-0x18(%rbp)
cmpl   $0x2,-0x18(%rbp)
jge    11ec <main+0x7c>
movslq -0x18(%rbp),%rax
lea    -0x30(%rbp),%rdi
shl    $0x3,%rax
add    %rax,%rdi
call   1060 <pipe@plt>
cmp    $0x0,%rax
jge    11d9 <main+0x69>
mov    -0x18(%rbp),%esi
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe68(%rip),%rdi
call   1070 <perror@plt>
xor    %edi,%edi
call   1080 <exit@plt>
jmp    11de <main+0x6e>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    118d <main+0x1d>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1218 <main+0xa8>
lea    0xe33(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    123a <main+0xca>
lea    -0x30(%rbp),%rdi
lea    -0x30(%rbp),%rsi
add    $0x8,%rsi
call   1050 <child_process@plt>
xor    %edi,%edi
call   1080 <exit@plt>
lea    -0x30(%rbp),%rdi
lea    -0x30(%rbp),%rsi
add    $0x8,%rsi
call   1030 <parent_process@plt>
jmp    1250 <main+0xe0>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    %rsp,%rdi
call   1060 <pipe@plt>
test   %rax,%rax
js     11b4 <main+0x44>
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <pipe@plt>
test   %rax,%rax
js     11b8 <main+0x48>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
js     11de <main+0x6e>
mov    %rsp,%rdi
mov    %rbx,%rsi
je     11f6 <main+0x86>
call   1030 <parent_process@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
xor    %esi,%esi
jmp    11bd <main+0x4d>
mov    $0x1,%esi
lea    0xe3c(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe63(%rip),%rdi
call   1070 <perror@plt>
xor    %edi,%edi
call   1080 <exit@plt>
lea    0xe55(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
call   1050 <child_process@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [53 x i8] c"We had an error when we tried to create the pipes %d\00"
@.str.1 = [5 x i8] c"pipe\00"
@.str.2 = [38 x i8] c"We had an erorr when we tried to fork\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [2 x [2 x i32]]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %7
br label %10
10: ; preds = %26, %2
%11 = load i32, i32* %7
%12 = icmp slt i32 %11, 2
br i1 %12, label %13, label %29
13: ; preds = %10
%14 = load i32, i32* %7
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds [2 x [2 x i32]], [2 x [2 x i32]]* %9, i64 0, i64 %15
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %16, i64 0, i64 0
%18 = call i64 @pipe(i32* %17)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %13
%21 = load i32, i32* %7
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @exit(i32 0)
unreachable
25: ; preds = %13
br label %26
26: ; preds = %25
%27 = load i32, i32* %7
%28 = add nsw i32 %27, 1
store i32 %28, i32* %7
br label %10
29: ; preds = %10
%30 = call i32 (...) @fork()
store i32 %30, i32* %8
%31 = load i32, i32* %8
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %29
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %29
%37 = load i32, i32* %8
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %46
39: ; preds = %36
%40 = getelementptr inbounds [2 x [2 x i32]], [2 x [2 x i32]]* %9, i64 0, i64 0
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %40, i64 0, i64 0
%42 = getelementptr inbounds [2 x [2 x i32]], [2 x [2 x i32]]* %9, i64 0, i64 1
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %42, i64 0, i64 0
%44 = call i32 @child_process(i32* %41, i32* %43)
%45 = call i32 @exit(i32 0)
unreachable
46: ; preds = %36
%47 = getelementptr inbounds [2 x [2 x i32]], [2 x [2 x i32]]* %9, i64 0, i64 0
%48 = getelementptr inbounds [2 x i32], [2 x i32]* %47, i64 0, i64 0
%49 = getelementptr inbounds [2 x [2 x i32]], [2 x [2 x i32]]* %9, i64 0, i64 1
%50 = getelementptr inbounds [2 x i32], [2 x i32]* %49, i64 0, i64 0
%51 = call i32 @parent_process(i32* %48, i32* %50)
br label %52
52: ; preds = %46
br label %53
53: ; preds = %52
ret i32 0
}

LLVM IR (-O2):
@.str = [53 x i8] c"We had an error when we tried to create the pipes %d\00"
@.str.1 = [5 x i8] c"pipe\00"
@.str.2 = [38 x i8] c"We had an erorr when we tried to fork\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [2 x [2 x i32]]
%4 = bitcast [2 x [2 x i32]]* %3 to i8*
%5 = getelementptr inbounds [2 x [2 x i32]], [2 x [2 x i32]]* %3, i64 0, i64 0, i64 0
%6 = call i64 @pipe(i32* %5)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %13
8: ; preds = %13, %2
%9 = phi i32 [ 0, %2 ], [ 1, %13 ]
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @exit(i32 0)
unreachable
13: ; preds = %2
%14 = getelementptr inbounds [2 x [2 x i32]], [2 x [2 x i32]]* %3, i64 0, i64 1, i64 0
%15 = call i64 @pipe(i32* %14)
%16 = icmp slt i64 %15, 0
br i1 %16, label %8, label %17
17: ; preds = %13
%18 = call i32 (...) @fork()
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %17
%24 = icmp eq i32 %18, 0
br i1 %24, label %25, label %28
25: ; preds = %23
%26 = call i32 @child_process(i32* %5, i32* %14)
%27 = call i32 @exit(i32 0)
unreachable
28: ; preds = %23
%29 = call i32 @parent_process(i32* %5, i32* %14)
ret i32 0
}