// File: mniip[P]sandbox/wakeup.cpp
// Function: do_wakeup()
void do_wakeup()
{
	pthread_cancel(feed_in->native_handle());
	feed_in->join();
	fcntl(out_pipe[0], F_SETFL, fcntl(out_pipe[0], F_GETFL) | O_NONBLOCK);
	pthread_kill(feed_out->native_handle(), SIGUSR1);
	feed_out->join();
	fcntl(out_pipe[0], F_SETFL, fcntl(out_pipe[0], F_GETFL) & ~O_NONBLOCK);
	delete feed_in;
	delete feed_out;
	if(old_client != -1)
		close(old_client);
	else
	{
		kill(getppid(), SIGUSR1);
		close(fileno(stdin));
		close(fileno(stdout));
		close(fileno(stderr));
	}
	unset_timer();

	int server = socket(AF_UNIX, SOCK_STREAM, 0);
	if(server < 0)
		panic_errno("socket");

	struct sockaddr_un addr;
	memset(&addr, 0, sizeof addr);
	addr.sun_family = AF_UNIX;
	strncpy(addr.sun_path, (conf_sockdir + "/socket_" + ident).c_str(), sizeof addr.sun_path - 1);
	if(bind(server, (struct sockaddr *)&addr, sizeof addr))
		panic_errno("bind");

	if(listen(server, 1))
		panic_errno("listen");

	int client = accept(server, NULL, NULL);
	if(client < 0)
		panic_errno("accept");

	close(server);
	unlink((conf_sockdir + "/socket_" + ident).c_str());

	feed_in = new std::thread(feed_data, client, in_pipe[1], false, !conf_wakeup);
	feed_out = new std::thread(feed_data, out_pipe[0], client, false, false);

	old_client = client;
	set_timer();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x220, %rsp            # imm = 0x220
movq 0x12e1e(%rip), %rdi     # 0x27560
callq 0x15770
movq %rax, %rdi
callq 0x5710
movq 0x12e0a(%rip), %rdi     # 0x27560
callq 0x58a0
movl 0x12df7(%rip), %eax     # 0x27558
movl %eax, -0x1c0(%rbp)
movl 0x12deb(%rip), %edi     # 0x27558
movl $0x3, %esi
movb $0x0, %al
callq 0x52e0
movl -0x1c0(%rbp), %edi
movl %eax, %edx
orl $0x800, %edx            # imm = 0x800
movl $0x4, %esi
movb $0x0, %al
callq 0x52e0
movq 0x12dce(%rip), %rdi     # 0x27568
callq 0x15770
movq %rax, %rdi
movl $0xa, %esi
callq 0x5670
movq 0x12db5(%rip), %rdi     # 0x27568
callq 0x58a0
movl 0x12d9a(%rip), %eax     # 0x27558
movl %eax, -0x1bc(%rbp)
movl 0x12d8e(%rip), %edi     # 0x27558
movl $0x3, %esi
movb $0x0, %al
callq 0x52e0
movl -0x1bc(%rbp), %edi
movl %eax, %edx
andl $0xfffff7ff, %edx       # imm = 0xFFFFF7FF
movl $0x4, %esi
movb $0x0, %al
callq 0x52e0
movq 0x12d69(%rip), %rax     # 0x27560
movq %rax, -0x1b8(%rbp)
cmpq $0x0, %rax
je 0x14821
movq -0x1b8(%rbp), %rdi
callq 0x15790
movq -0x1b8(%rbp), %rdi
movl $0x8, %esi
callq 0x54f0
movq 0x12d40(%rip), %rax     # 0x27568
movq %rax, -0x1c8(%rbp)
cmpq $0x0, %rax
je 0x14852
movq -0x1c8(%rbp), %rdi
callq 0x15790
movq -0x1c8(%rbp), %rdi
movl $0x8, %esi
callq 0x54f0
cmpl $-0x1, 0x12c9f(%rip)    # 0x274f8
je 0x14868
movl 0x12c97(%rip), %edi     # 0x274f8
callq 0x5970
jmp 0x148bb
callq 0x5720
movl %eax, %edi
movl $0xa, %esi
callq 0x5740
movq 0x12748(%rip), %rax     # 0x26fc8
movq (%rax), %rdi
callq 0x5540
movl %eax, %edi
callq 0x5970
movq 0x1270a(%rip), %rax     # 0x26fa0
movq (%rax), %rdi
callq 0x5540
movl %eax, %edi
callq 0x5970
movq 0x1272c(%rip), %rax     # 0x26fd8
movq (%rax), %rdi
callq 0x5540
movl %eax, %edi
callq 0x5970
callq 0x146d0
movl $0x1, %esi
xorl %edx, %edx
movl %esi, %edi
callq 0x5120
movl %eax, -0x4(%rbp)
cmpl $0x0, -0x4(%rbp)
jge 0x14950
leaq -0x29(%rbp), %rdi
movq %rdi, -0x1d0(%rbp)
callq 0x5900
movq -0x1d0(%rbp), %rdx
leaq 0x7b04(%rip), %rsi      # 0x1c3f9
leaq -0x28(%rbp), %rdi
callq 0x72d0
jmp 0x14900
leaq -0x28(%rbp), %rdi
callq 0x130c0
jmp 0x1490b
leaq -0x28(%rbp), %rdi
callq 0x59b8
leaq -0x29(%rbp), %rdi
callq 0x5560
jmp 0x14950
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x14942
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x59b8
leaq -0x29(%rbp), %rdi
callq 0x5560
jmp 0x14dce
xorps %xmm0, %xmm0
movups %xmm0, -0x52(%rbp)
movaps %xmm0, -0x60(%rbp)
movaps %xmm0, -0x70(%rbp)
movaps %xmm0, -0x80(%rbp)
movaps %xmm0, -0x90(%rbp)
movaps %xmm0, -0xa0(%rbp)
movaps %xmm0, -0xb0(%rbp)
movw $0x1, -0xb0(%rbp)
leaq -0xae(%rbp), %rax
movq %rax, -0x1e0(%rbp)
leaq 0x12d0a(%rip), %rsi     # 0x276a0
leaq 0x7a63(%rip), %rdx      # 0x1c400
leaq -0xf0(%rbp), %rdi
movq %rdi, -0x1d8(%rbp)
callq 0x10ec0
movq -0x1d8(%rbp), %rsi
movq 0x12b8a(%rip), %rdx     # 0x27548
leaq -0xd0(%rbp), %rdi
callq 0x13520
jmp 0x149cc
leaq -0xd0(%rbp), %rdi
callq 0x5200
movq -0x1e0(%rbp), %rdi
movq %rax, %rsi
movl $0x6b, %edx
callq 0x52a0
leaq -0xd0(%rbp), %rdi
callq 0x59b8
leaq -0xf0(%rbp), %rdi
callq 0x59b8
movl -0x4(%rbp), %edi
leaq -0xb0(%rbp), %rsi
movl $0x6e, %edx
callq 0x50a0
cmpl $0x0, %eax
je 0x14acc
leaq -0x111(%rbp), %rdi
movq %rdi, -0x1e8(%rbp)
callq 0x5900
movq -0x1e8(%rbp), %rdx
leaq 0x79c7(%rip), %rsi      # 0x1c409
leaq -0x110(%rbp), %rdi
callq 0x72d0
jmp 0x14a50
leaq -0x110(%rbp), %rdi
callq 0x130c0
jmp 0x14a5e
leaq -0x110(%rbp), %rdi
callq 0x59b8
leaq -0x111(%rbp), %rdi
callq 0x5560
jmp 0x14acc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0x59b8
jmp 0x14dce
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x14abb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x110(%rbp), %rdi
callq 0x59b8
leaq -0x111(%rbp), %rdi
callq 0x5560
jmp 0x14dce
movl -0x4(%rbp), %edi
movl $0x1, %esi
callq 0x55a0
cmpl $0x0, %eax
je 0x14b70
leaq -0x139(%rbp), %rdi
movq %rdi, -0x1f0(%rbp)
callq 0x5900
movq -0x1f0(%rbp), %rdx
leaq 0x790b(%rip), %rsi      # 0x1c40e
leaq -0x138(%rbp), %rdi
callq 0x72d0
jmp 0x14b11
leaq -0x138(%rbp), %rdi
callq 0x130c0
jmp 0x14b1f
leaq -0x138(%rbp), %rdi
callq 0x59b8
leaq -0x139(%rbp), %rdi
callq 0x5560
jmp 0x14b70
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x14b5f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x138(%rbp), %rdi
callq 0x59b8
leaq -0x139(%rbp), %rdi
callq 0x5560
jmp 0x14dce
movl -0x4(%rbp), %edi
xorl %eax, %eax
movl %eax, %edx
movq %rdx, %rsi
callq 0x51a0
movl %eax, -0x140(%rbp)
cmpl $0x0, -0x140(%rbp)
jge 0x14c20
leaq -0x161(%rbp), %rdi
movq %rdi, -0x1f8(%rbp)
callq 0x5900
movq -0x1f8(%rbp), %rdx
leaq 0x7862(%rip), %rsi      # 0x1c415
leaq -0x160(%rbp), %rdi
callq 0x72d0
jmp 0x14bc1
leaq -0x160(%rbp), %rdi
callq 0x130c0
jmp 0x14bcf
leaq -0x160(%rbp), %rdi
callq 0x59b8
leaq -0x161(%rbp), %rdi
callq 0x5560
jmp 0x14c20
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x14c0f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x160(%rbp), %rdi
callq 0x59b8
leaq -0x161(%rbp), %rdi
callq 0x5560
jmp 0x14dce
movl -0x4(%rbp), %edi
callq 0x5970
leaq 0x12a71(%rip), %rsi     # 0x276a0
leaq 0x77ca(%rip), %rdx      # 0x1c400
leaq -0x1a8(%rbp), %rdi
movq %rdi, -0x200(%rbp)
callq 0x10ec0
movq -0x200(%rbp), %rsi
movq 0x128f1(%rip), %rdx     # 0x27548
leaq -0x188(%rbp), %rdi
callq 0x13520
jmp 0x14c65
leaq -0x188(%rbp), %rdi
movq %rdi, -0x210(%rbp)
callq 0x5200
movq %rax, %rdi
callq 0x5270
movq -0x210(%rbp), %rdi
callq 0x59b8
leaq -0x1a8(%rbp), %rdi
callq 0x59b8
movl $0x8, %edi
callq 0x54e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x208(%rbp)
movb $0x0, -0x1a9(%rbp)
leaq 0x12a03(%rip), %rax     # 0x276c0
movb (%rax), %al
notb %al
andb $0x1, %al
movb %al, -0x1aa(%rbp)
leaq -0x810(%rip), %rsi      # 0x144c0
leaq 0x1287d(%rip), %rcx     # 0x27554
leaq -0x140(%rbp), %rdx
leaq -0x1a9(%rbp), %r8
leaq -0x1aa(%rbp), %r9
callq 0x157c0
jmp 0x14cf3
movq -0x208(%rbp), %rax
movq %rax, 0x1285f(%rip)     # 0x27560
movl $0x8, %edi
callq 0x54e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x218(%rbp)
movb $0x0, -0x1ab(%rbp)
movb $0x0, -0x1ac(%rbp)
leaq -0x86d(%rip), %rsi      # 0x144c0
leaq 0x12824(%rip), %rdx     # 0x27558
leaq -0x140(%rbp), %rcx
leaq -0x1ab(%rbp), %r8
leaq -0x1ac(%rbp), %r9
callq 0x157c0
jmp 0x14d50
movq -0x218(%rbp), %rax
movq %rax, 0x1280a(%rip)     # 0x27568
movl -0x140(%rbp), %eax
movl %eax, 0x1278e(%rip)     # 0x274f8
callq 0x14670
addq $0x220, %rsp            # imm = 0x220
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x1a8(%rbp), %rdi
callq 0x59b8
jmp 0x14dce
movq -0x208(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
movl $0x8, %esi
callq 0x54f0
jmp 0x14dce
movq -0x218(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
movl $0x8, %esi
callq 0x54f0
movq -0x38(%rbp), %rdi
callq 0x58f0
nopw (%rax,%rax)