// File: eniac[P]Mantis/src/compile/compile_p4.cpp
// Function: transformTableWithRefRead(MblRefNode*, TableNode*, P4RMalleableFieldNode const&)
static void transformTableWithRefRead(MblRefNode* ref, TableNode* table,
                               const P4RMalleableFieldNode& variable) {
    auto readStmtsNode = dynamic_cast<TableReadStmtsNode*>(ref->parent_->parent_);

    // Assemble list of alts
    vector<FieldNode*> alts = findAllAlts(variable);

    bool first = true;
    TableReadStmtNode::MatchType matchType;
    for (FieldNode* fn : alts) {
        const string& headerName = *fn->headerName_->word_;
        const string& fieldName = *fn->fieldName_->word_;

        if (first) {
            ref->transform(headerName, fieldName);
            auto readStmt = dynamic_cast<TableReadStmtNode*>(ref->parent_);
            if (readStmt->matchType_ == TableReadStmtNode::EXACT) {
                readStmt->matchType_ = TableReadStmtNode::TERNARY;
            }
            matchType = readStmt->matchType_;

            first = false;
        } else {
            auto newStmtNode = new TableReadStmtNode(matchType, fn);
            readStmtsNode->push_back(newStmtNode);
        }
    }

    // Add exact match bit for the meta field of the variable
    const string variableName = *ref->name_->word_;
    ostringstream oss;
    oss << kP4rIngMetadataName << "." << variableName << kP4rIndexSuffix;
    if (!findTableReadStmt(*table, oss.str())) {
        auto newReadStmtNode =
            new TableReadStmtNode(TableReadStmtNode::EXACT,
                                  new StrNode(new string(oss.str())));
        table->reads_->push_back(newReadStmtNode);
    }    
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2c0, %rsp            # imm = 0x2C0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rax
movq 0x30(%rax), %rax
movq %rax, -0x258(%rbp)
cmpq $0x0, %rax
je 0x126117
movq -0x258(%rbp), %rdi
leaq 0x4c4e2(%rip), %rsi     # 0x1725e0
leaq 0x4cab3(%rip), %rdx     # 0x172bb8
xorl %eax, %eax
movl %eax, %ecx
callq 0xaa660
movq %rax, -0x260(%rbp)
jmp 0x126122
xorl %eax, %eax
movq %rax, -0x260(%rbp)
jmp 0x126122
movq -0x260(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x12d7e0
movb $0x1, -0x39(%rbp)
leaq -0x38(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
callq 0xb7b80
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rdi
callq 0xb7a10
movq %rax, -0x58(%rbp)
leaq -0x50(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0xcb0f0
testb $0x1, %al
jne 0x126176
jmp 0x1262ce
leaq -0x50(%rbp), %rdi
callq 0xcb130
movq (%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
movq 0x40(%rax), %rax
movq 0x40(%rax), %rax
movq %rax, -0x68(%rbp)
movq -0x60(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x40(%rax), %rax
movq %rax, -0x70(%rbp)
testb $0x1, -0x39(%rbp)
je 0x126257
movq -0x8(%rbp), %rdi
movq -0x68(%rbp), %rsi
movq -0x70(%rbp), %rdx
callq 0xcac20
jmp 0x1261c3
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x268(%rbp)
cmpq $0x0, %rax
je 0x126210
movq -0x268(%rbp), %rdi
leaq 0x4c3fa(%rip), %rsi     # 0x1725e0
leaq 0x4cbdb(%rip), %rdx     # 0x172dc8
xorl %eax, %eax
movl %eax, %ecx
callq 0xaa660
movq %rax, -0x270(%rbp)
jmp 0x12621b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x126535
xorl %eax, %eax
movq %rax, -0x270(%rbp)
jmp 0x12621b
movq -0x270(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
cmpl $0x0, 0x3c(%rax)
jne 0x126244
movq -0x88(%rbp), %rax
movl $0x1, 0x3c(%rax)
movq -0x88(%rbp), %rax
movl 0x3c(%rax), %eax
movl %eax, -0x40(%rbp)
movb $0x0, -0x39(%rbp)
jmp 0x1262be
movl $0x48, %edi
callq 0xaa560
movq %rax, -0x278(%rbp)
jmp 0x12626a
movq -0x278(%rbp), %rdi
movl -0x40(%rbp), %esi
movq -0x60(%rbp), %rdx
callq 0xbc9c0
jmp 0x12627f
movq -0x278(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x20(%rbp), %rdi
movq -0x90(%rbp), %rsi
callq 0xb2570
jmp 0x12629f
jmp 0x1262be
movq -0x278(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
callq 0xaa4e0
jmp 0x126535
jmp 0x1262c0
leaq -0x50(%rbp), %rdi
callq 0xcb150
jmp 0x126160
movq -0x8(%rbp), %rax
movq 0x80(%rax), %rax
movq 0x40(%rax), %rsi
leaq -0xb0(%rbp), %rdi
callq 0xaa290
jmp 0x1262eb
leaq -0x228(%rbp), %rdi
callq 0xaa7b0
jmp 0x1262f9
leaq 0x15bc3(%rip), %rsi     # 0x13bec3
leaq -0x228(%rbp), %rdi
callq 0xaa540
movq %rax, -0x280(%rbp)
jmp 0x126315
movq -0x280(%rbp), %rdi
leaq 0x158ce(%rip), %rsi     # 0x13bbf1
callq 0xaa540
movq %rax, -0x288(%rbp)
jmp 0x126331
movq -0x288(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0xaa4c0
movq %rax, -0x290(%rbp)
jmp 0x12634d
movq -0x290(%rbp), %rdi
leaq 0x15eab(%rip), %rsi     # 0x13c206
callq 0xaa540
jmp 0x126362
movq -0x10(%rbp), %rax
movq %rax, -0x298(%rbp)
leaq -0x248(%rbp), %rdi
leaq -0x228(%rbp), %rsi
callq 0xaa580
jmp 0x126382
movq -0x298(%rbp), %rdi
leaq -0x248(%rbp), %rsi
callq 0x12d4d0
movb %al, -0x299(%rbp)
jmp 0x12639d
movb -0x299(%rbp), %al
xorb $-0x1, %al
movb %al, -0x29a(%rbp)
leaq -0x248(%rbp), %rdi
callq 0xaabe8
movb -0x29a(%rbp), %al
testb $0x1, %al
jne 0x1263c6
jmp 0x1264f3
movl $0x48, %edi
callq 0xaa560
movq %rax, -0x2a8(%rbp)
jmp 0x1263d9
movl $0x48, %edi
callq 0xaa560
movq %rax, -0x2b0(%rbp)
jmp 0x1263ec
movl $0x20, %edi
callq 0xaa560
movq %rax, -0x2b8(%rbp)
jmp 0x1263ff
movq -0x2b8(%rbp), %rdi
leaq -0x228(%rbp), %rsi
callq 0xaa580
jmp 0x126414
movq -0x2b8(%rbp), %rsi
movq -0x2b0(%rbp), %rdi
callq 0xb2890
jmp 0x126429
movq -0x2b0(%rbp), %rdx
movq -0x2a8(%rbp), %rdi
xorl %esi, %esi
callq 0xbc9c0
jmp 0x126440
movq -0x2a8(%rbp), %rax
movq %rax, -0x250(%rbp)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
movq -0x250(%rbp), %rsi
callq 0xb2570
jmp 0x126464
jmp 0x1264f3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x126529
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x12651d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x248(%rbp), %rdi
callq 0xaabe8
jmp 0x12651d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x1264e5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x1264d9
movq -0x2b8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
callq 0xaa4e0
movq -0x2b0(%rbp), %rdi
callq 0xaa4e0
movq -0x2a8(%rbp), %rdi
callq 0xaa4e0
jmp 0x12651d
leaq -0x228(%rbp), %rdi
callq 0xaa130
leaq -0xb0(%rbp), %rdi
callq 0xaabe8
leaq -0x38(%rbp), %rdi
callq 0x127f90
addq $0x2c0, %rsp            # imm = 0x2C0
popq %rbp
retq
leaq -0x228(%rbp), %rdi
callq 0xaa130
leaq -0xb0(%rbp), %rdi
callq 0xaabe8
leaq -0x38(%rbp), %rdi
callq 0x127f90
movq -0x78(%rbp), %rdi
callq 0xaaa60
nopw (%rax,%rax)