// File: /workspace/github_cpp_makefile_star2_O3/davmac314[P]dinit/src/includes/dinit-client.h
// Function: check_protocol_version(int, int, cpbuffer<1024u>&, int)
inline uint16_t check_protocol_version(int minversion, int version, cpbuffer_t &rbuffer, int fd)
{
    using namespace dinit_cptypes;
    constexpr int bufsize = 1;
    char buf[bufsize] = { (char)cp_cmd::QUERYVERSION };
    write_all_x(fd, buf, bufsize);

    wait_for_reply(rbuffer, fd);
    if (rbuffer[0] != (char)cp_rply::CPVERSION) {
        throw cp_read_exception{0};
    }

    // cp_rply::CVERSION, (2 byte) minimum compatible version, (2 byte) actual version
    constexpr int rbufsize = 1 + 2 * sizeof(uint16_t);
    fill_buffer_to(rbuffer, fd, rbufsize);
    uint16_t rminversion;
    uint16_t cpversion;

    rbuffer.extract(reinterpret_cast<char *>(&rminversion), 1, sizeof(uint16_t));
    rbuffer.extract(reinterpret_cast<char *>(&cpversion), 1 + sizeof(uint16_t), sizeof(uint16_t));
    rbuffer.consume(rbufsize);

    if (rminversion > version) {
        // We are too old
        throw cp_old_client_exception();
    }
    if (cpversion < minversion) {
        // Server is too old
        throw cp_old_server_exception();
    }

    return cpversion;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %ecx, %r15d
movq %rdx, %r14
movl %esi, %ebp
movl %edi, 0x14(%rsp)
leaq 0x13(%rsp), %r12
movb $0x0, (%r12)
movl $0x1, %ebx
xorl %r13d, %r13d
movl %ebx, %edx
movl %r15d, %edi
movq %r12, %rsi
callq 0x30c0
cmpl $-0x1, %eax
je 0x4441
addl %eax, %r13d
movslq %eax, %rcx
addq %rcx, %r12
subl %eax, %ebx
jmp 0x444b
callq 0x3040
cmpl $0x4, (%rax)
jne 0x44c0
testl %ebx, %ebx
jg 0x4422
cmpl $-0x1, %r13d
je 0x44c5
movq %r14, %rdi
movl %r15d, %esi
callq 0x4653
movl 0x400(%r14), %eax
leal -0x400(%rax), %ecx
cmpl $0x400, %eax            # imm = 0x400
cmovbl %eax, %ecx
cmpb $0x3a, (%r14,%rcx)
jne 0x460c
cmpl $0x4, 0x404(%r14)
ja 0x44f2
movq %r14, %rdi
movl %r15d, %esi
callq 0x4980
testl %eax, %eax
jg 0x4480
cmpl $-0x1, %eax
jne 0x44ea
callq 0x3040
cmpl $0x4, (%rax)
je 0x4480
movq %rax, %r12
movl $0x4, %edi
callq 0x3130
movl (%r12), %ecx
movl %ecx, (%rax)
jmp 0x461c
testl %r13d, %r13d
jg 0x4455
movl $0x4, %edi
callq 0x3130
movq %rax, %rbx
callq 0x3040
movl (%rax), %eax
movl %eax, (%rbx)
leaq 0x75be(%rip), %rsi      # 0xbaa0
movq %rbx, %rdi
jmp 0x4626
testl %eax, %eax
je 0x460c
movl 0x400(%r14), %r13d
leal 0x1(%r13), %eax
movl $0xfffffc01, %ecx       # imm = 0xFFFFFC01
leal (%rcx,%r13), %r15d
cmpl $0x400, %eax            # imm = 0x400
cmovbl %eax, %r15d
addl %r15d, %ecx
cmpl $0xfffffbfe, %ecx       # imm = 0xFFFFFBFE
ja 0x4552
movl $0x400, %r12d           # imm = 0x400
subl %r15d, %r12d
movl %r15d, %esi
addq %r14, %rsi
leaq 0x10(%rsp), %rdi
movq %r12, %rdx
callq 0x31f0
leaq (%rsp,%r12), %rdi
addq $0x10, %rdi
addl $0xfffffc02, %r15d      # imm = 0xFFFFFC02
movq %r14, %rsi
movq %r15, %rdx
callq 0x31f0
jmp 0x455f
movl %r15d, %eax
movzwl (%r14,%rax), %eax
movw %ax, 0x10(%rsp)
leal 0x3(%r13), %eax
leal -0x3fd(%r13), %r15d
cmpl $0x400, %eax            # imm = 0x400
cmovbl %eax, %r15d
leal -0x3ff(%r15), %eax
cmpl $0xfffffbfe, %eax       # imm = 0xFFFFFBFE
ja 0x45b9
movl $0x400, %r12d           # imm = 0x400
subl %r15d, %r12d
movl %r15d, %esi
addq %r14, %rsi
leaq 0xe(%rsp), %rdi
movq %r12, %rdx
callq 0x31f0
leaq (%rsp,%r12), %rdi
addq $0xe, %rdi
addl $0xfffffc02, %r15d      # imm = 0xFFFFFC02
movq %r14, %rsi
movq %r15, %rdx
callq 0x31f0
jmp 0x45c6
movl %r15d, %eax
movzwl (%r14,%rax), %eax
movw %ax, 0xe(%rsp)
leal 0x5(%r13), %eax
addl $0xfffffc05, %r13d      # imm = 0xFFFFFC05
cmpl $0x400, %eax            # imm = 0x400
cmovbl %eax, %r13d
movl %r13d, 0x400(%r14)
addl $-0x5, 0x404(%r14)
movzwl 0x10(%rsp), %eax
cmpl %ebp, %eax
jg 0x462d
movzwl 0xe(%rsp), %eax
cmpl 0x14(%rsp), %eax
jl 0x4640
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x4, %edi
callq 0x3130
movl $0x0, (%rax)
leaq 0x746d(%rip), %rsi      # 0xba90
movq %rax, %rdi
xorl %edx, %edx
callq 0x3430
movl $0x1, %edi
callq 0x3130
leaq 0x7422(%rip), %rsi      # 0xba60
jmp 0x4623
movl $0x1, %edi
callq 0x3130
leaq 0x7427(%rip), %rsi      # 0xba78
jmp 0x4623