// File: lega911[P]ijson/src/server.cpp
// Function: Loop::on_disconnect(Connect*)
void Loop::on_disconnect(Connect *conn) {
    if(conn->worker_item) conn->worker_item->pop();

    if(!conn->direct_message.empty()) {
        GC gc;
        for(auto const &it : conn->direct_message) {
            gc.add(it, GC::DirectMessage);
        }
        conn->direct_message.clear();
    }

    if(conn->status == Status::client_wait_result && !conn->id.empty()) {
        auto it = server->wait_response.find(conn->id.as_string());
        if(it != server->wait_response.end()) {
            server->wait_lock.lock();
            server->wait_response.erase(it);
            server->wait_lock.unlock();
            conn->unlink();
        }
    };
    if(!conn->fail_on_disconnect) return;
    if(conn->noid) {
        if(conn->status == Status::worker_wait_result) {
            if(!conn->client) THROW("No client");
            if(!conn->client->is_closed()) conn->client->send.status("503 Service Unavailable")->done(-1);
            conn->client->status = Status::net;
        } else if(conn->client) THROW("Client is linked to pending worker");
    } else if(conn->client) {
        worker_result(conn->client->id, NULL);
    }
    if(conn->client) {
        conn->client->unlink();
        conn->client = NULL;
    };
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x178(%rax)
je 0x2aefc
movq -0x10(%rbp), %rax
movq 0x178(%rax), %rdi
callq 0x37930
movq -0x10(%rbp), %rdi
addq $0x190, %rdi            # imm = 0x190
callq 0x1e780
testb $0x1, %al
jne 0x2afbf
leaq -0x28(%rbp), %rdi
callq 0x2d200
movq -0x10(%rbp), %rax
addq $0x190, %rax            # imm = 0x190
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rsi
leaq -0x50(%rbp), %rdi
callq 0x210a0
movq -0x30(%rbp), %rsi
leaq -0x70(%rbp), %rdi
callq 0x2d750
leaq -0x50(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x2d780
testb $0x1, %al
jne 0x2af58
jmp 0x2afa6
leaq -0x50(%rbp), %rdi
callq 0x210d0
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movq (%rax), %rsi
leaq -0x28(%rbp), %rdi
movl $0x2, %edx
callq 0x36ce0
jmp 0x2af7c
jmp 0x2af7e
leaq -0x50(%rbp), %rdi
callq 0x2d7b0
jmp 0x2af45
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x1e970
jmp 0x2b33c
movq -0x10(%rbp), %rdi
addq $0x190, %rdi            # imm = 0x190
callq 0x2d800
leaq -0x28(%rbp), %rdi
callq 0x1e970
movq -0x10(%rbp), %rax
cmpl $0x4, 0xcc(%rax)
jne 0x2b124
movq -0x10(%rbp), %rdi
addq $0xe0, %rdi
callq 0x1eb40
testb $0x1, %al
jne 0x2b124
movq -0xe0(%rbp), %rax
movq 0x18(%rax), %rax
addq $0x20128, %rax          # imm = 0x20128
movq %rax, -0xf8(%rbp)
movq -0x10(%rbp), %rsi
addq $0xe0, %rsi
leaq -0xb0(%rbp), %rdi
movq %rdi, -0xf0(%rbp)
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x1f340
movq -0xf8(%rbp), %rdi
movq -0xf0(%rbp), %rsi
callq 0x2d980
movq %rax, -0xe8(%rbp)
jmp 0x2b03f
movq -0xe8(%rbp), %rax
movq %rax, -0x90(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x191e0
movq -0xe0(%rbp), %rax
movq 0x18(%rax), %rdi
addq $0x20128, %rdi          # imm = 0x20128
callq 0x2d9b0
movq %rax, -0xb8(%rbp)
leaq -0x90(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x2d950
testb $0x1, %al
jne 0x2b093
jmp 0x2b122
movq -0xe0(%rbp), %rax
movq 0x18(%rax), %rdi
addq $0x20158, %rdi          # imm = 0x20158
callq 0x1fa50
movq -0xe0(%rbp), %rax
movq 0x18(%rax), %rdi
addq $0x20128, %rdi          # imm = 0x20128
movq -0x90(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0xc0(%rbp), %rsi
callq 0x2e030
movq %rax, %rcx
movq -0xe0(%rbp), %rax
movq %rcx, -0xc8(%rbp)
movq 0x18(%rax), %rdi
addq $0x20158, %rdi          # imm = 0x20158
callq 0x1fa80
movq -0x10(%rbp), %rdi
callq 0x19950
jmp 0x2b122
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x191e0
jmp 0x2b33c
jmp 0x2b124
movq -0x10(%rbp), %rax
testb $0x1, 0xf0(%rax)
jne 0x2b136
jmp 0x2b333
movq -0x10(%rbp), %rax
testb $0x1, 0xf1(%rax)
je 0x2b2b2
movq -0x10(%rbp), %rax
cmpl $0x2, 0xcc(%rax)
jne 0x2b22e
movq -0x10(%rbp), %rax
cmpq $0x0, 0x120(%rax)
jne 0x2b1d8
movl $0x30, %edi
callq 0x19180
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x100(%rbp)
leaq 0xe52c(%rip), %rsi      # 0x396b0
leaq 0xe0bb(%rip), %rdx      # 0x39246
leaq 0xe528(%rip), %r8       # 0x396ba
movl $0x3d4, %ecx            # imm = 0x3D4
callq 0x22ba0
jmp 0x2b19e
movq -0x100(%rbp), %rdi
leaq 0x1bb54(%rip), %rsi     # 0x46d00
leaq -0x80a3(%rip), %rdx     # 0x23110
callq 0x19480
movq -0x100(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
callq 0x19210
jmp 0x2b33c
movq -0x10(%rbp), %rax
movq 0x120(%rax), %rdi
callq 0x1fcc0
testb $0x1, %al
jne 0x2b214
movq -0x10(%rbp), %rax
movq 0x120(%rax), %rdi
addq $0x18, %rdi
leaq 0xe451(%rip), %rsi      # 0x39653
callq 0x1b6d0
movq %rax, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x1b770
movq -0x10(%rbp), %rax
movq 0x120(%rax), %rax
movl $0x0, 0xcc(%rax)
jmp 0x2b2b0
movq -0x10(%rbp), %rax
cmpq $0x0, 0x120(%rax)
je 0x2b2ae
movl $0x30, %edi
callq 0x19180
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x108(%rbp)
leaq 0xe484(%rip), %rsi      # 0x396de
leaq 0xdfe5(%rip), %rdx      # 0x39246
leaq 0xe452(%rip), %r8       # 0x396ba
movl $0x3d7, %ecx            # imm = 0x3D7
callq 0x22ba0
jmp 0x2b274
movq -0x108(%rbp), %rdi
leaq 0x1ba7e(%rip), %rsi     # 0x46d00
leaq -0x8179(%rip), %rdx     # 0x23110
callq 0x19480
movq -0x108(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
callq 0x19210
jmp 0x2b33c
jmp 0x2b2b0
jmp 0x2b306
movq -0x10(%rbp), %rax
cmpq $0x0, 0x120(%rax)
je 0x2b304
movq -0xe0(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x120(%rax), %rax
movq 0xe0(%rax), %rcx
movq %rcx, -0xd8(%rbp)
movq 0xe8(%rax), %rax
movq %rax, -0xd0(%rbp)
movq -0xd8(%rbp), %rsi
movl -0xd0(%rbp), %edx
xorl %eax, %eax
movl %eax, %ecx
callq 0x2c620
jmp 0x2b306
movq -0x10(%rbp), %rax
cmpq $0x0, 0x120(%rax)
je 0x2b333
movq -0x10(%rbp), %rax
movq 0x120(%rax), %rdi
callq 0x19950
movq -0x10(%rbp), %rax
movq $0x0, 0x120(%rax)
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
movq -0x80(%rbp), %rdi
callq 0x194a0
nopw %cs:(%rax,%rax)