// File: /workspace/github_c_makefile_star2_O2/tstword[P]MIPSAssemblerC/src/assembler.c
// Function: verify_operand_list
int verify_operand_list(struct reserved_entry *res_entry, struct operand_node *operand_list) {
    /* Check if reserved_entry is valid */
    if(res_entry->token != TOK_MNEMONIC && res_entry->token != TOK_DIRECTIVE) return 0;

    /* Verify operand list */
    struct operand_node *current_operand = operand_list;
    struct opcode_entry *entry = (struct opcode_entry *)res_entry->attrptr;

    const char *op_string = res_entry->token == TOK_DIRECTIVE ? "directive" : "mnemonic";

    for(int i = 0; i < MAX_OPERAND_COUNT; ++i) {
        if(entry->operand[i] & OPERAND_REPEAT) {
            if(current_operand == NULL || !(entry->operand[i] & current_operand->operand)) {
                report_cfg("Invalid operand combination for %s '%s' on line %ld", op_string, res_entry->id, cfg_assembler->lineno);
                return 0;
            } 
            if(current_operand->operand & OPERAND_LABEL) {
                if(get_symbol_table(cfg_assembler->symbol_table, current_operand->identifier) == NULL)
                    insert_front(cfg_assembler->decl_symlist, insert_symbol_table(cfg_assembler->symbol_table, current_operand->identifier));
            }
            
            /* Check for more operands in repeat... */
            current_operand = current_operand->next;
            while(current_operand != NULL && entry->operand[i] & current_operand->operand) {
                if(current_operand->operand & OPERAND_LABEL) {
                    if(get_symbol_table(cfg_assembler->symbol_table, current_operand->identifier) == NULL)
                        insert_front(cfg_assembler->decl_symlist, insert_symbol_table(cfg_assembler->symbol_table, current_operand->identifier));
                }
                current_operand = current_operand->next;
            }
        } else {
            if(entry->operand[i] == OPERAND_NONE) {
                if(current_operand != NULL) {
                    report_cfg("Invalid operand combiniation for %s '%s' on line %ld", op_string, res_entry->id, cfg_assembler->lineno);
                    return 0;
                }
                break;
            } else {
                if(current_operand == NULL || !(entry->operand[i] & current_operand->operand)) {
                    if(entry->operand[i] & OPERAND_OPTIONAL) continue;
                    report_cfg("Invalid operand combiniation for %s '%s' on line %ld", op_string, res_entry->id, cfg_assembler->lineno);
                    return 0;
                }
                if(current_operand->operand & OPERAND_LABEL) {
                    if(get_symbol_table(cfg_assembler->symbol_table, current_operand->identifier) == NULL)
                        insert_front(cfg_assembler->decl_symlist, insert_symbol_table(cfg_assembler->symbol_table, current_operand->identifier));
                }
            }
            current_operand = current_operand->next;
        }
    }

    if(current_operand != NULL) {
        report_cfg("Invalid operand combiniation for %s '%s' on line %ld", op_string, res_entry->id, cfg_assembler->lineno);
        return 0;
    }

    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl 0x8(%rdi), %eax
movl %eax, %ecx
orl $0x4, %ecx
cmpl $0xc, %ecx
jne 0x3d03
movq %rsi, %r12
movq %rdi, %rbx
cmpl $0xc, %eax
leaq 0x38e0(%rip), %rax      # 0x74bf
leaq 0x38e3(%rip), %r14      # 0x74c9
cmoveq %rax, %r14
movq 0x10(%rdi), %rbp
xorl %r15d, %r15d
cmpq $0x3, %r15
je 0x3cf2
movzbl 0x3(%rbp,%r15), %eax
testb $0x20, %al
jne 0x3c68
testl %eax, %eax
je 0x3cf2
testq %r12, %r12
je 0x3c85
movzbl (%r12), %ecx
testl %eax, %ecx
je 0x3c85
testb $0x1, %cl
je 0x3c5e
movq 0x8991(%rip), %rax      # 0xc5b8
movq 0x10(%rax), %rdi
movq 0x8(%r12), %rsi
callq 0x5c1a
testq %rax, %rax
jne 0x3c5e
movq 0x8977(%rip), %rax      # 0xc5b8
movq 0x10(%rax), %rdi
movq 0x18(%rax), %r13
movq 0x8(%r12), %rsi
callq 0x5bac
movq %r13, %rdi
movq %rax, %rsi
callq 0x547b
movq 0x10(%r12), %r12
jmp 0x3cea
testq %r12, %r12
je 0x3d07
movzbl (%r12), %ecx
testl %eax, %ecx
je 0x3d07
testb $0x1, %cl
je 0x3c8e
jmp 0x3ca7
testb $0x40, %al
jne 0x3cea
jmp 0x3d10
movq 0x10(%r12), %r12
testq %r12, %r12
je 0x3ce7
movb (%r12), %al
testb %al, 0x3(%rbp,%r15)
je 0x3cea
testb $0x1, %al
je 0x3c8e
movq 0x890a(%rip), %rax      # 0xc5b8
movq 0x10(%rax), %rdi
movq 0x8(%r12), %rsi
callq 0x5c1a
testq %rax, %rax
jne 0x3c8e
movq 0x88f0(%rip), %rax      # 0xc5b8
movq 0x10(%rax), %rdi
movq 0x18(%rax), %r13
movq 0x8(%r12), %rsi
callq 0x5bac
movq %r13, %rdi
movq %rax, %rsi
callq 0x547b
jmp 0x3c8e
xorl %r12d, %r12d
incq %r15
jmp 0x3bf1
testq %r12, %r12
leaq 0x380a(%rip), %rdi      # 0x7506
jne 0x3d17
pushq $0x1
popq %rbx
jmp 0x3d34
xorl %ebx, %ebx
jmp 0x3d34
leaq 0x37c4(%rip), %rdi      # 0x74d2
jmp 0x3d17
leaq 0x37ef(%rip), %rdi      # 0x7506
movq (%rbx), %rdx
movq 0x8897(%rip), %rax      # 0xc5b8
movq 0x98(%rax), %rcx
xorl %ebx, %ebx
movq %r14, %rsi
xorl %eax, %eax
callq 0x328c
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq