// File: eniac[P]Mantis/src/compile/compile_p4.cpp
// Function: generateIngDigestPacking(std::vector<AstNode*, std::allocator<AstNode*>>*, std::vector<ReactionArgNode*, std::allocator<ReactionArgNode*>> const&, std::unordered_map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::vector<FieldDecNode*, std::allocator<FieldDecNode*>>*, std::hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::equal_to<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const, std::vector<FieldDecNode*, std::allocator<FieldDecNode*>>*>>> const&, std::unordered_map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, P4RMalleableValueNode*, std::hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::equal_to<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const, P4RMalleableValueNode*>>> const&, std::unordered_map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, P4RMalleableFieldNode*, std::hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::equal_to<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const, P4RMalleableFieldNode*>>> const&, std::vector<AstNode*, std::allocator<AstNode*>> const&, int*)
vector<ReactionArgBin> generateIngDigestPacking(
            vector<AstNode*>* newNodes,
            const vector<ReactionArgNode*>& reaction_args,
            const HeaderDecsMap& headerDecsMap,
            const unordered_map<string, P4RMalleableValueNode*>& mblValues,
            const unordered_map<string, P4RMalleableFieldNode*>& mblFields,
            const vector<AstNode*>& astNodes, int* ing_iso_opt) {
    if (reaction_args.size() == 0) {
        return vector<ReactionArgBin>();
    }

    vector<pair<ReactionArgNode*, int> > argSizes =
            findAllReactionArgSizes(reaction_args, headerDecsMap, mblValues,
                                    mblFields);

    // Currently pack arguments into 32-bit bins
    // Note HW target could support 64-bit reg --- further reducing the number of packed regs by half (therefore latency)
    vector<ReactionArgBin> argBins = runBinPackForIng(&argSizes, astNodes, true);
    vector<MblRefNode*> mblRefs = generatePackingTablesForIng(newNodes, argBins, true);

    // Update meas isolation option
    if(argBins.size()<=1) {
        vector<ReactionArgNode*> reaction_args = findReactionArgs(astNodes);
        bool has_regarg = false;
        for (auto ra : reaction_args) {    
            if (ra->argType_==ReactionArgNode::REGISTER) {
                if(findRegargInIng(ra, astNodes)) {
                    has_regarg = true;
                }
            }    
        }
        if(!has_regarg) {
            if((*ing_iso_opt)==1) {
                *ing_iso_opt = 0;
            } else if ((*ing_iso_opt)==3) {
                *ing_iso_opt = 2;
            }
            PRINT_VERBOSE("Update ing isolation opt to %d\n", *ing_iso_opt);
        }
    }

    generateArgRegistersForIng(newNodes, argBins, *ing_iso_opt, true);

    // Redo the transformations to capture any malleable references in the generated code
    PRINT_VERBOSE("Found %d generated malleable refs for ing\n", mblRefs.size());
    transformMalleableRefs(&mblRefs, mblValues, mblFields, newNodes);

    return argBins;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0xf8(%rbp)
movq %rdi, %rax
movq %rax, -0xf0(%rbp)
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq -0x18(%rbp), %rdi
callq 0xb7500
cmpq $0x0, %rax
jne 0x11fd0f
movq -0xf8(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0xaa330
movq -0xf8(%rbp), %rdi
callq 0xcc7d0
jmp 0x11ffe5
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %r8
leaq -0x48(%rbp), %rdi
callq 0x12e0c0
movb $0x0, -0x49(%rbp)
movq 0x10(%rbp), %rsi
leaq -0x68(%rbp), %rdi
callq 0xb2bc0
jmp 0x11fd3b
movq -0xf8(%rbp), %rdi
leaq -0x48(%rbp), %rsi
leaq -0x68(%rbp), %rdx
movl $0x1, %ecx
callq 0x120000
jmp 0x11fd56
leaq -0x68(%rbp), %rdi
callq 0xb22f0
movq -0xf8(%rbp), %rdx
movq -0x10(%rbp), %rsi
leaq -0x90(%rbp), %rdi
movl $0x1, %ecx
callq 0x120290
jmp 0x11fd7d
movq -0xf8(%rbp), %rdi
callq 0xd1800
cmpq $0x1, %rax
ja 0x11ff5a
movq 0x10(%rbp), %rsi
leaq -0xa8(%rbp), %rdi
callq 0x12dc10
jmp 0x11fda5
movb $0x0, -0xa9(%rbp)
leaq -0xa8(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0xb8(%rbp), %rdi
callq 0xb73b0
movq %rax, -0xc0(%rbp)
movq -0xb8(%rbp), %rdi
callq 0xb7240
movq %rax, -0xc8(%rbp)
leaq -0xc0(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0xcb210
testb $0x1, %al
jne 0x11fdfc
jmp 0x11ff09
leaq -0xc0(%rbp), %rdi
callq 0xcb250
movq (%rax), %rax
movq %rax, -0xd0(%rbp)
movq -0xd0(%rbp), %rax
cmpl $0x4, 0x3c(%rax)
jne 0x11fef6
movq -0xd0(%rbp), %rax
movq %rax, -0x100(%rbp)
movq 0x10(%rbp), %rsi
leaq -0xe8(%rbp), %rdi
callq 0xb2bc0
jmp 0x11fe43
movq -0x100(%rbp), %rdi
leaq -0xe8(%rbp), %rsi
callq 0x12e7f0
movb %al, -0x101(%rbp)
jmp 0x11fe5e
leaq -0xe8(%rbp), %rdi
callq 0xb22f0
movb -0x101(%rbp), %al
testb $0x1, %al
jne 0x11fe76
jmp 0x11fef4
movb $0x1, -0xa9(%rbp)
jmp 0x11fef4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x11ffda
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x68(%rbp), %rdi
callq 0xb22f0
jmp 0x11ffda
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x11ffce
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x11ffa9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x11ff4c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0xe8(%rbp), %rdi
callq 0xb22f0
jmp 0x11ff4c
jmp 0x11fef6
jmp 0x11fef8
leaq -0xc0(%rbp), %rdi
callq 0xcb270
jmp 0x11fde0
testb $0x1, -0xa9(%rbp)
jne 0x11ff3e
movq 0x18(%rbp), %rax
cmpl $0x1, (%rax)
jne 0x11ff27
movq 0x18(%rbp), %rax
movl $0x0, (%rax)
jmp 0x11ff3c
movq 0x18(%rbp), %rax
cmpl $0x3, (%rax)
jne 0x11ff3a
movq 0x18(%rbp), %rax
movl $0x2, (%rax)
jmp 0x11ff3c
jmp 0x11ff3e
leaq -0xa8(%rbp), %rdi
callq 0xccb90
jmp 0x11ff5a
leaq -0xa8(%rbp), %rdi
callq 0xccb90
jmp 0x11ffa9
movq -0xf8(%rbp), %rsi
movq -0x10(%rbp), %rdi
movq 0x18(%rbp), %rax
movl (%rax), %edx
movl $0x1, %ecx
callq 0x122490
jmp 0x11ff77
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0x10(%rbp), %rcx
leaq -0x90(%rbp), %rdi
callq 0x119120
jmp 0x11ff91
movb $0x1, -0x49(%rbp)
leaq -0x90(%rbp), %rdi
callq 0xccb00
testb $0x1, -0x49(%rbp)
jne 0x11ffc3
jmp 0x11ffb7
leaq -0x90(%rbp), %rdi
callq 0xccb00
jmp 0x11ffce
movq -0xf8(%rbp), %rdi
callq 0xcc7f0
leaq -0x48(%rbp), %rdi
callq 0xd09b0
jmp 0x11ffe5
movq -0xf8(%rbp), %rdi
callq 0xcc7f0
leaq -0x48(%rbp), %rdi
callq 0xd09b0
jmp 0x11fff5
movq -0xf0(%rbp), %rax
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
movq -0x70(%rbp), %rdi
callq 0xaaa60
nop