// File: mniip[P]sandbox/main.cpp
// Function: main
int main(int argc, char **argv)
{
	if(argc < 3 || (std::string(argv[2]) == "kill" && argc < 4))
	{
		fprintf(stderr, "Usage: %s <config.conf> <ident> [prog [args...]]\n", argv[0]);
		fprintf(stderr, "       %s <config.conf> kill <ident>\n", argv[0]);
		abort();
	}

	ident = std::string(argv[2]) == "kill" ? argv[3] : argv[2];
	read_conf(argv[1]);

	if(std::string(argv[2]) == "kill")
	{
		kill_session();
		printf("Done\n");
		exit(EXIT_SUCCESS);
	}

	try_connect();

	signal(SIGUSR1, ignore);

	int pid = fork();
	if(pid < 0)
		panic_errno("fork");

	if(!pid)
	{
		if(pipe(in_pipe))
			panic_errno("pipe");
		if(pipe(out_pipe))
			panic_errno("pipe");
		if(argc >= 4)
		{
			main_process = sandbox.spawn_process(argv[3], argc - 3, argv + 3, set_limits);
		}
		else
		{
			std::vector<char *> av;
			for(std::vector<std::string>::iterator i = conf_arg.begin(), end = conf_arg.end(); i != end; i++)
				av.push_back(&(*i)[0]);
			main_process = sandbox.spawn_process(conf_program.c_str(), av.size(), av.data(), set_limits);
		}
		close(in_pipe[0]);
		close(out_pipe[1]);
		feed_in = new std::thread(feed_data, fileno(stdin), in_pipe[1], true, !conf_wakeup);
		feed_out = new std::thread(feed_data, out_pipe[0], fileno(stdout), false, false);
		sandbox.event_loop();
		exit(0);
	}
	else
	{
		signal(SIGUSR1, detach);
		wait(NULL);
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x240, %rsp            # imm = 0x240
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl -0x8(%rbp), %ecx
movb $0x0, -0x32(%rbp)
movb $0x0, -0x45(%rbp)
movb $0x1, %al
cmpl $0x3, %ecx
movb %al, -0x17f(%rbp)
jl 0x663b
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x190(%rbp)
leaq -0x31(%rbp), %rdi
movq %rdi, -0x188(%rbp)
callq 0x5900
movq -0x190(%rbp), %rsi
movq -0x188(%rbp), %rdx
movb $0x1, -0x32(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x72d0
jmp 0x65f1
movb $0x1, -0x45(%rbp)
leaq 0x15a47(%rip), %rsi     # 0x1c043
leaq -0x30(%rbp), %rdi
callq 0x72a0
movb %al, -0x191(%rbp)
jmp 0x660d
movb -0x191(%rbp), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x192(%rbp)
jne 0x6622
jmp 0x662f
cmpl $0x4, -0x8(%rbp)
setl %al
movb %al, -0x192(%rbp)
movb -0x192(%rbp), %al
movb %al, -0x17f(%rbp)
movb -0x17f(%rbp), %al
movb %al, -0x193(%rbp)
testb $0x1, -0x45(%rbp)
jne 0x664f
jmp 0x6658
leaq -0x30(%rbp), %rdi
callq 0x59b8
testb $0x1, -0x32(%rbp)
jne 0x6660
jmp 0x6669
leaq -0x31(%rbp), %rdi
callq 0x5560
movb -0x193(%rbp), %al
testb $0x1, %al
jne 0x6678
jmp 0x66fe
movq 0x20959(%rip), %rax     # 0x26fd8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
leaq 0x159b8(%rip), %rsi     # 0x1c048
movb $0x0, %al
callq 0x5760
movq 0x2093a(%rip), %rax     # 0x26fd8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
leaq 0x159cb(%rip), %rsi     # 0x1c07a
movb $0x0, %al
callq 0x5760
callq 0x52c0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x66e8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
testb $0x1, -0x45(%rbp)
jne 0x66dd
jmp 0x66e6
leaq -0x30(%rbp), %rdi
callq 0x59b8
jmp 0x66e8
testb $0x1, -0x32(%rbp)
jne 0x66f0
jmp 0x66f9
leaq -0x31(%rbp), %rdi
callq 0x5560
jmp 0x6e68
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x1a8(%rbp)
leaq -0x69(%rbp), %rdi
movq %rdi, -0x1a0(%rbp)
callq 0x5900
movq -0x1a8(%rbp), %rsi
movq -0x1a0(%rbp), %rdx
leaq -0x68(%rbp), %rdi
callq 0x72d0
jmp 0x6736
leaq 0x15906(%rip), %rsi     # 0x1c043
leaq -0x68(%rbp), %rdi
callq 0x72a0
movb %al, -0x1a9(%rbp)
jmp 0x674e
movb -0x1a9(%rbp), %al
testb $0x1, %al
jne 0x675a
jmp 0x676b
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x1b8(%rbp)
jmp 0x677a
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x1b8(%rbp)
movq -0x1b8(%rbp), %rcx
leaq 0x20dc0(%rip), %rax     # 0x27548
movq %rcx, (%rax)
leaq -0x68(%rbp), %rdi
callq 0x59b8
leaq -0x69(%rbp), %rdi
callq 0x5560
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x1c8(%rbp)
leaq -0x91(%rbp), %rdi
movq %rdi, -0x1c0(%rbp)
callq 0x5900
movq -0x1c8(%rbp), %rsi
movq -0x1c0(%rbp), %rdx
leaq -0x90(%rbp), %rdi
callq 0x72d0
jmp 0x67db
leaq -0x90(%rbp), %rdi
callq 0x15d50
jmp 0x67e9
leaq -0x90(%rbp), %rdi
callq 0x59b8
leaq -0x91(%rbp), %rdi
callq 0x5560
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x1d8(%rbp)
leaq -0xb9(%rbp), %rdi
movq %rdi, -0x1d0(%rbp)
callq 0x5900
movq -0x1d8(%rbp), %rsi
movq -0x1d0(%rbp), %rdx
leaq -0xb8(%rbp), %rdi
callq 0x72d0
jmp 0x683f
leaq 0x157fd(%rip), %rsi     # 0x1c043
leaq -0xb8(%rbp), %rdi
callq 0x72a0
movb %al, -0x1d9(%rbp)
jmp 0x685a
leaq -0xb8(%rbp), %rdi
callq 0x59b8
leaq -0xb9(%rbp), %rdi
callq 0x5560
movb -0x1d9(%rbp), %al
testb $0x1, %al
jne 0x6881
jmp 0x693a
callq 0x150b0
leaq 0x15813(%rip), %rdi     # 0x1c0a0
movb $0x0, %al
callq 0x5070
xorl %edi, %edi
callq 0x55e0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x68be
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x59b8
leaq -0x69(%rbp), %rdi
callq 0x5560
jmp 0x6e68
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x68f2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x59b8
leaq -0x91(%rbp), %rdi
callq 0x5560
jmp 0x6e68
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x6929
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x59b8
leaq -0xb9(%rbp), %rdi
callq 0x5560
jmp 0x6e68
callq 0x14de0
movl $0xa, %edi
leaq -0x3db(%rip), %rsi      # 0x6570
callq 0x55c0
callq 0x5150
movl %eax, -0xc0(%rbp)
cmpl $0x0, -0xc0(%rbp)
jge 0x69f6
leaq -0xe1(%rbp), %rdi
movq %rdi, -0x1e8(%rbp)
callq 0x5900
movq -0x1e8(%rbp), %rdx
leaq 0x1571d(%rip), %rsi     # 0x1c0a6
leaq -0xe0(%rbp), %rdi
callq 0x72d0
jmp 0x6997
leaq -0xe0(%rbp), %rdi
callq 0x130c0
jmp 0x69a5
leaq -0xe0(%rbp), %rdi
callq 0x59b8
leaq -0xe1(%rbp), %rdi
callq 0x5560
jmp 0x69f6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x69e5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0x59b8
leaq -0xe1(%rbp), %rdi
callq 0x5560
jmp 0x6e68
cmpl $0x0, -0xc0(%rbp)
jne 0x6e42
leaq 0x20b46(%rip), %rdi     # 0x27550
callq 0x56e0
cmpl $0x0, %eax
je 0x6aa6
leaq -0x109(%rbp), %rdi
movq %rdi, -0x1f0(%rbp)
callq 0x5900
movq -0x1f0(%rbp), %rdx
leaq 0x15672(%rip), %rsi     # 0x1c0ab
leaq -0x108(%rbp), %rdi
callq 0x72d0
jmp 0x6a47
leaq -0x108(%rbp), %rdi
callq 0x130c0
jmp 0x6a55
leaq -0x108(%rbp), %rdi
callq 0x59b8
leaq -0x109(%rbp), %rdi
callq 0x5560
jmp 0x6aa6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x6a95
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x108(%rbp), %rdi
callq 0x59b8
leaq -0x109(%rbp), %rdi
callq 0x5560
jmp 0x6e68
leaq 0x20aab(%rip), %rdi     # 0x27558
callq 0x56e0
cmpl $0x0, %eax
je 0x6b49
leaq -0x131(%rbp), %rdi
movq %rdi, -0x1f8(%rbp)
callq 0x5900
movq -0x1f8(%rbp), %rdx
leaq 0x155cf(%rip), %rsi     # 0x1c0ab
leaq -0x130(%rbp), %rdi
callq 0x72d0
jmp 0x6aea
leaq -0x130(%rbp), %rdi
callq 0x130c0
jmp 0x6af8
leaq -0x130(%rbp), %rdi
callq 0x59b8
leaq -0x131(%rbp), %rdi
callq 0x5560
jmp 0x6b49
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x6b38
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x130(%rbp), %rdi
callq 0x59b8
leaq -0x131(%rbp), %rdi
callq 0x5560
jmp 0x6e68
cmpl $0x4, -0x8(%rbp)
jl 0x6b83
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
movl -0x8(%rbp), %edx
subl $0x3, %edx
movq -0x10(%rbp), %rcx
addq $0x18, %rcx
leaq 0x209ac(%rip), %rdi     # 0x27518
leaq -0x903(%rip), %r8       # 0x6270
callq 0x7380
movl %eax, 0x20992(%rip)     # 0x27510
jmp 0x6cc3
leaq -0x150(%rbp), %rdi
callq 0x7660
leaq 0x20a12(%rip), %rdi     # 0x275a8
callq 0x7020
movq %rax, -0x158(%rbp)
leaq 0x209ff(%rip), %rdi     # 0x275a8
callq 0x7090
movq %rax, -0x160(%rbp)
leaq -0x158(%rbp), %rdi
leaq -0x160(%rbp), %rsi
callq 0x7680
testb $0x1, %al
jne 0x6bce
jmp 0x6c48
leaq -0x158(%rbp), %rdi
callq 0x76f0
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x59a0
movq %rax, -0x200(%rbp)
jmp 0x6bef
movq -0x200(%rbp), %rax
movq %rax, -0x168(%rbp)
leaq -0x150(%rbp), %rdi
leaq -0x168(%rbp), %rsi
callq 0x76c0
jmp 0x6c12
jmp 0x6c14
leaq -0x158(%rbp), %rdi
xorl %esi, %esi
callq 0x7710
movq %rax, -0x170(%rbp)
jmp 0x6bb5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x150(%rbp), %rdi
callq 0x7790
jmp 0x6e68
leaq 0x20939(%rip), %rdi     # 0x27588
callq 0x5200
movq %rax, -0x210(%rbp)
leaq -0x150(%rbp), %rdi
movq %rdi, -0x218(%rbp)
callq 0x7750
movq -0x218(%rbp), %rdi
movl %eax, -0x208(%rbp)
callq 0x7770
movq -0x210(%rbp), %rsi
movl -0x208(%rbp), %edx
movq %rax, %rcx
leaq 0x20881(%rip), %rdi     # 0x27518
leaq -0xa2e(%rip), %r8       # 0x6270
callq 0x7380
movl %eax, -0x204(%rbp)
jmp 0x6cab
movl -0x204(%rbp), %eax
movl %eax, 0x20859(%rip)     # 0x27510
leaq -0x150(%rbp), %rdi
callq 0x7790
leaq 0x20886(%rip), %rax     # 0x27550
movq %rax, -0x228(%rbp)
movl (%rax), %edi
callq 0x5970
leaq 0x20879(%rip), %rax     # 0x27558
movl 0x4(%rax), %edi
callq 0x5970
movl $0x8, %edi
callq 0x54e0
movq %rax, -0x220(%rbp)
movq %rax, -0x230(%rbp)
movq 0x202c2(%rip), %rax     # 0x26fc8
movq (%rax), %rdi
callq 0x5540
movq -0x228(%rbp), %rcx
movq -0x220(%rbp), %rdi
movl %eax, -0x174(%rbp)
movb $0x1, -0x175(%rbp)
leaq 0x20990(%rip), %rax     # 0x276c0
movb (%rax), %al
notb %al
andb $0x1, %al
movb %al, -0x176(%rbp)
addq $0x4, %rcx
leaq 0xd779(%rip), %rsi      # 0x144c0
leaq -0x174(%rbp), %rdx
leaq -0x175(%rbp), %r8
leaq -0x176(%rbp), %r9
callq 0x77f0
jmp 0x6d63
movq -0x230(%rbp), %rcx
leaq 0x207ef(%rip), %rax     # 0x27560
movq %rcx, (%rax)
movl $0x8, %edi
callq 0x54e0
movq %rax, -0x238(%rbp)
movq %rax, -0x240(%rbp)
movq 0x2020d(%rip), %rax     # 0x26fa0
movq (%rax), %rdi
callq 0x5540
movq -0x238(%rbp), %rdi
movl %eax, -0x17c(%rbp)
movb $0x0, -0x17d(%rbp)
movb $0x0, -0x17e(%rbp)
leaq 0xd703(%rip), %rsi      # 0x144c0
leaq 0x20794(%rip), %rdx     # 0x27558
leaq -0x17c(%rbp), %rcx
leaq -0x17d(%rbp), %r8
leaq -0x17e(%rbp), %r9
callq 0x78d0
jmp 0x6de0
movq -0x240(%rbp), %rcx
leaq 0x2077a(%rip), %rax     # 0x27568
movq %rcx, (%rax)
leaq 0x20720(%rip), %rdi     # 0x27518
callq 0x79b0
xorl %edi, %edi
callq 0x55e0
movq -0x230(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
movl $0x8, %esi
callq 0x54f0
jmp 0x6e68
movq -0x240(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
movl $0x8, %esi
callq 0x54f0
jmp 0x6e68
movl $0xa, %edi
leaq -0x8fe(%rip), %rsi      # 0x6550
callq 0x55c0
xorl %eax, %eax
movl %eax, %edi
callq 0x5610
movl -0x4(%rbp), %eax
addq $0x240, %rsp            # imm = 0x240
popq %rbp
retq
movq -0x40(%rbp), %rdi
callq 0x58f0
nopw %cs:(%rax,%rax)
nopl (%rax,%rax)