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

/* Variables and functions */
int /*<<< orphan*/ SIGKILL;
int /*<<< orphan*/ err(int, char *, char *);
int file_broker();
int /*<<< orphan*/ kill(int, int /*<<< orphan*/);
int /*<<< orphan*/ puts(char *);
int read_binary();
int /*<<< orphan*/ recv_str(int, char *, int);
int sandbox(int, int *);
int /*<<< orphan*/ send_fd(int, int);
int /*<<< orphan*/ setbuf(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
scalar_t__ strcmp(char *, char *);
int main(int argc, char *argv[]) {
  setbuf(stdin, NULL);
  setbuf(stdout, NULL);
  setbuf(stderr, NULL);
  int broker_fd = file_broker();
  int sandbox_pid = 0;
  int sandbox_fd = sandbox(broker_fd, &sandbox_pid);
  puts("sandbox initialized");
  while (1) {
    char msg[128] = "";
    int bin_fd = read_binary();
    send_fd(sandbox_fd, bin_fd);
    recv_str(sandbox_fd, msg, sizeof(msg));
    if (strcmp(msg, "OK")) {
      kill(sandbox_pid, SIGKILL);
      err(1, "sandboxee msg: %s", msg);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <setbuf@plt>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <setbuf@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <setbuf@plt>
mov    $0x0,%al
call   10d0 <file_broker@plt>
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   10b0 <sandbox@plt>
mov    %eax,-0x1c(%rbp)
lea    0xdda(%rip),%rdi
call   1040 <puts@plt>
lea    -0xa0(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1070 <memset@plt>
mov    $0x0,%al
call   1030 <read_binary@plt>
mov    %eax,-0xa4(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0xa4(%rbp),%esi
call   1060 <send_fd@plt>
mov    -0x1c(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    $0x80,%edx
call   10c0 <recv_str@plt>
lea    -0xa0(%rbp),%rdi
lea    0xd99(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     12b3 <main+0x103>
mov    -0x18(%rbp),%edi
mov    0x2d3c(%rip),%rax
mov    (%rax),%esi
call   1090 <kill@plt>
lea    -0xa0(%rbp),%rdx
mov    $0x1,%edi
lea    0xd69(%rip),%rsi
call   10a0 <err@plt>
jmp    122b <main+0x7b>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x90,%rsp
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1050 <setbuf@plt>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1050 <setbuf@plt>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1050 <setbuf@plt>
xor    %eax,%eax
call   10c0 <file_broker@plt>
movl   $0x0,0xc(%rsp)
lea    0xc(%rsp),%rsi
mov    %eax,%edi
call   10a0 <sandbox@plt>
mov    %eax,%ebx
lea    0xdfc(%rip),%rdi
call   1040 <puts@plt>
lea    0x10(%rsp),%r14
lea    0xdff(%rip),%r15
mov    0x2db4(%rip),%r13
lea    0xdf4(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xorps  %xmm0,%xmm0
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
xor    %eax,%eax
call   1030 <read_binary@plt>
mov    %ebx,%edi
mov    %eax,%esi
call   1060 <send_fd@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x80,%edx
call   10b0 <recv_str@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1230 <main+0x90>
mov    0xc(%rsp),%edi
mov    0x0(%r13),%esi
call   1080 <kill@plt>
mov    $0x1,%edi
mov    %r12,%rsi
mov    %r14,%rdx
call   1090 <err@plt>
jmp    1230 <main+0x90>

LLVM IR (-O0):
@stdin = global i32 0
@stdout = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"sandbox initialized\00"
@.str.1 = [3 x i8] c"OK\00"
@SIGKILL = global i32 0
@.str.2 = [18 x i8] c"sandboxee msg: %s\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 [128 x i8]
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* @stdin
%12 = call i32 @setbuf(i32 %11, i32* null)
%13 = load i32, i32* @stdout
%14 = call i32 @setbuf(i32 %13, i32* null)
%15 = load i32, i32* @stderr
%16 = call i32 @setbuf(i32 %15, i32* null)
%17 = call i32 (...) @file_broker()
store i32 %17, i32* %6
store i32 0, i32* %7
%18 = load i32, i32* %6
%19 = call i32 @sandbox(i32 %18, i32* %7)
store i32 %19, i32* %8
%20 = call i32 @puts(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %2, %39
%22 = bitcast [128 x i8]* %9 to i8*
%23 = call i32 (...) @read_binary()
store i32 %23, i32* %10
%24 = load i32, i32* %8
%25 = load i32, i32* %10
%26 = call i32 @send_fd(i32 %24, i32 %25)
%27 = load i32, i32* %8
%28 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%29 = call i32 @recv_str(i32 %27, i8* %28, i32 128)
%30 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%31 = call i64 @strcmp(i8* %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %39
33: ; preds = %21
%34 = load i32, i32* %7
%35 = load i32, i32* @SIGKILL
%36 = call i32 @kill(i32 %34, i32 %35)
%37 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%38 = call i32 @err(i32 1, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %37)
br label %39
39: ; preds = %33, %21
br label %21
}

LLVM IR (-O2):
@stdin = global i32 0
@stdout = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"sandbox initialized\00"
@.str.1 = [3 x i8] c"OK\00"
@SIGKILL = global i32 0
@.str.2 = [18 x i8] c"sandboxee msg: %s\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca [128 x i8]
%5 = load i32, i32* @stdin
%6 = tail call i32 @setbuf(i32 %5, i32* null)
%7 = load i32, i32* @stdout
%8 = tail call i32 @setbuf(i32 %7, i32* null)
%9 = load i32, i32* @stderr
%10 = tail call i32 @setbuf(i32 %9, i32* null)
%11 = tail call i32 (...) @file_broker()
%12 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%13 = call i32 @sandbox(i32 %11, i32* %3)
%14 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%15 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
br label %16
16: ; preds = %27, %2
%17 = call i32 (...) @read_binary()
%18 = call i32 @send_fd(i32 %13, i32 %17)
%19 = call i32 @recv_str(i32 %13, i8* %15, i32 128)
%20 = call i64 @strcmp(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%21 = icmp eq i64 %20, 0
br i1 %21, label %27, label %22
22: ; preds = %16
%23 = load i32, i32* %3
%24 = load i32, i32* @SIGKILL
%25 = call i32 @kill(i32 %23, i32 %24)
%26 = call i32 @err(i32 1, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %15)
br label %27
27: ; preds = %22, %16
br label %16
}