// File: Rjerk[P]ratabase/Parser.cpp
// Function: ratabase::Parser::parse(cmd_v&)
bool Parser::parse(cmd_v& cmd)
{
    string buf = string(buffer_);
    istringstream cmdstr(buf);
    cmd.cmd_name.clear();
    cmdstr >> cmd.cmd_name; // read command name
    cmd.obj_name.clear();
    cmdstr >> cmd.obj_name; // read object name
    string param;
    cmd.params.clear();
    while (cmdstr >> param) {
        cmd.params.push_back(param);
    }

    if (cmd_map.find(cmd.cmd_name) == cmd_map.end()) {
        string reply = "Unknown command \"" + cmd.cmd_name + "\"\n";
        writeReplyBuf(reply);
        LOG_ERROR << reply;
        return false;
    }
    cmd.type = cmd_map[cmd.cmd_name];
    switch (cmd.type) {
        case cmd_type::DISPLAY: // check 0 argument after command
            return check0(cmd);
        case cmd_type::CREATE:
        case cmd_type::SELECT:
        case cmd_type::DELETE:
        case cmd_type::SLEN:
        case cmd_type::SCLR:
        case cmd_type::LPOP:
        case cmd_type::LSIZE:
        case cmd_type::DSIZE:
            return check1(cmd); // check 1 argument after command

        case cmd_type::SSET:
        case cmd_type::SAPP:
        case cmd_type::LSET:
        case cmd_type::LPUSH:
        case cmd_type::LIDX:
        case cmd_type::LGET:
        case cmd_type::LDEL:
        case cmd_type::DGET:
        case cmd_type::DDEL:
			return check2(cmd); // check 2 arguments after command

        case cmd_type::DSET:
        case cmd_type::DADD:
			return checkMore(cmd); // check more arguments
        default:
            LOG_FATAL << "No way you come here ?!";
            return false; // never come.
    };
}

