#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 EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int fork();
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int /*<<< orphan*/ pipe_get(int /*<<< orphan*/, int *);
int /*<<< orphan*/ spawn(char *, char **);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  static char *cmd1[] = {"./csnake_ai", "map01_ai", NULL},
              *cmd2[] = {"./csnake", "map01", NULL};
  int p[2] = {-1, -1}, st, ret = EXIT_FAILURE;
  pid_t q[2] = {0, 0};
  if (pipe(p) == -1) {
    perror("pipe");
    goto error;
  }
  do {
    (q[0]) = fork();
    if ((q[0]) == -1) {
      perror("fork");
      goto error;
    } else if ((q[0]) == 0) {
      pipe_get(STDOUT_FILENO, p);
      spawn(argv[0], cmd1);
    }
  } while (0);
  ;
  do {
    (q[1]) = fork();
    if ((q[1]) == -1) {
      perror("fork");
      goto error;
    } else if ((q[1]) == 0) {
      pipe_get(STDIN_FILENO, p);
      spawn(argv[0], cmd2);
    }
  } while (0);
  ;
  ret = EXIT_SUCCESS;
error:
  if (p[0] != -1)
    close(p[0]);
  if (p[1] != -1)
    close(p[1]);
  if (q[1] != 0)
    waitpid(q[1], &st, 0);
  if (q[0] != 0)
    waitpid(q[0], &st, 0);
  return ret;
  (void)argc;
}


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)
mov    0xe93(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e30(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
lea    -0x18(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11e0 <main+0x60>
lea    0xe4e(%rip),%rdi
call   1090 <perror@plt>
jmp    12b2 <main+0x132>
jmp    11e5 <main+0x65>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    120a <main+0x8a>
lea    0xe29(%rip),%rdi
call   1090 <perror@plt>
jmp    12b2 <main+0x132>
cmpl   $0x0,-0x28(%rbp)
jne    1239 <main+0xb9>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1060 <pipe_get@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0x2e3c(%rip),%rsi
call   1070 <spawn@plt>
jmp    123e <main+0xbe>
jmp    1243 <main+0xc3>
jmp    1248 <main+0xc8>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    126d <main+0xed>
lea    0xdc6(%rip),%rdi
call   1090 <perror@plt>
jmp    12b2 <main+0x132>
cmpl   $0x0,-0x24(%rbp)
jne    129c <main+0x11c>
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1060 <pipe_get@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0x2df9(%rip),%rsi
call   1070 <spawn@plt>
jmp    12a1 <main+0x121>
jmp    12a6 <main+0x126>
mov    0x2d23(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
je     12c4 <main+0x144>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
cmpl   $0xffffffff,-0x14(%rbp)
je     12d6 <main+0x156>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
cmpl   $0x0,-0x24(%rbp)
je     12ee <main+0x16e>
mov    -0x24(%rbp),%edi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
cmpl   $0x0,-0x28(%rbp)
je     1306 <main+0x186>
mov    -0x28(%rbp),%edi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    -0x20(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r15
movq   $0xffffffffffffffff,0x8(%rsp)
ff ff
mov    0x2e4b(%rip),%rax
mov    (%rax),%r14d
lea    0x8(%rsp),%rdi
call   1040 <pipe@plt>
cmp    $0xffffffff,%eax
je     128b <main+0x11b>
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,%ebp
test   %eax,%eax
je     11ca <main+0x5a>
cmp    $0xffffffff,%ebp
jne    11ec <main+0x7c>
lea    0xe6d(%rip),%rdi
call   1080 <perror@plt>
xor    %ebx,%ebx
mov    $0xffffffff,%ebp
jmp    123d <main+0xcd>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1050 <pipe_get@plt>
mov    (%r15),%rdi
lea    0x2e79(%rip),%rsi
call   1060 <spawn@plt>
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,%ebx
test   %eax,%eax
je     1211 <main+0xa1>
cmp    $0xffffffff,%ebx
jne    1233 <main+0xc3>
lea    0xe24(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebx
jmp    123d <main+0xcd>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1050 <pipe_get@plt>
mov    (%r15),%rdi
lea    0x2e52(%rip),%rsi
call   1060 <spawn@plt>
mov    0x2d96(%rip),%rax
mov    (%rax),%r14d
mov    0x8(%rsp),%edi
cmp    $0xffffffff,%edi
je     124b <main+0xdb>
call   1030 <close@plt>
mov    0xc(%rsp),%edi
cmp    $0xffffffff,%edi
je     1259 <main+0xe9>
call   1030 <close@plt>
test   %ebx,%ebx
je     126b <main+0xfb>
lea    0x14(%rsp),%rsi
mov    %ebx,%edi
xor    %edx,%edx
call   1070 <waitpid@plt>
test   %ebp,%ebp
je     127d <main+0x10d>
lea    0x14(%rsp),%rsi
mov    %ebp,%edi
xor    %edx,%edx
call   1070 <waitpid@plt>
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd92(%rip),%rdi
call   1080 <perror@plt>
xor    %ebp,%ebp
xor    %ebx,%ebx
jmp    123d <main+0xcd>

LLVM IR (-O0):
@main.cmd1 = internal global [3 x i8*] [i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i32 0, i32 0), i8* null]
@.str = [12 x i8] c"./csnake_ai\00"
@.str.1 = [9 x i8] c"map01_ai\00"
@main.cmd2 = internal global [3 x i8*] [i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i32 0, i32 0), i8* null]
@.str.2 = [9 x i8] c"./csnake\00"
@.str.3 = [6 x i8] c"map01\00"
@__const.main.p = [2 x i32] [i32 -1, i32 -1]
@EXIT_FAILURE = global i32 0
@.str.4 = [5 x i8] c"pipe\00"
@.str.5 = [5 x i8] c"fork\00"
@STDOUT_FILENO = global i32 0
@STDIN_FILENO = global i32 0
@EXIT_SUCCESS = global i32 0
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 [2 x i32]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = bitcast [2 x i32]* %6 to i8*
%11 = load i32, i32* @EXIT_FAILURE
store i32 %11, i32* %8
%12 = bitcast [2 x i32]* %9 to i8*
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%14 = call i32 @pipe(i32* %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %66
18: ; preds = %2
br label %19
19: ; preds = %18
%20 = call i32 (...) @fork()
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
store i32 %20, i32* %21
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%23 = load i32, i32* %22
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %19
%26 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %66
27: ; preds = %19
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%29 = load i32, i32* %28
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %39
31: ; preds = %27
%32 = load i32, i32* @STDOUT_FILENO
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%34 = call i32 @pipe_get(i32 %32, i32* %33)
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 0
%37 = load i8*, i8** %36
%38 = call i32 @spawn(i8* %37, i8** getelementptr inbounds ([3 x i8*], [3 x i8*]* @main.cmd1, i64 0, i64 0))
br label %39
39: ; preds = %31, %27
br label %40
40: ; preds = %39
br label %41
41: ; preds = %40
br label %42
42: ; preds = %41
%43 = call i32 (...) @fork()
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
store i32 %43, i32* %44
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%46 = load i32, i32* %45
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %50
48: ; preds = %42
%49 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %66
50: ; preds = %42
%51 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%52 = load i32, i32* %51
%53 = icmp eq i32 %52, 0
br i1 %53, label %54, label %62
54: ; preds = %50
%55 = load i32, i32* @STDIN_FILENO
%56 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%57 = call i32 @pipe_get(i32 %55, i32* %56)
%58 = load i8**, i8*** %5
%59 = getelementptr inbounds i8*, i8** %58, i64 0
%60 = load i8*, i8** %59
%61 = call i32 @spawn(i8* %60, i8** getelementptr inbounds ([3 x i8*], [3 x i8*]* @main.cmd2, i64 0, i64 0))
br label %62
62: ; preds = %54, %50
br label %63
63: ; preds = %62
br label %64
64: ; preds = %63
%65 = load i32, i32* @EXIT_SUCCESS
store i32 %65, i32* %8
br label %66
66: ; preds = %64, %48, %25, %16
%67 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%68 = load i32, i32* %67
%69 = icmp ne i32 %68, -1
br i1 %69, label %70, label %74
70: ; preds = %66
%71 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%72 = load i32, i32* %71
%73 = call i32 @close(i32 %72)
br label %74
74: ; preds = %70, %66
%75 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%76 = load i32, i32* %75
%77 = icmp ne i32 %76, -1
br i1 %77, label %78, label %82
78: ; preds = %74
%79 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%80 = load i32, i32* %79
%81 = call i32 @close(i32 %80)
br label %82
82: ; preds = %78, %74
%83 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%84 = load i32, i32* %83
%85 = icmp ne i32 %84, 0
br i1 %85, label %86, label %90
86: ; preds = %82
%87 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%88 = load i32, i32* %87
%89 = call i32 @waitpid(i32 %88, i32* %7, i32 0)
br label %90
90: ; preds = %86, %82
%91 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%92 = load i32, i32* %91
%93 = icmp ne i32 %92, 0
br i1 %93, label %94, label %98
94: ; preds = %90
%95 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%96 = load i32, i32* %95
%97 = call i32 @waitpid(i32 %96, i32* %7, i32 0)
br label %98
98: ; preds = %94, %90
%99 = load i32, i32* %8
ret i32 %99
}

LLVM IR (-O2):
@main.cmd1 = internal global [3 x i8*] [i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i32 0, i32 0), i8* null]
@.str = [12 x i8] c"./csnake_ai\00"
@.str.1 = [9 x i8] c"map01_ai\00"
@main.cmd2 = internal global [3 x i8*] [i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i32 0, i32 0), i8* null]
@.str.2 = [9 x i8] c"./csnake\00"
@.str.3 = [6 x i8] c"map01\00"
@EXIT_FAILURE = global i32 0
@.str.4 = [5 x i8] c"pipe\00"
@.str.5 = [5 x i8] c"fork\00"
@STDOUT_FILENO = global i32 0
@STDIN_FILENO = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to [2 x i32]*
%5 = alloca i32
%6 = bitcast i64* %3 to i8*
store i64 -1, i64* %3
%7 = bitcast i32* %5 to i8*
%8 = load i32, i32* @EXIT_FAILURE
%9 = bitcast i64* %3 to i32*
%10 = call i32 @pipe(i32* %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %34
14: ; preds = %2
%15 = call i32 (...) @fork()
switch i32 %15, label %23 [
i32 -1, label %16
i32 0, label %18
]
16: ; preds = %14
%17 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %34
18: ; preds = %14
%19 = load i32, i32* @STDOUT_FILENO
%20 = call i32 @pipe_get(i32 %19, i32* %9)
%21 = load i8*, i8** %1
%22 = call i32 @spawn(i8* %21, i8** getelementptr inbounds ([3 x i8*], [3 x i8*]* @main.cmd1, i64 0, i64 0))
br label %23
23: ; preds = %14, %18
%24 = call i32 (...) @fork()
switch i32 %24, label %32 [
i32 -1, label %25
i32 0, label %27
]
25: ; preds = %23
%26 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %34
27: ; preds = %23
%28 = load i32, i32* @STDIN_FILENO
%29 = call i32 @pipe_get(i32 %28, i32* %9)
%30 = load i8*, i8** %1
%31 = call i32 @spawn(i8* %30, i8** getelementptr inbounds ([3 x i8*], [3 x i8*]* @main.cmd2, i64 0, i64 0))
br label %32
32: ; preds = %23, %27
%33 = load i32, i32* @EXIT_SUCCESS
br label %34
34: ; preds = %32, %25, %16, %12
%35 = phi i32 [ %8, %12 ], [ %8, %16 ], [ %8, %25 ], [ %33, %32 ]
%36 = phi i32 [ 0, %12 ], [ -1, %16 ], [ %15, %25 ], [ %15, %32 ]
%37 = phi i32 [ 0, %12 ], [ 0, %16 ], [ -1, %25 ], [ %24, %32 ]
%38 = load i32, i32* %9
%39 = icmp eq i32 %38, -1
br i1 %39, label %42, label %40
40: ; preds = %34
%41 = call i32 @close(i32 %38)
br label %42
42: ; preds = %40, %34
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%44 = load i32, i32* %43
%45 = icmp eq i32 %44, -1
br i1 %45, label %48, label %46
46: ; preds = %42
%47 = call i32 @close(i32 %44)
br label %48
48: ; preds = %46, %42
%49 = icmp eq i32 %37, 0
br i1 %49, label %52, label %50
50: ; preds = %48
%51 = call i32 @waitpid(i32 %37, i32* %5, i32 0)
br label %52
52: ; preds = %50, %48
%53 = icmp eq i32 %36, 0
br i1 %53, label %56, label %54
54: ; preds = %52
%55 = call i32 @waitpid(i32 %36, i32* %5, i32 0)
br label %56
56: ; preds = %54, %52
ret i32 %35
}