// File: /workspace/github_cpp_makefile_star2_O3/eniac[P]Mantis/src/compile/compile_p4.cpp
// Function: generateRegArgGateControl(std::vector<AstNode*, std::allocator<AstNode*>>*, std::vector<AstNode*, std::allocator<AstNode*>>*, std::vector<ReactionArgNode*, std::allocator<ReactionArgNode*>> const&, int, int)
void generateRegArgGateControl(vector<AstNode*>* newNodes,
                         vector<AstNode*>* nodeArray,
                         const vector<ReactionArgNode*>& reaction_args,
                         int ing_iso_opt, int egr_iso_opt) {

    // Generate control flow branching on mv bit
    ostringstream oss;
    oss << "control " << kRegArgGateIngControlName << " {\n";
    
    // An alternative is to use single reg with double the size and index elements with original_index<<1+mv
    // Though less memory overhead but larger latency
    if (((unsigned int)ing_iso_opt) & 0b1) {
        for (auto ra : reaction_args) {
            if (ra->argType_==ReactionArgNode::REGISTER && findRegargInIng(ra, *nodeArray)) {
                oss << "  if (" << kP4rIngMetadataName << ".__mv == 0 ) {\n"
                << "    apply (" << kP4rRegReplicasTablePrefix << ra->toString() << kP4rRegReplicasSuffix0 << ");\n"
                << "  }\n"
                << "  else { \n"
                << "    apply (" << kP4rRegReplicasTablePrefix << ra->toString() << kP4rRegReplicasSuffix1 << ");\n"
                << "  }\n";
            }
        }    
    }
    oss << "}\n\n";
    newNodes->push_back(new UnanchoredNode(new string(oss.str()),
                                           new string("control"),
                                           new string(kRegArgGateIngControlName)));  

    oss.str("");
    oss << "control " << kRegArgGateEgrControlName << " {\n";
    
    if (((unsigned int)egr_iso_opt) & 0b1) {
        for (auto ra : reaction_args) {
            if (ra->argType_==ReactionArgNode::REGISTER && !findRegargInIng(ra, *nodeArray)) {
                oss << "  if (" << kP4rEgrMetadataName << ".__mv == 0 ) {\n"
                    << "    apply (" << kP4rRegReplicasTablePrefix << ra->toString() << kP4rRegReplicasSuffix0 << ");\n"
                    << "  }\n"
                    << "  else { \n"
                    << "    apply (" << kP4rRegReplicasTablePrefix << ra->toString() << kP4rRegReplicasSuffix1 << ");\n"
                    << "  }\n";
            }
        }         
    }
    oss << "}\n\n";    

    newNodes->push_back(new UnanchoredNode(new string(oss.str()),
                                           new string("control"),
                                           new string(kRegArgGateEgrControlName)));    
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x208, %rsp            # imm = 0x208
movl %r8d, -0x4c(%rbp)
movl %ecx, %ebx
movq %rdx, -0x78(%rbp)
movq %rsi, -0x88(%rbp)
movq %rdi, -0x80(%rbp)
leaq -0x230(%rbp), %r15
movq %r15, %rdi
callq 0x2e550
leaq 0x1437e(%rip), %rsi     # 0x79b61
movl $0x8, %edx
movq %r15, %rdi
callq 0x2e440
leaq 0x14202(%rip), %rsi     # 0x799f9
leaq -0x230(%rbp), %rdi
movl $0xe, %edx
callq 0x2e440
leaq 0x143c0(%rip), %rsi     # 0x79bcf
leaq -0x230(%rbp), %rdi
movl $0x3, %edx
callq 0x2e440
testb $0x1, %bl
je 0x65a16
movq -0x78(%rbp), %rax
movq (%rax), %r14
movq 0x8(%rax), %r13
cmpq %r13, %r14
je 0x65a16
leaq -0x230(%rbp), %r12
movq (%r14), %r15
cmpl $0x4, 0x3c(%r15)
jne 0x65a09
leaq -0xb8(%rbp), %rdi
movq -0x88(%rbp), %rsi
callq 0x32ac0
movq %r15, %rdi
leaq -0xb8(%rbp), %rsi
callq 0x6fdc6
movl %eax, %ebx
movq -0xb8(%rbp), %rdi
testq %rdi, %rdi
je 0x65887
callq 0x2e360
testb %bl, %bl
je 0x65a09
movl $0x6, %edx
movq %r12, %rdi
leaq 0x1431e(%rip), %rsi     # 0x79bbc
callq 0x2e440
movl $0xc, %edx
movq %r12, %rdi
leaq 0x12349(%rip), %rsi     # 0x77bfb
callq 0x2e440
movl $0xf, %edx
movq %r12, %rdi
leaq 0x142fd(%rip), %rsi     # 0x79bc3
callq 0x2e440
movl $0xb, %edx
movq %r12, %rdi
leaq 0x142f9(%rip), %rsi     # 0x79bd3
callq 0x2e440
movl $0x6, %edx
movq %r12, %rdi
leaq 0x142f1(%rip), %rsi     # 0x79bdf
callq 0x2e440
movq (%r15), %rax
leaq -0x48(%rbp), %rdi
movq %r15, %rsi
callq *(%rax)
movq -0x48(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq %r12, %rdi
callq 0x2e440
movq %rax, %rbx
movl $0xe, %edx
movq %rax, %rdi
leaq 0x142c5(%rip), %rsi     # 0x79be6
callq 0x2e440
movl $0x3, %edx
movq %rbx, %rdi
leaq 0x104ef(%rip), %rsi     # 0x75e24
callq 0x2e440
movl $0x4, %edx
movq %rbx, %rdi
leaq 0x11a6d(%rip), %rsi     # 0x773b6
callq 0x2e440
movl $0xa, %edx
movq %rbx, %rdi
leaq 0x14298(%rip), %rsi     # 0x79bf5
callq 0x2e440
movl $0xb, %edx
movq %rbx, %rdi
leaq 0x14262(%rip), %rsi     # 0x79bd3
callq 0x2e440
movl $0x6, %edx
movq %rbx, %rdi
leaq 0x1425a(%rip), %rsi     # 0x79bdf
callq 0x2e440
movq (%r15), %rax
leaq -0x70(%rbp), %rdi
movq %r15, %rsi
callq *(%rax)
movq -0x70(%rbp), %rsi
movq -0x68(%rbp), %rdx
movq %rbx, %rdi
callq 0x2e440
movq %rax, %rbx
movl $0xe, %edx
movq %rax, %rdi
leaq 0x14248(%rip), %rsi     # 0x79c00
callq 0x2e440
movl $0x3, %edx
movq %rbx, %rdi
leaq 0x10458(%rip), %rsi     # 0x75e24
callq 0x2e440
movl $0x4, %edx
movq %rbx, %rdi
leaq 0x119d6(%rip), %rsi     # 0x773b6
callq 0x2e440
movq -0x70(%rbp), %rdi
leaq -0x60(%rbp), %rax
cmpq %rax, %rdi
je 0x659f7
callq 0x2e360
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0x65a09
callq 0x2e360
addq $0x8, %r14
cmpq %r13, %r14
jne 0x65844
leaq 0x14670(%rip), %rsi     # 0x7a08d
leaq -0x230(%rbp), %rdi
movl $0x3, %edx
callq 0x2e440
movl $0x58, %edi
callq 0x2e3a0
movq %rax, %r15
movl $0x20, %edi
callq 0x2e3a0
movq %rax, %r12
leaq -0x228(%rbp), %rsi
movq %rax, %rdi
callq 0x2e660
movl $0x20, %edi
callq 0x2e3a0
movq %rax, %r13
addq $0x10, %rax
movq %rax, (%r13)
leaq 0x1411c(%rip), %rsi     # 0x79b8f
leaq 0x1411c(%rip), %rdx     # 0x79b96
movq %r13, %rdi
callq 0x32e40
movl $0x20, %edi
callq 0x2e3a0
movq %rax, %rbx
addq $0x10, %rax
movq %rax, (%rbx)
leaq 0x13f5c(%rip), %rsi     # 0x799f9
leaq 0x13f63(%rip), %rdx     # 0x79a07
movq %rbx, %rdi
callq 0x32e40
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x3e120
movq %r15, -0x48(%rbp)
movq -0x80(%rbp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x65ad9
movq %r15, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x65ae2
leaq -0x48(%rbp), %rdx
callq 0x3311a
leaq -0x38(%rbp), %rax
movq %rax, -0x10(%rax)
leaq 0x11dea(%rip), %rdx     # 0x778db
leaq -0x48(%rbp), %rdi
movq %rdx, %rsi
callq 0x32e40
leaq -0x48(%rbp), %rsi
leaq -0x228(%rbp), %rdi
callq 0x2e610
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0x65b1f
callq 0x2e360
leaq 0x1403b(%rip), %rsi     # 0x79b61
leaq -0x230(%rbp), %rdi
movl $0x8, %edx
callq 0x2e440
leaq 0x13f78(%rip), %rsi     # 0x79ab6
leaq -0x230(%rbp), %rdi
movl $0xe, %edx
callq 0x2e440
leaq 0x14079(%rip), %rsi     # 0x79bcf
leaq -0x230(%rbp), %rdi
movl $0x3, %edx
callq 0x2e440
testb $0x1, -0x4c(%rbp)
je 0x65d61
movq -0x78(%rbp), %rax
movq (%rax), %r14
movq 0x8(%rax), %r15
cmpq %r15, %r14
je 0x65d61
leaq -0x230(%rbp), %r13
movq (%r14), %r12
cmpl $0x4, 0x3c(%r12)
jne 0x65d54
leaq -0xa0(%rbp), %rdi
movq -0x88(%rbp), %rsi
callq 0x32ac0
movq %r12, %rdi
leaq -0xa0(%rbp), %rsi
callq 0x6fdc6
movl %eax, %ebx
movq -0xa0(%rbp), %rdi
testq %rdi, %rdi
je 0x65bd0
callq 0x2e360
testb %bl, %bl
jne 0x65d54
movl $0x6, %edx
movq %r13, %rdi
leaq 0x13fd5(%rip), %rsi     # 0x79bbc
callq 0x2e440
movl $0xc, %edx
movq %r13, %rdi
leaq 0x1201e(%rip), %rsi     # 0x77c19
callq 0x2e440
movl $0xf, %edx
movq %r13, %rdi
leaq 0x13fb4(%rip), %rsi     # 0x79bc3
callq 0x2e440
movl $0xb, %edx
movq %r13, %rdi
leaq 0x13fb0(%rip), %rsi     # 0x79bd3
callq 0x2e440
movl $0x6, %edx
movq %r13, %rdi
leaq 0x13fa8(%rip), %rsi     # 0x79bdf
callq 0x2e440
movq (%r12), %rax
leaq -0x48(%rbp), %rdi
movq %r12, %rsi
callq *(%rax)
movq -0x48(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq %r13, %rdi
callq 0x2e440
movq %rax, %rbx
movl $0xe, %edx
movq %rax, %rdi
leaq 0x13f7b(%rip), %rsi     # 0x79be6
callq 0x2e440
movl $0x3, %edx
movq %rbx, %rdi
leaq 0x101a5(%rip), %rsi     # 0x75e24
callq 0x2e440
movl $0x4, %edx
movq %rbx, %rdi
leaq 0x11723(%rip), %rsi     # 0x773b6
callq 0x2e440
movl $0xa, %edx
movq %rbx, %rdi
leaq 0x13f4e(%rip), %rsi     # 0x79bf5
callq 0x2e440
movl $0xb, %edx
movq %rbx, %rdi
leaq 0x13f18(%rip), %rsi     # 0x79bd3
callq 0x2e440
movl $0x6, %edx
movq %rbx, %rdi
leaq 0x13f10(%rip), %rsi     # 0x79bdf
callq 0x2e440
movq (%r12), %rax
leaq -0x70(%rbp), %rdi
movq %r12, %rsi
callq *(%rax)
movq -0x70(%rbp), %rsi
movq -0x68(%rbp), %rdx
movq %rbx, %rdi
callq 0x2e440
movq %rax, %rbx
movl $0xe, %edx
movq %rax, %rdi
leaq 0x13efd(%rip), %rsi     # 0x79c00
callq 0x2e440
movl $0x3, %edx
movq %rbx, %rdi
leaq 0x1010d(%rip), %rsi     # 0x75e24
callq 0x2e440
movl $0x4, %edx
movq %rbx, %rdi
leaq 0x1168b(%rip), %rsi     # 0x773b6
callq 0x2e440
movq -0x70(%rbp), %rdi
leaq -0x60(%rbp), %rax
cmpq %rax, %rdi
je 0x65d42
callq 0x2e360
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0x65d54
callq 0x2e360
addq $0x8, %r14
cmpq %r15, %r14
jne 0x65b8c
leaq 0x14325(%rip), %rsi     # 0x7a08d
leaq -0x230(%rbp), %rdi
movl $0x3, %edx
callq 0x2e440
movl $0x58, %edi
callq 0x2e3a0
movq %rax, %r14
movl $0x20, %edi
callq 0x2e3a0
movq %rax, %r15
movq %rax, %rdi
leaq -0x228(%rbp), %rsi
callq 0x2e660
movl $0x20, %edi
callq 0x2e3a0
movq %rax, %r12
addq $0x10, %rax
movq %rax, (%r12)
leaq 0x13dd1(%rip), %rsi     # 0x79b8f
leaq 0x13dd1(%rip), %rdx     # 0x79b96
movq %r12, %rdi
callq 0x32e40
movl $0x20, %edi
callq 0x2e3a0
movq %rax, %rbx
addq $0x10, %rax
movq %rax, (%rbx)
leaq 0x13cce(%rip), %rsi     # 0x79ab6
leaq 0x13cd5(%rip), %rdx     # 0x79ac4
movq %rbx, %rdi
callq 0x32e40
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x3e120
movq %r14, -0x48(%rbp)
movq -0x80(%rbp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x65e24
movq %r14, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x65e2d
leaq -0x48(%rbp), %rdx
callq 0x3311a
movq 0x28174(%rip), %rsi     # 0x8dfa8
leaq -0x230(%rbp), %rdi
callq 0x2e230
leaq -0x1c0(%rbp), %rdi
callq 0x2e150
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x65f2e
jmp 0x65f2e
jmp 0x65ebb
movq %rax, %r13
movq %rbx, %rdi
jmp 0x65e82
jmp 0x65ebb
movq %rax, %r13
movq %r12, %rdi
jmp 0x65e82
movq %rax, %r13
movq %r15, %rdi
callq 0x2e360
jmp 0x65ebe
jmp 0x65f2e
jmp 0x65f17
jmp 0x65f2e
jmp 0x65ec3
movq %rax, %r14
movq %rbx, %rdi
jmp 0x65eb2
jmp 0x65ec3
movq %rax, %r14
movq %r13, %rdi
jmp 0x65eb2
movq %rax, %r14
movq %r12, %rdi
callq 0x2e360
jmp 0x65ec6
jmp 0x65f2e
movq %rax, %r13
movq %r14, %rdi
jmp 0x65f27
movq %rax, %r14
movq %r15, %rdi
callq 0x2e360
movq %r14, %r13
jmp 0x65f31
jmp 0x65f2e
jmp 0x65f17
jmp 0x65f2e
jmp 0x65f17
movq %rax, %r13
movq -0xa0(%rbp), %rdi
jmp 0x65ef1
movq %rax, %r13
movq -0xb8(%rbp), %rdi
testq %rdi, %rdi
jne 0x65f27
jmp 0x65f31
jmp 0x65f2e
jmp 0x65efc
movq %rax, %r13
movq -0x70(%rbp), %rdi
leaq -0x60(%rbp), %rax
cmpq %rax, %rdi
je 0x65f1a
callq 0x2e360
jmp 0x65f1a
jmp 0x65f17
jmp 0x65f2e
movq %rax, %r13
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0x65f31
callq 0x2e360
jmp 0x65f31
movq %rax, %r13
movq 0x28070(%rip), %rsi     # 0x8dfa8
leaq -0x230(%rbp), %rdi
callq 0x2e230
leaq -0x1c0(%rbp), %rdi
callq 0x2e150
movq %r13, %rdi
callq 0x2e730