// File: davmac314[P]dinit/src/dinitctl.cc
// Function: get_service_name[abi:cxx11](int, cpbuffer<1024u>&, unsigned int)
static std::string get_service_name(int socknum, cpbuffer_t &rbuffer, handle_t handle)
{
    auto m = membuf()
            .append((char) cp_cmd::QUERYSERVICENAME)
            .append((char) 0)
            .append(handle);
    write_all_x(socknum, m);

    wait_for_reply(rbuffer, socknum);

    if (rbuffer[0] != (char)cp_rply::SERVICENAME) {
        throw cp_read_exception{0};
    }

    // 1 byte reserved
    // uint16_t size
    fill_buffer_to(rbuffer, socknum, 2 + sizeof(uint16_t));
    uint16_t namesize;
    rbuffer.extract(&namesize, 2, sizeof(uint16_t));
    rbuffer.consume(2 + sizeof(uint16_t));

    std::string name;

    do {
        if (rbuffer.get_length() == 0) {
            fill_some(rbuffer, socknum);
        }

        size_t to_extract = std::min(size_t(rbuffer.get_length()), namesize - name.length());
        size_t contiguous_len = rbuffer.get_contiguous_length(rbuffer.get_ptr(0));
        if (contiguous_len <= to_extract) {
            name.append(rbuffer.get_ptr(0), contiguous_len);
            rbuffer.consume(contiguous_len);
            name.append(rbuffer.get_ptr(0), to_extract - contiguous_len);
            rbuffer.consume(to_extract - contiguous_len);
        }
        else {
            name.append(rbuffer.get_ptr(0), to_extract);
            rbuffer.consume(to_extract);
            break;
        }

    } while (name.length() < namesize);

    return name;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x68(%rbp)
movq %rdi, %rax
movq %rax, -0x60(%rbp)
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movb $0xf, -0x27(%rbp)
leaq -0x26(%rbp), %rdi
leaq -0x27(%rbp), %rsi
callq 0x11b60
movb %al, -0x25(%rbp)
movb $0x0, -0x28(%rbp)
leaq -0x25(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x11b90
movw %ax, -0x24(%rbp)
leaq -0x24(%rbp), %rdi
leaq -0x1c(%rbp), %rsi
callq 0x11bc0
movl %eax, %ecx
movl %ecx, -0x22(%rbp)
shrq $0x20, %rax
movw %ax, -0x1e(%rbp)
movl -0xc(%rbp), %edi
leaq -0x22(%rbp), %rsi
callq 0x11c10
movq -0x18(%rbp), %rdi
movl -0xc(%rbp), %esi
callq 0x11350
movq -0x18(%rbp), %rdi
xorl %esi, %esi
callq 0x113e0
movsbl %al, %eax
cmpl $0x42, %eax
je 0xc77d
movl $0x4, %edi
callq 0x51c0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x70(%rbp)
xorl %esi, %esi
callq 0x11420
jmp 0xc74f
movq -0x70(%rbp), %rdi
leaq 0x253c6(%rip), %rsi     # 0x31b20
xorl %eax, %eax
movl %eax, %edx
callq 0x56b0
movq -0x70(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
callq 0x52f0
jmp 0xc96c
movq -0x18(%rbp), %rdi
movl -0xc(%rbp), %esi
movl $0x4, %edx
callq 0x11440
movq -0x18(%rbp), %rdi
leaq -0x36(%rbp), %rsi
movl $0x2, %ecx
movl %ecx, %edx
callq 0x11540
movq -0x18(%rbp), %rdi
movl $0x4, %esi
callq 0x115f0
movq -0x68(%rbp), %rdi
movb $0x0, -0x37(%rbp)
callq 0x55c0
movq -0x18(%rbp), %rdi
callq 0x12f00
cmpl $0x0, %eax
jne 0xc7f5
movq -0x18(%rbp), %rdi
movl -0xc(%rbp), %esi
callq 0x12f20
jmp 0xc7d9
jmp 0xc7f5
movq -0x68(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
callq 0x5280
jmp 0xc96c
movq -0x18(%rbp), %rdi
callq 0x12f00
movq -0x68(%rbp), %rdi
movl %eax, %eax
movq %rax, -0x48(%rbp)
movzwl -0x36(%rbp), %eax
movq %rax, -0x88(%rbp)
callq 0x5620
movq %rax, %rcx
movq -0x88(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x50(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x12920
movq (%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rdi
movq %rdi, -0x80(%rbp)
xorl %esi, %esi
callq 0x130a0
movq %rax, -0x78(%rbp)
jmp 0xc852
movq -0x78(%rbp), %rsi
movq -0x80(%rbp), %rdi
callq 0x13010
movl %eax, -0x8c(%rbp)
jmp 0xc867
movl -0x8c(%rbp), %eax
movl %eax, %eax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x40(%rbp), %rax
ja 0xc8f9
movq -0x18(%rbp), %rdi
xorl %esi, %esi
callq 0x130a0
movq %rax, -0x98(%rbp)
jmp 0xc891
movq -0x98(%rbp), %rsi
movq -0x68(%rbp), %rdi
movq -0x58(%rbp), %rdx
callq 0x5080
jmp 0xc8a7
movq -0x18(%rbp), %rdi
movl -0x58(%rbp), %esi
callq 0x115f0
movq -0x18(%rbp), %rdi
xorl %esi, %esi
callq 0x130a0
movq %rax, -0xa0(%rbp)
jmp 0xc8c7
movq -0xa0(%rbp), %rsi
movq -0x68(%rbp), %rdi
movq -0x40(%rbp), %rdx
movq -0x58(%rbp), %rax
subq %rax, %rdx
callq 0x5080
jmp 0xc8e4
movq -0x18(%rbp), %rdi
movq -0x40(%rbp), %rax
subq -0x58(%rbp), %rax
movl %eax, %esi
callq 0x115f0
jmp 0xc934
movq -0x18(%rbp), %rdi
xorl %esi, %esi
callq 0x130a0
movq %rax, -0xa8(%rbp)
jmp 0xc90d
movq -0xa8(%rbp), %rsi
movq -0x68(%rbp), %rdi
movq -0x40(%rbp), %rdx
callq 0x5080
jmp 0xc923
movq -0x18(%rbp), %rdi
movq -0x40(%rbp), %rax
movl %eax, %esi
callq 0x115f0
jmp 0xc94c
jmp 0xc936
movq -0x68(%rbp), %rdi
callq 0x5620
movzwl -0x36(%rbp), %ecx
cmpq %rcx, %rax
jb 0xc7bd
movb $0x1, -0x37(%rbp)
testb $0x1, -0x37(%rbp)
jne 0xc95f
movq -0x68(%rbp), %rdi
callq 0x5280
movq -0x60(%rbp), %rax
addq $0xb0, %rsp
popq %rbp
retq
movq -0x30(%rbp), %rdi
callq 0x56f0
nopw %cs:(%rax,%rax)