// File: /workspace/github_cpp_makefile_star2_O2/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 $0x218, %rsp            # imm = 0x218
movl %r8d, -0x3c(%rbp)
movl %ecx, %ebx
movq %rdx, -0x48(%rbp)
movq %rsi, -0x58(%rbp)
movq %rdi, -0x50(%rbp)
leaq -0x240(%rbp), %r14
movq %r14, %rdi
callq 0x415a0
leaq 0x11a1c(%rip), %rsi     # 0x7cbf1
movq %r14, %rdi
callq 0x413d0
leaq 0x118a5(%rip), %rsi     # 0x7ca89
movq %rax, %rdi
callq 0x413d0
leaq 0x11a6c(%rip), %rsi     # 0x7cc5f
movq %rax, %rdi
callq 0x413d0
testb $0x1, %bl
jne 0x6b3bd
leaq 0x11f12(%rip), %rsi     # 0x7d11d
leaq -0x240(%rbp), %rdi
callq 0x413d0
pushq $0x58
popq %rdi
callq 0x413f0
movq %rax, %r15
pushq $0x20
popq %rdi
callq 0x413f0
movq %rax, %r12
leaq -0x238(%rbp), %rsi
movq %rax, %rdi
callq 0x416b0
pushq $0x20
popq %rdi
callq 0x413f0
movq %rax, %r13
leaq 0x119d1(%rip), %rsi     # 0x7cc1f
leaq -0x98(%rbp), %rdx
movq %rax, %rdi
callq 0x411c0
pushq $0x20
popq %rdi
callq 0x413f0
movq %rax, %rbx
leaq 0x1181a(%rip), %rsi     # 0x7ca89
leaq -0x29(%rbp), %rdx
movq %rax, %rdi
callq 0x411c0
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x4dd8e
leaq -0x78(%rbp), %rsi
movq %r15, (%rsi)
movq -0x50(%rbp), %rdi
callq 0x44d46
leaq 0xf618(%rip), %rsi      # 0x7a8bb
leaq -0x78(%rbp), %rdi
leaq -0x98(%rbp), %rdx
callq 0x411c0
leaq -0x78(%rbp), %rsi
leaq -0x238(%rbp), %rdi
callq 0x41670
leaq -0x78(%rbp), %rdi
callq 0x418c8
leaq 0x1191e(%rip), %rsi     # 0x7cbf1
leaq -0x240(%rbp), %rdi
callq 0x413d0
leaq 0x11860(%rip), %rsi     # 0x7cb46
movq %rax, %rdi
callq 0x413d0
leaq 0x1196a(%rip), %rsi     # 0x7cc5f
movq %rax, %rdi
callq 0x413d0
testb $0x1, -0x3c(%rbp)
jne 0x6b54a
leaq 0x11e0f(%rip), %rsi     # 0x7d11d
leaq -0x240(%rbp), %rdi
callq 0x413d0
pushq $0x58
popq %rdi
callq 0x413f0
movq %rax, %r14
pushq $0x20
popq %rdi
callq 0x413f0
movq %rax, %r15
movq %rax, %rdi
leaq -0x238(%rbp), %rsi
callq 0x416b0
pushq $0x20
popq %rdi
callq 0x413f0
movq %rax, %r12
leaq 0x118ce(%rip), %rsi     # 0x7cc1f
leaq -0x98(%rbp), %rdx
movq %rax, %rdi
callq 0x411c0
pushq $0x20
popq %rdi
callq 0x413f0
movq %rax, %rbx
leaq 0x117d4(%rip), %rsi     # 0x7cb46
leaq -0x29(%rbp), %rdx
movq %rax, %rdi
callq 0x411c0
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x4dd8e
leaq -0x78(%rbp), %rsi
movq %r14, (%rsi)
movq -0x50(%rbp), %rdi
callq 0x44d46
leaq -0x240(%rbp), %rdi
callq 0x410e0
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x48(%rbp), %rax
movq (%rax), %rbx
movq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
leaq -0xc8(%rbp), %r12
cmpq -0x38(%rbp), %rbx
je 0x6b204
movq (%rbx), %r15
cmpl $0x4, 0x3c(%r15)
jne 0x6b541
movq %r12, %rdi
movq -0x58(%rbp), %rsi
callq 0x4486c
movq %r15, %rdi
movq %r12, %r13
movq %r12, %rsi
callq 0x72d72
movl %eax, %r14d
movq %r13, %rdi
callq 0x44be0
testb %r14b, %r14b
je 0x6b541
leaq -0x240(%rbp), %rdi
leaq 0x11825(%rip), %rsi     # 0x7cc4c
callq 0x413d0
movq %rax, %rdi
leaq 0xf7a5(%rip), %rsi      # 0x7abdb
callq 0x413d0
movq %rax, %rdi
leaq 0x1180e(%rip), %rsi     # 0x7cc53
callq 0x413d0
movq %rax, %rdi
leaq 0x1180f(%rip), %rsi     # 0x7cc63
callq 0x413d0
movq %rax, %rdi
leaq 0x1180c(%rip), %rsi     # 0x7cc6f
callq 0x413d0
movq %rax, %r14
movq (%r15), %rax
leaq -0x78(%rbp), %r13
movq %r13, %rdi
movq %r15, %rsi
callq *(%rax)
movq %r14, %rdi
movq %r13, %rsi
callq 0x41380
movq %rax, %rdi
leaq 0x117e7(%rip), %rsi     # 0x7cc76
callq 0x413d0
movq %rax, %rdi
leaq 0xd966(%rip), %rsi      # 0x78e04
callq 0x413d0
movq %rax, %rdi
leaq 0xeee9(%rip), %rsi      # 0x7a396
callq 0x413d0
movq %rax, %rdi
leaq 0x117c9(%rip), %rsi     # 0x7cc85
callq 0x413d0
movq %rax, %rdi
leaq 0x11798(%rip), %rsi     # 0x7cc63
callq 0x413d0
movq %rax, %rdi
leaq 0x11795(%rip), %rsi     # 0x7cc6f
callq 0x413d0
movq %rax, %r14
movq (%r15), %rax
leaq -0x98(%rbp), %r13
movq %r13, %rdi
movq %r15, %rsi
callq *(%rax)
movq %r14, %rdi
movq %r13, %rsi
callq 0x41380
movq %rax, %rdi
leaq 0x11787(%rip), %rsi     # 0x7cc90
callq 0x413d0
movq %rax, %rdi
leaq 0xd8ec(%rip), %rsi      # 0x78e04
callq 0x413d0
movq %rax, %rdi
leaq 0xee6f(%rip), %rsi      # 0x7a396
callq 0x413d0
leaq -0x98(%rbp), %rdi
callq 0x418c8
leaq -0x78(%rbp), %rdi
callq 0x418c8
addq $0x8, %rbx
jmp 0x6b3d3
movq -0x48(%rbp), %rax
movq (%rax), %rbx
movq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
leaq -0xb0(%rbp), %r12
cmpq -0x38(%rbp), %rbx
je 0x6b307
movq (%rbx), %r13
cmpl $0x4, 0x3c(%r13)
jne 0x6b6d0
movq %r12, %rdi
movq -0x58(%rbp), %rsi
callq 0x4486c
movq %r13, %rdi
movq %r12, %r15
movq %r12, %rsi
callq 0x72d72
movl %eax, %r14d
movq %r15, %rdi
callq 0x44be0
testb %r14b, %r14b
jne 0x6b6d0
leaq -0x240(%rbp), %rdi
leaq 0x11698(%rip), %rsi     # 0x7cc4c
callq 0x413d0
movq %rax, %rdi
leaq 0xf636(%rip), %rsi      # 0x7abf9
callq 0x413d0
movq %rax, %rdi
leaq 0x11681(%rip), %rsi     # 0x7cc53
callq 0x413d0
movq %rax, %rdi
leaq 0x11682(%rip), %rsi     # 0x7cc63
callq 0x413d0
movq %rax, %rdi
leaq 0x1167f(%rip), %rsi     # 0x7cc6f
callq 0x413d0
movq %rax, %r14
movq (%r13), %rax
leaq -0x78(%rbp), %r15
movq %r15, %rdi
movq %r13, %rsi
callq *(%rax)
movq %r14, %rdi
movq %r15, %rsi
callq 0x41380
movq %rax, %rdi
leaq 0x11659(%rip), %rsi     # 0x7cc76
callq 0x413d0
movq %rax, %rdi
leaq 0xd7d8(%rip), %rsi      # 0x78e04
callq 0x413d0
movq %rax, %rdi
leaq 0xed5b(%rip), %rsi      # 0x7a396
callq 0x413d0
movq %rax, %rdi
leaq 0x1163b(%rip), %rsi     # 0x7cc85
callq 0x413d0
movq %rax, %rdi
leaq 0x1160a(%rip), %rsi     # 0x7cc63
callq 0x413d0
movq %rax, %rdi
leaq 0x11607(%rip), %rsi     # 0x7cc6f
callq 0x413d0
movq %rax, %r14
movq (%r13), %rax
leaq -0x98(%rbp), %r15
movq %r15, %rdi
movq %r13, %rsi
callq *(%rax)
movq %r14, %rdi
movq %r15, %rsi
callq 0x41380
movq %rax, %rdi
leaq 0x115f8(%rip), %rsi     # 0x7cc90
callq 0x413d0
movq %rax, %rdi
leaq 0xd75d(%rip), %rsi      # 0x78e04
callq 0x413d0
movq %rax, %rdi
leaq 0xece0(%rip), %rsi      # 0x7a396
callq 0x413d0
leaq -0x98(%rbp), %rdi
callq 0x418c8
leaq -0x78(%rbp), %rdi
callq 0x418c8
addq $0x8, %rbx
jmp 0x6b560
jmp 0x6b6fc
jmp 0x6b733
movq %rax, %r13
movq %rbx, %rdi
jmp 0x6b6f5
jmp 0x6b733
movq %rax, %r13
movq %r12, %rdi
jmp 0x6b6f5
movq %rax, %r13
movq %r15, %rdi
callq 0x413a0
jmp 0x6b736
movq %rax, %r13
jmp 0x6b73e
jmp 0x6b791
jmp 0x6b79f
jmp 0x6b79f
jmp 0x6b743
movq %rax, %r14
movq %rbx, %rdi
jmp 0x6b72a
jmp 0x6b743
movq %rax, %r14
movq %r13, %rdi
jmp 0x6b72a
movq %rax, %r14
movq %r12, %rdi
callq 0x413a0
jmp 0x6b746
jmp 0x6b79f
movq %rax, %r13
movq %r14, %rdi
callq 0x413a0
movq %r13, %rbx
jmp 0x6b7a2
movq %rax, %r14
movq %r15, %rdi
callq 0x413a0
movq %r14, %rbx
jmp 0x6b7a2
jmp 0x6b79f
jmp 0x6b791
jmp 0x6b79f
jmp 0x6b791
movq %rax, %rbx
leaq -0xb0(%rbp), %rdi
jmp 0x6b771
movq %rax, %rbx
leaq -0xc8(%rbp), %rdi
callq 0x44be0
jmp 0x6b7a2
jmp 0x6b79f
jmp 0x6b77c
movq %rax, %rbx
leaq -0x98(%rbp), %rdi
callq 0x418c8
jmp 0x6b794
jmp 0x6b791
jmp 0x6b79f
movq %rax, %rbx
leaq -0x78(%rbp), %rdi
callq 0x418c8
jmp 0x6b7a2
movq %rax, %rbx
leaq -0x240(%rbp), %rdi
callq 0x410e0
movq %rbx, %rdi
callq 0x41790