// File: /workspace/github_cpp_makefile_star2_O2/eniac[P]Mantis/src/compile/find_nodes.cpp
// Function: findRegargInIng(ReactionArgNode*, std::vector<AstNode*, std::allocator<AstNode*>>)
bool findRegargInIng(ReactionArgNode* regarg, std::vector<AstNode*> nodeArray) {

    if(regarg->argType_!=ReactionArgNode::REGISTER) {
        PRINT_VERBOSE("Miscall findRegargInIng for arg %s\n", regarg->toString().c_str());
        return false;
    }

    vector<P4ExprNode*> blackboxes = findBlackbox(nodeArray);
    
    std::string prog_name;
    for (auto blackbox : blackboxes) {
        prog_name = blackbox->name2_->toString();
        std::stringstream ss(blackbox->body_->toString());
        std::string reg_name;
        std::string item;
        while (std::getline(ss, item, ';'))
        {
            std::stringstream ss_(item);
            std::string item_;
            while (std::getline(ss_, item_, ':')) {
                boost::algorithm::trim(item_);
                if(item_.compare("reg")==0) {
                    std::getline(ss_, item_, ':');
                    boost::algorithm::trim(item_);
                    reg_name = item_;
                }
            }
        }
        if(reg_name.compare(regarg->toString())==0) {
            break;
        }
    }
    string action_name;
    bool found = false;
    for (auto tmp_node : nodeArray) {
        if(typeContains(tmp_node, "ActionNode") && !typeContains(tmp_node, "P4R")) {
            ActionNode* tmp_action_node = dynamic_cast<ActionNode*>(tmp_node);
            string tmp_action_name = tmp_action_node->name_->toString();
            ActionStmtsNode* actionstmts = tmp_action_node->stmts_;
            for (ActionStmtNode* as : *actionstmts->list_) {
                if(as->toString().find("execute_stateful_alu")!=string::npos &&
                    as->toString().find(prog_name)!=string::npos) {
                    found = true;
                    action_name = tmp_action_node->name_->toString();
                    break;
                }
            }
            if (found) {
                break;
            }
        }
    }
    if(!found) {
        PANIC("Action missing to execute the stateful alu for %s\n", regarg->toString().c_str());
    }
    string table_name;
    found = false;
    for (auto node : nodeArray) {
        if(typeContains(node, "TableNode") && !typeContains(node, "P4R")) {
            TableNode* table = dynamic_cast<TableNode*>(node);
            table_name = *(table->name_->word_);
            TableActionStmtsNode* actions = table->actions_;
            for (TableActionStmtNode* tas : *actions->list_) {
                string tmp_action_name = *tas->name_->word_;
                if(tmp_action_name.compare(action_name)==0) {
                    found = true;
                    break;
                }
            }
            if(found) {
                break;
            }        
        }
    }
    if(!found) {
        PANIC("Table missing for %s\n", regarg->toString().c_str());
    }    
    // Now locate ing/egr for the table
    P4ExprNode* ing_node = findIngress(nodeArray);
    P4ExprNode* egr_node = findEgress(nodeArray);
    if(ing_node==NULL || egr_node==NULL) {
        PANIC("Missing ing/egr node for %s\n", regarg->toString().c_str());
    }
    // A valid table will either be applied at ing or egr
    if(ing_node->body_->toString().find(table_name) != string::npos) {
        return true;
    } else if (egr_node->body_->toString().find(table_name) != string::npos) {
        return false;
    } else {
        PANIC("Failed to locate %s for %s\n", table_name.c_str(), regarg->toString().c_str());
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3e8, %rsp            # imm = 0x3E8
movq %rdi, -0x38(%rbp)
cmpl $0x4, 0x3c(%rdi)
jne 0x7314a
leaq -0xf8(%rbp), %rbx
movq %rbx, %rdi
movq %rsi, -0x68(%rbp)
callq 0x72002
leaq -0xd0(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
movq %rcx, -0x70(%rbp)
leaq -0x408(%rbp), %rcx
leaq -0x280(%rbp), %r13
leaq -0xc0(%rbp), %r14
pushq $0x3a
popq %rbx
leaq -0x80(%rbp), %r15
leaq 0xa298(%rip), %r12      # 0x7d080
cmpq -0x70(%rbp), %rax
je 0x72fc4
movq %rax, -0x40(%rbp)
movq (%rax), %rax
movq %rax, -0x30(%rbp)
movq 0x50(%rax), %rsi
movq (%rsi), %rax
movq %rcx, %rdi
callq *(%rax)
leaq -0xe0(%rbp), %rdi
leaq -0x408(%rbp), %rsi
callq 0x41500
leaq -0x408(%rbp), %rdi
callq 0x418c8
movq -0x30(%rbp), %rax
movq 0x60(%rax), %rsi
movq (%rsi), %rax
movq %r13, %rdi
callq *(%rax)
leaq -0x408(%rbp), %rdi
movq %r13, %rsi
pushq $0x18
popq %rdx
callq 0x416c0
movq %r13, %rdi
callq 0x418c8
leaq -0x50(%rbp), %rax
movq %rax, -0x60(%rbp)
andq $0x0, -0x58(%rbp)
xorl %ecx, %ecx
movb %cl, -0x50(%rbp)
leaq -0x90(%rbp), %rax
movq %rax, -0xa0(%rbp)
andq $0x0, -0x98(%rbp)
movb %cl, -0x90(%rbp)
leaq -0x408(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
pushq $0x3b
popq %rdx
callq 0x417f0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x72f67
movq %r13, %rdi
leaq -0xa0(%rbp), %rsi
pushq $0x18
popq %rdx
callq 0x416c0
leaq -0xb0(%rbp), %rax
movq %rax, -0xc0(%rbp)
andq $0x0, -0xb8(%rbp)
movb $0x0, -0xb0(%rbp)
movq %r13, %rdi
movq %r14, %rsi
movl %ebx, %edx
callq 0x417f0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x72f52
movq %r15, %rdi
callq 0x418a0
movq %r14, %rdi
movq %r15, %rsi
callq 0x49aca
movq %r15, %rdi
callq 0x415e0
movq %r14, %rdi
movq %r12, %rsi
callq 0x41130
testl %eax, %eax
jne 0x72ed7
movq %r13, %rdi
movq %r14, %rsi
movl %ebx, %edx
callq 0x417f0
movq %r15, %rdi
callq 0x418a0
movq %r14, %rdi
movq %r15, %rsi
callq 0x49aca
movq %r15, %rdi
callq 0x415e0
leaq -0x60(%rbp), %rdi
movq %r14, %rsi
callq 0x41290
jmp 0x72ed7
movq %r14, %rdi
callq 0x418c8
movq %r13, %rdi
callq 0x41330
jmp 0x72e80
movq -0x38(%rbp), %rsi
movq (%rsi), %rax
movq %r13, %rdi
callq *(%rax)
leaq -0x60(%rbp), %rdi
movq %r13, %rsi
callq 0x41040
movl %eax, -0x30(%rbp)
movq %r13, %rdi
callq 0x418c8
leaq -0xa0(%rbp), %rdi
callq 0x418c8
leaq -0x60(%rbp), %rdi
callq 0x418c8
leaq -0x408(%rbp), %rdi
callq 0x41330
leaq -0x408(%rbp), %rcx
movq -0x40(%rbp), %rax
addq $0x8, %rax
cmpl $0x0, -0x30(%rbp)
jne 0x72de8
leaq -0x3f8(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq -0x68(%rbp), %rax
movq (%rax), %r15
movq 0x8(%rax), %r13
movq $0x0, -0x40(%rbp)
leaq -0x60(%rbp), %r12
leaq -0xa0(%rbp), %r14
movq %r13, -0x78(%rbp)
cmpq %r13, %r15
je 0x73151
movq (%r15), %rbx
movq %rbx, %rdi
leaq 0x7940(%rip), %rsi      # 0x7a94f
callq 0x71cf8
testb %al, %al
je 0x73141
movq %rbx, %rdi
leaq 0x7884(%rip), %rsi      # 0x7a8aa
callq 0x71cf8
testb %al, %al
jne 0x73141
testq %rbx, %rbx
je 0x73055
movq %rbx, %rdi
leaq 0x2164e(%rip), %rsi     # 0x94690
leaq 0x21f07(%rip), %rdx     # 0x94f50
xorl %ecx, %ecx
callq 0x41480
movq %rax, %rbx
jmp 0x73057
xorl %ebx, %ebx
movq %r15, -0x30(%rbp)
movq 0x40(%rbx), %rsi
movq (%rsi), %rax
leaq -0x280(%rbp), %rdi
callq *(%rax)
movq %rbx, -0x70(%rbp)
movq 0x50(%rbx), %rax
movq 0x40(%rax), %rax
movq (%rax), %rbx
movq 0x8(%rax), %r13
cmpq %r13, %rbx
je 0x730ed
movq (%rbx), %r15
movq (%r15), %rax
movq %r12, %rdi
movq %r15, %rsi
callq *(%rax)
movq %r12, %rdi
leaq 0x9d64(%rip), %rsi      # 0x7cdff
xorl %edx, %edx
callq 0x41880
cmpq $-0x1, %rax
je 0x730df
movq (%r15), %rax
movq %r14, %rdi
movq %r15, %rsi
callq *(%rax)
movq %r14, %rdi
leaq -0xe0(%rbp), %rsi
xorl %edx, %edx
callq 0x41890
movq %rax, %r15
movq %r14, %rdi
callq 0x418c8
movq %r12, %rdi
callq 0x418c8
cmpq $-0x1, %r15
je 0x730e7
jmp 0x730fb
movq %r12, %rdi
callq 0x418c8
addq $0x8, %rbx
jmp 0x7307e
movq -0x30(%rbp), %r15
movq -0x78(%rbp), %r13
movq -0x40(%rbp), %rbx
jmp 0x7312c
movq -0x70(%rbp), %rax
movq 0x40(%rax), %rsi
movq (%rsi), %rax
movq %r12, %rdi
callq *(%rax)
leaq -0x408(%rbp), %rdi
movq %r12, %rsi
callq 0x41500
movq %r12, %rdi
callq 0x418c8
movb $0x1, %bl
movq -0x30(%rbp), %r15
movq -0x78(%rbp), %r13
leaq -0x280(%rbp), %rdi
callq 0x418c8
movq %rbx, -0x40(%rbp)
testb $0x1, %bl
jne 0x73151
addq $0x8, %r15
jmp 0x72ff9
xorl %ebx, %ebx
jmp 0x73306
testb $0x1, -0x40(%rbp)
je 0x7336c
leaq -0x270(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq -0x68(%rbp), %rax
movq (%rax), %r14
movq 0x8(%rax), %rax
movq %rax, -0x30(%rbp)
leaq -0x60(%rbp), %r12
cmpq -0x30(%rbp), %r14
je 0x7331a
movq (%r14), %r15
movq %r15, %rdi
leaq 0x7a39(%rip), %rsi      # 0x7abd1
callq 0x71cf8
testb %al, %al
je 0x731b4
movq %r15, %rdi
leaq 0x76ff(%rip), %rsi      # 0x7a8aa
callq 0x71cf8
testb %al, %al
je 0x731ba
addq $0x8, %r14
jmp 0x73181
testq %r15, %r15
je 0x731dc
movq %r15, %rdi
leaq 0x214c7(%rip), %rsi     # 0x94690
leaq 0x21cd8(%rip), %rdx     # 0x94ea8
xorl %ecx, %ecx
callq 0x41480
movq %rax, %r15
jmp 0x731df
xorl %r15d, %r15d
movq 0x40(%r15), %rax
movq 0x40(%rax), %rsi
leaq -0x280(%rbp), %rdi
callq 0x41290
movq 0x50(%r15), %rax
movq 0x40(%rax), %rax
movq (%rax), %rbx
movq 0x8(%rax), %r13
cmpq %r13, %rbx
je 0x731b4
movq (%rbx), %rax
movq 0x40(%rax), %rax
movq 0x40(%rax), %rsi
movq %r12, %rdi
callq 0x415b0
movq %r12, %rdi
leaq -0x408(%rbp), %rsi
callq 0x41040
movl %eax, %r15d
movq %r12, %rdi
callq 0x418c8
addq $0x8, %rbx
testl %r15d, %r15d
jne 0x73202
movq -0x68(%rbp), %r14
movq %r14, %rdi
callq 0x71d9c
movq %rax, %rbx
movq %r14, %rdi
callq 0x71ecf
testq %rbx, %rbx
je 0x733af
movq %rax, %r14
testq %rax, %rax
je 0x733af
movq 0x60(%rbx), %rsi
movq (%rsi), %rax
leaq -0x60(%rbp), %rdi
callq *(%rax)
leaq -0x60(%rbp), %rbx
leaq -0x280(%rbp), %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x41890
movq %rax, %r15
movq %rbx, %rdi
callq 0x418c8
cmpq $-0x1, %r15
setne %bl
jne 0x732d6
movq 0x60(%r14), %rsi
movq (%rsi), %rax
leaq -0x60(%rbp), %rdi
callq *(%rax)
leaq -0x60(%rbp), %r14
leaq -0x280(%rbp), %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x41890
movq %rax, %r15
movq %r14, %rdi
callq 0x418c8
cmpq $-0x1, %r15
je 0x733ef
leaq -0x280(%rbp), %rdi
callq 0x418c8
leaq -0x408(%rbp), %rdi
callq 0x418c8
leaq -0xe0(%rbp), %rdi
callq 0x418c8
leaq -0xf8(%rbp), %rdi
callq 0x71ce2
movl %ebx, %eax
addq $0x3e8, %rsp            # imm = 0x3E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x22c8f(%rip), %rax     # 0x95fb0
movq (%rax), %rbx
movq -0x38(%rbp), %rsi
movq (%rsi), %rax
leaq -0x60(%rbp), %rdi
callq *(%rax)
leaq -0x60(%rbp), %r14
movq (%r14), %r9
leaq 0xa24a(%rip), %rsi      # 0x7d589
leaq 0xa053(%rip), %rdx      # 0x7d399
leaq 0xa22c(%rip), %r8       # 0x7d579
movq %rbx, %rdi
movl $0x190, %ecx            # imm = 0x190
xorl %eax, %eax
callq 0x41600
movq %r14, %rdi
callq 0x418c8
pushq $0x1
popq %rdi
callq 0x414f0
movq 0x22c3d(%rip), %rax     # 0x95fb0
movq (%rax), %rbx
movq -0x38(%rbp), %rsi
movq (%rsi), %rax
leaq -0x280(%rbp), %rdi
callq *(%rax)
leaq -0x280(%rbp), %r14
movq (%r14), %r9
leaq 0xa19e(%rip), %rsi      # 0x7d535
leaq 0x9ffb(%rip), %rdx      # 0x7d399
leaq 0xa1d4(%rip), %r8       # 0x7d579
movq %rbx, %rdi
movl $0x17a, %ecx            # imm = 0x17A
jmp 0x73355
movq 0x22bfa(%rip), %rax     # 0x95fb0
movq (%rax), %rbx
movq -0x38(%rbp), %rsi
movq (%rsi), %rax
leaq -0x60(%rbp), %rdi
callq *(%rax)
leaq -0x60(%rbp), %r14
movq (%r14), %r9
leaq 0xa0e7(%rip), %rsi      # 0x7d4bb
leaq 0x9fbe(%rip), %rdx      # 0x7d399
leaq 0xa197(%rip), %r8       # 0x7d579
movq %rbx, %rdi
movl $0x196, %ecx            # imm = 0x196
jmp 0x73355
movq 0x22bba(%rip), %rax     # 0x95fb0
movq (%rax), %r14
movq -0x280(%rbp), %rbx
movq -0x38(%rbp), %rsi
movq (%rsi), %rax
leaq -0x60(%rbp), %rdi
callq *(%rax)
leaq -0x60(%rbp), %r15
movq (%r15), %rax
movq %rax, (%rsp)
leaq 0xa191(%rip), %rsi      # 0x7d5b0
leaq 0x9f73(%rip), %rdx      # 0x7d399
leaq 0xa14c(%rip), %r8       # 0x7d579
movq %r14, %rdi
movl $0x19e, %ecx            # imm = 0x19E
movq %rbx, %r9
xorl %eax, %eax
callq 0x41600
movq %r15, %rdi
jmp 0x7335f
jmp 0x7349f
jmp 0x7349f
jmp 0x7349f
jmp 0x7348c
jmp 0x7349f
jmp 0x7349f
jmp 0x7349f
jmp 0x7349f
jmp 0x7349f
jmp 0x7349f
jmp 0x7348c
jmp 0x7349f
movq %rax, %rbx
leaq -0x280(%rbp), %rdi
callq 0x418c8
jmp 0x734ee
jmp 0x734be
jmp 0x73480
movq %rax, %rbx
leaq -0x280(%rbp), %rdi
jmp 0x734b5
movq %rax, %rbx
jmp 0x7350f
jmp 0x73491
jmp 0x7349f
movq %rax, %rbx
jmp 0x734ae
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0x418c8
jmp 0x734a2
movq %rax, %rbx
leaq -0x280(%rbp), %rdi
callq 0x418c8
leaq -0x408(%rbp), %rdi
callq 0x418c8
jmp 0x7350f
jmp 0x734be
movq %rax, %rbx
jmp 0x734ee
jmp 0x734c5
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
callq 0x415e0
jmp 0x734d6
movq %rax, %rbx
leaq -0xc0(%rbp), %rdi
callq 0x418c8
leaq -0x280(%rbp), %rdi
callq 0x41330
leaq -0xa0(%rbp), %rdi
callq 0x418c8
leaq -0x60(%rbp), %rdi
callq 0x418c8
leaq -0x408(%rbp), %rdi
callq 0x41330
leaq -0xe0(%rbp), %rdi
callq 0x418c8
leaq -0xf8(%rbp), %rdi
callq 0x71ce2
movq %rbx, %rdi
callq 0x41790