// File: /workspace/github_cpp_makefile_star2_O3/davmac314[P]dinit/src/control.cc
// Function: control_conn_t::process_close_handle()
bool control_conn_t::process_close_handle()
{
    constexpr int pkt_size = 1 + sizeof(handle_t);

    if (rbuf.get_length() < pkt_size) {
        chklen = pkt_size;
        return true;
    }

    handle_t handle;
    rbuf.extract((char *) &handle, 1, sizeof(handle));

    rbuf.consume(pkt_size);
    chklen = 0;

    auto key_it = key_service_map.find(handle);
    if (key_it == key_service_map.end()) {
        // Service handle is bad
        char badreq_rep[] = { (char)cp_rply::BADREQ };
        if (!queue_packet(badreq_rep, 1)) return false;
        bad_conn_close = true;
        return true;
    }

    service_record *service = key_it->second;
    key_service_map.erase(key_it);

    bool have_other_handle = false;
    auto handle_range = service_key_map.equal_range(service);
    auto it = handle_range.first;
    while (it->second != handle) {
        have_other_handle = true;
        ++it;
    }
    if (!have_other_handle) {
        // check if more handles beyond the found handle
        have_other_handle = std::next(it) != handle_range.second;
    }
    service_key_map.erase(it);

    if (!have_other_handle) {
        service->remove_listener(this);
    }

    char ack_reply[] = { (char)cp_rply::ACK };
    return queue_packet(ack_reply, sizeof(ack_reply));
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movl 0x484(%rdi), %ebp
cmpl $0x4, %ebp
ja 0x284ee
movl $0x5, 0x7c(%rbx)
movb $0x1, %al
jmp 0x28661
leaq 0x80(%rbx), %r14
movl 0x480(%rbx), %r13d
leal 0x1(%r13), %eax
leal -0x3ff(%r13), %r15d
cmpl $0x400, %eax            # imm = 0x400
cmovbl %eax, %r15d
leal -0x3fd(%r15), %eax
cmpl $0xfffffbfe, %eax       # imm = 0xFFFFFBFE
ja 0x28556
movl $0x400, %r12d           # imm = 0x400
subl %r15d, %r12d
movl %r15d, %esi
addq %r14, %rsi
leaq 0x8(%rsp), %rdi
movq %r12, %rdx
callq 0x84d0
leaq (%rsp,%r12), %rdi
addq $0x8, %rdi
addl $0xfffffc04, %r15d      # imm = 0xFFFFFC04
movq %r14, %rsi
movq %r15, %rdx
callq 0x84d0
jmp 0x28561
movl %r15d, %eax
movl (%r14,%rax), %eax
movl %eax, 0x8(%rsp)
leal 0x5(%r13), %eax
addl $0xfffffc05, %r13d      # imm = 0xFFFFFC05
cmpl $0x400, %eax            # imm = 0x400
cmovbl %eax, %r13d
movl %r13d, 0x480(%rbx)
addl $-0x5, %ebp
movl %ebp, 0x484(%rbx)
movl $0x0, 0x7c(%rbx)
movq 0x4d0(%rbx), %rax
testq %rax, %rax
je 0x285cd
leaq 0x4c0(%rbx), %rdi
leaq 0x4c8(%rbx), %rcx
movl 0x8(%rsp), %ebp
movq %rcx, %rsi
xorl %edx, %edx
cmpl %ebp, 0x20(%rax)
setb %dl
cmovaeq %rax, %rsi
movq 0x10(%rax,%rdx,8), %rax
testq %rax, %rax
jne 0x285ad
cmpq %rcx, %rsi
je 0x285cd
cmpl 0x20(%rsi), %ebp
jae 0x285ec
leaq 0xf(%rsp), %rsi
movb $0x34, (%rsi)
movq %rbx, %rdi
movl $0x1, %edx
callq 0x28036
testb %al, %al
je 0x28661
movb $0x1, 0x78(%rbx)
jmp 0x28661
movq 0x28(%rsi), %rax
leaq 0x10(%rsp), %r15
movq %rax, (%r15)
callq 0x2ce0e
leaq 0x488(%rbx), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x2ce4a
cmpl %ebp, 0x10(%rax)
jne 0x28639
movq %rdx, %r15
movq (%rax), %r12
movq %r14, %rdi
movq %rax, %rsi
callq 0x2cecc
cmpq %r15, %r12
jne 0x2864c
movq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x2b8e6
jmp 0x2864c
movq (%rax), %rax
cmpl %ebp, 0x10(%rax)
jne 0x28639
movq %r14, %rdi
movq %rax, %rsi
callq 0x2cecc
leaq 0xe(%rsp), %rsi
movb $0x32, (%rsi)
movq %rbx, %rdi
movl $0x1, %edx
callq 0x28036
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq