// File: /workspace/github_cpp_makefile_star2_O1/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, -0x64(%rbp)
movl %ecx, %ebx
movq %rdx, -0x90(%rbp)
movq %rsi, -0x40(%rbp)
movq %rdi, -0x30(%rbp)
leaq -0x238(%rbp), %r14
movq %r14, %rdi
callq 0x2e550
leaq 0x13c8e(%rip), %rsi     # 0x79b61
movl $0x8, %edx
movq %r14, %rdi
callq 0x2e440
leaq 0x13b12(%rip), %rsi     # 0x799f9
leaq -0x238(%rbp), %rdi
movl $0xe, %edx
callq 0x2e440
leaq 0x13cd0(%rip), %rsi     # 0x79bcf
leaq -0x238(%rbp), %rdi
movl $0x3, %edx
callq 0x2e440
testb $0x1, %bl
je 0x66120
movq -0x90(%rbp), %rax
movq (%rax), %r14
movq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
cmpq %rax, %r14
je 0x66120
leaq -0x238(%rbp), %r15
movq (%r14), %rbx
movl 0x3c(%rbx), %r13d
cmpl $0x4, %r13d
jne 0x65f6c
leaq -0xc0(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x32d96
movq %rbx, %rdi
leaq -0xc0(%rbp), %rsi
callq 0x708a0
movl %eax, %r12d
jmp 0x65f6f
xorl %r12d, %r12d
cmpl $0x4, %r13d
jne 0x65f86
movq -0xc0(%rbp), %rdi
testq %rdi, %rdi
je 0x65f86
callq 0x2e360
testb %r12b, %r12b
je 0x66112
movl $0x6, %edx
movq %r15, %rdi
leaq 0x13c1e(%rip), %rsi     # 0x79bbc
callq 0x2e440
movl $0xc, %edx
movq %r15, %rdi
leaq 0x11c49(%rip), %rsi     # 0x77bfb
callq 0x2e440
movl $0xf, %edx
movq %r15, %rdi
leaq 0x13bfd(%rip), %rsi     # 0x79bc3
callq 0x2e440
movl $0xb, %edx
movq %r15, %rdi
leaq 0x13bf9(%rip), %rsi     # 0x79bd3
callq 0x2e440
movl $0x6, %edx
movq %r15, %rdi
leaq 0x13bf1(%rip), %rsi     # 0x79bdf
callq 0x2e440
movq (%rbx), %rax
leaq -0x60(%rbp), %rdi
movq %rbx, %rsi
callq *(%rax)
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq %r15, %rdi
callq 0x2e440
movq %rax, %r12
movl $0xe, %edx
movq %rax, %rdi
leaq 0x13bc5(%rip), %rsi     # 0x79be6
callq 0x2e440
movl $0x3, %edx
movq %r12, %rdi
leaq 0xfdef(%rip), %rsi      # 0x75e24
callq 0x2e440
movl $0x4, %edx
movq %r12, %rdi
leaq 0x1136d(%rip), %rsi     # 0x773b6
callq 0x2e440
movl $0xa, %edx
movq %r12, %rdi
leaq 0x13b98(%rip), %rsi     # 0x79bf5
callq 0x2e440
movl $0xb, %edx
movq %r12, %rdi
leaq 0x13b62(%rip), %rsi     # 0x79bd3
callq 0x2e440
movl $0x6, %edx
movq %r12, %rdi
leaq 0x13b5a(%rip), %rsi     # 0x79bdf
callq 0x2e440
movq (%rbx), %rax
leaq -0x88(%rbp), %rdi
movq %rbx, %rsi
callq *(%rax)
movq -0x88(%rbp), %rsi
movq -0x80(%rbp), %rdx
movq %r12, %rdi
callq 0x2e440
movq %rax, %rbx
movl $0xe, %edx
movq %rax, %rdi
leaq 0x13b42(%rip), %rsi     # 0x79c00
callq 0x2e440
movl $0x3, %edx
movq %rbx, %rdi
leaq 0xfd52(%rip), %rsi      # 0x75e24
callq 0x2e440
movl $0x4, %edx
movq %rbx, %rdi
leaq 0x112d0(%rip), %rsi     # 0x773b6
callq 0x2e440
movq -0x88(%rbp), %rdi
leaq -0x78(%rbp), %rax
cmpq %rax, %rdi
je 0x66100
callq 0x2e360
movq -0x60(%rbp), %rdi
leaq -0x50(%rbp), %rax
cmpq %rax, %rdi
je 0x66112
callq 0x2e360
addq $0x8, %r14
cmpq -0x38(%rbp), %r14
jne 0x65f3b
leaq 0x13f66(%rip), %rsi     # 0x7a08d
leaq -0x238(%rbp), %rdi
movl $0x3, %edx
callq 0x2e440
movl $0x58, %edi
callq 0x2e3a0
movq %rax, %r14
movl $0x20, %edi
callq 0x2e3a0
movq %rax, %r12
leaq -0x230(%rbp), %rsi
movq %rax, %rdi
callq 0x2e660
movl $0x20, %edi
callq 0x2e3a0
movq %rax, %r15
addq $0x10, %rax
movq %rax, (%r15)
movb $0x1, %al
movl %eax, -0x38(%rbp)
leaq 0x13a0e(%rip), %rsi     # 0x79b8f
leaq 0x13a0e(%rip), %rdx     # 0x79b96
movb $0x1, %bl
movq %r15, %rdi
callq 0x33112
xorl %ebx, %ebx
movl $0x20, %edi
callq 0x2e3a0
movq %rax, %r13
addq $0x10, %rax
movq %rax, (%r13)
movb $0x1, %al
movl %eax, -0x38(%rbp)
leaq 0x13844(%rip), %rsi     # 0x799f9
leaq 0x1384b(%rip), %rdx     # 0x79a07
movb $0x1, %bl
movq %r13, %rdi
callq 0x33112
xorl %ebx, %ebx
movq %r14, %rdi
movq %r12, %rsi
movq %r15, %rdx
movq %r13, %rcx
callq 0x3e566
movq %r14, -0x60(%rbp)
movq -0x30(%rbp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x661f5
movq %r14, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x66207
xorl %ebx, %ebx
leaq -0x60(%rbp), %rdx
movl $0x0, -0x38(%rbp)
callq 0x333de
leaq -0x50(%rbp), %rax
movq %rax, -0x10(%rax)
leaq 0x116c5(%rip), %rdx     # 0x778db
leaq -0x60(%rbp), %rdi
movq %rdx, %rsi
callq 0x33112
leaq -0x60(%rbp), %rsi
leaq -0x230(%rbp), %rdi
callq 0x2e610
movq -0x60(%rbp), %rdi
leaq -0x50(%rbp), %rax
cmpq %rax, %rdi
je 0x66244
callq 0x2e360
leaq 0x13916(%rip), %rsi     # 0x79b61
leaq -0x238(%rbp), %rdi
movl $0x8, %edx
callq 0x2e440
leaq 0x13853(%rip), %rsi     # 0x79ab6
leaq -0x238(%rbp), %rdi
movl $0xe, %edx
callq 0x2e440
leaq 0x13954(%rip), %rsi     # 0x79bcf
leaq -0x238(%rbp), %rdi
movl $0x3, %edx
callq 0x2e440
testb $0x1, -0x64(%rbp)
je 0x664a1
movq -0x90(%rbp), %rax
movq (%rax), %r14
movq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
cmpq %rax, %r14
je 0x664a1
leaq -0x238(%rbp), %r13
movq (%r14), %rbx
movl 0x3c(%rbx), %r15d
cmpl $0x4, %r15d
jne 0x662ed
leaq -0xa8(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x32d96
movq %rbx, %rdi
leaq -0xa8(%rbp), %rsi
callq 0x708a0
movl %eax, %r12d
xorb $0x1, %r12b
jmp 0x662f0
xorl %r12d, %r12d
cmpl $0x4, %r15d
jne 0x66307
movq -0xa8(%rbp), %rdi
testq %rdi, %rdi
je 0x66307
callq 0x2e360
testb %r12b, %r12b
je 0x66493
movl $0x6, %edx
movq %r13, %rdi
leaq 0x1389d(%rip), %rsi     # 0x79bbc
callq 0x2e440
movl $0xc, %edx
movq %r13, %rdi
leaq 0x118e6(%rip), %rsi     # 0x77c19
callq 0x2e440
movl $0xf, %edx
movq %r13, %rdi
leaq 0x1387c(%rip), %rsi     # 0x79bc3
callq 0x2e440
movl $0xb, %edx
movq %r13, %rdi
leaq 0x13878(%rip), %rsi     # 0x79bd3
callq 0x2e440
movl $0x6, %edx
movq %r13, %rdi
leaq 0x13870(%rip), %rsi     # 0x79bdf
callq 0x2e440
movq (%rbx), %rax
leaq -0x60(%rbp), %rdi
movq %rbx, %rsi
callq *(%rax)
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq %r13, %rdi
callq 0x2e440
movq %rax, %r12
movl $0xe, %edx
movq %rax, %rdi
leaq 0x13844(%rip), %rsi     # 0x79be6
callq 0x2e440
movl $0x3, %edx
movq %r12, %rdi
leaq 0xfa6e(%rip), %rsi      # 0x75e24
callq 0x2e440
movl $0x4, %edx
movq %r12, %rdi
leaq 0x10fec(%rip), %rsi     # 0x773b6
callq 0x2e440
movl $0xa, %edx
movq %r12, %rdi
leaq 0x13817(%rip), %rsi     # 0x79bf5
callq 0x2e440
movl $0xb, %edx
movq %r12, %rdi
leaq 0x137e1(%rip), %rsi     # 0x79bd3
callq 0x2e440
movl $0x6, %edx
movq %r12, %rdi
leaq 0x137d9(%rip), %rsi     # 0x79bdf
callq 0x2e440
movq (%rbx), %rax
leaq -0x88(%rbp), %rdi
movq %rbx, %rsi
callq *(%rax)
movq -0x88(%rbp), %rsi
movq -0x80(%rbp), %rdx
movq %r12, %rdi
callq 0x2e440
movq %rax, %rbx
movl $0xe, %edx
movq %rax, %rdi
leaq 0x137c1(%rip), %rsi     # 0x79c00
callq 0x2e440
movl $0x3, %edx
movq %rbx, %rdi
leaq 0xf9d1(%rip), %rsi      # 0x75e24
callq 0x2e440
movl $0x4, %edx
movq %rbx, %rdi
leaq 0x10f4f(%rip), %rsi     # 0x773b6
callq 0x2e440
movq -0x88(%rbp), %rdi
leaq -0x78(%rbp), %rax
cmpq %rax, %rdi
je 0x66481
callq 0x2e360
movq -0x60(%rbp), %rdi
leaq -0x50(%rbp), %rax
cmpq %rax, %rdi
je 0x66493
callq 0x2e360
addq $0x8, %r14
cmpq -0x38(%rbp), %r14
jne 0x662b8
leaq 0x13be5(%rip), %rsi     # 0x7a08d
leaq -0x238(%rbp), %rdi
movl $0x3, %edx
callq 0x2e440
movl $0x58, %edi
callq 0x2e3a0
movq %rax, %r14
movl $0x20, %edi
callq 0x2e3a0
movq %rax, %r12
movq %rax, %rdi
leaq -0x230(%rbp), %rsi
callq 0x2e660
movl $0x20, %edi
callq 0x2e3a0
movq %rax, %r15
addq $0x10, %rax
movq %rax, (%r15)
movb $0x1, %al
movl %eax, -0x40(%rbp)
leaq 0x1368d(%rip), %rsi     # 0x79b8f
leaq 0x1368d(%rip), %rdx     # 0x79b96
movb $0x1, %bl
movq %r15, %rdi
callq 0x33112
xorl %ebx, %ebx
movl $0x20, %edi
callq 0x2e3a0
movq %rax, %r13
addq $0x10, %rax
movq %rax, (%r13)
movb $0x1, %al
movl %eax, -0x40(%rbp)
leaq 0x13580(%rip), %rsi     # 0x79ab6
leaq 0x13587(%rip), %rdx     # 0x79ac4
movb $0x1, %bl
movq %r13, %rdi
callq 0x33112
xorl %ebx, %ebx
movq %r14, %rdi
movq %r12, %rsi
movq %r15, %rdx
movq %r13, %rcx
callq 0x3e566
movq %r14, -0x60(%rbp)
movq -0x30(%rbp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x66576
movq %r14, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x66588
xorl %ebx, %ebx
leaq -0x60(%rbp), %rdx
movl $0x0, -0x40(%rbp)
callq 0x333de
movq 0x27a19(%rip), %rsi     # 0x8dfa8
leaq -0x238(%rbp), %rdi
callq 0x2e230
leaq -0x1c8(%rbp), %rdi
callq 0x2e150
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, -0x30(%rbp)
movq %r12, %rdi
callq 0x2e360
jmp 0x665f1
jmp 0x665e1
jmp 0x666ad
jmp 0x666a6
movq %rax, -0x30(%rbp)
movq %r12, %rdi
callq 0x2e360
jmp 0x66602
movq %rax, -0x30(%rbp)
jmp 0x6665b
movq %rax, -0x30(%rbp)
jmp 0x6661b
movq %rax, -0x30(%rbp)
movb $0x1, %al
movl %eax, -0x40(%rbp)
jmp 0x66627
movq %rax, -0x30(%rbp)
jmp 0x66641
movq %rax, -0x30(%rbp)
movb $0x1, %al
movl %eax, -0x38(%rbp)
jmp 0x6664d
movq %rax, -0x30(%rbp)
testb %bl, %bl
je 0x66619
movq %r13, %rdi
callq 0x2e360
xorl %ebx, %ebx
testb %bl, %bl
je 0x66627
movq %r15, %rdi
callq 0x2e360
cmpb $0x0, -0x40(%rbp)
jne 0x66653
jmp 0x6665b
movq %rax, -0x30(%rbp)
testb %bl, %bl
je 0x6663f
movq %r13, %rdi
callq 0x2e360
xorl %ebx, %ebx
testb %bl, %bl
je 0x6664d
movq %r15, %rdi
callq 0x2e360
cmpb $0x0, -0x38(%rbp)
je 0x6665b
movq %r14, %rdi
callq 0x2e360
movq -0x30(%rbp), %rbx
jmp 0x666c2
movq %rax, %rbx
movq -0xa8(%rbp), %rdi
jmp 0x6667b
jmp 0x666a6
jmp 0x666ad
movq %rax, %rbx
movq -0xc0(%rbp), %rdi
testq %rdi, %rdi
jne 0x666bd
jmp 0x666c2
jmp 0x666a6
jmp 0x666ad
jmp 0x666a6
jmp 0x6668a
movq %rax, %rbx
movq -0x88(%rbp), %rdi
leaq -0x78(%rbp), %rax
cmpq %rax, %rdi
je 0x666b0
callq 0x2e360
jmp 0x666b0
jmp 0x666a6
movq %rax, %rbx
jmp 0x666c2
jmp 0x666ad
movq %rax, %rbx
movq -0x60(%rbp), %rdi
leaq -0x50(%rbp), %rax
cmpq %rax, %rdi
je 0x666c2
callq 0x2e360
movq 0x278df(%rip), %rsi     # 0x8dfa8
leaq -0x238(%rbp), %rdi
callq 0x2e230
leaq -0x1c8(%rbp), %rdi
callq 0x2e150
movq %rbx, %rdi
callq 0x2e730