// File: /workspace/github_cpp_makefile_star2_O2/eniac[P]Mantis/src/compile/compile_c.cpp
// Function: generateDialogueNode(std::vector<AstNode*, std::allocator<AstNode*>>, std::__cxx11::basic_ostringstream<char, std::char_traits<char>, std::allocator<char>>&, std::__cxx11::basic_ostringstream<char, std::char_traits<char>, std::allocator<char>>&)
UnanchoredNode* generateDialogueNode(std::vector<AstNode*> nodeArray, ostringstream& oss_reaction_mirror, ostringstream& oss_reaction_update) {

	// Assume single global reaction and initialization node (if any)
	P4RReactionNode * react_node = findReaction(nodeArray);
    string * dialogue_str = NULL;
    string p4r_reaction_user_str = "";
    if (react_node != 0) {
        p4r_reaction_user_str = react_node->body_->toString();
    }

    // Translate mantis style variable assignment to macros to be further processed by C preprocessor
    // regrex matching ${var} = <alter>;
    std::regex e_val ("\\$\\{([^ ]*)\\}\\s*[=]\\s*([^\\; ]*)\\s*\\;");
    p4r_reaction_user_str = std::regex_replace (p4r_reaction_user_str, e_val, "__mantis__mod_var_$1($2);");
    // regrex matching ${var} = <header>.<field>;
    std::regex e_field ("\\$\\{([^ ]*)\\}\\s*[=]\\s*([^\\. ]*)\\s*\\.\\s*([^\\; ]*)\\s*\\;");
    p4r_reaction_user_str = std::regex_replace (p4r_reaction_user_str, e_field, "__mantis__mod_var_$1_$2_$3;");

    // Change field arg format to C compatible variable
    vector<ReactionArgNode*> reaction_args = findReactionArgs(nodeArray);
    for (auto ra : reaction_args) {    
        if (ra->argType_==ReactionArgNode::INGRESS_FIELD) {
            FieldNode* fieldnode = dynamic_cast<FieldNode*>(ra->arg_);
            std::regex e_fieldarg (fieldnode->headerName_->toString()+"\\s*([^\\. ]*)\\s*\\.\\s*"+fieldnode->fieldName_->toString());
            p4r_reaction_user_str = std::regex_replace (p4r_reaction_user_str, e_fieldarg, fieldnode->headerName_->toString()+"_"+fieldnode->fieldName_->toString());
        }    
    }

    if (react_node != 0) {
        dialogue_str = new string(
            str(boost::format(kDialogueT) % oss_reaction_mirror.str() % p4r_reaction_user_str % oss_reaction_update.str()));
    } else {
        dialogue_str = new string(
            str(boost::format(kDialogueT) % oss_reaction_mirror.str() % "" % oss_reaction_update.str()));
    }

    UnanchoredNode * dialogue_cnode = new UnanchoredNode(dialogue_str, new string ("dialogue"), new string("pd_dialogue"));
    return dialogue_cnode;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x238, %rsp            # imm = 0x238
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %rbx
leaq -0x158(%rbp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x4486c
movq %r14, %rdi
callq 0x72600
movq %rax, %r14
leaq -0x158(%rbp), %rdi
callq 0x44be0
leaq 0x2a767(%rip), %rsi     # 0x7a8bb
leaq -0x98(%rbp), %rdi
leaq -0x258(%rbp), %rdx
callq 0x411c0
testq %r14, %r14
je 0x501a0
movq %r14, %r13
movq 0x50(%r14), %rsi
movq (%rsi), %rax
leaq -0x258(%rbp), %rdi
callq *(%rax)
leaq -0x98(%rbp), %rdi
leaq -0x258(%rbp), %r14
movq %r14, %rsi
callq 0x41500
movq %r14, %rdi
callq 0x418c8
movq %r13, %r14
leaq 0x29529(%rip), %rsi     # 0x796d0
leaq -0x140(%rbp), %rdi
pushq $0x10
popq %rdx
callq 0x58ca6
movq %r14, -0xa0(%rbp)
leaq 0x29531(%rip), %rcx     # 0x796f5
leaq -0x258(%rbp), %rdi
leaq -0x98(%rbp), %rsi
leaq -0x140(%rbp), %rdx
xorl %r8d, %r8d
callq 0x58d10
leaq -0x98(%rbp), %rdi
leaq -0x258(%rbp), %r14
movq %r14, %rsi
callq 0x41500
movq %r14, %rdi
callq 0x418c8
leaq 0x29509(%rip), %rsi     # 0x7970f
leaq -0x120(%rbp), %rdi
pushq $0x10
popq %rdx
callq 0x58ca6
leaq 0x29529(%rip), %rcx     # 0x79745
leaq -0x258(%rbp), %rdi
leaq -0x98(%rbp), %rsi
leaq -0x120(%rbp), %rdx
xorl %r8d, %r8d
callq 0x58d10
movq %r12, -0x38(%rbp)
movq %r15, -0x30(%rbp)
leaq -0x98(%rbp), %rdi
movq %r14, %rsi
callq 0x41500
movq %r14, %rdi
callq 0x418c8
leaq -0xc0(%rbp), %rdi
movq %rbx, %rsi
callq 0x7272a
movq -0xc0(%rbp), %r12
movq -0xb8(%rbp), %rcx
leaq -0x78(%rbp), %r15
movq %rcx, -0xa8(%rbp)
cmpq %rcx, %r12
je 0x50403
movq (%r12), %rax
cmpl $0x0, 0x3c(%rax)
jne 0x503fa
movq 0x40(%rax), %rdi
testq %rdi, %rdi
je 0x502ba
leaq 0x443e9(%rip), %rsi     # 0x94690
leaq 0x45152(%rip), %rdx     # 0x95400
xorl %ecx, %ecx
callq 0x41480
movq %rax, %r13
jmp 0x502bd
xorl %r13d, %r13d
movq 0x40(%r13), %rsi
movq (%rsi), %rax
leaq -0x100(%rbp), %rbx
movq %rbx, %rdi
callq *(%rax)
movq %r15, %rdi
movq %rbx, %rsi
leaq 0x29484(%rip), %rdx     # 0x79761
callq 0x4ed31
movq 0x48(%r13), %rsi
movq (%rsi), %rax
leaq -0xe0(%rbp), %rbx
movq %rbx, %rdi
callq *(%rax)
leaq -0x58(%rbp), %r14
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x443b8
leaq -0x258(%rbp), %rdi
movq %r14, %rsi
pushq $0x10
popq %rdx
callq 0x58d60
movq %r14, %rdi
callq 0x418c8
leaq -0xe0(%rbp), %r14
movq %r14, %rdi
callq 0x418c8
movq %r15, %rdi
callq 0x418c8
leaq -0x100(%rbp), %rbx
movq %rbx, %rdi
callq 0x418c8
movq 0x40(%r13), %rsi
movq (%rsi), %rax
movq %r14, %rdi
callq *(%rax)
movq %rbx, %rdi
movq %r14, %rsi
leaq 0x2a58a(%rip), %rdx     # 0x7a8ea
callq 0x4ed31
movq 0x48(%r13), %rsi
movq (%rsi), %rax
leaq -0x178(%rbp), %r14
movq %r14, %rdi
callq *(%rax)
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x443b8
leaq -0x58(%rbp), %r14
leaq -0x258(%rbp), %rax
movq %r14, %rdi
leaq -0x98(%rbp), %r13
movq %r13, %rsi
movq %rax, %rdx
movq %r15, %rcx
xorl %r8d, %r8d
callq 0x58dc7
movq %r13, %rdi
movq %r14, %rsi
callq 0x41500
movq %r14, %rdi
callq 0x418c8
movq %r15, %rdi
callq 0x418c8
leaq -0x178(%rbp), %rdi
callq 0x418c8
movq %rbx, %rdi
callq 0x418c8
leaq -0xe0(%rbp), %rdi
callq 0x418c8
leaq -0x258(%rbp), %rdi
callq 0x58e18
movq -0xa8(%rbp), %rcx
addq $0x8, %r12
jmp 0x50280
cmpq $0x0, -0xa0(%rbp)
je 0x5048b
pushq $0x20
popq %rdi
callq 0x413f0
movq -0x38(%rbp), %rbx
movq %rax, %r15
leaq 0x29353(%rip), %rsi     # 0x79776
leaq -0x258(%rbp), %rdi
callq 0x58b7c
addq $0x8, %rbx
leaq -0x58(%rbp), %rdi
movq %rbx, %rsi
callq 0x416b0
leaq -0x258(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x5b849
leaq -0x98(%rbp), %rsi
movq %rax, %rdi
callq 0x6919f
movq %rax, %rbx
movq -0x30(%rbp), %rsi
addq $0x8, %rsi
leaq -0x78(%rbp), %rdi
callq 0x416b0
leaq -0x78(%rbp), %rsi
movq %rbx, %rdi
callq 0x5b849
movq %r15, %rdi
movq %rax, %rsi
callq 0x5c6f6
jmp 0x50507
pushq $0x20
popq %rdi
callq 0x413f0
movq -0x38(%rbp), %rbx
movq %rax, %r15
leaq 0x292d5(%rip), %rsi     # 0x79776
leaq -0x258(%rbp), %rdi
callq 0x58b7c
addq $0x8, %rbx
leaq -0x58(%rbp), %rdi
movq %rbx, %rsi
callq 0x416b0
leaq -0x258(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x5b849
leaq 0x2a3e7(%rip), %rsi     # 0x7a8bb
movq %rax, %rdi
callq 0x5cafc
movq %rax, %rbx
movq -0x30(%rbp), %rsi
addq $0x8, %rsi
leaq -0x78(%rbp), %rdi
callq 0x416b0
leaq -0x78(%rbp), %rsi
movq %rbx, %rdi
callq 0x5b849
movq %r15, %rdi
movq %rax, %rsi
callq 0x5c6f6
leaq -0x78(%rbp), %rdi
callq 0x418c8
leaq -0x58(%rbp), %rdi
callq 0x418c8
leaq -0x258(%rbp), %rdi
callq 0x58c68
pushq $0x58
popq %rdi
callq 0x413f0
movq %rax, %r14
pushq $0x20
popq %rdi
callq 0x413f0
movq %rax, %r12
leaq 0x292d4(%rip), %rsi     # 0x79816
leaq -0x258(%rbp), %rdx
movq %rax, %rdi
callq 0x411c0
pushq $0x20
popq %rdi
callq 0x413f0
movq %rax, %r13
leaq 0x292b0(%rip), %rsi     # 0x79813
leaq -0x58(%rbp), %rdx
movq %rax, %rdi
callq 0x411c0
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %r13, %rcx
callq 0x4dd8e
leaq -0xc0(%rbp), %rdi
callq 0x4f87c
leaq -0x120(%rbp), %rdi
callq 0x58e18
leaq -0x140(%rbp), %rdi
callq 0x58e18
leaq -0x98(%rbp), %rdi
callq 0x418c8
movq %r14, %rax
addq $0x238, %rsp            # imm = 0x238
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x50657
jmp 0x505d3
jmp 0x505db
jmp 0x50657
movq %rax, %rbx
jmp 0x50663
movq %rax, %rbx
jmp 0x5066f
jmp 0x50625
jmp 0x50649
jmp 0x50657
jmp 0x50602
movq %rax, %rbx
movq %r13, %rdi
jmp 0x505fb
jmp 0x50602
movq %rax, %rbx
movq %r12, %rdi
callq 0x413a0
jmp 0x50605
movq %rax, %rbx
movq %r14, %rdi
jmp 0x50672
jmp 0x5068f
jmp 0x5068f
jmp 0x50613
movq %rax, %rbx
jmp 0x5071b
jmp 0x5061d
movq %rax, %rbx
jmp 0x50727
movq %rax, %rbx
jmp 0x50733
movq %rax, %rbx
jmp 0x5073f
movq %rax, %rbx
leaq -0x158(%rbp), %rdi
callq 0x44be0
jmp 0x5073f
movq %rax, %rbx
leaq -0x78(%rbp), %rdi
callq 0x418c8
jmp 0x5065a
movq %rax, %rbx
leaq -0x58(%rbp), %rdi
callq 0x418c8
leaq -0x258(%rbp), %rdi
callq 0x58c68
movq %r15, %rdi
callq 0x413a0
jmp 0x5070f
movq %rax, %rbx
jmp 0x506c9
movq %rax, %rbx
leaq -0x58(%rbp), %rdi
callq 0x418c8
jmp 0x5069c
movq %rax, %rbx
jmp 0x5070f
movq %rax, %rbx
jmp 0x506d5
movq %rax, %rbx
leaq -0xe0(%rbp), %rdi
callq 0x418c8
jmp 0x506b2
movq %rax, %rbx
jmp 0x506e1
movq %rax, %rbx
leaq -0x78(%rbp), %rdi
callq 0x418c8
jmp 0x50703
movq %rax, %rbx
leaq -0x78(%rbp), %rdi
callq 0x418c8
leaq -0x178(%rbp), %rdi
callq 0x418c8
leaq -0x100(%rbp), %rdi
callq 0x418c8
leaq -0xe0(%rbp), %rdi
callq 0x418c8
jmp 0x506f2
movq %rax, %rbx
leaq -0x258(%rbp), %rdi
callq 0x58e18
jmp 0x5070f
movq %rax, %rbx
leaq -0x100(%rbp), %rdi
callq 0x418c8
leaq -0xc0(%rbp), %rdi
callq 0x4f87c
leaq -0x120(%rbp), %rdi
callq 0x58e18
leaq -0x140(%rbp), %rdi
callq 0x58e18
leaq -0x98(%rbp), %rdi
callq 0x418c8
movq %rbx, %rdi
callq 0x41790