#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*/ ssize_t;
typedef int pid_t;
typedef int /*<<< orphan*/ buff;

/* Variables and functions */
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execv(char *, char **);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, ...);
int /*<<< orphan*/ stockfish_read_until(int, char *, int, char *);
int /*<<< orphan*/ stockfish_write(int, char *);
int /*<<< orphan*/ strcat(char *, char *);
scalar_t__ strcmp(char *, char *);
int strlen(char *);
scalar_t__ strstr(char *, char *);
int main(int ac, char **av) {
  int rc = -1;
  int i;
  ssize_t n;
  char colorchar = ' ';
  pid_t childpid;
  char buff[4096];
  char fen[512];
  char *args_stockfish[] = {"/usr/games/stockfish", NULL};
  char *search_move;
  int fds_down[2];
  int fds_up[2];
  if (ac < 3) {
    printf("syntax: %s <move|\"all\"> <fen>\n", av[0]);
    goto cleanup;
  }
  search_move = av[1];
  fen[0] = '\0';
  for (i = 2; i < ac; ++i) {
    strcat(fen, av[i]);
    strcat(fen, " ");
  }
  fen[strlen(fen) - 1] = '\0';
  pipe(fds_down);
  pipe(fds_up);
  childpid = fork();
  if (childpid == -1) {
    printf("ERROR: fork()\n");
    goto cleanup;
  }
  if (childpid == 0) {
    close(fds_down[1]);
    close(fds_up[0]);
    i = dup2(fds_down[0], STDIN_FILENO);
    if (i >= 0) {
      printf("dup2() on STDIN returned: %d\n", i);
    } else {
      perror("dup2()");
      goto cleanup;
    }
    i = dup2(fds_up[1], STDOUT_FILENO);
    if (i >= 0) {
      printf("dup2() on STDOUT returned: %d\n", i);
    } else {
      perror("dup2()");
      goto cleanup;
    }
    execv("/usr/games/stockfish", args_stockfish);
    printf("if I made it this far, stockfish isn't running, fuck!\n");
  } else {
    printf("spawned child process: %d\n", childpid);
    close(fds_down[0]);
    close(fds_up[1]);
    stockfish_read_until(fds_up[0], buff, sizeof(buff), "\n");
    printf("%s\n", buff);
    stockfish_write(fds_down[1], "uci");
    if (strcmp(search_move, "all") == 0) {
      stockfish_write(fds_down[1], "setoption name MultiPV value 3");
    }
    sprintf(buff, "position fen %s", fen);
    stockfish_write(fds_down[1], buff);
    sprintf(buff, "go movetime 2000");
    if (strcmp(search_move, "all") != 0) {
      sprintf(buff, "go movetime 2000 searchmoves %s", search_move);
    }
    stockfish_write(fds_down[1], buff);
    while (1) {
      stockfish_read_until(fds_up[0], buff, sizeof(buff), "\n");
      printf("%s\n", buff);
      if (strstr(buff, "bestmove")) {
        printf("done! breaking!\n");
        break;
      }
    }
    printf("done\n");
  }
cleanup:
  return rc;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1260,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movb   $0x20,-0x1d(%rbp)
mov    0x2bf5(%rip),%rax
mov    %rax,-0x1240(%rbp)
mov    0x2bef(%rip),%rax
mov    %rax,-0x1238(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    1244 <main+0x64>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xddd(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1578 <main+0x398>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x1248(%rbp)
movb   $0x0,-0x1230(%rbp)
movl   $0x2,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    12a6 <main+0xc6>
lea    -0x1230(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
call   10c0 <strcat@plt>
lea    -0x1230(%rbp),%rdi
lea    0xda1(%rip),%rsi
call   10c0 <strcat@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1261 <main+0x81>
lea    -0x1230(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0x1230(%rbp,%rax,1)
lea    -0x1250(%rbp),%rdi
call   1070 <pipe@plt>
lea    -0x1258(%rbp),%rdi
call   1070 <pipe@plt>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    12fe <main+0x11e>
lea    0xd44(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1578 <main+0x398>
cmpl   $0x0,-0x24(%rbp)
jne    13d4 <main+0x1f4>
mov    -0x124c(%rbp),%edi
call   1060 <close@plt>
mov    -0x1258(%rbp),%edi
call   1060 <close@plt>
mov    -0x1250(%rbp),%edi
mov    0x2cad(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jl     1355 <main+0x175>
mov    -0x18(%rbp),%esi
lea    0xcfc(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1366 <main+0x186>
lea    0xd07(%rip),%rdi
call   10b0 <perror@plt>
jmp    1578 <main+0x398>
mov    -0x1254(%rbp),%edi
mov    0x2c7d(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jl     139d <main+0x1bd>
mov    -0x18(%rbp),%esi
lea    0xcd9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    13ae <main+0x1ce>
lea    0xcbf(%rip),%rdi
call   10b0 <perror@plt>
jmp    1578 <main+0x398>
lea    -0x1240(%rbp),%rsi
lea    0xc44(%rip),%rdi
call   10e0 <execv@plt>
lea    0xcc1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1573 <main+0x393>
mov    -0x24(%rbp),%esi
lea    0xce2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x1250(%rbp),%edi
call   1060 <close@plt>
mov    -0x1254(%rbp),%edi
call   1060 <close@plt>
mov    -0x1258(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    $0x1000,%edx
lea    0xd4b(%rip),%rcx
call   10a0 <stockfish_read_until@plt>
lea    -0x1030(%rbp),%rsi
lea    0xcb4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x124c(%rbp),%edi
lea    0xca4(%rip),%rsi
call   1080 <stockfish_write@plt>
mov    -0x1248(%rbp),%rdi
lea    0xc95(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    146f <main+0x28f>
mov    -0x124c(%rbp),%edi
lea    0xc7d(%rip),%rsi
call   1080 <stockfish_write@plt>
lea    -0x1030(%rbp),%rdi
lea    -0x1230(%rbp),%rdx
lea    0xc82(%rip),%rsi
mov    $0x0,%al
call   10d0 <sprintf@plt>
mov    -0x124c(%rbp),%edi
lea    -0x1030(%rbp),%rsi
call   1080 <stockfish_write@plt>
lea    -0x1030(%rbp),%rdi
lea    0xc6b(%rip),%rsi
mov    $0x0,%al
call   10d0 <sprintf@plt>
mov    -0x1248(%rbp),%rdi
lea    0xc23(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     14eb <main+0x30b>
lea    -0x1030(%rbp),%rdi
mov    -0x1248(%rbp),%rdx
lea    0xc43(%rip),%rsi
mov    $0x0,%al
call   10d0 <sprintf@plt>
mov    -0x124c(%rbp),%edi
lea    -0x1030(%rbp),%rsi
call   1080 <stockfish_write@plt>
mov    -0x1258(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    $0x1000,%edx
lea    0xc49(%rip),%rcx
call   10a0 <stockfish_read_until@plt>
lea    -0x1030(%rbp),%rsi
lea    0xbb2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x1030(%rbp),%rdi
lea    0xc09(%rip),%rsi
call   1100 <strstr@plt>
cmp    $0x0,%rax
je     1560 <main+0x380>
lea    0xbfc(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1565 <main+0x385>
jmp    14fd <main+0x31d>
lea    0xbf5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1578 <main+0x398>
mov    -0x14(%rbp),%eax
add    $0x1260,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1228,%rsp
mov    %rsi,%r15
movaps 0x2bf5(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
cmp    $0x2,%edi
jg     122b <main+0x3b>
mov    (%r15),%rsi
lea    0xdf6(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1494 <main+0x2a4>
mov    0x8(%r15),%r14
movb   $0x0,0x20(%rsp)
mov    %edi,%ebx
mov    $0x2,%ebp
lea    0x20(%rsp),%r13
lea    0xded(%rip),%r12
nopw   0x0(%rax,%rax,1)
mov    (%r15,%rbp,8),%rsi
mov    %r13,%rdi
call   10d0 <strcat@plt>
mov    %r13,%rdi
mov    %r12,%rsi
call   10d0 <strcat@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1250 <main+0x60>
mov    %r13,%rdi
call   1040 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%r13,1)
mov    %rsp,%rdi
call   1080 <pipe@plt>
lea    0x8(%rsp),%rdi
call   1080 <pipe@plt>
xor    %eax,%eax
call   1100 <fork@plt>
test   %eax,%eax
je     12b2 <main+0xc2>
cmp    $0xffffffff,%eax
jne    1334 <main+0x144>
lea    0xea4(%rip),%rdi
jmp    148f <main+0x29f>
mov    0x4(%rsp),%edi
call   1070 <close@plt>
mov    0x8(%rsp),%edi
call   1070 <close@plt>
mov    (%rsp),%edi
mov    0x2d0a(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
test   %eax,%eax
js     14ab <main+0x2bb>
lea    0xd52(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0xc(%rsp),%edi
mov    0x2cf8(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
test   %eax,%eax
js     14ab <main+0x2bb>
lea    0xd4d(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xce2(%rip),%rdi
lea    0x10(%rsp),%rsi
call   10f0 <execv@plt>
lea    0xdec(%rip),%rdi
jmp    148f <main+0x29f>
lea    0xd3f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    (%rsp),%edi
call   1070 <close@plt>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
mov    0x8(%rsp),%edi
lea    0xcd2(%rip),%rcx
lea    0x220(%rsp),%rbx
mov    %rbx,%rsi
mov    $0x1000,%edx
call   10b0 <stockfish_read_until@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    0x4(%rsp),%edi
lea    0xd0d(%rip),%rsi
call   1090 <stockfish_write@plt>
lea    0xd05(%rip),%rsi
mov    %r14,%rdi
call   10a0 <strcmp@plt>
test   %rax,%rax
jne    13b1 <main+0x1c1>
mov    0x4(%rsp),%edi
lea    0xcf1(%rip),%rsi
call   1090 <stockfish_write@plt>
lea    0xd04(%rip),%rsi
lea    0x220(%rsp),%rbx
lea    0x20(%rsp),%rdx
mov    %rbx,%rdi
xor    %eax,%eax
call   10e0 <sprintf@plt>
mov    0x4(%rsp),%edi
mov    %rbx,%rsi
call   1090 <stockfish_write@plt>
movups 0xcea(%rip),%xmm0
movaps %xmm0,0x220(%rsp)
movb   $0x0,0x230(%rsp)
lea    0xca0(%rip),%rsi
mov    %r14,%rdi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     141f <main+0x22f>
lea    0xcd0(%rip),%rsi
lea    0x220(%rsp),%rdi
mov    %r14,%rdx
xor    %eax,%eax
call   10e0 <sprintf@plt>
mov    0x4(%rsp),%edi
lea    0x220(%rsp),%rbx
mov    %rbx,%rsi
call   1090 <stockfish_write@plt>
lea    0xbf8(%rip),%r14
lea    0xcbc(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
mov    %r14,%rcx
call   10b0 <stockfish_read_until@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1110 <strstr@plt>
test   %rax,%rax
je     1450 <main+0x260>
lea    0xc83(%rip),%rdi
call   1030 <puts@plt>
lea    0xc87(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x1228,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xba2(%rip),%rdi
call   10c0 <perror@plt>
jmp    1494 <main+0x2a4>

LLVM IR (-O0):
@.str = [21 x i8] c"/usr/games/stockfish\00"
@__const.main.args_stockfish = [2 x i8*] [i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i32 0, i32 0), i8* null]
@.str.1 = [31 x i8] c"syntax: %s <move|\22all\22> <fen>\0A\00"
@.str.2 = [2 x i8] c" \00"
@.str.3 = [15 x i8] c"ERROR: fork()\0A\00"
@STDIN_FILENO = global i32 0
@.str.4 = [30 x i8] c"dup2() on STDIN returned: %d\0A\00"
@.str.5 = [7 x i8] c"dup2()\00"
@STDOUT_FILENO = global i32 0
@.str.6 = [31 x i8] c"dup2() on STDOUT returned: %d\0A\00"
@.str.7 = [55 x i8] c"if I made it this far, stockfish isn't running, fuck!\0A\00"
@.str.8 = [27 x i8] c"spawned child process: %d\0A\00"
@.str.9 = [2 x i8] c"\0A\00"
@.str.10 = [4 x i8] c"%s\0A\00"
@.str.11 = [4 x i8] c"uci\00"
@.str.12 = [4 x i8] c"all\00"
@.str.13 = [31 x i8] c"setoption name MultiPV value 3\00"
@.str.14 = [16 x i8] c"position fen %s\00"
@.str.15 = [17 x i8] c"go movetime 2000\00"
@.str.16 = [32 x i8] c"go movetime 2000 searchmoves %s\00"
@.str.17 = [9 x i8] c"bestmove\00"
@.str.18 = [17 x i8] c"done! breaking!\0A\00"
@.str.19 = [6 x i8] c"done\0A\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 i8
%10 = alloca i32
%11 = alloca [4096 x i8]
%12 = alloca [512 x i8]
%13 = alloca [2 x i8*]
%14 = alloca i8*
%15 = alloca [2 x i32]
%16 = alloca [2 x i32]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %6
store i8 32, i8* %9
%17 = bitcast [2 x i8*]* %13 to i8*
%18 = load i32, i32* %4
%19 = icmp slt i32 %18, 3
br i1 %19, label %20, label %25
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 0
%23 = load i8*, i8** %22
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %23)
br label %162
25: ; preds = %2
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
store i8* %28, i8** %14
%29 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
store i8 0, i8* %29
store i32 2, i32* %7
br label %30
30: ; preds = %44, %25
%31 = load i32, i32* %7
%32 = load i32, i32* %4
%33 = icmp slt i32 %31, %32
br i1 %33, label %34, label %47
34: ; preds = %30
%35 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%36 = load i8**, i8*** %5
%37 = load i32, i32* %7
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8*, i8** %36, i64 %38
%40 = load i8*, i8** %39
%41 = call i32 @strcat(i8* %35, i8* %40)
%42 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%43 = call i32 @strcat(i8* %42, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %44
44: ; preds = %34
%45 = load i32, i32* %7
%46 = add nsw i32 %45, 1
store i32 %46, i32* %7
br label %30
47: ; preds = %30
%48 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%49 = call i32 @strlen(i8* %48)
%50 = sub nsw i32 %49, 1
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 %51
store i8 0, i8* %52
%53 = getelementptr inbounds [2 x i32], [2 x i32]* %15, i64 0, i64 0
%54 = call i32 @pipe(i32* %53)
%55 = getelementptr inbounds [2 x i32], [2 x i32]* %16, i64 0, i64 0
%56 = call i32 @pipe(i32* %55)
%57 = call i32 (...) @fork()
store i32 %57, i32* %10
%58 = load i32, i32* %10
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %62
60: ; preds = %47
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %162
62: ; preds = %47
%63 = load i32, i32* %10
%64 = icmp eq i32 %63, 0
br i1 %64, label %65, label %99
65: ; preds = %62
%66 = getelementptr inbounds [2 x i32], [2 x i32]* %15, i64 0, i64 1
%67 = load i32, i32* %66
%68 = call i32 @close(i32 %67)
%69 = getelementptr inbounds [2 x i32], [2 x i32]* %16, i64 0, i64 0
%70 = load i32, i32* %69
%71 = call i32 @close(i32 %70)
%72 = getelementptr inbounds [2 x i32], [2 x i32]* %15, i64 0, i64 0
%73 = load i32, i32* %72
%74 = load i32, i32* @STDIN_FILENO
%75 = call i32 @dup2(i32 %73, i32 %74)
store i32 %75, i32* %7
%76 = load i32, i32* %7
%77 = icmp sge i32 %76, 0
br i1 %77, label %78, label %81
78: ; preds = %65
%79 = load i32, i32* %7
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i32 %79)
br label %83
81: ; preds = %65
%82 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %162
83: ; preds = %78
%84 = getelementptr inbounds [2 x i32], [2 x i32]* %16, i64 0, i64 1
%85 = load i32, i32* %84
%86 = load i32, i32* @STDOUT_FILENO
%87 = call i32 @dup2(i32 %85, i32 %86)
store i32 %87, i32* %7
%88 = load i32, i32* %7
%89 = icmp sge i32 %88, 0
br i1 %89, label %90, label %93
90: ; preds = %83
%91 = load i32, i32* %7
%92 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), i32 %91)
br label %95
93: ; preds = %83
%94 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %162
95: ; preds = %90
%96 = getelementptr inbounds [2 x i8*], [2 x i8*]* %13, i64 0, i64 0
%97 = call i32 @execv(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8** %96)
%98 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.7, i64 0, i64 0))
br label %161
99: ; preds = %62
%100 = load i32, i32* %10
%101 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.8, i64 0, i64 0), i32 %100)
%102 = getelementptr inbounds [2 x i32], [2 x i32]* %15, i64 0, i64 0
%103 = load i32, i32* %102
%104 = call i32 @close(i32 %103)
%105 = getelementptr inbounds [2 x i32], [2 x i32]* %16, i64 0, i64 1
%106 = load i32, i32* %105
%107 = call i32 @close(i32 %106)
%108 = getelementptr inbounds [2 x i32], [2 x i32]* %16, i64 0, i64 0
%109 = load i32, i32* %108
%110 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%111 = call i32 @stockfish_read_until(i32 %109, i8* %110, i32 4096, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0))
%112 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%113 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.10, i64 0, i64 0), i8* %112)
%114 = getelementptr inbounds [2 x i32], [2 x i32]* %15, i64 0, i64 1
%115 = load i32, i32* %114
%116 = call i32 @stockfish_write(i32 %115, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.11, i64 0, i64 0))
%117 = load i8*, i8** %14
%118 = call i64 @strcmp(i8* %117, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.12, i64 0, i64 0))
%119 = icmp eq i64 %118, 0
br i1 %119, label %120, label %124
120: ; preds = %99
%121 = getelementptr inbounds [2 x i32], [2 x i32]* %15, i64 0, i64 1
%122 = load i32, i32* %121
%123 = call i32 @stockfish_write(i32 %122, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.13, i64 0, i64 0))
br label %124
124: ; preds = %120, %99
%125 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%126 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%127 = call i32 (i8*, i8*, ...) @sprintf(i8* %125, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.14, i64 0, i64 0), i8* %126)
%128 = getelementptr inbounds [2 x i32], [2 x i32]* %15, i64 0, i64 1
%129 = load i32, i32* %128
%130 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%131 = call i32 @stockfish_write(i32 %129, i8* %130)
%132 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%133 = call i32 (i8*, i8*, ...) @sprintf(i8* %132, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.15, i64 0, i64 0))
%134 = load i8*, i8** %14
%135 = call i64 @strcmp(i8* %134, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.12, i64 0, i64 0))
%136 = icmp ne i64 %135, 0
br i1 %136, label %137, label %141
137: ; preds = %124
%138 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%139 = load i8*, i8** %14
%140 = call i32 (i8*, i8*, ...) @sprintf(i8* %138, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.16, i64 0, i64 0), i8* %139)
br label %141
141: ; preds = %137, %124
%142 = getelementptr inbounds [2 x i32], [2 x i32]* %15, i64 0, i64 1
%143 = load i32, i32* %142
%144 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%145 = call i32 @stockfish_write(i32 %143, i8* %144)
br label %146
146: ; preds = %141, %158
%147 = getelementptr inbounds [2 x i32], [2 x i32]* %16, i64 0, i64 0
%148 = load i32, i32* %147
%149 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%150 = call i32 @stockfish_read_until(i32 %148, i8* %149, i32 4096, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0))
%151 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%152 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.10, i64 0, i64 0), i8* %151)
%153 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%154 = call i64 @strstr(i8* %153, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.17, i64 0, i64 0))
%155 = icmp ne i64 %154, 0
br i1 %155, label %156, label %158
156: ; preds = %146
%157 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.18, i64 0, i64 0))
br label %159
158: ; preds = %146
br label %146
159: ; preds = %156
%160 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.19, i64 0, i64 0))
br label %161
161: ; preds = %159, %95
br label %162
162: ; preds = %161, %93, %81, %60, %20
%163 = load i32, i32* %6
ret i32 %163
}

