// File: davmac314[P]dinit/src/control.cc
// Function: control_conn_t::allocate_service_handle(service_record*)
handle_t control_conn_t::allocate_service_handle(service_record *record)
{
    // Try to find a unique handle (integer) in a single pass. Since the map is ordered, we can search until
    // we find a gap in the handle values.
    handle_t candidate = 0;
    for (auto p : key_service_map) {
        if (p.first == candidate) ++candidate;
        else break;
    }

    bool is_unique = (service_key_map.find(record) == service_key_map.end());

    // The following operations perform allocation (can throw std::bad_alloc). If an exception occurs we
    // must undo any previous actions:
    if (is_unique) {
        record->add_listener(this);
    }
    
    try {
        key_service_map[candidate] = record;
        service_key_map.insert(std::make_pair(record, candidate));
    }
    catch (...) {
        if (is_unique) {
            record->remove_listener(this);
        }

        key_service_map.erase(candidate);
    }
    
    return candidate;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x98(%rbp)
movl $0x0, -0x14(%rbp)
addq $0x4c0, %rax            # imm = 0x4C0
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
callq 0x5f890
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rdi
callq 0x5e700
movq %rax, -0x30(%rbp)
leaq -0x28(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x5f8c0
testb $0x1, %al
jne 0x5be9c
jmp 0x5bed6
leaq -0x28(%rbp), %rdi
callq 0x5f8f0
movq (%rax), %rcx
movq %rcx, -0x40(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
movl -0x40(%rbp), %eax
cmpl -0x14(%rbp), %eax
jne 0x5bec7
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x5bec9
jmp 0x5bed6
jmp 0x5becb
leaq -0x28(%rbp), %rdi
callq 0x5f910
jmp 0x5be89
movq -0x98(%rbp), %rdi
addq $0x488, %rdi            # imm = 0x488
leaq -0x10(%rbp), %rsi
callq 0x5f970
movq -0x98(%rbp), %rdi
movq %rax, -0x50(%rbp)
addq $0x488, %rdi            # imm = 0x488
callq 0x5f9a0
movq %rax, -0x58(%rbp)
leaq -0x50(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x5f940
andb $0x1, %al
movb %al, -0x41(%rbp)
testb $0x1, -0x41(%rbp)
je 0x5bf30
movq -0x98(%rbp), %rsi
movq -0x10(%rbp), %rdi
callq 0x5f9d0
movq -0x98(%rbp), %rdi
movq -0x10(%rbp), %rax
movq %rax, -0xa8(%rbp)
addq $0x4c0, %rdi            # imm = 0x4C0
leaq -0x14(%rbp), %rsi
callq 0x5fa10
movq %rax, -0xa0(%rbp)
jmp 0x5bf5b
movq -0x98(%rbp), %rax
movq -0xa0(%rbp), %rcx
movq -0xa8(%rbp), %rdx
movq %rdx, (%rcx)
addq $0x488, %rax            # imm = 0x488
movq %rax, -0xc0(%rbp)
leaq -0x10(%rbp), %rdi
leaq -0x14(%rbp), %rsi
callq 0x5fb20
movl %edx, -0xb4(%rbp)
movq %rax, -0xb0(%rbp)
jmp 0x5bf9c
movq -0xc0(%rbp), %rdi
movl -0xb4(%rbp), %eax
movq -0xb0(%rbp), %rcx
movq %rcx, -0x88(%rbp)
movl %eax, -0x80(%rbp)
movl -0x80(%rbp), %eax
movl %eax, -0x70(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0x78(%rbp)
leaq -0x78(%rbp), %rsi
callq 0x5faf0
movq %rax, -0xc8(%rbp)
jmp 0x5bfdd
movq -0xc8(%rbp), %rax
movq %rax, -0x90(%rbp)
jmp 0x5c036
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
movq -0x60(%rbp), %rdi
callq 0xa240
testb $0x1, -0x41(%rbp)
je 0x5c018
movq -0x98(%rbp), %rsi
movq -0x10(%rbp), %rdi
callq 0x5e8d0
movq -0x98(%rbp), %rdi
addq $0x4c0, %rdi            # imm = 0x4C0
leaq -0x14(%rbp), %rsi
callq 0x5eee0
jmp 0x5c031
callq 0xac30
movl -0x14(%rbp), %eax
addq $0xd0, %rsp
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
callq 0xac30
jmp 0x5c055
jmp 0x5c057
movq -0x60(%rbp), %rdi
callq 0xacc0
movq %rax, %rdi
callq 0xfa30
nopl (%rax,%rax)