// File: /workspace/github_cpp_makefile_star2_O3/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, -0x88(%rbp)
movq %rdi, %rax
movq (%rdi), %rdi
movq 0x8(%rax), %rdx
cmpq %rdx, %rdi
je 0x6426b
leaq -0x68(%rbp), %r14
leaq -0xa8(%rbp), %r12
leaq 0x13aff(%rip), %r13     # 0x7796f
movq %rax, -0x50(%rbp)
movq (%rdi), %rbx
leaq 0x8(%rdi), %rsi
cmpq %rdx, %rsi
je 0x63e90
subq %rsi, %rdx
callq 0x2e690
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rdx
addq $-0x8, %rdx
movq %rdx, 0x8(%rax)
movq %rax, %r15
movq 0x80(%rbx), %rax
movq 0x40(%rax), %rbx
movq -0x40(%rbp), %rdi
movq %rbx, %rsi
callq 0x63018
testq %rax, %rax
jne 0x64258
movq %rbx, -0x48(%rbp)
movq -0x88(%rbp), %rax
movq (%rax), %rcx
movq 0x8(%rax), %rax
movq %rax, -0xb0(%rbp)
cmpq %rax, %rcx
je 0x642c6
movl $0x0, -0x30(%rbp)
movl $0x0, -0x2c(%rbp)
leaq 0x13cff(%rip), %r15     # 0x77bf1
movq %rcx, -0x80(%rbp)
movq (%rcx), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x6e968
testb %al, %al
je 0x641f7
movq %rbx, %rdi
leaq 0x139b4(%rip), %rsi     # 0x778ca
callq 0x6e968
testb %al, %al
jne 0x641f7
testq %rbx, %rbx
je 0x63f45
movq %rbx, %rdi
leaq 0x2877e(%rip), %rsi     # 0x8c6b0
leaq 0x28f8f(%rip), %rdx     # 0x8cec8
xorl %ecx, %ecx
callq 0x2e430
movq %rax, %r15
jmp 0x63f48
xorl %r15d, %r15d
movq 0x40(%r15), %rax
movq 0x40(%rax), %rax
movq %r14, -0x78(%rbp)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq -0x78(%rbp), %rdi
callq 0x32cbc
movq (%r15), %rax
movq %r12, %rdi
movq %r15, %rsi
callq *(%rax)
movq -0x48(%rbp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rcx
movq %r12, %rdi
xorl %edx, %edx
callq 0x2e710
movq %rax, %rbx
movq -0xa8(%rbp), %rdi
leaq -0x98(%rbp), %rax
cmpq %rax, %rdi
je 0x63fa2
callq 0x2e360
cmpq $-0x1, %rbx
je 0x64020
leaq -0xe0(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq -0x78(%rbp), %rsi
movq -0x70(%rbp), %rdx
addq %rsi, %rdx
leaq -0xf0(%rbp), %rbx
movq %rbx, %rdi
callq 0x32cbc
movq %rbx, %rdi
movq -0x88(%rbp), %rsi
callq 0x6fcf1
movl %eax, %ebx
movq -0xf0(%rbp), %rdi
leaq -0xe0(%rbp), %rax
cmpq %rax, %rdi
leaq 0x13bf8(%rip), %r15     # 0x77bf1
je 0x64000
callq 0x2e360
movl -0x2c(%rbp), %ecx
movzbl %cl, %ecx
testb %bl, %bl
movl $0x1, %eax
cmovel %eax, %ecx
movl %ecx, -0x2c(%rbp)
movl -0x30(%rbp), %eax
orb %bl, %al
movl %eax, -0x30(%rbp)
jmp 0x641e9
movq 0x50(%r15), %rax
movq 0x40(%rax), %rax
movq (%rax), %rcx
movq 0x8(%rax), %rax
movq %rax, -0xb8(%rbp)
cmpq %rax, %rcx
je 0x641e2
movl $0x0, -0x34(%rbp)
movq -0x88(%rbp), %rax
movq (%rax), %r14
movq 0x8(%rax), %r15
cmpq %r15, %r14
movq %rcx, -0xc0(%rbp)
je 0x641b6
movq (%rcx), %rax
movq %rax, -0xd0(%rbp)
movq %r15, -0xc8(%rbp)
movq (%r14), %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x6e968
testb %al, %al
je 0x641a9
movq %rbx, %rdi
leaq 0x2861b(%rip), %rsi     # 0x8c6b0
leaq 0x28ed4(%rip), %rdx     # 0x8cf70
xorl %ecx, %ecx
callq 0x2e430
movq %rax, %r15
movq 0x40(%rax), %rsi
movq (%rsi), %rax
movq %r12, %rdi
callq *(%rax)
movq -0xd0(%rbp), %rax
movq 0x40(%rax), %rsi
movq (%rsi), %rax
leaq -0x110(%rbp), %rdi
callq *(%rax)
movq -0xa0(%rbp), %r13
movq -0x110(%rbp), %rbx
movq -0x108(%rbp), %r12
cmpq %r13, %r12
movq %r13, %rdx
cmovbq %r12, %rdx
testq %rdx, %rdx
je 0x64105
movq -0xa8(%rbp), %rdi
movq %rbx, %rsi
callq 0x2e3c0
testl %eax, %eax
je 0x64105
xorl %r12d, %r12d
jmp 0x6410c
cmpq %r12, %r13
sete %r12b
leaq -0x100(%rbp), %rax
cmpq %rax, %rbx
leaq 0x13852(%rip), %r13     # 0x7796f
je 0x64127
movq %rbx, %rdi
callq 0x2e360
testb %r12b, %r12b
leaq -0xa8(%rbp), %r12
je 0x64184
movq (%r15), %rax
leaq -0x110(%rbp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq *(%rax)
movq -0x48(%rbp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rcx
movq %rbx, %rdi
xorl %edx, %edx
callq 0x2e710
movq %rax, %rbx
movq -0x110(%rbp), %rdi
leaq -0x100(%rbp), %rax
cmpq %rax, %rdi
je 0x64175
callq 0x2e360
cmpq $-0x1, %rbx
je 0x64184
xorl %ebx, %ebx
movb $0x1, %al
movl %eax, -0x34(%rbp)
jmp 0x64186
movb $0x1, %bl
movq -0xa8(%rbp), %rdi
leaq -0x98(%rbp), %rax
cmpq %rax, %rdi
movq -0xc8(%rbp), %r15
je 0x641a5
callq 0x2e360
testb %bl, %bl
je 0x641b6
addq $0x8, %r14
cmpq %r15, %r14
jne 0x64075
testb $0x1, -0x34(%rbp)
movq -0xc0(%rbp), %rcx
jne 0x641d4
addq $0x8, %rcx
cmpq -0xb8(%rbp), %rcx
jne 0x64046
testb $0x1, -0x34(%rbp)
leaq -0x68(%rbp), %r14
jne 0x63fa8
leaq 0x13a08(%rip), %r15     # 0x77bf1
movq -0x78(%rbp), %rdi
cmpq %r14, %rdi
je 0x641f7
callq 0x2e360
movq -0x80(%rbp), %rcx
addq $0x8, %rcx
cmpq -0xb0(%rbp), %rcx
jne 0x63ef2
testb $0x1, -0x30(%rbp)
je 0x64225
testb $0x1, -0x2c(%rbp)
movq -0x48(%rbp), %rsi
je 0x64240
movl $0x2, -0x78(%rbp)
jmp 0x64247
testb $0x1, -0x2c(%rbp)
je 0x642c6
movl $0x1, -0x78(%rbp)
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rsi
jmp 0x6424b
movl $0x0, -0x78(%rbp)
movq -0x40(%rbp), %rdi
leaq -0x78(%rbp), %rdx
callq 0x6e3fc
movq -0x50(%rbp), %r15
movq (%r15), %rdi
movq 0x8(%r15), %rdx
cmpq %rdx, %rdi
movq %r15, %rax
jne 0x63e74
movq -0x40(%rbp), %rax
movq 0x10(%rax), %r14
testq %r14, %r14
je 0x642b4
leaq -0x68(%rbp), %r15
leaq -0x78(%rbp), %rbx
movq %r15, -0x78(%rbp)
movq 0x8(%r14), %rsi
movq 0x10(%r14), %rdx
addq %rsi, %rdx
movq %rbx, %rdi
callq 0x32cbc
movl 0x28(%r14), %eax
movl %eax, -0x58(%rbp)
movq -0x78(%rbp), %rdi
cmpq %r15, %rdi
je 0x642ac
callq 0x2e360
movq (%r14), %r14
testq %r14, %r14
jne 0x64280
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x2ef90
jmp 0x642e9
movq %rax, %rbx
movq -0xf0(%rbp), %rdi
leaq -0xe0(%rbp), %rax
cmpq %rax, %rdi
jne 0x6430a
jmp 0x6431a
movq %rax, %rbx
jmp 0x6431a
jmp 0x642f0
movq %rax, %rbx
movq -0xa8(%rbp), %rdi
leaq -0x98(%rbp), %rax
cmpq %rax, %rdi
leaq -0x68(%rbp), %r14
je 0x6431a
callq 0x2e360
jmp 0x6431a
jmp 0x64313
movq %rax, %rbx
leaq -0x68(%rbp), %r14
movq -0x78(%rbp), %rdi
cmpq %r14, %rdi
je 0x64328
callq 0x2e360
movq %rbx, %rdi
callq 0x2e730