// File: 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
subq $0x5b0, %rsp            # imm = 0x5B0
movq %rsi, %rax
movq %rdi, %rsi
movq %rsi, -0x510(%rbp)
movq %rsi, -0x8(%rbp)
movq %rax, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
leaq -0x38(%rbp), %rdi
movq %rdi, -0x508(%rbp)
callq 0xb2bc0
movq -0x508(%rbp), %rdi
callq 0x12d980
movq %rax, -0x500(%rbp)
jmp 0xd3419
leaq -0x38(%rbp), %rdi
callq 0xb22f0
movq -0x500(%rbp), %rax
movq %rax, -0x20(%rbp)
movq $0x0, -0x50(%rbp)
leaq -0x71(%rbp), %rdi
movq %rdi, -0x518(%rbp)
callq 0xaaa70
movq -0x518(%rbp), %rdx
leaq 0x68750(%rip), %rsi     # 0x13bba3
leaq -0x70(%rbp), %rdi
callq 0xaa7d0
jmp 0xd345e
leaq -0x71(%rbp), %rdi
callq 0xaa5f0
cmpq $0x0, -0x20(%rbp)
je 0xd34ea
movq -0x20(%rbp), %rax
movq 0x50(%rax), %rsi
movq (%rsi), %rax
movq (%rax), %rax
leaq -0x98(%rbp), %rdi
callq *%rax
jmp 0xd3487
leaq -0x70(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0xaa6d0
leaq -0x98(%rbp), %rdi
callq 0xaabe8
jmp 0xd34ea
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x38(%rbp), %rdi
callq 0xb22f0
jmp 0xd3e65
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x71(%rbp), %rdi
callq 0xaa5f0
jmp 0xd3e65
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0xd3e5c
leaq 0x674c7(%rip), %rsi     # 0x13a9b8
leaq -0xb8(%rbp), %rdi
movl $0x10, %edx
callq 0xe3150
jmp 0xd3504
leaq 0x674d2(%rip), %rcx     # 0x13a9dd
leaq -0xd8(%rbp), %rdi
leaq -0x70(%rbp), %rsi
leaq -0xb8(%rbp), %rdx
xorl %r8d, %r8d
callq 0xe31f0
jmp 0xd3527
leaq -0x70(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
movq %rsi, -0x520(%rbp)
callq 0xaa6d0
movq -0x520(%rbp), %rdi
callq 0xaabe8
leaq 0x674a6(%rip), %rsi     # 0x13a9f7
leaq -0xf8(%rbp), %rdi
movl $0x10, %edx
callq 0xe3150
jmp 0xd3564
leaq 0x674c2(%rip), %rcx     # 0x13aa2d
leaq -0x118(%rbp), %rdi
leaq -0x70(%rbp), %rsi
leaq -0xf8(%rbp), %rdx
xorl %r8d, %r8d
callq 0xe31f0
jmp 0xd3587
leaq -0x70(%rbp), %rdi
leaq -0x118(%rbp), %rsi
movq %rsi, -0x528(%rbp)
callq 0xaa6d0
movq -0x528(%rbp), %rdi
callq 0xaabe8
movq -0x510(%rbp), %rsi
leaq -0x130(%rbp), %rdi
callq 0x12dc10
jmp 0xd35bf
leaq -0x130(%rbp), %rax
movq %rax, -0x138(%rbp)
movq -0x138(%rbp), %rdi
callq 0xb73b0
movq %rax, -0x140(%rbp)
movq -0x138(%rbp), %rdi
callq 0xb7240
movq %rax, -0x148(%rbp)
leaq -0x140(%rbp), %rdi
leaq -0x148(%rbp), %rsi
callq 0xcb210
testb $0x1, %al
jne 0xd360f
jmp 0xd396e
leaq -0x140(%rbp), %rdi
callq 0xcb250
movq (%rax), %rax
movq %rax, -0x150(%rbp)
movq -0x150(%rbp), %rax
cmpl $0x0, 0x3c(%rax)
jne 0xd395b
movq -0x150(%rbp), %rax
movq 0x40(%rax), %rax
movq %rax, -0x530(%rbp)
cmpq $0x0, %rax
je 0xd3697
movq -0x530(%rbp), %rdi
leaq 0x9ef84(%rip), %rsi     # 0x1725e0
leaq 0x9fced(%rip), %rdx     # 0x173350
xorl %eax, %eax
movl %eax, %ecx
callq 0xaa660
movq %rax, -0x538(%rbp)
jmp 0xd36a2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0xd3e50
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0xd3e44
xorl %eax, %eax
movq %rax, -0x538(%rbp)
jmp 0xd36a2
movq -0x538(%rbp), %rax
movq %rax, -0x158(%rbp)
movq -0x158(%rbp), %rax
movq 0x40(%rax), %rsi
movq (%rsi), %rax
movq (%rax), %rax
leaq -0x1d8(%rbp), %rdi
callq *%rax
jmp 0xd36cc
leaq 0x67376(%rip), %rdx     # 0x13aa49
leaq -0x1b8(%rbp), %rdi
leaq -0x1d8(%rbp), %rsi
callq 0xccc30
jmp 0xd36e8
movq -0x158(%rbp), %rax
movq 0x48(%rax), %rsi
movq (%rsi), %rax
movq (%rax), %rax
leaq -0x1f8(%rbp), %rdi
callq *%rax
jmp 0xd3704
leaq -0x198(%rbp), %rdi
leaq -0x1b8(%rbp), %rsi
leaq -0x1f8(%rbp), %rdx
callq 0xb2200
jmp 0xd3720
leaq -0x178(%rbp), %rdi
leaq -0x198(%rbp), %rsi
movl $0x10, %edx
callq 0xe32d0
jmp 0xd373a
leaq -0x198(%rbp), %rdi
callq 0xaabe8
leaq -0x1f8(%rbp), %rdi
callq 0xaabe8
leaq -0x1b8(%rbp), %rdi
callq 0xaabe8
leaq -0x1d8(%rbp), %rdi
callq 0xaabe8
movq -0x158(%rbp), %rax
movq 0x40(%rax), %rsi
movq (%rsi), %rax
movq (%rax), %rax
leaq -0x278(%rbp), %rdi
callq *%rax
jmp 0xd3786
leaq 0x68445(%rip), %rdx     # 0x13bbd2
leaq -0x258(%rbp), %rdi
leaq -0x278(%rbp), %rsi
callq 0xccc30
jmp 0xd37a2
movq -0x158(%rbp), %rax
movq 0x48(%rax), %rsi
movq (%rsi), %rax
movq (%rax), %rax
leaq -0x298(%rbp), %rdi
callq *%rax
jmp 0xd37be
leaq -0x238(%rbp), %rdi
leaq -0x258(%rbp), %rsi
leaq -0x298(%rbp), %rdx
callq 0xb2200
jmp 0xd37da
leaq -0x218(%rbp), %rdi
leaq -0x70(%rbp), %rsi
leaq -0x178(%rbp), %rdx
leaq -0x238(%rbp), %rcx
xorl %r8d, %r8d
callq 0xe3380
jmp 0xd37fd
leaq -0x70(%rbp), %rdi
leaq -0x218(%rbp), %rsi
callq 0xaa6d0
leaq -0x218(%rbp), %rdi
callq 0xaabe8
leaq -0x238(%rbp), %rdi
callq 0xaabe8
leaq -0x298(%rbp), %rdi
callq 0xaabe8
leaq -0x258(%rbp), %rdi
callq 0xaabe8
leaq -0x278(%rbp), %rdi
callq 0xaabe8
leaq -0x178(%rbp), %rdi
callq 0xe3460
jmp 0xd395b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0xd3e38
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0xd38c5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0xd38b9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0xd38ad
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x198(%rbp), %rdi
callq 0xaabe8
leaq -0x1f8(%rbp), %rdi
callq 0xaabe8
leaq -0x1b8(%rbp), %rdi
callq 0xaabe8
leaq -0x1d8(%rbp), %rdi
callq 0xaabe8
jmp 0xd3e38
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0xd394a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0xd393e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0xd3932
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0xd3926
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x238(%rbp), %rdi
callq 0xaabe8
leaq -0x298(%rbp), %rdi
callq 0xaabe8
leaq -0x258(%rbp), %rdi
callq 0xaabe8
leaq -0x278(%rbp), %rdi
callq 0xaabe8
leaq -0x178(%rbp), %rdi
callq 0xe3460
jmp 0xd3e38
jmp 0xd395d
leaq -0x140(%rbp), %rdi
callq 0xcb270
jmp 0xd35f3
cmpq $0x0, -0x20(%rbp)
je 0xd3ae3
movl $0x20, %edi
callq 0xaa560
movq %rax, -0x540(%rbp)
jmp 0xd398c
movb $0x1, -0x3b9(%rbp)
leaq 0x670c4(%rip), %rsi     # 0x13aa5e
leaq -0x378(%rbp), %rdi
callq 0xe2f10
jmp 0xd39a8
movq -0x10(%rbp), %rsi
leaq -0x398(%rbp), %rdi
callq 0xaa580
jmp 0xd39ba
leaq -0x378(%rbp), %rdi
leaq -0x398(%rbp), %rsi
callq 0xe3090
movq %rax, -0x548(%rbp)
jmp 0xd39d6
movq -0x548(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0xe3490
movq %rax, -0x550(%rbp)
jmp 0xd39ef
movq -0x18(%rbp), %rsi
leaq -0x3b8(%rbp), %rdi
callq 0xaa580
jmp 0xd3a01
movq -0x550(%rbp), %rdi
leaq -0x3b8(%rbp), %rsi
callq 0xe3090
movq %rax, -0x558(%rbp)
jmp 0xd3a1d
movq -0x558(%rbp), %rsi
movq -0x540(%rbp), %rdi
callq 0xe2ee0
jmp 0xd3a32
movq -0x540(%rbp), %rax
movb $0x0, -0x3b9(%rbp)
movq %rax, -0x50(%rbp)
leaq -0x3b8(%rbp), %rdi
callq 0xaabe8
leaq -0x398(%rbp), %rdi
callq 0xaabe8
leaq -0x378(%rbp), %rdi
callq 0xe30c0
jmp 0xd3c4d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0xd3ac7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0xd3abb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0xd3aaf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x3b8(%rbp), %rdi
callq 0xaabe8
leaq -0x398(%rbp), %rdi
callq 0xaabe8
leaq -0x378(%rbp), %rdi
callq 0xe30c0
testb $0x1, -0x3b9(%rbp)
jne 0xd3ad2
jmp 0xd3ade
movq -0x540(%rbp), %rdi
callq 0xaa4e0
jmp 0xd3e38
movl $0x20, %edi
callq 0xaa560
movq %rax, -0x560(%rbp)
jmp 0xd3af6
movb $0x1, -0x4e1(%rbp)
leaq 0x66f5a(%rip), %rsi     # 0x13aa5e
leaq -0x4a0(%rbp), %rdi
callq 0xe2f10
jmp 0xd3b12
movq -0x10(%rbp), %rsi
leaq -0x4c0(%rbp), %rdi
callq 0xaa580
jmp 0xd3b24
leaq -0x4a0(%rbp), %rdi
leaq -0x4c0(%rbp), %rsi
callq 0xe3090
movq %rax, -0x568(%rbp)
jmp 0xd3b40
movq -0x568(%rbp), %rdi
leaq 0x68055(%rip), %rsi     # 0x13bba3
callq 0xe3120
movq %rax, -0x570(%rbp)
jmp 0xd3b5c
movq -0x18(%rbp), %rsi
leaq -0x4e0(%rbp), %rdi
callq 0xaa580
jmp 0xd3b6e
movq -0x570(%rbp), %rdi
leaq -0x4e0(%rbp), %rsi
callq 0xe3090
movq %rax, -0x578(%rbp)
jmp 0xd3b8a
movq -0x578(%rbp), %rsi
movq -0x560(%rbp), %rdi
callq 0xe2ee0
jmp 0xd3b9f
movq -0x560(%rbp), %rax
movb $0x0, -0x4e1(%rbp)
movq %rax, -0x50(%rbp)
leaq -0x4e0(%rbp), %rdi
callq 0xaabe8
leaq -0x4c0(%rbp), %rdi
callq 0xaabe8
leaq -0x4a0(%rbp), %rdi
callq 0xe30c0
jmp 0xd3c4d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0xd3c31
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0xd3c25
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0xd3c19
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x4e0(%rbp), %rdi
callq 0xaabe8
leaq -0x4c0(%rbp), %rdi
callq 0xaabe8
leaq -0x4a0(%rbp), %rdi
callq 0xe30c0
testb $0x1, -0x4e1(%rbp)
jne 0xd3c3c
jmp 0xd3c48
movq -0x560(%rbp), %rdi
callq 0xaa4e0
jmp 0xd3e38
movl $0x58, %edi
callq 0xaa560
movq %rax, -0x580(%rbp)
jmp 0xd3c60
movb $0x1, -0x4f5(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x590(%rbp)
movl $0x20, %edi
callq 0xaa560
movq %rax, -0x588(%rbp)
jmp 0xd3c85
movb $0x1, -0x4f2(%rbp)
leaq -0x4f1(%rbp), %rdi
movq %rdi, -0x598(%rbp)
callq 0xaaa70
movq -0x588(%rbp), %rdi
movq -0x598(%rbp), %rdx
leaq 0x66e4a(%rip), %rsi     # 0x13aafe
callq 0xaa7d0
jmp 0xd3cbb
movb $0x0, -0x4f2(%rbp)
movl $0x20, %edi
callq 0xaa560
movq %rax, -0x5a0(%rbp)
jmp 0xd3cd5
movb $0x1, -0x4f4(%rbp)
leaq -0x4f3(%rbp), %rdi
movq %rdi, -0x5a8(%rbp)
callq 0xaaa70
movq -0x5a0(%rbp), %rdi
movq -0x5a8(%rbp), %rdx
leaq 0x66df7(%rip), %rsi     # 0x13aafb
callq 0xaa7d0
jmp 0xd3d0b
movq -0x5a0(%rbp), %rcx
movq -0x588(%rbp), %rdx
movq -0x590(%rbp), %rsi
movq -0x580(%rbp), %rdi
movb $0x0, -0x4f4(%rbp)
callq 0xcaee0
jmp 0xd3d35
movb $0x0, -0x4f5(%rbp)
leaq -0x4f3(%rbp), %rdi
callq 0xaa5f0
leaq -0x4f1(%rbp), %rdi
callq 0xaa5f0
movq -0x580(%rbp), %rax
movq %rax, -0x4f0(%rbp)
movq -0x4f0(%rbp), %rax
movq %rax, -0x5b0(%rbp)
leaq -0x130(%rbp), %rdi
callq 0xccb90
leaq -0xf8(%rbp), %rdi
callq 0xe3460
leaq -0xb8(%rbp), %rdi
callq 0xe3460
leaq -0x70(%rbp), %rdi
callq 0xaabe8
movq -0x5b0(%rbp), %rax
addq $0x5b0, %rsp            # imm = 0x5B0
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0xd3e1f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0xd3dfa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x4f3(%rbp), %rdi
callq 0xaa5f0
testb $0x1, -0x4f4(%rbp)
jne 0xd3dec
jmp 0xd3df8
movq -0x5a0(%rbp), %rdi
callq 0xaa4e0
jmp 0xd3dfa
leaq -0x4f1(%rbp), %rdi
callq 0xaa5f0
testb $0x1, -0x4f2(%rbp)
jne 0xd3e11
jmp 0xd3e1d
movq -0x588(%rbp), %rdi
callq 0xaa4e0
jmp 0xd3e1f
testb $0x1, -0x4f5(%rbp)
jne 0xd3e2a
jmp 0xd3e36
movq -0x580(%rbp), %rdi
callq 0xaa4e0
jmp 0xd3e38
leaq -0x130(%rbp), %rdi
callq 0xccb90
leaq -0xf8(%rbp), %rdi
callq 0xe3460
leaq -0xb8(%rbp), %rdi
callq 0xe3460
leaq -0x70(%rbp), %rdi
callq 0xaabe8
movq -0x40(%rbp), %rdi
callq 0xaaa60
nop