// Assembly:
subq $0x5b8, %rsp            # imm = 0x5B8
movq %rdi, 0x5a8(%rsp)
movq %rsi, 0x5a0(%rsp)
movq 0x5a8(%rsp), %rax
movq %rax, 0x48(%rsp)
addq $0x8, %rax
movq %rax, 0x50(%rsp)
leaq 0x57f(%rsp), %rdi
movq %rdi, 0x58(%rsp)
callq 0x35a0
movq 0x50(%rsp), %rsi
movq 0x58(%rsp), %rdx
leaq 0x580(%rsp), %rdi
callq 0x3480
jmp 0xa228
leaq 0x57f(%rsp), %rdi
callq 0x33d0
leaq 0x3e8(%rsp), %rdi
leaq 0x580(%rsp), %rsi
movl $0x8, %edx
callq 0x3540
jmp 0xa251
movq 0x5a0(%rsp), %rdi
addq $0x8, %rdi
callq 0x3450
movq 0x5a0(%rsp), %rsi
addq $0x8, %rsi
leaq 0x3e8(%rsp), %rdi
callq 0x3410
jmp 0xa27d
movq 0x5a0(%rsp), %rdi
addq $0x28, %rdi
callq 0x3450
movq 0x5a0(%rsp), %rsi
addq $0x28, %rsi
leaq 0x3e8(%rsp), %rdi
callq 0x3410
jmp 0xa2a9
leaq 0x3c8(%rsp), %rdi
callq 0x3490
movq 0x5a0(%rsp), %rdi
addq $0x48, %rdi
callq 0xae30
leaq 0x3e8(%rsp), %rdi
leaq 0x3c8(%rsp), %rsi
callq 0x3410
movq %rax, 0x40(%rsp)
jmp 0xa2e3
movq 0x40(%rsp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x3400
movb %al, 0x3f(%rsp)
jmp 0xa2fd
movb 0x3f(%rsp), %al
testb $0x1, %al
jne 0xa30a
jmp 0xa398
movq 0x5a0(%rsp), %rdi
addq $0x48, %rdi
leaq 0x3c8(%rsp), %rsi
callq 0xae50
jmp 0xa325
jmp 0xa2c7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x570(%rsp)
movl %eax, 0x56c(%rsp)
leaq 0x57f(%rsp), %rdi
callq 0x33d0
jmp 0xa767
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x570(%rsp)
movl %eax, 0x56c(%rsp)
jmp 0xa75a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x570(%rsp)
movl %eax, 0x56c(%rsp)
jmp 0xa74d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x570(%rsp)
movl %eax, 0x56c(%rsp)
jmp 0xa740
movq 0x5a0(%rsp), %rsi
addq $0x8, %rsi
leaq 0x11035(%rip), %rdi     # 0x1b3e0
callq 0xaee0
movq %rax, 0x30(%rsp)
jmp 0xa3b7
movq 0x30(%rsp), %rax
movq %rax, 0x3c0(%rsp)
leaq 0x11015(%rip), %rdi     # 0x1b3e0
callq 0xaf10
movq %rax, 0x3b8(%rsp)
leaq 0x3c0(%rsp), %rdi
leaq 0x3b8(%rsp), %rsi
callq 0xaec0
testb $0x1, %al
jne 0xa3f6
jmp 0xa54a
movq 0x5a0(%rsp), %rdx
addq $0x8, %rdx
leaq 0x9f1b(%rip), %rsi      # 0x14324
leaq 0x378(%rsp), %rdi
callq 0xaf90
jmp 0xa418
leaq 0x9f17(%rip), %rdx      # 0x14336
leaq 0x398(%rsp), %rdi
leaq 0x378(%rsp), %rsi
callq 0xaf40
jmp 0xa436
leaq 0x378(%rsp), %rdi
callq 0x3280
movq 0x48(%rsp), %rdi
leaq 0x398(%rsp), %rsi
callq 0xa0e0
leaq 0x9edd(%rip), %rsi      # 0x14339
leaq 0x9ee1(%rip), %rcx      # 0x14344
leaq 0x1f0(%rsp), %rdi
movl $0x3d, %edx
movl $0x4, %r8d
callq 0x11e00
jmp 0xa47d
leaq 0x1f0(%rsp), %rdi
callq 0x121a0
movq %rax, 0x28(%rsp)
jmp 0xa491
movq 0x28(%rsp), %rdi
addq $0x10, %rdi
leaq 0x398(%rsp), %rsi
callq 0x3350
jmp 0xa4a9
leaq 0x1f0(%rsp), %rdi
callq 0x12060
movb $0x0, 0x5b7(%rsp)
movl $0x1, 0x1ec(%rsp)
leaq 0x398(%rsp), %rdi
callq 0x3280
jmp 0xa708
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x570(%rsp)
movl %eax, 0x56c(%rsp)
leaq 0x378(%rsp), %rdi
callq 0x3280
jmp 0xa740
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x570(%rsp)
movl %eax, 0x56c(%rsp)
jmp 0xa538
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x570(%rsp)
movl %eax, 0x56c(%rsp)
leaq 0x1f0(%rsp), %rdi
callq 0x12060
leaq 0x398(%rsp), %rdi
callq 0x3280
jmp 0xa740
movq 0x5a0(%rsp), %rsi
addq $0x8, %rsi
leaq 0x10e83(%rip), %rdi     # 0x1b3e0
callq 0xb0c0
movq %rax, 0x20(%rsp)
jmp 0xa569
movq 0x20(%rsp), %rax
movl (%rax), %ecx
movq 0x5a0(%rsp), %rax
movl %ecx, (%rax)
movq 0x5a0(%rsp), %rax
movl (%rax), %eax
movq %rax, 0x18(%rsp)
subq $0x14, %rax
ja 0xa67c
movq 0x18(%rsp), %rax
leaq 0x9d2d(%rip), %rcx      # 0x142cc
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x48(%rsp), %rdi
movq 0x5a0(%rsp), %rsi
callq 0xa780
movb %al, 0x17(%rsp)
jmp 0xa5c0
movb 0x17(%rsp), %al
andb $0x1, %al
movb %al, 0x5b7(%rsp)
movl $0x1, 0x1ec(%rsp)
jmp 0xa708
movq 0x48(%rsp), %rdi
movq 0x5a0(%rsp), %rsi
callq 0xa920
movb %al, 0x16(%rsp)
jmp 0xa5f5
movb 0x16(%rsp), %al
andb $0x1, %al
movb %al, 0x5b7(%rsp)
movl $0x1, 0x1ec(%rsp)
jmp 0xa708
movq 0x48(%rsp), %rdi
movq 0x5a0(%rsp), %rsi
callq 0xaad0
movb %al, 0x15(%rsp)
jmp 0xa62a
movb 0x15(%rsp), %al
andb $0x1, %al
movb %al, 0x5b7(%rsp)
movl $0x1, 0x1ec(%rsp)
jmp 0xa708
movq 0x48(%rsp), %rdi
movq 0x5a0(%rsp), %rsi
callq 0xac80
movb %al, 0x14(%rsp)
jmp 0xa65f
movb 0x14(%rsp), %al
andb $0x1, %al
movb %al, 0x5b7(%rsp)
movl $0x1, 0x1ec(%rsp)
jmp 0xa708
leaq 0x9cb6(%rip), %rsi      # 0x14339
leaq 0x9cba(%rip), %rcx      # 0x14344
leaq 0x60(%rsp), %rdi
movl $0x5d, %edx
movl $0x5, %r8d
callq 0x11e00
jmp 0xa6a1
leaq 0x60(%rsp), %rdi
callq 0x121a0
movq %rax, 0x8(%rsp)
jmp 0xa6b2
movq 0x8(%rsp), %rdi
addq $0x10, %rdi
leaq 0x9c88(%rip), %rsi      # 0x1434a
callq 0x3390
jmp 0xa6c9
leaq 0x60(%rsp), %rdi
callq 0x12060
movb $0x0, 0x5b7(%rsp)
movl $0x1, 0x1ec(%rsp)
jmp 0xa708
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x570(%rsp)
movl %eax, 0x56c(%rsp)
leaq 0x60(%rsp), %rdi
callq 0x12060
jmp 0xa740
leaq 0x3c8(%rsp), %rdi
callq 0x3280
leaq 0x3e8(%rsp), %rdi
callq 0x31f0
leaq 0x580(%rsp), %rdi
callq 0x3280
movb 0x5b7(%rsp), %al
andb $0x1, %al
addq $0x5b8, %rsp            # imm = 0x5B8
retq
leaq 0x3c8(%rsp), %rdi
callq 0x3280
leaq 0x3e8(%rsp), %rdi
callq 0x31f0
leaq 0x580(%rsp), %rdi
callq 0x3280
movq 0x570(%rsp), %rdi
callq 0x3590
nopw %cs:(%rax,%rax)