// File: /workspace/github_c_makefile_star2_O3/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 0x4145
movq %rsi, %r12
movq %rdi, %rbx
cmpl $0xc, %eax
leaq 0x349b(%rip), %rax      # 0x74bf
leaq 0x349e(%rip), %r14      # 0x74c9
cmoveq %rax, %r14
movq 0x10(%rdi), %rbp
xorl %r15d, %r15d
movzbl 0x3(%rbp,%r15), %eax
testb $0x20, %al
jne 0x40a3
testl %eax, %eax
je 0x4132
testq %r12, %r12
je 0x40c0
movzbl (%r12), %ecx
testl %eax, %ecx
je 0x40c0
testb $0x1, %cl
je 0x4099
movq 0x8556(%rip), %rax      # 0xc5b8
movq 0x10(%rax), %rdi
movq 0x8(%r12), %rsi
callq 0x616b
testq %rax, %rax
jne 0x4099
movq 0x853c(%rip), %rax      # 0xc5b8
movq 0x10(%rax), %rdi
movq 0x18(%rax), %r13
movq 0x8(%r12), %rsi
callq 0x60b1
movq %r13, %rdi
movq %rax, %rsi
callq 0x5963
movq 0x10(%r12), %r12
jmp 0x4125
testq %r12, %r12
je 0x4149
movzbl (%r12), %ecx
testl %eax, %ecx
je 0x4149
testb $0x1, %cl
je 0x40c9
jmp 0x40e2
testb $0x40, %al
jne 0x4125
jmp 0x4152
movq 0x10(%r12), %r12
testq %r12, %r12
je 0x4122
movb (%r12), %al
testb %al, 0x3(%rbp,%r15)
je 0x4125
testb $0x1, %al
je 0x40c9
movq 0x84cf(%rip), %rax      # 0xc5b8
movq 0x10(%rax), %rdi
movq 0x8(%r12), %rsi
callq 0x616b
testq %rax, %rax
jne 0x40c9
movq 0x84b5(%rip), %rax      # 0xc5b8
movq 0x10(%rax), %rdi
movq 0x18(%rax), %r13
movq 0x8(%r12), %rsi
callq 0x60b1
movq %r13, %rdi
movq %rax, %rsi
callq 0x5963
jmp 0x40c9
xorl %r12d, %r12d
incq %r15
cmpq $0x3, %r15
jne 0x4036
testq %r12, %r12
leaq 0x33ca(%rip), %rdi      # 0x7506
jne 0x4159
movl $0x1, %ebx
jmp 0x4176
xorl %ebx, %ebx
jmp 0x4176
leaq 0x3382(%rip), %rdi      # 0x74d2
jmp 0x4159
leaq 0x33ad(%rip), %rdi      # 0x7506
movq (%rbx), %rdx
movq 0x8455(%rip), %rax      # 0xc5b8
movq 0x98(%rax), %rcx
xorl %ebx, %ebx
movq %r14, %rsi
xorl %eax, %eax
callq 0x365c
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq