#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 atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
int open_walnut_console(int, char *, char *);
int /*<<< orphan*/ printf(char *);
int recv_fd(int);
int recv_msg(int, char **, char **);
scalar_t__ recv_msg_len(int, char **, char **, int);
int send_msg(int, char *, char *);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
int do_console(int sd, int rcv_cmd) {
  int ret = -1;
  char *code = NULL;
  char *rmsg = NULL;
  char *cmd = NULL, *args = NULL;
  if (recv_msg_len(sd, &code, &rmsg, 8) == -1) {
    fprintf(stderr, "Invalid or no response received\n");
    goto out;
  }
  if (strcmp(code, "0") != 0) {
    char *code2 = NULL, *rmsg2 = NULL;
    if (recv_msg(sd, &code2, &rmsg2) == -1)
      fprintf(stderr, "Error communicating with walnutD\n");
    else
      fprintf(stderr, "%s%s %s\n", rmsg, code2, rmsg2);
    free(code2);
    free(rmsg2);
    goto out;
  }
  if (rcv_cmd) {
    char *L, *cmdlen_str = NULL;
    int cmdlen = 0;
    if (recv_msg_len(sd, &L, &cmdlen_str, 6) < 0) {
      fprintf(stderr, "Error communicating with walnutD\n");
      goto out;
    }
    if (*L != 'L') {
      fprintf(stderr, "something's fishy with command (%s)\n", cmdlen_str);
      goto out;
    }
    cmdlen = atoi(cmdlen_str);
    free(L);
    free(cmdlen_str);
    if (recv_msg_len(sd, &cmd, &args, cmdlen) < 0) {
      fprintf(stderr, "Unable to receive command\n");
      cmd = args = NULL;
      goto out;
    }
    if (!cmd || !*cmd) {
      fprintf(stderr, "invalid command: dropping to "
                      "standard console\n");
      free(cmd);
      free(args);
      cmd = args = NULL;
    }
  }
  if (send_msg(sd, "0 %s", "ready") == -1) {
    fprintf(stderr, "Handshake error in console setup!\n");
    goto out;
  }
  int fd = recv_fd(sd);
  if (fd == -1) {
    fprintf(stderr, "Failed to open console\n");
    goto out;
  }
  if (!cmd)
    printf("Console opened. Use Ctrl-D to exit.\n"
           "Press enter if you don't see a prompt.\n");
  ret = open_walnut_console(fd, cmd, args);
  if (ret < 0)
    fprintf(stderr, "Failed to open console\n");
  close(fd);
out:
  free(cmd);
  free(args);
  free(code);
  free(rmsg);
  return ret;
}


