// File: /workspace/github_cpp_makefile_star2_O1/CMU-SAFARI[P]ramulator/src/Memory.h
// Function: ramulator::Memory<ramulator::DDR3, ramulator::Controller>::init_mapping_with_file(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
void init_mapping_with_file(string filename){
        ifstream file(filename);
        assert(file.good() && "Bad mapping file");
        // possible line types are:
        // 0. Empty line
        // 1. Direct bit assignment   : component N   = x
        // 2. Direct range assignment : component N:M = x:y
        // 3. XOR bit assignment      : component N   = x y z ...
        // 4. Comment line            : # comment here
        string line;
        char delim[] = " \t";
        while (getline(file, line)) {
            short capture_flags = 0;
            int level = -1;
            int target_bit = -1, target_bit2 = -1;
            int source_bit = -1, source_bit2 = -1;
            // cout << "Processing: " << line << endl;
            bool is_range = false;
            while (true) { // process next word
                size_t start = line.find_first_not_of(delim);
                if (start == string::npos) // no more words
                    break;
                size_t end = line.find_first_of(delim, start);
                string word = line.substr(start, end - start);
                
                if (word.at(0) == '#')// starting a comment
                    break;
                
                size_t col_index;
                int source_min, target_min, target_max;
                switch (capture_flags){
                    case 0: // capturing the component name
                        // fetch component level from channel spec
                        for (int i = 0; i < int(T::Level::MAX); i++)
                            if (word.find(T::level_str[i]) != string::npos) {
                                level = i;
                                capture_flags ++;
                            }
                        break;

                    case 1: // capturing target bit(s)
                        col_index = word.find(":");
                        if ( col_index != string::npos ){
                            target_bit2 = stoi(word.substr(col_index+1));
                            word = word.substr(0,col_index);
                            is_range = true;
                        }
                        target_bit = stoi(word);
                        capture_flags ++;
                        break;

                    case 2: //this should be the delimiter
                        assert(word.find("=") != string::npos);
                        capture_flags ++;
                        break;

                    case 3:
                        if (is_range){
                            col_index = word.find(":");
                            source_bit  = stoi(word.substr(0,col_index));
                            source_bit2 = stoi(word.substr(col_index+1));
                            assert(source_bit2 - source_bit == target_bit2 - target_bit);
                            source_min = min(source_bit, source_bit2);
                            target_min = min(target_bit, target_bit2);
                            target_max = max(target_bit, target_bit2);
                            while (target_min <= target_max){
                                mapping_scheme[level][target_min].push_back(source_min);
                                // cout << target_min << " <- " << source_min << endl;
                                source_min ++;
                                target_min ++;
                            }
                        }
                        else {
                            source_bit = stoi(word);
                            mapping_scheme[level][target_bit].push_back(source_bit);
                        }
                }
                if (end == string::npos) { // this is the last word
                    break;
                }
                line = line.substr(end);
            }
        }
        if (dump_mapping)
            dump_mapping_scheme();
    }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2c8, %rsp            # imm = 0x2C8
