// File: eniac[P]Mantis/src/compile/compile_p4.cpp
// Function: augmentEgress(std::vector<AstNode*, std::allocator<AstNode*>>*)
bool augmentEgress(vector<AstNode*>* astNodes) {
    // Fetch egress
    P4ExprNode* egressNode = findEgress(*astNodes);
    if (!egressNode) {
        PANIC("Error: could not find the egress control block. Perhaps you didn't call it 'egress'?\n");
        exit(1);
    }

    // Rename egress
    egressNode->name1_->word_ = new string(kOrigEgrControlName);

    // Generate new ingress function that wraps original
    ostringstream oss;
    oss << "control egress {\n"
            << "  " << kSetmblEgrControlName << "();\n"
            << "  " << kOrigEgrControlName << "();\n" 
            << "  " << kSetargsEgrControlName << "();\n"
            << "  " << kRegArgGateEgrControlName << "();\n"
        << "}\n\n";
    StrNode* newEgressNode = new StrNode(new string(oss.str()));

    // Inject wrapper right before original
    InputNode* firstInput = dynamic_cast<InputNode*>(egressNode->parent_);
    InputNode* secondInput = new InputNode(firstInput->next_,
                                           firstInput->expression_);
    firstInput->next_ = secondInput;
    secondInput->parent_ = firstInput;

    firstInput->expression_ = newEgressNode;
    astNodes->push_back(secondInput);

    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x270, %rsp            # imm = 0x270
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rdi
callq 0x12cb20
movq %rax, -0x10(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0x1185e8
movq 0x5b9f6(%rip), %rax     # 0x173fb0
movq (%rax), %rdi
leaq 0x2580c(%rip), %rsi     # 0x13ddd0
leaq 0x2578c(%rip), %rdx     # 0x13dd57
movl $0xb1, %ecx
leaq 0x25860(%rip), %r8      # 0x13de37
movb $0x0, %al
callq 0xaa850
movl $0x1, %edi
callq 0xaa6c0
movl $0x20, %edi
callq 0xaa560
movq %rax, -0x1d0(%rbp)
movq %rax, -0x1d8(%rbp)
movb $0x1, -0x25(%rbp)
leaq -0x11(%rbp), %rdi
movq %rdi, -0x1c8(%rbp)
callq 0xaaa70
movq -0x1d0(%rbp), %rdi
movq -0x1c8(%rbp), %rdx
leaq 0x2581c(%rip), %rsi     # 0x13de45
callq 0xaa7d0
jmp 0x118630
movq -0x1d8(%rbp), %rcx
movb $0x0, -0x25(%rbp)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq %rcx, 0x40(%rax)
leaq -0x11(%rbp), %rdi
callq 0xaa5f0
leaq -0x1a0(%rbp), %rdi
movq %rdi, -0x1e8(%rbp)
callq 0xaa7b0
movq -0x1e8(%rbp), %rdi
leaq 0x257e3(%rip), %rsi     # 0x13de54
callq 0xaa540
movq %rax, -0x1e0(%rbp)
jmp 0x11867f
movq -0x1e0(%rbp), %rdi
leaq 0x23cab(%rip), %rsi     # 0x13c338
callq 0xaa540
movq %rax, -0x1f0(%rbp)
jmp 0x11869b
movq -0x1f0(%rbp), %rdi
leaq 0x257bd(%rip), %rsi     # 0x13de66
callq 0xaa540
movq %rax, -0x1f8(%rbp)
jmp 0x1186b7
movq -0x1f8(%rbp), %rdi
leaq 0x256eb(%rip), %rsi     # 0x13ddb0
callq 0xaa540
movq %rax, -0x200(%rbp)
jmp 0x1186d3
movq -0x200(%rbp), %rdi
leaq 0x23c57(%rip), %rsi     # 0x13c338
callq 0xaa540
movq %rax, -0x208(%rbp)
jmp 0x1186ef
movq -0x208(%rbp), %rdi
leaq 0x25748(%rip), %rsi     # 0x13de45
callq 0xaa540
movq %rax, -0x210(%rbp)
jmp 0x11870b
movq -0x210(%rbp), %rdi
leaq 0x25697(%rip), %rsi     # 0x13ddb0
callq 0xaa540
movq %rax, -0x218(%rbp)
jmp 0x118727
movq -0x218(%rbp), %rdi
leaq 0x23c03(%rip), %rsi     # 0x13c338
callq 0xaa540
movq %rax, -0x220(%rbp)
jmp 0x118743
movq -0x220(%rbp), %rdi
leaq 0x25721(%rip), %rsi     # 0x13de72
callq 0xaa540
movq %rax, -0x228(%rbp)
jmp 0x11875f
movq -0x228(%rbp), %rdi
leaq 0x25643(%rip), %rsi     # 0x13ddb0
callq 0xaa540
movq %rax, -0x230(%rbp)
jmp 0x11877b
movq -0x230(%rbp), %rdi
leaq 0x23baf(%rip), %rsi     # 0x13c338
callq 0xaa540
movq %rax, -0x238(%rbp)
jmp 0x118797
movq -0x238(%rbp), %rdi
leaq 0x256d9(%rip), %rsi     # 0x13de7e
callq 0xaa540
movq %rax, -0x240(%rbp)
jmp 0x1187b3
movq -0x240(%rbp), %rdi
leaq 0x255ef(%rip), %rsi     # 0x13ddb0
callq 0xaa540
movq %rax, -0x248(%rbp)
jmp 0x1187cf
movq -0x248(%rbp), %rdi
leaq 0x25c78(%rip), %rsi     # 0x13e455
callq 0xaa540
jmp 0x1187e4
movl $0x48, %edi
callq 0xaa560
movq %rax, -0x250(%rbp)
jmp 0x1187f7
movl $0x20, %edi
callq 0xaa560
movq %rax, -0x258(%rbp)
jmp 0x11880a
movq -0x258(%rbp), %rdi
leaq -0x1a0(%rbp), %rsi
callq 0xaa580
jmp 0x11881f
movq -0x258(%rbp), %rsi
movq -0x250(%rbp), %rdi
callq 0xb2890
jmp 0x118834
movq -0x250(%rbp), %rax
movq %rax, -0x1a8(%rbp)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x260(%rbp)
cmpq $0x0, %rax
je 0x1188fb
movq -0x260(%rbp), %rdi
leaq 0x59d77(%rip), %rsi     # 0x1725e0
leaq 0x5a0a8(%rip), %rdx     # 0x172918
xorl %eax, %eax
movl %eax, %ecx
callq 0xaa660
movq %rax, -0x268(%rbp)
jmp 0x118906
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0x11(%rbp), %rdi
callq 0xaa5f0
testb $0x1, -0x25(%rbp)
jne 0x1188a2
jmp 0x1188ae
movq -0x1d8(%rbp), %rdi
callq 0xaa4e0
jmp 0x1189e5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x1189d9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x1188ea
movq -0x258(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
callq 0xaa4e0
movq -0x250(%rbp), %rdi
callq 0xaa4e0
jmp 0x1189d9
xorl %eax, %eax
movq %rax, -0x268(%rbp)
jmp 0x118906
movq -0x268(%rbp), %rax
movq %rax, -0x1b0(%rbp)
movl $0x50, %edi
callq 0xaa560
movq %rax, -0x270(%rbp)
jmp 0x118927
movq -0x270(%rbp), %rdi
movq -0x1b0(%rbp), %rax
movq 0x40(%rax), %rsi
movq 0x48(%rax), %rdx
callq 0xb2350
jmp 0x118944
movq -0x270(%rbp), %rax
movq %rax, -0x1b8(%rbp)
movq -0x1b8(%rbp), %rcx
movq -0x1b0(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x1b0(%rbp), %rcx
movq -0x1b8(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x1a8(%rbp), %rcx
movq -0x1b0(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x8(%rbp), %rdi
movq -0x1b8(%rbp), %rax
movq %rax, -0x1c0(%rbp)
leaq -0x1c0(%rbp), %rsi
callq 0xb2540
jmp 0x1189a8
leaq -0x1a0(%rbp), %rdi
callq 0xaa130
movb $0x1, %al
andb $0x1, %al
addq $0x270, %rsp            # imm = 0x270
popq %rbp
retq
movq -0x270(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
callq 0xaa4e0
leaq -0x1a0(%rbp), %rdi
callq 0xaa130
movq -0x20(%rbp), %rdi
callq 0xaaa60
nop