// File: /workspace/github_cpp_makefile_star2_O3/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 $0x408, %rsp            # imm = 0x408
movq %rdi, -0x68(%rbp)
cmpl $0x4, 0x3c(%rdi)
jne 0x704a9
leaq -0x118(%rbp), %rbx
movq %rbx, %rdi
movq %rsi, -0x60(%rbp)
callq 0x6ed20
leaq -0xf0(%rbp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq (%rbx), %rdx
movq 0x8(%rbx), %rax
movq %rax, -0xe0(%rbp)
cmpq %rax, %rdx
je 0x70154
leaq -0xb0(%rbp), %r13
movl $0x80, %eax
leaq -0x2a0(%rbp), %rbx
leaq (%rbx,%rax), %rcx
movq %rcx, -0x58(%rbp)
leaq -0x428(%rbp), %r14
addq %r14, %rax
movq %rax, -0xa0(%rbp)
leaq -0xd8(%rbp), %r12
leaq -0xa8(%rbp), %r15
movq %rdx, -0x78(%rbp)
movq (%rdx), %rax
movq %rax, -0x30(%rbp)
movq 0x50(%rax), %rsi
movq (%rsi), %rax
movq %r14, %rdi
callq *(%rax)
leaq -0x100(%rbp), %rdi
movq %r14, %rsi
callq 0x2e4c0
movq -0x428(%rbp), %rdi
leaq -0x418(%rbp), %rax
cmpq %rax, %rdi
je 0x6fea0
callq 0x2e360
movq -0x30(%rbp), %rax
movq 0x60(%rax), %rsi
movq (%rsi), %rax
movq %rbx, %rdi
callq *(%rax)
leaq 0xa139(%rip), %r14      # 0x79ff0
leaq -0x428(%rbp), %rdi
movq %rbx, %rsi
movl $0x18, %edx
callq 0x2e670
movq -0x2a0(%rbp), %rdi
leaq -0x290(%rbp), %rax
cmpq %rax, %rdi
je 0x6fee3
callq 0x2e360
leaq -0x40(%rbp), %rax
movq %rax, -0x50(%rbp)
xorl %ecx, %ecx
movq %rcx, -0x48(%rbp)
movb %cl, -0x40(%rbp)
leaq -0x88(%rbp), %rax
movq %rax, -0x98(%rbp)
movq %rcx, -0x90(%rbp)
movb %cl, -0x88(%rbp)
leaq -0x428(%rbp), %rdi
leaq -0x98(%rbp), %rsi
movl $0x3b, %edx
callq 0x2e790
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x70078
movq %rbx, %rdi
leaq -0x98(%rbp), %rsi
movl $0x18, %edx
callq 0x2e670
leaq -0xc8(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq $0x0, -0xd0(%rbp)
movb $0x0, -0xc8(%rbp)
movq %rbx, %rdi
movq %r12, %rsi
movl $0x3a, %edx
callq 0x2e790
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x70043
movq %r15, %rdi
callq 0x2e800
movw $0x2000, -0xb8(%rbp)    # imm = 0x2000
movq %r13, %rdi
movq %r15, %rsi
callq 0x2e270
movq %r12, %rdi
leaq -0xb8(%rbp), %rsi
callq 0x398b9
movq %r13, %rdi
callq 0x2e580
movq %r15, %rdi
callq 0x2e580
movq %r12, %rdi
movq %r14, %rsi
callq 0x2e0f0
testl %eax, %eax
jne 0x6ff6d
movq %rbx, %rdi
movq %r12, %rsi
movl $0x3a, %edx
callq 0x2e790
movq %r15, %rdi
callq 0x2e800
movw $0x2000, -0xb8(%rbp)    # imm = 0x2000
leaq -0xb0(%rbp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x2e270
movq %r12, %rdi
leaq -0xb8(%rbp), %rsi
callq 0x398b9
movq %r14, %rdi
callq 0x2e580
movq %r15, %rdi
callq 0x2e580
leaq -0x50(%rbp), %rdi
movq %r12, %rsi
callq 0x2e240
leaq 0x9fb2(%rip), %r14      # 0x79ff0
jmp 0x6ff6d
movq -0xd8(%rbp), %rdi
leaq -0xc8(%rbp), %rax
cmpq %rax, %rdi
je 0x7005b
callq 0x2e360
movq %rbx, %rdi
movq 0x1def3(%rip), %rsi     # 0x8df58
callq 0x2e2f0
movq -0x58(%rbp), %rdi
callq 0x2e150
jmp 0x6ff0f
movq -0x68(%rbp), %rsi
movq (%rsi), %rax
movq %rbx, %rdi
callq *(%rax)
movq -0x48(%rbp), %rax
movq -0x2a0(%rbp), %r14
movq -0x298(%rbp), %rcx
cmpq %rax, %rcx
movq %rax, %rdx
cmovbq %rcx, %rdx
testq %rdx, %rdx
je 0x700ce
movq -0x50(%rbp), %rdi
movq %r14, %rsi
movq %rax, -0x30(%rbp)
movq %rcx, -0x70(%rbp)
callq 0x2e3c0
movq -0x70(%rbp), %rcx
testl %eax, %eax
movq -0x30(%rbp), %rax
je 0x700ce
movl $0x0, -0x30(%rbp)
jmp 0x700d7
cmpq %rcx, %rax
sete %al
movl %eax, -0x30(%rbp)
leaq -0x290(%rbp), %rax
cmpq %rax, %r14
je 0x700eb
movq %r14, %rdi
callq 0x2e360
movq -0x98(%rbp), %rdi
leaq -0x88(%rbp), %rax
cmpq %rax, %rdi
je 0x70103
callq 0x2e360
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x70115
callq 0x2e360
leaq -0x428(%rbp), %r14
movq %r14, %rdi
movq 0x1de32(%rip), %rsi     # 0x8df58
callq 0x2e2f0
movq -0xa0(%rbp), %rdi
callq 0x2e150
movq -0x78(%rbp), %rdx
addq $0x8, %rdx
cmpq -0xe0(%rbp), %rdx
sete %al
movl -0x30(%rbp), %ecx
orb %al, %cl
je 0x6fe62
leaq -0x418(%rbp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq -0x60(%rbp), %rbx
movq (%rbx), %r13
movq 0x8(%rbx), %r15
cmpq %r15, %r13
je 0x705db
movq $0x0, -0x30(%rbp)
leaq -0x50(%rbp), %r12
movq %r15, -0x70(%rbp)
movq (%r13), %r14
movq %r14, %rdi
leaq 0x77d3(%rip), %rsi      # 0x7796f
callq 0x6e968
testb %al, %al
je 0x70320
movq %r14, %rdi
leaq 0x7717(%rip), %rsi      # 0x778ca
callq 0x6e968
testb %al, %al
jne 0x70320
testq %r14, %r14
je 0x701e2
movq %r14, %rdi
leaq 0x1c4e1(%rip), %rsi     # 0x8c6b0
leaq 0x1cd9a(%rip), %rdx     # 0x8cf70
xorl %ecx, %ecx
callq 0x2e430
movq %rax, %rbx
jmp 0x701e4
xorl %ebx, %ebx
movq 0x40(%rbx), %rsi
movq (%rsi), %rax
leaq -0x2a0(%rbp), %rdi
callq *(%rax)
movq %rbx, -0x78(%rbp)
movq 0x50(%rbx), %rax
movq 0x40(%rax), %rax
movq (%rax), %r14
movq 0x8(%rax), %rax
movq %rax, -0x58(%rbp)
cmpq %rax, %r14
je 0x702b6
movq (%r14), %rbx
movq (%rbx), %rax
movq %r12, %rdi
movq %rbx, %rsi
callq *(%rax)
movq %r12, %rdi
xorl %r12d, %r12d
movl $0x14, %ecx
movq %rdi, %r15
leaq 0x9b38(%rip), %rsi      # 0x79d6f
xorl %edx, %edx
callq 0x2e710
cmpq $-0x1, %rax
je 0x7028e
movq (%rbx), %rax
leaq -0x98(%rbp), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq *(%rax)
movq -0x100(%rbp), %rsi
movq -0xf8(%rbp), %rcx
movq %r12, %rdi
xorl %edx, %edx
callq 0x2e710
cmpq $-0x1, %rax
setne %r12b
movq -0x98(%rbp), %rdi
leaq -0x88(%rbp), %rax
cmpq %rax, %rdi
je 0x7028e
callq 0x2e360
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x702a0
callq 0x2e360
testb %r12b, %r12b
jne 0x702c0
addq $0x8, %r14
cmpq -0x58(%rbp), %r14
movq %r15, %r12
jne 0x70214
movq -0x60(%rbp), %rbx
movq -0x70(%rbp), %r15
jmp 0x70302
movq -0x78(%rbp), %rax
movq 0x40(%rax), %rsi
movq (%rsi), %rax
movq %r15, %r12
movq %r15, %rdi
callq *(%rax)
leaq -0x428(%rbp), %rdi
movq %r12, %rsi
callq 0x2e4c0
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
movq -0x60(%rbp), %rbx
movq -0x70(%rbp), %r15
je 0x702fc
callq 0x2e360
movb $0x1, %al
movq %rax, -0x30(%rbp)
movq -0x2a0(%rbp), %rdi
leaq -0x290(%rbp), %rax
cmpq %rax, %rdi
je 0x7031a
callq 0x2e360
testb $0x1, -0x30(%rbp)
jne 0x7032d
addq $0x8, %r13
cmpq %r15, %r13
jne 0x7018e
testb $0x1, -0x30(%rbp)
je 0x705db
leaq -0x290(%rbp), %rax
movq %rax, -0x2a0(%rbp)
movq $0x0, -0x298(%rbp)
movb $0x0, -0x290(%rbp)
movq (%rbx), %r15
movq 0x8(%rbx), %rax
movq %rax, -0x30(%rbp)
cmpq %rax, %r15
je 0x70486
movq (%r15), %r13
movq %r13, %rdi
leaq 0x7879(%rip), %rsi      # 0x77bf1
callq 0x6e968
testb %al, %al
je 0x70478
movq %r13, %rdi
leaq 0x753b(%rip), %rsi      # 0x778ca
callq 0x6e968
testb %al, %al
jne 0x70478
testq %r13, %r13
je 0x703be
movq %r13, %rdi
leaq 0x1c305(%rip), %rsi     # 0x8c6b0
leaq 0x1cb16(%rip), %rdx     # 0x8cec8
xorl %ecx, %ecx
callq 0x2e430
movq %rax, %r13
jmp 0x703c1
xorl %r13d, %r13d
movq 0x40(%r13), %rax
movq 0x40(%rax), %rsi
leaq -0x2a0(%rbp), %rdi
callq 0x2e240
movq 0x50(%r13), %rax
movq 0x40(%rax), %rax
movq (%rax), %rbx
movq 0x8(%rax), %rax
movq %rax, -0x58(%rbp)
cmpq %rax, %rbx
je 0x70478
movq (%rbx), %rax
movq 0x40(%rax), %rax
movq 0x40(%rax), %rax
leaq -0x40(%rbp), %rcx
movq %rcx, -0x50(%rbp)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq -0x50(%rbp), %rdi
callq 0x32cbc
movq -0x50(%rbp), %r13
movq -0x48(%rbp), %r12
movq -0x420(%rbp), %r14
cmpq %r12, %r14
movq %r12, %rdx
cmovbq %r14, %rdx
testq %rdx, %rdx
je 0x7044d
movq -0x428(%rbp), %rsi
movq %r13, %rdi
callq 0x2e3c0
testl %eax, %eax
je 0x7044d
xorl %r14d, %r14d
jmp 0x70454
cmpq %r14, %r12
sete %r14b
leaq -0x40(%rbp), %rax
cmpq %rax, %r13
je 0x70465
movq %r13, %rdi
callq 0x2e360
testb %r14b, %r14b
jne 0x704b0
addq $0x8, %rbx
cmpq -0x58(%rbp), %rbx
jne 0x703f1
addq $0x8, %r15
cmpq -0x30(%rbp), %r15
jne 0x7036b
movq 0x1db33(%rip), %rax     # 0x8dfc0
movq (%rax), %rbx
movq -0x68(%rbp), %rsi
movq (%rsi), %rax
leaq -0x50(%rbp), %rdi
callq *(%rax)
leaq -0x50(%rbp), %rdi
movq %rbx, %rsi
callq 0x2f20a
xorl %ebx, %ebx
jmp 0x705c7
movq -0x60(%rbp), %r14
movq %r14, %rdi
callq 0x6ea2c
movq %rax, %rbx
movq %r14, %rdi
callq 0x6eba6
testq %rbx, %rbx
je 0x70604
movq %rax, %r14
testq %rax, %rax
je 0x70604
movq 0x60(%rbx), %rsi
movq (%rsi), %rax
leaq -0x50(%rbp), %rdi
callq *(%rax)
movq -0x2a0(%rbp), %rsi
movq -0x298(%rbp), %rcx
leaq -0x50(%rbp), %r12
movq %r12, %rdi
xorl %edx, %edx
callq 0x2e710
movq %rax, %r15
cmpq $-0x1, %rax
setne %bl
movq (%r12), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x70521
callq 0x2e360
cmpq $-0x1, %r15
jne 0x7056e
movq 0x60(%r14), %rsi
movq (%rsi), %rax
leaq -0x50(%rbp), %rdi
callq *(%rax)
movq -0x2a0(%rbp), %rsi
movq -0x298(%rbp), %rcx
leaq -0x50(%rbp), %r15
movq %r15, %rdi
xorl %edx, %edx
callq 0x2e710
movq %rax, %r14
movq (%r15), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x70564
callq 0x2e360
cmpq $-0x1, %r14
je 0x70627
movq -0x2a0(%rbp), %rdi
leaq -0x290(%rbp), %rax
cmpq %rax, %rdi
je 0x70586
callq 0x2e360
movq -0x428(%rbp), %rdi
leaq -0x418(%rbp), %rax
cmpq %rax, %rdi
je 0x7059e
callq 0x2e360
movq -0x100(%rbp), %rdi
leaq -0xf0(%rbp), %rax
cmpq %rax, %rdi
je 0x705b6
callq 0x2e360
movq -0x118(%rbp), %rdi
testq %rdi, %rdi
je 0x705c7
callq 0x2e360
movl %ebx, %eax
addq $0x408, %rsp            # imm = 0x408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1d9de(%rip), %rax     # 0x8dfc0
movq (%rax), %rbx
movq -0x68(%rbp), %rsi
movq (%rsi), %rax
leaq -0x2a0(%rbp), %rdi
callq *(%rax)
leaq -0x2a0(%rbp), %rdi
movq %rbx, %rsi
callq 0x2f24d
movq 0x1d9b5(%rip), %rax     # 0x8dfc0
movq (%rax), %rbx
movq -0x68(%rbp), %rsi
movq (%rsi), %rax
leaq -0x50(%rbp), %rdi
callq *(%rax)
leaq -0x50(%rbp), %rdi
movq %rbx, %rsi
callq 0x2f1c7
movq 0x1d992(%rip), %rax     # 0x8dfc0
movq (%rax), %rbx
movq -0x2a0(%rbp), %r14
movq -0x68(%rbp), %rsi
movq (%rsi), %rax
leaq -0x50(%rbp), %rdi
callq *(%rax)
leaq -0x50(%rbp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x2f17d
jmp 0x706a9
jmp 0x706a9
jmp 0x706a9
jmp 0x706a9
jmp 0x706a9
jmp 0x706a9
jmp 0x7068d
jmp 0x706a9
jmp 0x706a9
jmp 0x706a9
jmp 0x7068d
jmp 0x706a9
jmp 0x706e7
jmp 0x70670
movq %rax, %rbx
jmp 0x7078d
movq %rax, %rbx
movq -0x2a0(%rbp), %rdi
leaq -0x290(%rbp), %rax
jmp 0x706d2
jmp 0x706a9
movq %rax, %rbx
jmp 0x706c4
movq %rax, %rbx
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x706ac
callq 0x2e360
jmp 0x706ac
movq %rax, %rbx
movq -0x2a0(%rbp), %rdi
leaq -0x290(%rbp), %rax
cmpq %rax, %rdi
je 0x706c4
callq 0x2e360
movq -0x428(%rbp), %rdi
leaq -0x418(%rbp), %rax
cmpq %rax, %rdi
je 0x7078d
callq 0x2e360
jmp 0x7078d
jmp 0x706e7
movq %rax, %rbx
jmp 0x70744
movq %rax, %rbx
movq %r14, %rdi
jmp 0x706fa
movq %rax, %rbx
movq %r13, %rdi
callq 0x2e580
leaq -0xa8(%rbp), %rdi
callq 0x2e580
jmp 0x70710
movq %rax, %rbx
movq -0xd8(%rbp), %rdi
leaq -0xc8(%rbp), %rax
cmpq %rax, %rdi
je 0x70728
callq 0x2e360
movq 0x1d829(%rip), %rsi     # 0x8df58
leaq -0x2a0(%rbp), %rdi
callq 0x2e2f0
movq -0x58(%rbp), %rdi
callq 0x2e150
movq -0x98(%rbp), %rdi
leaq -0x88(%rbp), %rax
cmpq %rax, %rdi
je 0x7075c
callq 0x2e360
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x7076e
callq 0x2e360
movq 0x1d7e3(%rip), %rsi     # 0x8df58
leaq -0x428(%rbp), %rdi
callq 0x2e2f0
movq -0xa0(%rbp), %rdi
callq 0x2e150
movq -0x100(%rbp), %rdi
leaq -0xf0(%rbp), %rax
cmpq %rax, %rdi
je 0x707a5
callq 0x2e360
movq -0x118(%rbp), %rdi
testq %rdi, %rdi
je 0x707b6
callq 0x2e360
movq %rbx, %rdi
callq 0x2e730