movq %rdi, %rbx
leaq 0xc0(%rsp), %r14
movq %r14, %rdi
movl $0x8, %edx
callq 0xb470
movq (%r14), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0xe0(%rsp,%rax)
jne 0x266b3
leaq 0x90(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq $0x0, -0x8(%rcx)
xorl %eax, %eax
movb %al, (%rcx)
leaq 0x4(%rsp), %r12
movb %al, 0x2(%r12)
movw $0x920, (%r12)          # imm = 0x920
movq %rbx, 0xb0(%rsp)
leaq 0xb50(%rbx), %rax
movq %rax, 0xa8(%rsp)
leaq 0x80(%rsp), %r13
leaq 0x17e56b(%rip), %rax    # 0x1a4450
addq $0x8, %rax
movq %rax, 0xb8(%rsp)
movq 0xc0(%rsp), %rax
movq -0x18(%rax), %rdi
leaq 0xc0(%rsp), %rbx
addq %rbx, %rdi
movl $0xa, %esi
callq 0xb2c0
movsbl %al, %edx
movq %rbx, %rdi
movq %r13, %rsi
callq 0xb540
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x26593
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0x50(%rsp)
movl $0x0, 0x5c(%rsp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0xa0(%rsp)
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
xorl %ebp, %ebp
movq %r12, %rdi
callq 0xb180
xorl %r14d, %r14d
movq %r13, %rdi
movq %r12, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0xb4c0
cmpq $-0x1, %rax
je 0x264ae
movq %rax, %rbx
movq %r12, %rdi
callq 0xb180
movq %r13, %rdi
movq %r12, %rsi
movq %rbx, %rdx
movq %rax, %rcx
callq 0xb400
movq %rax, 0x40(%rsp)
movq %rax, %rcx
subq %rbx, %rcx
leaq 0x60(%rsp), %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0xb310
movq 0x68(%rsp), %rdx
testq %rdx, %rdx
je 0x265ed
movq 0x60(%rsp), %r13
cmpb $0x23, (%r13)
jne 0x25fdf
xorl %r14d, %r14d
leaq 0x80(%rsp), %r13
jmp 0x26492
cmpw $0x3, %bp
ja 0x26439
movzwl %bp, %eax
leaq 0x12709d(%rip), %rcx    # 0x14d090
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0xb8(%rsp), %rbx
xorl %r12d, %r12d
leaq 0x80(%rsp), %r13
leaq 0x60(%rsp), %r14
movq -0x8(%rbx), %rsi
movq (%rbx), %rcx
movq %r14, %rdi
xorl %edx, %edx
callq 0xb500
xorl %ecx, %ecx
cmpq $-0x1, %rax
setne %cl
cmovnel %r12d, %r15d
addl %ecx, %ebp
incq %r12
addq $0x20, %rbx
cmpq $0x5, %r12
jne 0x26014
leaq 0x4(%rsp), %r12
jmp 0x26441
movl $0x1, %ecx
leaq 0x60(%rsp), %rdi
leaq 0x1272cb(%rip), %rsi    # 0x14d327
xorl %edx, %edx
callq 0xb500
cmpq $-0x1, %rax
leaq 0x80(%rsp), %r13
movq 0x40(%rsp), %rdx
je 0x26615
incl %ebp
leaq 0x18(%rsp), %r14
jmp 0x2644b
testb $0x1, 0x5c(%rsp)
je 0x264bc
movl $0x1, %ecx
leaq 0x60(%rsp), %rbx
movq %rbx, %rdi
leaq 0x128a7c(%rip), %rsi    # 0x14eb21
xorl %edx, %edx
callq 0xb500
movq %rax, %r13
leaq 0x18(%rsp), %rdi
movq %rbx, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0xb310
movq 0x18(%rsp), %r12
callq 0xb050
movl (%rax), %ecx
movl %ecx, 0x14(%rsp)
movq %rax, 0x8(%rsp)
movl $0x0, (%rax)
movq %r12, %rdi
leaq 0x38(%rsp), %rsi
movl $0xa, %edx
callq 0xb3b0
cmpq %r12, 0x38(%rsp)
je 0x2667c
movq %rax, %rbx
addq $-0x80000000, %rax      # imm = 0x80000000
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
cmpq %rcx, %rax
jb 0x26688
movq 0x8(%rsp), %rax
movl (%rax), %eax
cmpl $0x22, %eax
je 0x26688
testl %eax, %eax
leaq 0x60(%rsp), %r14
jne 0x26139
movq 0x8(%rsp), %rax
movl 0x14(%rsp), %ecx
movl %ecx, (%rax)
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x26155
movq 0x28(%rsp), %rsi
incq %rsi
callq 0xb2f0
incq %r13
leaq 0x18(%rsp), %rdi
movq %r14, %rsi
movq %r13, %rdx
movq $-0x1, %rcx
callq 0xb310
movq 0x18(%rsp), %r12
movq 0x8(%rsp), %rax
movl (%rax), %ecx
movl %ecx, 0x14(%rsp)
movl $0x0, (%rax)
movq %r12, %rdi
leaq 0x38(%rsp), %rsi
movl $0xa, %edx
callq 0xb3b0
cmpq %r12, 0x38(%rsp)
je 0x26670
movq %rax, %r13
addq $-0x80000000, %rax      # imm = 0x80000000
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
cmpq %rcx, %rax
leaq 0x18(%rsp), %r14
movq 0x50(%rsp), %r12
jb 0x26658
movq 0x8(%rsp), %rax
movl (%rax), %eax
cmpl $0x22, %eax
je 0x26658
testl %eax, %eax
jne 0x261e7
movq 0x8(%rsp), %rax
movl 0x14(%rsp), %ecx
movl %ecx, (%rax)
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x26203
movq 0x28(%rsp), %rsi
incq %rsi
callq 0xb2f0
movl %r13d, %eax
subl %ebx, %eax
movq 0xa0(%rsp), %rdx
movl %edx, %ecx
subl %r12d, %ecx
cmpl %ecx, %eax
jne 0x26694
cmpl %ebx, %r13d
cmovll %r13d, %ebx
cmpl %r12d, %edx
movq %r12, %rax
cmovll %edx, %r12d
movl %edx, %r13d
cmovll %eax, %r13d
incl %r13d
movl %r15d, 0x18(%rsp)
movq 0xa8(%rsp), %rdi
movq %r14, %rsi
callq 0x274bc
movl %r12d, 0x38(%rsp)
movq %rax, %rdi
leaq 0x38(%rsp), %rsi
callq 0x27520
movl %ebx, 0x4c(%rsp)
movq 0x8(%rax), %rsi
cmpq 0x10(%rax), %rsi
je 0x26279
movl %ebx, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rax)
jmp 0x26286
movq %rax, %rdi
leaq 0x4c(%rsp), %rdx
callq 0x27c2c
incl %ebx
incl %r12d
cmpl %r12d, %r13d
jne 0x26238
leaq 0x4(%rsp), %r12
leaq 0x80(%rsp), %r13
jmp 0x26446
movl $0x1, %ecx
leaq 0x60(%rsp), %r14
movq %r14, %rdi
leaq 0x12886b(%rip), %rsi    # 0x14eb21
xorl %edx, %edx
callq 0xb500
cmpq $-0x1, %rax
je 0x263ba
movq %rax, %rbx
leaq 0x1(%rax), %rdx
leaq 0x18(%rsp), %rdi
movq %r14, %rsi
movq $-0x1, %rcx
callq 0xb310
movq 0x18(%rsp), %r13
callq 0xb050
movq %rax, %r12
movl (%rax), %eax
movl %eax, 0x50(%rsp)
movl $0x0, (%r12)
movq %r13, %rdi
leaq 0x38(%rsp), %rsi
movl $0xa, %edx
callq 0xb3b0
cmpq %r13, 0x38(%rsp)
je 0x26634
movq %rax, 0xa0(%rsp)
addq $-0x80000000, %rax      # imm = 0x80000000
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
cmpq %rcx, %rax
leaq 0x18(%rsp), %r14
jb 0x26640
movl (%r12), %eax
cmpl $0x22, %eax
je 0x26640
testl %eax, %eax
jne 0x26359
movl 0x50(%rsp), %eax
movl %eax, (%r12)
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x26375
movq 0x28(%rsp), %rsi
incq %rsi
callq 0xb2f0
movq %r14, %rdi
movq %r14, %r12
leaq 0x60(%rsp), %r14
movq %r14, %rsi
xorl %edx, %edx
movq %rbx, %rcx
callq 0xb310
movq %r14, %rdi
movq %r12, %rsi
callq 0xb370
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x263b4
movq 0x28(%rsp), %rsi
incq %rsi
callq 0xb2f0
movb $0x1, %al
movl %eax, 0x5c(%rsp)
movq 0x60(%rsp), %rbx
callq 0xb050
movq %rax, %r12
movl (%rax), %r13d
movl $0x0, (%rax)
movq %rbx, %rdi
leaq 0x18(%rsp), %r14
movq %r14, %rsi
movl $0xa, %edx
callq 0xb3b0
cmpq %rbx, 0x18(%rsp)
je 0x26609
movq %rax, 0x50(%rsp)
addq $-0x80000000, %rax      # imm = 0x80000000
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
cmpq %rcx, %rax
movq 0x40(%rsp), %rdx
jb 0x265fd
movl (%r12), %eax
cmpl $0x22, %eax
je 0x265fd
testl %eax, %eax
jne 0x26428
movl %r13d, (%r12)
incl %ebp
leaq 0x4(%rsp), %r12
leaq 0x80(%rsp), %r13
jmp 0x2644b
leaq 0x80(%rsp), %r13
leaq 0x18(%rsp), %r14
movq 0x40(%rsp), %rdx
cmpq $-0x1, %rdx
je 0x2648f
movq %r14, %rdi
movq %r13, %rsi
movq $-0x1, %rcx
callq 0xb310
movq %r13, %rdi
movq %r14, %rsi
callq 0xb370
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x2648a
movq 0x28(%rsp), %rsi
incq %rsi
callq 0xb2f0
movb $0x1, %r14b
jmp 0x26492
xorl %r14d, %r14d
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x264ae
movq 0x70(%rsp), %rsi
incq %rsi
callq 0xb2f0
testb %r14b, %r14b
jne 0x25f59
jmp 0x25ef1
callq 0xb050
movq %rax, %r12
movl (%rax), %eax
movl %eax, 0x8(%rsp)
movl $0x0, (%r12)
movq %r13, %rdi
leaq 0x18(%rsp), %r14
movq %r14, %rsi
movl $0xa, %edx
callq 0xb3b0
cmpq %r13, 0x18(%rsp)
je 0x26664
movq %rax, %rbx
addq $-0x80000000, %rax      # imm = 0x80000000
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
cmpq %rcx, %rax
leaq 0x80(%rsp), %r13
jb 0x2664c
movl (%r12), %eax
cmpl $0x22, %eax
je 0x2664c
testl %eax, %eax
jne 0x2652f
movl 0x8(%rsp), %eax
movl %eax, (%r12)
movl %r15d, 0x18(%rsp)
movq 0xa8(%rsp), %rdi
movq %r14, %rsi
callq 0x274bc
movq 0x50(%rsp), %rcx
movl %ecx, 0x38(%rsp)
movq %rax, %rdi
leaq 0x38(%rsp), %rsi
callq 0x27520
leaq 0x4(%rsp), %r12
movl %ebx, 0x4c(%rsp)
movq 0x8(%rax), %rsi
cmpq 0x10(%rax), %rsi
je 0x2657c
movl %ebx, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rax)
jmp 0x26446
movq %rax, %rdi
leaq 0x4c(%rsp), %rdx
callq 0x27c2c
movq 0x40(%rsp), %rdx
jmp 0x2644b
movq 0xb0(%rsp), %rdi
cmpb $0x1, 0xc61(%rdi)
jne 0x265a9
callq 0x27584
movq 0x80(%rsp), %rdi
leaq 0x90(%rsp), %rax
cmpq %rax, %rdi
je 0x265ce
movq 0x90(%rsp), %rsi
incq %rsi
callq 0xb2f0
leaq 0xc0(%rsp), %rdi
callq 0xb0d0
addq $0x2c8, %rsp            # imm = 0x2C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x128297(%rip), %rdi    # 0x14e88b
xorl %esi, %esi
xorl %eax, %eax
callq 0xb3f0
leaq 0x1282cb(%rip), %rdi    # 0x14e8cf
callq 0xb3d0
leaq 0x1282bf(%rip), %rdi    # 0x14e8cf
callq 0xb1f0
leaq 0x12821b(%rip), %rdi    # 0x14e837
leaq 0x127b8e(%rip), %rsi    # 0x14e1b1
leaq 0x12818b(%rip), %rcx    # 0x14e7b5
movl $0x194, %edx            # imm = 0x194
callq 0xb200
leaq 0x128294(%rip), %rdi    # 0x14e8cf
callq 0xb1f0
leaq 0x128288(%rip), %rdi    # 0x14e8cf
callq 0xb3d0
leaq 0x12827c(%rip), %rdi    # 0x14e8cf
callq 0xb3d0
leaq 0x128270(%rip), %rdi    # 0x14e8cf
callq 0xb3d0
leaq 0x128264(%rip), %rdi    # 0x14e8cf
callq 0xb1f0
leaq 0x128258(%rip), %rdi    # 0x14e8cf
callq 0xb1f0
leaq 0x12824c(%rip), %rdi    # 0x14e8cf
callq 0xb1f0
leaq 0x128240(%rip), %rdi    # 0x14e8cf
callq 0xb3d0
leaq 0x1281bb(%rip), %rdi    # 0x14e856
leaq 0x127b0f(%rip), %rsi    # 0x14e1b1
leaq 0x12810c(%rip), %rcx    # 0x14e7b5
movl $0x19d, %edx            # imm = 0x19D
callq 0xb200
leaq 0x1280d9(%rip), %rdi    # 0x14e793
leaq 0x127af0(%rip), %rsi    # 0x14e1b1
leaq 0x1280ed(%rip), %rcx    # 0x14e7b5
movl $0x162, %edx            # imm = 0x162
callq 0xb200
jmp 0x2676d
jmp 0x26776
jmp 0x26776
jmp 0x26776
jmp 0x26776
jmp 0x26776
jmp 0x26776
jmp 0x26776
jmp 0x266f9
movq %rax, %rbx
movq 0x8(%rsp), %rax
cmpl $0x0, (%rax)
jne 0x2673b
movq 0x8(%rsp), %rax
movl 0x14(%rsp), %ecx
movl %ecx, (%rax)
jmp 0x2673b
movq %rax, %rbx
cmpl $0x0, (%r12)
jne 0x26779
movl 0x8(%rsp), %eax
movl %eax, (%r12)
jmp 0x26779
jmp 0x2676d
movq %rax, %rbx
cmpl $0x0, (%r12)
jne 0x2673b
movl 0x50(%rsp), %eax
movl %eax, (%r12)
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x26779
movq 0x28(%rsp), %rsi
incq %rsi
callq 0xb2f0
jmp 0x26779
movq %rax, %rbx
cmpl $0x0, (%r12)
jne 0x26779
movl %r13d, (%r12)
jmp 0x26779
jmp 0x26776
jmp 0x26776
movq %rax, %rbx
jmp 0x26795
jmp 0x26776
jmp 0x26776
movq %rax, %rbx
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x26795
movq 0x70(%rsp), %rsi
incq %rsi
callq 0xb2f0
movq 0x80(%rsp), %rdi
leaq 0x90(%rsp), %rax
cmpq %rax, %rdi
je 0x267ba
movq 0x90(%rsp), %rsi
incq %rsi
callq 0xb2f0
leaq 0xc0(%rsp), %rdi
callq 0xb0d0
movq %rbx, %rdi
callq 0xb510
nop