// File: cybozu[P]yrmcds/src/counter/counter.cpp
// Function: yrmcds::counter::request::parse(char const*, unsigned long)
void request::parse(const char* p, std::size_t len) noexcept {
    if( len < HEADER_SIZE ) return;  // incomplete
    std::uint32_t body_length;
    cybozu::ntoh(p + 4, body_length);
    if( len < HEADER_SIZE + body_length ) return;  // incomplete

    m_command = (counter::command)*(const uint8_t*)(p + 1);
    m_flags = *(const uint8_t*)(p + 2);
    m_body_length = body_length;
    m_opaque = p + 8;

    m_request_length = HEADER_SIZE + body_length;
    const char* body = p + HEADER_SIZE;
    std::uint16_t name_len;

    if( *(const std::uint8_t*)p != REQUEST_MAGIC )
        return;  // invalid

    switch( m_command ) {
    case counter::command::Noop:
    case counter::command::Stats:
    case counter::command::Dump:
        // no body
        break;

    case counter::command::Get:
        if( body_length < 2 )
            return;     // invalid
        cybozu::ntoh(body, name_len);
        if( name_len == 0 || 2U + name_len > body_length )
            return;     // invalid
        m_name.len = name_len;
        m_name.p = body + 2;
        break;

    case counter::command::Acquire:
        if( body_length < 10 )
            return;     // invalid
        cybozu::ntoh(body, m_resources);
        if( m_resources == 0 )
            return;     // invalid
        cybozu::ntoh(body + 4, m_maximum);
        if( m_maximum < m_resources )
            return;
        cybozu::ntoh(body + 8, name_len);
        if( name_len == 0 || 10U + name_len > body_length )
            return;     // invalid
        m_name.len = name_len;
        m_name.p = body + 10;
        break;

    case counter::command::Release:
        if( body_length < 6 )
            return;     // invalid
        cybozu::ntoh(body, m_resources);
        cybozu::ntoh(body + 4, name_len);
        if( name_len == 0 || 6U + name_len > body_length )
            return;     // invalid
        m_name.len = name_len;
        m_name.p = body + 6;
        break;

    default:
        m_command = counter::command::Unknown;
        m_status = counter::status::UnknownCommand;
        return;
    }

    m_status = counter::status::OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x38(%rbp)
cmpq $0xc, -0x18(%rbp)
jae 0x5bdf8
jmp 0x5bff6
movq -0x10(%rbp), %rdi
addq $0x4, %rdi
leaq -0x1c(%rbp), %rsi
callq 0x5d360
movq -0x18(%rbp), %rax
movl -0x1c(%rbp), %ecx
addq $0xc, %rcx
cmpq %rcx, %rax
jae 0x5be1e
jmp 0x5bff6
movq -0x38(%rbp), %rax
movq -0x10(%rbp), %rcx
movb 0x1(%rcx), %cl
movb %cl, 0x8(%rax)
movq -0x10(%rbp), %rcx
movb 0x2(%rcx), %cl
movb %cl, 0x9(%rax)
movl -0x1c(%rbp), %ecx
movl %ecx, 0xc(%rax)
movq -0x10(%rbp), %rcx
addq $0x8, %rcx
movq %rcx, 0x10(%rax)
movl -0x1c(%rbp), %ecx
addq $0xc, %rcx
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
addq $0xc, %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x90, %eax
je 0x5be71
jmp 0x5bff6
movq -0x38(%rbp), %rax
movzbl 0x8(%rax), %eax
movq %rax, -0x40(%rbp)
subq $0x11, %rax
ja 0x5bfe0
movq -0x40(%rbp), %rax
leaq 0x20dce(%rip), %rcx     # 0x7cc60
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
jmp 0x5bfee
cmpl $0x2, -0x1c(%rbp)
jae 0x5beab
jmp 0x5bff6
movq -0x28(%rbp), %rdi
leaq -0x2a(%rbp), %rsi
callq 0x5d3a0
movzwl -0x2a(%rbp), %eax
cmpl $0x0, %eax
je 0x5becd
movzwl -0x2a(%rbp), %eax
addl $0x2, %eax
cmpl -0x1c(%rbp), %eax
jbe 0x5bed2
jmp 0x5bff6
movq -0x38(%rbp), %rax
movzwl -0x2a(%rbp), %ecx
movq %rcx, 0x28(%rax)
movq -0x28(%rbp), %rcx
addq $0x2, %rcx
movq %rcx, 0x20(%rax)
jmp 0x5bfee
cmpl $0xa, -0x1c(%rbp)
jae 0x5befa
jmp 0x5bff6
movq -0x38(%rbp), %rsi
movq -0x28(%rbp), %rdi
addq $0x18, %rsi
callq 0x5d360
movq -0x38(%rbp), %rax
cmpl $0x0, 0x18(%rax)
jne 0x5bf1a
jmp 0x5bff6
movq -0x38(%rbp), %rsi
movq -0x28(%rbp), %rdi
addq $0x4, %rdi
addq $0x1c, %rsi
callq 0x5d360
movq -0x38(%rbp), %rcx
movl 0x1c(%rcx), %eax
cmpl 0x18(%rcx), %eax
jae 0x5bf40
jmp 0x5bff6
movq -0x28(%rbp), %rdi
addq $0x8, %rdi
leaq -0x2a(%rbp), %rsi
callq 0x5d3a0
movzwl -0x2a(%rbp), %eax
cmpl $0x0, %eax
je 0x5bf66
movzwl -0x2a(%rbp), %eax
addl $0xa, %eax
cmpl -0x1c(%rbp), %eax
jbe 0x5bf6b
jmp 0x5bff6
movq -0x38(%rbp), %rax
movzwl -0x2a(%rbp), %ecx
movq %rcx, 0x28(%rax)
movq -0x28(%rbp), %rcx
addq $0xa, %rcx
movq %rcx, 0x20(%rax)
jmp 0x5bfee
cmpl $0x6, -0x1c(%rbp)
jae 0x5bf8d
jmp 0x5bff6
movq -0x38(%rbp), %rsi
movq -0x28(%rbp), %rdi
addq $0x18, %rsi
callq 0x5d360
movq -0x28(%rbp), %rdi
addq $0x4, %rdi
leaq -0x2a(%rbp), %rsi
callq 0x5d3a0
movzwl -0x2a(%rbp), %eax
cmpl $0x0, %eax
je 0x5bfc4
movzwl -0x2a(%rbp), %eax
addl $0x6, %eax
cmpl -0x1c(%rbp), %eax
jbe 0x5bfc6
jmp 0x5bff6
movq -0x38(%rbp), %rax
movzwl -0x2a(%rbp), %ecx
movq %rcx, 0x28(%rax)
movq -0x28(%rbp), %rcx
addq $0x6, %rcx
movq %rcx, 0x20(%rax)
jmp 0x5bfee
movq -0x38(%rbp), %rax
movb $0x12, 0x8(%rax)
movb $-0x7f, 0x30(%rax)
jmp 0x5bff6
movq -0x38(%rbp), %rax
movb $0x0, 0x30(%rax)
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)