x86 assembly (clang, -O0):
<do_console>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x20(%rbp),%rdx
mov    $0x8,%ecx
call   1070 <recv_msg_len@plt>
cmp    $0xffffffffffffffff,%rax
jne    1220 <do_console+0x70>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
lea    0xdec(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
jmp    14b9 <do_console+0x309>
mov    -0x18(%rbp),%rdi
lea    0xdf6(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     12b9 <do_console+0x109>
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x40(%rbp),%rdx
call   1050 <recv_msg@plt>
cmp    $0xffffffff,%eax
jne    127f <do_console+0xcf>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
lea    0xdb0(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
jmp    12a2 <do_console+0xf2>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
mov    -0x38(%rbp),%rcx
mov    -0x40(%rbp),%r8
lea    0xdaa(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
jmp    14b9 <do_console+0x309>
cmpl   $0x0,-0x8(%rbp)
je     13fa <do_console+0x24a>
movq   $0x0,-0x50(%rbp)
movl   $0x0,-0x54(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x48(%rbp),%rsi
lea    -0x50(%rbp),%rdx
mov    $0x6,%ecx
call   1070 <recv_msg_len@plt>
cmp    $0x0,%rax
jge    130d <do_console+0x15d>
mov    0x2ce8(%rip),%rax
mov    (%rax),%edi
lea    0xd22(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
jmp    14b9 <do_console+0x309>
mov    -0x48(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x4c,%eax
je     133d <do_console+0x18d>
mov    0x2cbc(%rip),%rax
mov    (%rax),%edi
mov    -0x50(%rbp),%rdx
lea    0xd1d(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
jmp    14b9 <do_console+0x309>
mov    -0x50(%rbp),%rdi
call   10d0 <atoi@plt>
mov    %eax,-0x54(%rbp)
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x50(%rbp),%rdi
call   1030 <free@plt>
mov    -0x4(%rbp),%edi
mov    -0x54(%rbp),%ecx
lea    -0x28(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   1070 <recv_msg_len@plt>
cmp    $0x0,%rax
jge    13a4 <do_console+0x1f4>
mov    0x2c61(%rip),%rax
mov    (%rax),%edi
lea    0xceb(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
jmp    14b9 <do_console+0x309>
cmpq   $0x0,-0x28(%rbp)
je     13bc <do_console+0x20c>
mov    -0x28(%rbp),%rax
cmpb   $0x0,(%rax)
jne    13f5 <do_console+0x245>
mov    0x2c1d(%rip),%rax
mov    (%rax),%edi
lea    0xcc2(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
jmp    13fa <do_console+0x24a>
mov    -0x4(%rbp),%edi
lea    0xcb9(%rip),%rsi
lea    0xcb7(%rip),%rdx
call   10c0 <send_msg@plt>
cmp    $0xffffffff,%eax
jne    1435 <do_console+0x285>
mov    0x2bc0(%rip),%rax
mov    (%rax),%edi
lea    0xc9f(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
jmp    14b9 <do_console+0x309>
mov    -0x4(%rbp),%edi
call   10b0 <recv_fd@plt>
mov    %eax,-0x58(%rbp)
cmpl   $0xffffffff,-0x58(%rbp)
jne    1466 <do_console+0x2b6>
mov    0x2b8f(%rip),%rax
mov    (%rax),%edi
lea    0xc91(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
jmp    14b9 <do_console+0x309>
cmpq   $0x0,-0x28(%rbp)
jne    147d <do_console+0x2cd>
lea    0xc8b(%rip),%rdi
call   1060 <printf@plt>
mov    -0x58(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1040 <open_walnut_console@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    14b1 <do_console+0x301>
mov    0x2b3f(%rip),%rax
mov    (%rax),%edi
lea    0xc41(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    -0x58(%rbp),%edi
call   1080 <close@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0xc(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_console>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %esi,%ebp
mov    %edi,%ebx
movq   $0x0,0x28(%rsp)
movq   $0x0,0x20(%rsp)
movq   $0x0,0x8(%rsp)
movq   $0x0,0x10(%rsp)
lea    0x28(%rsp),%rsi
lea    0x20(%rsp),%rdx
mov    $0x8,%ecx
call   1070 <recv_msg_len@plt>
cmp    $0xffffffffffffffff,%rax
je     1263 <do_console+0xb3>
mov    0x28(%rsp),%rdi
lea    0xe1b(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     127f <do_console+0xcf>
movq   $0x0,0x18(%rsp)
movq   $0x0,(%rsp)
lea    0x18(%rsp),%rsi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1060 <recv_msg@plt>
mov    0x2da9(%rip),%rcx
mov    (%rcx),%edi
cmp    $0xffffffff,%eax
je     13be <do_console+0x20e>
mov    0x20(%rsp),%rdx
mov    0x18(%rsp),%rcx
mov    (%rsp),%r8
lea    0xdee(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
jmp    13cc <do_console+0x21c>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
lea    0xd8d(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
jmp    13df <do_console+0x22f>
test   %ebp,%ebp
je     1341 <do_console+0x191>
movq   $0x0,(%rsp)
lea    0x18(%rsp),%rsi
mov    %rsp,%rdx
mov    %ebx,%edi
mov    $0x6,%ecx
call   1070 <recv_msg_len@plt>
test   %rax,%rax
js     1417 <do_console+0x267>
mov    0x18(%rsp),%rbp
cmpb   $0x4c,0x0(%rbp)
jne    142c <do_console+0x27c>
mov    (%rsp),%rdi
call   10d0 <atoi@plt>
mov    %eax,%r14d
mov    %rbp,%rdi
call   1030 <free@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
lea    0x8(%rsp),%rsi
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
mov    %r14d,%ecx
call   1070 <recv_msg_len@plt>
test   %rax,%rax
js     1473 <do_console+0x2c3>
mov    0x8(%rsp),%rax
test   %rax,%rax
je     1304 <do_console+0x154>
cmpb   $0x0,(%rax)
jne    1341 <do_console+0x191>
mov    0x2cd5(%rip),%rax
mov    (%rax),%edi
lea    0xd7a(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
movq   $0x0,0x10(%rsp)
movq   $0x0,0x8(%rsp)
lea    0xd75(%rip),%rsi
lea    0xd73(%rip),%rdx
mov    %ebx,%edi
call   10c0 <send_msg@plt>
cmp    $0xffffffff,%eax
je     1449 <do_console+0x299>
mov    %ebx,%edi
call   10b0 <recv_fd@plt>
cmp    $0xffffffff,%eax
je     145e <do_console+0x2ae>
mov    %eax,%ebp
mov    0x8(%rsp),%rsi
test   %rsi,%rsi
jne    138c <do_console+0x1dc>
lea    0xd81(%rip),%rdi
call   1050 <puts@plt>
mov    0x8(%rsp),%rsi
mov    0x10(%rsp),%rdx
mov    %ebp,%edi
call   1040 <open_walnut_console@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    13b5 <do_console+0x205>
mov    0x2c3b(%rip),%rax
mov    (%rax),%edi
lea    0xd3d(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    %ebp,%edi
call   1080 <close@plt>
jmp    13e4 <do_console+0x234>
lea    0xc5e(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    0x18(%rsp),%rdi
call   1030 <free@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    $0xffffffff,%ebx
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
mov    0x28(%rsp),%rdi
call   1030 <free@plt>
mov    0x20(%rsp),%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2bc2(%rip),%rax
mov    (%rax),%edi
lea    0xbfc(%rip),%rsi
jmp    1273 <do_console+0xc3>
mov    0x2bad(%rip),%rax
mov    (%rax),%edi
mov    (%rsp),%rdx
lea    0xc0e(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
jmp    13df <do_console+0x22f>
mov    0x2b90(%rip),%rax
mov    (%rax),%edi
lea    0xc6f(%rip),%rsi
jmp    1273 <do_console+0xc3>
mov    0x2b7b(%rip),%rax
mov    (%rax),%edi
lea    0xc7d(%rip),%rsi
jmp    1273 <do_console+0xc3>
mov    0x2b66(%rip),%rax
mov    (%rax),%edi
lea    0xbf0(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
movq   $0x0,0x10(%rsp)
movq   $0x0,0x8(%rsp)
jmp    13df <do_console+0x22f>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [33 x i8] c"Invalid or no response received\0A\00"
@.str.1 = [2 x i8] c"0\00"
@.str.2 = [34 x i8] c"Error communicating with walnutD\0A\00"
@.str.3 = [9 x i8] c"%s%s %s\0A\00"
@.str.4 = [37 x i8] c"something's fishy with command (%s)\0A\00"
@.str.5 = [27 x i8] c"Unable to receive command\0A\00"
@.str.6 = [47 x i8] c"invalid command: dropping to standard console\0A\00"
@.str.7 = [5 x i8] c"0 %s\00"
@.str.8 = [6 x i8] c"ready\00"
@.str.9 = [35 x i8] c"Handshake error in console setup!\0A\00"
@.str.10 = [24 x i8] c"Failed to open console\0A\00"
@.str.11 = [76 x i8] c"Console opened. Use Ctrl-D to exit.\0APress enter if you don't see a prompt.\0A\00"
define i32 @do_console(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i32
%15 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 -1, i32* %5
store i8* null, i8** %6
store i8* null, i8** %7
store i8* null, i8** %8
store i8* null, i8** %9
%16 = load i32, i32* %3
%17 = call i64 @recv_msg_len(i32 %16, i8** %6, i8** %7, i32 8)
%18 = icmp eq i64 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %2
%20 = load i32, i32* @stderr
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %125
22: ; preds = %2
%23 = load i8*, i8** %6
%24 = call i64 @strcmp(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %44
26: ; preds = %22
store i8* null, i8** %10
store i8* null, i8** %11
%27 = load i32, i32* %3
%28 = call i32 @recv_msg(i32 %27, i8** %10, i8** %11)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = load i32, i32* @stderr
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
br label %39
33: ; preds = %26
%34 = load i32, i32* @stderr
%35 = load i8*, i8** %7
%36 = load i8*, i8** %10
%37 = load i8*, i8** %11
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %35, i8* %36, i8* %37)
br label %39
39: ; preds = %33, %30
%40 = load i8*, i8** %10
%41 = call i32 @free(i8* %40)
%42 = load i8*, i8** %11
%43 = call i32 @free(i8* %42)
br label %125
44: ; preds = %22
%45 = load i32, i32* %4
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %92
47: ; preds = %44
store i8* null, i8** %13
store i32 0, i32* %14
%48 = load i32, i32* %3
%49 = call i64 @recv_msg_len(i32 %48, i8** %12, i8** %13, i32 6)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %47
%52 = load i32, i32* @stderr
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
br label %125
54: ; preds = %47
%55 = load i8*, i8** %12
%56 = load i8, i8* %55
%57 = sext i8 %56 to i32
%58 = icmp ne i32 %57, 76
br i1 %58, label %59, label %63
59: ; preds = %54
%60 = load i32, i32* @stderr
%61 = load i8*, i8** %13
%62 = call i32 (i32, i8*, ...) @fprintf(i32 %60, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i8* %61)
br label %125
63: ; preds = %54
%64 = load i8*, i8** %13
%65 = call i32 @atoi(i8* %64)
store i32 %65, i32* %14
%66 = load i8*, i8** %12
%67 = call i32 @free(i8* %66)
%68 = load i8*, i8** %13
%69 = call i32 @free(i8* %68)
%70 = load i32, i32* %3
%71 = load i32, i32* %14
%72 = call i64 @recv_msg_len(i32 %70, i8** %8, i8** %9, i32 %71)
%73 = icmp slt i64 %72, 0
br i1 %73, label %74, label %77
74: ; preds = %63
%75 = load i32, i32* @stderr
%76 = call i32 (i32, i8*, ...) @fprintf(i32 %75, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
store i8* null, i8** %9
store i8* null, i8** %8
br label %125
77: ; preds = %63
%78 = load i8*, i8** %8
%79 = icmp ne i8* %78, null
br i1 %79, label %80, label %84
80: ; preds = %77
%81 = load i8*, i8** %8
%82 = load i8, i8* %81
%83 = icmp ne i8 %82, 0
br i1 %83, label %91, label %84
84: ; preds = %80, %77
%85 = load i32, i32* @stderr
%86 = call i32 (i32, i8*, ...) @fprintf(i32 %85, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.6, i64 0, i64 0))
%87 = load i8*, i8** %8
%88 = call i32 @free(i8* %87)
%89 = load i8*, i8** %9
%90 = call i32 @free(i8* %89)
store i8* null, i8** %9
store i8* null, i8** %8
br label %91
91: ; preds = %84, %80
br label %92
92: ; preds = %91, %44
%93 = load i32, i32* %3
%94 = call i32 @send_msg(i32 %93, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0))
%95 = icmp eq i32 %94, -1
br i1 %95, label %96, label %99
96: ; preds = %92
%97 = load i32, i32* @stderr
%98 = call i32 (i32, i8*, ...) @fprintf(i32 %97, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.9, i64 0, i64 0))
br label %125
99: ; preds = %92
%100 = load i32, i32* %3
%101 = call i32 @recv_fd(i32 %100)
store i32 %101, i32* %15
%102 = load i32, i32* %15
%103 = icmp eq i32 %102, -1
br i1 %103, label %104, label %107
104: ; preds = %99
%105 = load i32, i32* @stderr
%106 = call i32 (i32, i8*, ...) @fprintf(i32 %105, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.10, i64 0, i64 0))
br label %125
107: ; preds = %99
%108 = load i8*, i8** %8
%109 = icmp ne i8* %108, null
br i1 %109, label %112, label %110
110: ; preds = %107
%111 = call i32 @printf(i8* getelementptr inbounds ([76 x i8], [76 x i8]* @.str.11, i64 0, i64 0))
br label %112
112: ; preds = %110, %107
%113 = load i32, i32* %15
%114 = load i8*, i8** %8
%115 = load i8*, i8** %9
%116 = call i32 @open_walnut_console(i32 %113, i8* %114, i8* %115)
store i32 %116, i32* %5
%117 = load i32, i32* %5
%118 = icmp slt i32 %117, 0
br i1 %118, label %119, label %122
119: ; preds = %112
%120 = load i32, i32* @stderr
%121 = call i32 (i32, i8*, ...) @fprintf(i32 %120, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.10, i64 0, i64 0))
br label %122
122: ; preds = %119, %112
%123 = load i32, i32* %15
%124 = call i32 @close(i32 %123)
br label %125
125: ; preds = %122, %104, %96, %74, %59, %51, %39, %19
%126 = load i8*, i8** %8
%127 = call i32 @free(i8* %126)
%128 = load i8*, i8** %9
%129 = call i32 @free(i8* %128)
%130 = load i8*, i8** %6
%131 = call i32 @free(i8* %130)
%132 = load i8*, i8** %7
%133 = call i32 @free(i8* %132)
%134 = load i32, i32* %5
ret i32 %134
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [33 x i8] c"Invalid or no response received\0A\00"
@.str.1 = [2 x i8] c"0\00"
@.str.2 = [34 x i8] c"Error communicating with walnutD\0A\00"
@.str.3 = [9 x i8] c"%s%s %s\0A\00"
@.str.4 = [37 x i8] c"something's fishy with command (%s)\0A\00"
@.str.5 = [27 x i8] c"Unable to receive command\0A\00"
@.str.6 = [47 x i8] c"invalid command: dropping to standard console\0A\00"
@.str.7 = [5 x i8] c"0 %s\00"
@.str.8 = [6 x i8] c"ready\00"
@.str.9 = [35 x i8] c"Handshake error in console setup!\0A\00"
@.str.10 = [24 x i8] c"Failed to open console\0A\00"
@str = [75 x i8] c"Console opened. Use Ctrl-D to exit.\0APress enter if you don't see a prompt.\00"
define i32 @do_console(i32 %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%12 = bitcast i8** %4 to i8*
store i8* null, i8** %4
%13 = bitcast i8** %5 to i8*
store i8* null, i8** %5
%14 = bitcast i8** %6 to i8*
store i8* null, i8** %6
%15 = call i64 @recv_msg_len(i32 %0, i8** %3, i8** %4, i32 8)
%16 = icmp eq i64 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %114
20: ; preds = %2
%21 = load i8*, i8** %3
%22 = call i64 @strcmp(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%23 = icmp eq i64 %22, 0
br i1 %23, label %42, label %24
24: ; preds = %20
%25 = bitcast i8** %7 to i8*
store i8* null, i8** %7
%26 = bitcast i8** %8 to i8*
store i8* null, i8** %8
%27 = call i32 @recv_msg(i32 %0, i8** %7, i8** %8)
%28 = icmp eq i32 %27, -1
%29 = load i32, i32* @stderr
br i1 %28, label %30, label %32
30: ; preds = %24
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
br label %37
32: ; preds = %24
%33 = load i8*, i8** %4
%34 = load i8*, i8** %7
%35 = load i8*, i8** %8
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %33, i8* %34, i8* %35)
br label %37
37: ; preds = %32, %30
%38 = load i8*, i8** %7
%39 = call i32 @free(i8* %38)
%40 = load i8*, i8** %8
%41 = call i32 @free(i8* %40)
br label %114
42: ; preds = %20
%43 = icmp eq i32 %1, 0
br i1 %43, label %86, label %44
44: ; preds = %42
%45 = bitcast i8** %9 to i8*
%46 = bitcast i8** %10 to i8*
store i8* null, i8** %10
%47 = call i64 @recv_msg_len(i32 %0, i8** %9, i8** %10, i32 6)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %44
%50 = load i32, i32* @stderr
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
br label %84
52: ; preds = %44
%53 = load i8*, i8** %9
%54 = load i8, i8* %53
%55 = icmp eq i8 %54, 76
br i1 %55, label %60, label %56
56: ; preds = %52
%57 = load i32, i32* @stderr
%58 = load i8*, i8** %10
%59 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i8* %58)
br label %84
60: ; preds = %52
%61 = load i8*, i8** %10
%62 = call i32 @atoi(i8* %61)
%63 = call i32 @free(i8* %53)
%64 = load i8*, i8** %10
%65 = call i32 @free(i8* %64)
%66 = call i64 @recv_msg_len(i32 %0, i8** %5, i8** %6, i32 %62)
%67 = icmp slt i64 %66, 0
br i1 %67, label %68, label %71
68: ; preds = %60
%69 = load i32, i32* @stderr
%70 = call i32 (i32, i8*, ...) @fprintf(i32 %69, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
store i8* null, i8** %6
store i8* null, i8** %5
br label %84
71: ; preds = %60
%72 = load i8*, i8** %5
%73 = icmp eq i8* %72, null
br i1 %73, label %77, label %74
74: ; preds = %71
%75 = load i8, i8* %72
%76 = icmp eq i8 %75, 0
br i1 %76, label %77, label %85
77: ; preds = %74, %71
%78 = load i32, i32* @stderr
%79 = call i32 (i32, i8*, ...) @fprintf(i32 %78, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.6, i64 0, i64 0))
%80 = load i8*, i8** %5
%81 = call i32 @free(i8* %80)
%82 = load i8*, i8** %6
%83 = call i32 @free(i8* %82)
store i8* null, i8** %6
store i8* null, i8** %5
br label %85
84: ; preds = %49, %56, %68
br label %114
85: ; preds = %74, %77
br label %86
86: ; preds = %85, %42
%87 = call i32 @send_msg(i32 %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0))
%88 = icmp eq i32 %87, -1
br i1 %88, label %89, label %92
89: ; preds = %86
%90 = load i32, i32* @stderr
%91 = call i32 (i32, i8*, ...) @fprintf(i32 %90, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.9, i64 0, i64 0))
br label %114
92: ; preds = %86
%93 = call i32 @recv_fd(i32 %0)
%94 = icmp eq i32 %93, -1
br i1 %94, label %95, label %98
95: ; preds = %92
%96 = load i32, i32* @stderr
%97 = call i32 (i32, i8*, ...) @fprintf(i32 %96, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.10, i64 0, i64 0))
br label %114
98: ; preds = %92
%99 = load i8*, i8** %5
%100 = icmp eq i8* %99, null
br i1 %100, label %101, label %104
101: ; preds = %98
%102 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([75 x i8], [75 x i8]* @str, i64 0, i64 0))
%103 = load i8*, i8** %5
br label %104
104: ; preds = %101, %98
%105 = phi i8* [ %103, %101 ], [ %99, %98 ]
%106 = load i8*, i8** %6
%107 = call i32 @open_walnut_console(i32 %93, i8* %105, i8* %106)
%108 = icmp slt i32 %107, 0
br i1 %108, label %109, label %112
109: ; preds = %104
%110 = load i32, i32* @stderr
%111 = call i32 (i32, i8*, ...) @fprintf(i32 %110, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.10, i64 0, i64 0))
br label %112
112: ; preds = %109, %104
%113 = call i32 @close(i32 %93)
br label %114
114: ; preds = %84, %37, %112, %95, %89, %17
%115 = phi i32 [ -1, %17 ], [ -1, %37 ], [ -1, %89 ], [ -1, %95 ], [ %107, %112 ], [ -1, %84 ]
%116 = load i8*, i8** %5
%117 = call i32 @free(i8* %116)
%118 = load i8*, i8** %6
%119 = call i32 @free(i8* %118)
%120 = load i8*, i8** %3
%121 = call i32 @free(i8* %120)
%122 = load i8*, i8** %4
%123 = call i32 @free(i8* %122)
ret i32 %115
}