// File: davmac314[P]dinit/src/dinitctl.cc
// Function: wait_service_state(int, cpbuffer<1024u>&, unsigned int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, bool, bool)
static int wait_service_state(int socknum, cpbuffer_t &rbuffer, handle_t handle,
        const std::string &service_name, bool do_stop, bool verbose)
{
    // Wait until service started:
    int r = rbuffer.fill_to(socknum, 2);
    while (r > 0) {
        if (rbuffer[0] >= 100) {
            unsigned pktlen = (unsigned char) rbuffer[1];
            fill_buffer_to(rbuffer, socknum, pktlen);

            if (value((cp_info)rbuffer[0]).is_in(cp_info::SERVICEEVENT, cp_info::SERVICEEVENT5)) {
                int ret = process_service_event(rbuffer, pktlen, handle, service_name, do_stop, verbose);
                if (ret >= 0) {
                    return ret;
                }
            }
            else {
                rbuffer.consume(pktlen);
            }

            r = rbuffer.fill_to(socknum, 2);
        }
        else {
            // Not an information packet?
            throw dinit_protocol_error();
        }
    }

    if (r == -1) {
        perror("dinitctl: read");
    }
    else {
        throw dinit_protocol_error();
    }

    return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movl %r8d, -0x5c(%rbp)
movq %rcx, %rax
movl -0x5c(%rbp), %ecx
movq %rax, -0x58(%rbp)
movl %edx, %eax
movq -0x58(%rbp), %rdx
movl %eax, -0x4c(%rbp)
movq %rsi, %rax
movl -0x4c(%rbp), %esi
movq %rax, -0x48(%rbp)
movl %edi, %r8d
movq -0x48(%rbp), %rdi
movb %r9b, %al
movl %r8d, -0x8(%rbp)
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
andb $0x1, %cl
movb %cl, -0x21(%rbp)
andb $0x1, %al
movb %al, -0x22(%rbp)
movq -0x10(%rbp), %rdi
movl -0x8(%rbp), %esi
movl $0x2, %edx
callq 0x11710
movl %eax, -0x28(%rbp)
cmpl $0x0, -0x28(%rbp)
jle 0xcb0e
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0x113e0
movsbl %al, %eax
cmpl $0x64, %eax
jl 0xcaec
movq -0x10(%rbp), %rdi
movl $0x1, %esi
callq 0x113e0
movzbl %al, %eax
movl %eax, -0x2c(%rbp)
movq -0x10(%rbp), %rdi
movl -0x8(%rbp), %esi
movl -0x2c(%rbp), %edx
callq 0x11440
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0x113e0
movb %al, -0x39(%rbp)
leaq -0x39(%rbp), %rdi
callq 0x125d0
movq %rax, -0x38(%rbp)
movb $0x64, -0x3a(%rbp)
movb $0x65, -0x3b(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0x3a(%rbp), %rsi
leaq -0x3b(%rbp), %rdx
callq 0x12600
testb $0x1, %al
jne 0xca8f
jmp 0xcaca
movq -0x10(%rbp), %rdi
movl -0x2c(%rbp), %esi
movl -0x14(%rbp), %edx
movq -0x20(%rbp), %rcx
movb -0x21(%rbp), %r8b
movb -0x22(%rbp), %al
andb $0x1, %r8b
andb $0x1, %al
movzbl %r8b, %r8d
movzbl %al, %r9d
callq 0xcf00
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x40(%rbp)
jl 0xcac8
movl -0x40(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xcb46
jmp 0xcad6
movq -0x10(%rbp), %rdi
movl -0x2c(%rbp), %esi
callq 0x115f0
movq -0x10(%rbp), %rdi
movl -0x8(%rbp), %esi
movl $0x2, %edx
callq 0x11710
movl %eax, -0x28(%rbp)
jmp 0xcb09
movl $0x1, %edi
callq 0x51c0
movq %rax, %rdi
leaq 0x24fe0(%rip), %rsi     # 0x31ae0
xorl %eax, %eax
movl %eax, %edx
callq 0x56b0
jmp 0xca11
cmpl $-0x1, -0x28(%rbp)
jne 0xcb22
leaq 0x177fe(%rip), %rdi     # 0x24319
callq 0x5350
jmp 0xcb3f
movl $0x1, %edi
callq 0x51c0
movq %rax, %rdi
leaq 0x24faa(%rip), %rsi     # 0x31ae0
xorl %eax, %eax
movl %eax, %edx
callq 0x56b0
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nop