// File: /workspace/github_cpp_makefile_star2_O2/eniac[P]Mantis/src/compile/compile_p4.cpp
// Function: findMalleableUsage(std::vector<MblRefNode*, std::allocator<MblRefNode*>>, std::unordered_map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, P4RMalleableValueNode*, std::hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::equal_to<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const, P4RMalleableValueNode*>>>, std::unordered_map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, P4RMalleableFieldNode*, std::hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::equal_to<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const, P4RMalleableFieldNode*>>>, std::vector<AstNode*, std::allocator<AstNode*>>*, std::unordered_map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, int, std::hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::equal_to<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const, int>>>*)
void findMalleableUsage(
            vector<MblRefNode*> mblRefs,
            const unordered_map<string, P4RMalleableValueNode*> mblValues,
            const unordered_map<string, P4RMalleableFieldNode*> mblFields,
            vector<AstNode*>* nodeArray,
            unordered_map<string, int>* mblUsage) {

    while (!mblRefs.empty()) {
        MblRefNode* varRef = mblRefs.front();
        mblRefs.erase(mblRefs.begin());

        string* varName = varRef->name_->word_;

        if(mblUsage->find(*varName) != mblUsage->end()) {
            continue;
        }

        // Find the usage index: 0 - ingress, 1 - egress, 2 - both
        bool in_ingress = false;
        bool in_egress = false;
        // One var could be used in multiple tbls
        for (auto node : *nodeArray) {
            // Though filter P4RMalleableTableNode, its table node is still searched
            if(typeContains(node, "TableNode") && !typeContains(node, "P4R")) {
                TableNode* table = dynamic_cast<TableNode*>(node);
                string table_name = *(table->name_->word_);

                // Try to determine if the table uses the mblRef
                bool useMblRef = false;
                if(table->toString().find(*varName) != string::npos) {
                    useMblRef = true;
                } else {
                    // Check if used actions
                    for (auto action : *table->actions_->list_) {
                        for (auto tmp_node : *nodeArray) {
                            if(typeContains(tmp_node, "ActionNode")) {
                                ActionNode* tmp_action_node = dynamic_cast<ActionNode*>(tmp_node);
                                string tmp_action_name = tmp_action_node->name_->toString();
                                if(tmp_action_name.compare(action->name_->toString())==0) {
                                    if(tmp_action_node->toString().find(*varName) != string::npos) {
                                        useMblRef = true;
                                        break;
                                    }
                                }
                            }
                        }
                        if(useMblRef) {
                            break;
                        }                        
                    }
                }
                if(useMblRef) {
                    if(findTblInIng(table_name, *nodeArray)) {
                        in_ingress = true;
                    } else {
                        in_egress = true;
                    }
                }
            }
        }
        if(in_ingress && in_egress) {
            mblUsage->emplace(*varName, USAGE::BOTH);
        } else if (in_ingress) {
            mblUsage->emplace(*varName, USAGE::INGRESS);
        } else if (in_egress) {
            mblUsage->emplace(*varName, USAGE::EGRESS);
        } else {
            PANIC("Can not find the target mblRef %s usage\n", varName->c_str());
        }
    }
    PRINT_VERBOSE("mbl usage dict:\n");
    for (auto i : *mblUsage) {
        PRINT_VERBOSE("  %s : %d\n", i.first.c_str(), i.second);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %r8, -0x40(%rbp)
movq %rcx, -0x58(%rbp)
movq %rdi, %r14
leaq -0x110(%rbp), %r13
leaq 0x10bbf(%rip), %r15     # 0x7a94f
leaq -0xd0(%rbp), %r12
movq %rdi, -0x50(%rbp)
movq (%r14), %rsi
cmpq 0x8(%r14), %rsi
je 0x6a098
movq (%rsi), %rbx
movq %r14, %rdi
callq 0x71054
movq 0x80(%rbx), %rax
movq 0x40(%rax), %rbx
movq -0x40(%rbp), %rdi
movq %rbx, %rsi
callq 0x69246
testq %rax, %rax
jne 0x69d9b
movq -0x58(%rbp), %rax
movq (%rax), %rcx
movq 0x8(%rax), %rax
movq %rax, -0x90(%rbp)
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movq -0x40(%rbp), %rdi
movq %rbx, -0x48(%rbp)
cmpq -0x90(%rbp), %rcx
je 0x6a04f
movq %rcx, -0x60(%rbp)
movq (%rcx), %rbx
movq %rbx, %rdi
leaq 0x10dbc(%rip), %rsi     # 0x7abd1
callq 0x71cf8
testb %al, %al
je 0x6a03a
movq %rbx, %rdi
leaq 0x10a7e(%rip), %rsi     # 0x7a8aa
callq 0x71cf8
testb %al, %al
jne 0x6a03a
testq %rbx, %rbx
je 0x69e5b
movq %rbx, %rdi
leaq 0x2a848(%rip), %rsi     # 0x94690
leaq 0x2b059(%rip), %rdx     # 0x94ea8
xorl %ecx, %ecx
callq 0x41480
movq %rax, %rbx
jmp 0x69e5d
xorl %ebx, %ebx
movq 0x40(%rbx), %rax
movq 0x40(%rax), %rsi
leaq -0x88(%rbp), %rdi
callq 0x415b0
movq (%rbx), %rax
movq %r13, %rdi
movq %rbx, %rsi
callq *(%rax)
movq %r13, %rdi
movq -0x48(%rbp), %rsi
xorl %edx, %edx
callq 0x41890
movq %rax, %r14
movq %r13, %rdi
callq 0x418c8
cmpq $-0x1, %r14
jne 0x69ff0
movq 0x50(%rbx), %rax
movq 0x40(%rax), %rax
movq (%rax), %rcx
movq 0x8(%rax), %rax
movq %rax, -0x98(%rbp)
movq %rcx, %rax
movl $0x0, -0x34(%rbp)
cmpq -0x98(%rbp), %rax
je 0x69fea
movq %rax, -0xa0(%rbp)
movq (%rax), %rax
movq %rax, -0xb0(%rbp)
movq -0x58(%rbp), %rax
movq (%rax), %r12
movq 0x8(%rax), %r14
movq %r14, -0xa8(%rbp)
cmpq %r14, %r12
je 0x69fcc
movq (%r12), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x71cf8
testb %al, %al
je 0x69fc3
movq %rbx, %rdi
leaq 0x2a777(%rip), %rsi     # 0x94690
leaq 0x2b030(%rip), %rdx     # 0x94f50
xorl %ecx, %ecx
callq 0x41480
movq %rax, %rbx
movq 0x40(%rax), %rsi
movq (%rsi), %rax
movq %r13, %rdi
callq *(%rax)
movq -0xb0(%rbp), %rax
movq 0x40(%rax), %rsi
movq (%rsi), %rax
leaq -0xf0(%rbp), %r14
movq %r14, %rdi
callq *(%rax)
movq %r13, %r15
movq %r13, %rdi
movq %r14, %rsi
callq 0x41040
movl %eax, %r13d
movq %r14, %rdi
callq 0x418c8
testl %r13d, %r13d
je 0x69f73
movb $0x1, %r13b
jmp 0x69fa5
movq (%rbx), %rax
movq %r14, %rdi
movq %rbx, %rsi
callq *(%rax)
xorl %r13d, %r13d
movq %r14, %rdi
movq -0x48(%rbp), %rsi
xorl %edx, %edx
callq 0x41890
movq %rax, %rbx
movq %r14, %rdi
callq 0x418c8
cmpq $-0x1, %rbx
je 0x69f6e
movb $0x1, %al
movl %eax, -0x34(%rbp)
movq %r15, %rdi
callq 0x418c8
testb %r13b, %r13b
movq %r15, %r13
leaq 0x10995(%rip), %r15     # 0x7a94f
movq -0xa8(%rbp), %r14
je 0x69fcc
addq $0x8, %r12
jmp 0x69eef
movq -0xa0(%rbp), %rax
addq $0x8, %rax
testb $0x1, -0x34(%rbp)
leaq -0xd0(%rbp), %r12
je 0x69ebf
jmp 0x69ff0
testb $0x1, -0x34(%rbp)
je 0x6a02e
movq %r12, %rdi
leaq -0x88(%rbp), %rsi
callq 0x415b0
movq %r12, %rdi
movq -0x58(%rbp), %rsi
callq 0x72c6e
movl %eax, %ebx
movq %r12, %rdi
callq 0x418c8
movl -0x2c(%rbp), %ecx
movzbl %cl, %ecx
testb %bl, %bl
pushq $0x1
popq %rax
cmovel %eax, %ecx
movl %ecx, -0x2c(%rbp)
movl -0x30(%rbp), %eax
orb %bl, %al
movl %eax, -0x30(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x418c8
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rbx
movq -0x60(%rbp), %rcx
addq $0x8, %rcx
jmp 0x69df7
testb $0x1, -0x30(%rbp)
je 0x6a067
testb $0x1, -0x2c(%rbp)
je 0x6a079
movl $0x2, -0x88(%rbp)
jmp 0x6a080
testb $0x1, -0x2c(%rbp)
je 0x6a0dd
movl $0x1, -0x88(%rbp)
jmp 0x6a080
andl $0x0, -0x88(%rbp)
movq %rbx, %rsi
leaq -0x88(%rbp), %rdx
callq 0x71784
movq -0x50(%rbp), %r14
jmp 0x69d9b
movq -0x40(%rbp), %rax
addq $0x10, %rax
leaq -0x88(%rbp), %rbx
movq (%rax), %rax
testq %rax, %rax
je 0x6a0cb
leaq 0x8(%rax), %rsi
movq %rax, %r14
movq %rbx, %rdi
callq 0x7108e
movq %rbx, %rdi
callq 0x418c8
movq %r14, %rax
jmp 0x6a0a7
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2becc(%rip), %rax     # 0x95fb0
movq (%rax), %rdi
movq (%rbx), %r9
leaq 0x12a64(%rip), %rsi     # 0x7cb55
leaq 0x12927(%rip), %rdx     # 0x7ca1f
leaq 0x12a90(%rip), %r8      # 0x7cb8f
movl $0x1ac, %ecx            # imm = 0x1AC
xorl %eax, %eax
callq 0x41600
pushq $0x1
popq %rdi
callq 0x414f0
jmp 0x6a138
movq %rax, %rbx
leaq -0xd0(%rbp), %rdi
jmp 0x6a12f
jmp 0x6a138
jmp 0x6a125
movq %rax, %rbx
leaq -0x110(%rbp), %rdi
callq 0x418c8
jmp 0x6a13b
jmp 0x6a138
movq %rax, %rbx
leaq -0x88(%rbp), %rdi
callq 0x418c8
movq %rbx, %rdi
callq 0x41790