// File: /workspace/github_cpp_makefile_star2_O3/davmac314[P]dinit/src/includes/dinit-client.h
// Function: wait_for_reply(cpbuffer<1024u>&, int, unsigned int, observed_states_t*)
inline void wait_for_reply(cpbuffer_t &rbuffer, int fd, dinit_cptypes::handle_t handle, observed_states_t *seen_states)
{
    fill_buffer_to(rbuffer, fd, 1);

    while (rbuffer[0] >= 100) {
        // Information packet; discard.
        cp_info pkt_type = (cp_info) rbuffer[0];
        fill_buffer_to(rbuffer, fd, 2);
        unsigned pktlen = (unsigned char) rbuffer[1];

        rbuffer.consume(1);  // Consume one byte so we'll read one byte of the next packet
        fill_buffer_to(rbuffer, fd, pktlen);

        if (value(pkt_type).is_in(cp_info::SERVICEEVENT, cp_info::SERVICEEVENT5)
                && seen_states != nullptr) {

            // earlier versions do not include status info, the size in that case is
            // base_pkt_size:
            constexpr unsigned base_pkt_size = 2 + sizeof(dinit_cptypes::handle_t) + 1;

            if (pktlen < base_pkt_size) {
                throw dinit_protocol_error();
            }

            // Extract handle, check for match
            dinit_cptypes::handle_t ev_handle;
            rbuffer.extract((char *)&ev_handle, 1, sizeof(ev_handle));
            service_event_t event = static_cast<service_event_t>(rbuffer[1 + sizeof(ev_handle)]);

            if (ev_handle == handle) {
                if (event == service_event_t::STOPPED) {
                    seen_states->stopped = true;
                }
                if (event == service_event_t::STARTED) {
                    seen_states->started = true;
                }
                if (event == service_event_t::FAILEDSTART) {
                    stopped_reason_t stop_reason =
                            static_cast<stopped_reason_t>(rbuffer[base_pkt_size + 2]);

                    seen_states->failed_start = true;
                    seen_states->stop_reason = stop_reason;

                    int exit_status;
                    int exit_si_code;
                    int exit_si_status;
                    rbuffer.extract((char *)&exit_status, base_pkt_size + 5, sizeof(exit_status));
                    if (pkt_type == cp_info::SERVICEEVENT5) {
                        if (pktlen < base_pkt_size + STATUS_BUFFER5_SIZE) {
                            throw dinit_protocol_error();
                        }
                        exit_si_code = exit_status;
                        rbuffer.extract((char *)&exit_si_status,
                                base_pkt_size + 5 + sizeof(exit_si_code), sizeof(exit_si_status));

                        seen_states->exit_si_code = exit_si_code;
                        seen_states->exit_si_status = exit_si_status;
                    }

                    seen_states->exit_status = exit_status;
                }
            }
        }

        rbuffer.consume(pktlen - 1);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rcx, 0x18(%rsp)
movl %edx, 0x24(%rsp)
movl %esi, %r14d
movq %rdi, %r15
cmpl $0x0, 0x404(%r15)
jne 0xf05b
movq %r15, %rdi
movl %r14d, %esi
callq 0xef52
testl %eax, %eax
jg 0xf015
cmpl $-0x1, %eax
jne 0xf053
callq 0x4040
cmpl $0x4, (%rax)
je 0xf015
movq %rax, %r12
movl $0x4, %edi
callq 0x4190
movl (%r12), %ecx
jmp 0xf3b4
testl %eax, %eax
je 0xf391
movl 0x400(%r15), %eax
leal -0x400(%rax), %ecx
cmpl $0x400, %eax            # imm = 0x400
cmovbl %eax, %ecx
movb (%r15,%rcx), %bl
cmpb $0x64, %bl
movq 0x18(%rsp), %r12
jl 0xf382
movl 0x404(%r15), %eax
cmpl $0x1, %eax
ja 0xf0c0
movq %r15, %rdi
movl %r14d, %esi
callq 0xef52
testl %eax, %eax
jg 0xf082
cmpl $-0x1, %eax
jne 0xf0b1
callq 0x4040
cmpl $0x4, (%rax)
je 0xf082
jmp 0xf3a3
testl %eax, %eax
je 0xf391
movl 0x404(%r15), %eax
movl 0x400(%r15), %ecx
leal 0x1(%rcx), %edx
addl $0xfffffc01, %ecx       # imm = 0xFFFFFC01
cmpl $0x400, %edx            # imm = 0x400
cmovbl %edx, %ecx
movzbl (%r15,%rcx), %ebp
movl %ecx, 0x400(%r15)
decl %eax
movl %eax, 0x404(%r15)
cmpl %ebp, 0x404(%r15)
jae 0xf122
movq %r15, %rdi
movl %r14d, %esi
callq 0xef52
testl %eax, %eax
jg 0xf0ee
cmpl $-0x1, %eax
jne 0xf11a
callq 0x4040
cmpl $0x4, (%rax)
je 0xf0ee
jmp 0xf3a3
testl %eax, %eax
je 0xf391
testq %r12, %r12
je 0xf1b8
movl %ebx, %eax
andb $0x7e, %al
cmpb $0x64, %al
jne 0xf1b8
cmpb $0x6, %bpl
jbe 0xf3c7
movl 0x400(%r15), %edi
leal 0x1(%rdi), %eax
leal -0x3ff(%rdi), %r13d
cmpl $0x400, %eax            # imm = 0x400
cmovbl %eax, %r13d
leal -0x3fd(%r13), %eax
cmpl $0xfffffbfe, %eax       # imm = 0xFFFFFBFE
ja 0xf1c4
movl $0x400, %r12d           # imm = 0x400
subl %r13d, %r12d
movl %r13d, %esi
addq %r15, %rsi
leaq 0x14(%rsp), %rax
movq %rdi, 0x8(%rsp)
movq %rax, %rdi
movq %r12, %rdx
callq 0x4270
leaq (%rsp,%r12), %rdi
addq $0x14, %rdi
movq 0x18(%rsp), %r12
addl $0xfffffc04, %r13d      # imm = 0xFFFFFC04
movq %r15, %rsi
movq %r13, %rdx
callq 0x4270
movq 0x8(%rsp), %rdi
leaq 0x14(%rsp), %rax
jmp 0xf1ca
movl 0x400(%r15), %edi
jmp 0xf344
movl %r13d, %eax
addq %r15, %rax
movl 0x24(%rsp), %ecx
cmpl %ecx, (%rax)
jne 0xf344
leal 0x5(%rdi), %eax
leal -0x3fb(%rdi), %ecx
cmpl $0x400, %eax            # imm = 0x400
cmovbl %eax, %ecx
movzbl (%r15,%rcx), %eax
testl %eax, %eax
je 0xf295
cmpl $0x2, %eax
je 0xf20d
cmpl $0x1, %eax
jne 0xf344
movb $0x1, 0x1(%r12)
jmp 0xf344
leal 0x9(%rdi), %eax
leal -0x3f7(%rdi), %ecx
movl $0x400, %edx            # imm = 0x400
cmpl %edx, %eax
cmovbl %eax, %ecx
movsbl (%r15,%rcx), %eax
movb $0x1, 0x2(%r12)
leal 0xc(%rdi), %ecx
leal -0x3f4(%rdi), %r13d
cmpl %edx, %ecx
cmovbl %ecx, %r13d
movl %eax, 0x4(%r12)
leal -0x3fd(%r13), %eax
movl %r13d, %esi
cmpl $0xfffffbfe, %eax       # imm = 0xFFFFFBFE
ja 0xf29f
movl $0x400, %r12d           # imm = 0x400
subl %r13d, %r12d
addq %r15, %rsi
movq %rdi, 0x8(%rsp)
leaq 0x4(%rsp), %rdi
movq %r12, %rdx
callq 0x4270
leaq (%rsp,%r12), %rdi
addq $0x4, %rdi
movq 0x18(%rsp), %r12
addl $0xfffffc04, %r13d      # imm = 0xFFFFFC04
movq %r15, %rsi
movq %r13, %rdx
callq 0x4270
movq 0x8(%rsp), %rdi
jmp 0xf2a7
movb $0x1, (%r12)
jmp 0xf344
movl (%r15,%rsi), %eax
movl %eax, 0x4(%rsp)
cmpb $0x65, %bl
jne 0xf32a
cmpb $0x14, %bpl
jbe 0xf3c7
leal 0x10(%rdi), %eax
leal -0x3f0(%rdi), %r13d
cmpl $0x400, %eax            # imm = 0x400
cmovbl %eax, %r13d
movl 0x4(%rsp), %ebx
leal -0x3fd(%r13), %eax
movl %r13d, %esi
cmpl $0xfffffbfe, %eax       # imm = 0xFFFFFBFE
ja 0xf330
movl $0x400, %r12d           # imm = 0x400
subl %r13d, %r12d
addq %r15, %rsi
leaq 0x10(%rsp), %rax
movq %rdi, 0x8(%rsp)
movq %rax, %rdi
movq %r12, %rdx
callq 0x4270
leaq (%rsp,%r12), %rdi
addq $0x10, %rdi
movq 0x18(%rsp), %r12
addl $0xfffffc04, %r13d      # imm = 0xFFFFFC04
movq %r15, %rsi
movq %r13, %rdx
callq 0x4270
movq 0x8(%rsp), %rdi
leaq 0x10(%rsp), %rsi
jmp 0xf333
movl 0x4(%rsp), %ebx
jmp 0xf33f
addq %r15, %rsi
movl (%rsi), %eax
movl %ebx, 0xc(%r12)
movl %eax, 0x10(%r12)
movl %ebx, 0x8(%r12)
movl %ebp, %eax
decl %eax
leal (%rdi,%rbp), %ecx
decl %ecx
movl $0xfffffc00, %edi       # imm = 0xFFFFFC00
leal (%rcx,%rdi), %edx
movl $0x400, %esi            # imm = 0x400
cmpl %esi, %ecx
cmovbl %ecx, %edx
movl %edx, 0x400(%r15)
subl %eax, 0x404(%r15)
leal (%rdx,%rdi), %eax
cmpl %esi, %edx
cmovbl %edx, %eax
movb (%r15,%rax), %bl
cmpb $0x63, %bl
jg 0xf082
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x4, %edi
callq 0x4190
movl $0x0, (%rax)
jmp 0xf3b6
movq %rax, %r13
movl $0x4, %edi
callq 0x4190
movl (%r13), %ecx
movl %ecx, (%rax)
leaq 0xd783(%rip), %rsi      # 0x1cb40
movq %rax, %rdi
xorl %edx, %edx
callq 0x44d0
movl $0x1, %edi
callq 0x4190
leaq 0xd728(%rip), %rsi      # 0x1cb00
jmp 0xf3bd