LLVM IR (-O2):
@.str = [21 x i8] c"/usr/games/stockfish\00"
@__const.main.args_stockfish = [2 x i8*] [i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i32 0, i32 0), i8* null]
@.str.1 = [31 x i8] c"syntax: %s <move|\22all\22> <fen>\0A\00"
@.str.2 = [2 x i8] c" \00"
@STDIN_FILENO = global i32 0
@.str.4 = [30 x i8] c"dup2() on STDIN returned: %d\0A\00"
@.str.5 = [7 x i8] c"dup2()\00"
@STDOUT_FILENO = global i32 0
@.str.6 = [31 x i8] c"dup2() on STDOUT returned: %d\0A\00"
@.str.8 = [27 x i8] c"spawned child process: %d\0A\00"
@.str.9 = [2 x i8] c"\0A\00"
@.str.11 = [4 x i8] c"uci\00"
@.str.12 = [4 x i8] c"all\00"
@.str.13 = [31 x i8] c"setoption name MultiPV value 3\00"
@.str.14 = [16 x i8] c"position fen %s\00"
@.str.15 = [17 x i8] c"go movetime 2000\00"
@.str.16 = [32 x i8] c"go movetime 2000 searchmoves %s\00"
@.str.17 = [9 x i8] c"bestmove\00"
@str = [16 x i8] c"done! breaking!\00"
@str.20 = [5 x i8] c"done\00"
@str.21 = [54 x i8] c"if I made it this far, stockfish isn't running, fuck!\00"
@str.22 = [14 x i8] c"ERROR: fork()\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4096 x i8]
%4 = alloca [512 x i8]
%5 = alloca [2 x i8*]
%6 = alloca [2 x i32]
%7 = alloca [2 x i32]
%8 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%9 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%10 = bitcast [2 x i8*]* %5 to i8*
%11 = bitcast [2 x i32]* %6 to i8*
%12 = bitcast [2 x i32]* %7 to i8*
%13 = icmp slt i32 %0, 3
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i8*, i8** %1
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %15)
br label %105
17: ; preds = %2
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = load i8*, i8** %18
store i8 0, i8* %9
%20 = zext i32 %0 to i64
br label %21
21: ; preds = %17, %21
%22 = phi i64 [ 2, %17 ], [ %27, %21 ]
%23 = getelementptr inbounds i8*, i8** %1, i64 %22
%24 = load i8*, i8** %23
%25 = call i32 @strcat(i8* %9, i8* %24)
%26 = call i32 @strcat(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%27 = add nuw nsw i64 %22, 1
%28 = icmp eq i64 %27, %20
br i1 %28, label %29, label %21
29: ; preds = %21
%30 = call i32 @strlen(i8* %9)
%31 = add nsw i32 %30, -1
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 %32
store i8 0, i8* %33
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%35 = call i32 @pipe(i32* %34)
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%37 = call i32 @pipe(i32* %36)
%38 = call i32 (...) @fork()
switch i32 %38, label %67 [
i32 -1, label %39
i32 0, label %41
]
39: ; preds = %29
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.22, i64 0, i64 0))
br label %105
41: ; preds = %29
%42 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%43 = load i32, i32* %42
%44 = call i32 @close(i32 %43)
%45 = load i32, i32* %36
%46 = call i32 @close(i32 %45)
%47 = load i32, i32* %34
%48 = load i32, i32* @STDIN_FILENO
%49 = call i32 @dup2(i32 %47, i32 %48)
%50 = icmp sgt i32 %49, -1
br i1 %50, label %51, label %58
51: ; preds = %41
%52 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i32 %49)
%53 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%54 = load i32, i32* %53
%55 = load i32, i32* @STDOUT_FILENO
%56 = call i32 @dup2(i32 %54, i32 %55)
%57 = icmp sgt i32 %56, -1
br i1 %57, label %60, label %65
58: ; preds = %41
%59 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %105
60: ; preds = %51
%61 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), i32 %56)
%62 = getelementptr inbounds [2 x i8*], [2 x i8*]* %5, i64 0, i64 0
%63 = call i32 @execv(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8** %62)
%64 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @str.21, i64 0, i64 0))
br label %105
65: ; preds = %51
%66 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %105
67: ; preds = %29
%68 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.8, i64 0, i64 0), i32 %38)
%69 = load i32, i32* %34
%70 = call i32 @close(i32 %69)
%71 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%72 = load i32, i32* %71
%73 = call i32 @close(i32 %72)
%74 = load i32, i32* %36
%75 = call i32 @stockfish_read_until(i32 %74, i8* %8, i32 4096, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0))
%76 = call i32 @puts(i8* %8)
%77 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%78 = load i32, i32* %77
%79 = call i32 @stockfish_write(i32 %78, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.11, i64 0, i64 0))
%80 = call i64 @strcmp(i8* %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.12, i64 0, i64 0))
%81 = icmp eq i64 %80, 0
br i1 %81, label %82, label %85
82: ; preds = %67
%83 = load i32, i32* %77
%84 = call i32 @stockfish_write(i32 %83, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.13, i64 0, i64 0))
br label %85
85: ; preds = %82, %67
%86 = call i32 (i8*, i8*, ...) @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.14, i64 0, i64 0), i8* %9)
%87 = load i32, i32* %77
%88 = call i32 @stockfish_write(i32 %87, i8* %8)
%89 = call i64 @strcmp(i8* %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.12, i64 0, i64 0))
%90 = icmp eq i64 %89, 0
br i1 %90, label %93, label %91
91: ; preds = %85
%92 = call i32 (i8*, i8*, ...) @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.16, i64 0, i64 0), i8* %19)
br label %93
93: ; preds = %91, %85
%94 = load i32, i32* %77
%95 = call i32 @stockfish_write(i32 %94, i8* %8)
br label %96
96: ; preds = %96, %93
%97 = load i32, i32* %36
%98 = call i32 @stockfish_read_until(i32 %97, i8* %8, i32 4096, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0))
%99 = call i32 @puts(i8* %8)
%100 = call i64 @strstr(i8* %8, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.17, i64 0, i64 0))
%101 = icmp eq i64 %100, 0
br i1 %101, label %96, label %102
102: ; preds = %96
%103 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%104 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str.20, i64 0, i64 0))
br label %105
105: ; preds = %60, %102, %65, %58, %39, %14
ret i32 -1
}