// File: 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
movq %rsp, %rbp
subq $0x410, %rsp            # imm = 0x410
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x380(%rbp)
leaq -0x218(%rbp), %rdi
movl $0x8, %edx
callq 0xc530
movq -0x218(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x218(%rbp,%rax), %rdi
callq 0xc590
movb %al, -0x371(%rbp)
jmp 0x37c9f
movb -0x371(%rbp), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x381(%rbp)
jne 0x37cb4
jmp 0x37cbe
movb $0x1, %al
movb %al, -0x381(%rbp)
jmp 0x37cbe
movb -0x381(%rbp), %al
testb $0x1, %al
jne 0x37cca
jmp 0x37ccc
jmp 0x37d02
leaq 0x2192d9(%rip), %rdi    # 0x250fac
leaq 0x218cdf(%rip), %rsi    # 0x2509b9
movl $0x162, %edx            # imm = 0x162
leaq 0x2192e8(%rip), %rcx    # 0x250fce
callq 0xc280
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
jmp 0x384d8
leaq -0x248(%rbp), %rdi
callq 0xc480
movw 0x21933b(%rip), %ax     # 0x251050
movw %ax, -0x24b(%rbp)
movb 0x219330(%rip), %al     # 0x251052
movb %al, -0x249(%rbp)
leaq -0x218(%rbp), %rdi
leaq -0x248(%rbp), %rsi
callq 0xc0e0
movq %rax, -0x390(%rbp)
jmp 0x37d44
movq -0x390(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0xc440
movb %al, -0x391(%rbp)
jmp 0x37d62
movb -0x391(%rbp), %al
testb $0x1, %al
jne 0x37d71
jmp 0x3848b
movw $0x0, -0x24e(%rbp)
movl $0xffffffff, -0x254(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x258(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x25c(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x260(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x264(%rbp) # imm = 0xFFFFFFFF
movb $0x0, -0x265(%rbp)
jmp 0x37db5
leaq -0x24b(%rbp), %rsi
leaq -0x248(%rbp), %rdi
xorl %eax, %eax
movl %eax, %edx
callq 0xc240
movq %rax, -0x270(%rbp)
cmpq $-0x1, -0x270(%rbp)
jne 0x37df9
jmp 0x38486
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
jmp 0x384cc
movq -0x270(%rbp), %rdx
leaq -0x248(%rbp), %rdi
movq %rdi, -0x3a0(%rbp)
leaq -0x24b(%rbp), %rsi
callq 0xc200
movq -0x3a0(%rbp), %rsi
movq %rax, -0x278(%rbp)
movq -0x270(%rbp), %rdx
movq -0x278(%rbp), %rcx
subq %rdx, %rcx
leaq -0x298(%rbp), %rdi
callq 0xc380
jmp 0x37e47
xorl %eax, %eax
movl %eax, %esi
leaq -0x298(%rbp), %rdi
callq 0xc650
movq %rax, -0x3a8(%rbp)
jmp 0x37e60
movq -0x3a8(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x23, %eax
jne 0x37e95
movl $0x5, -0x29c(%rbp)
jmp 0x38459
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
jmp 0x38478
movswl -0x24e(%rbp), %eax
movl %eax, %ecx
movq %rcx, -0x3b0(%rbp)
subl $0x3, %eax
ja 0x383f7
movq -0x3b0(%rbp), %rax
leaq 0x217224(%rip), %rcx    # 0x24f0e0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x0, -0x2b8(%rbp)
cmpl $0x5, -0x2b8(%rbp)
jge 0x37f34
movslq -0x2b8(%rbp), %rax
leaq 0x31660a(%rip), %rsi    # 0x34e4f0
shlq $0x5, %rax
addq %rax, %rsi
leaq -0x298(%rbp), %rdi
xorl %eax, %eax
movl %eax, %edx
callq 0xc6a0
cmpq $-0x1, %rax
je 0x37f21
movl -0x2b8(%rbp), %eax
movl %eax, -0x254(%rbp)
movw -0x24e(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x24e(%rbp)
jmp 0x37f23
movl -0x2b8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2b8(%rbp)
jmp 0x37ecf
jmp 0x383f7
leaq -0x298(%rbp), %rdi
leaq 0x2193b2(%rip), %rsi    # 0x2512f9
xorl %eax, %eax
movl %eax, %edx
callq 0xc680
movq %rax, -0x2a8(%rbp)
cmpq $-0x1, -0x2a8(%rbp)
je 0x3802b
movq -0x2a8(%rbp), %rdx
incq %rdx
leaq -0x2d8(%rbp), %rdi
leaq -0x298(%rbp), %rsi
movq $-0x1, %rcx
callq 0xc380
jmp 0x37f8b
xorl %eax, %eax
movl %eax, %esi
leaq -0x2d8(%rbp), %rdi
movl $0xa, %edx
callq 0x3a920
movl %eax, -0x3b4(%rbp)
jmp 0x37fa8
movl -0x3b4(%rbp), %eax
movl %eax, -0x25c(%rbp)
leaq -0x2d8(%rbp), %rdi
callq 0xc6b8
movq -0x2a8(%rbp), %rcx
xorl %eax, %eax
movl %eax, %edx
leaq -0x2f8(%rbp), %rdi
leaq -0x298(%rbp), %rsi
callq 0xc380
jmp 0x37fe0
leaq -0x298(%rbp), %rdi
leaq -0x2f8(%rbp), %rsi
callq 0xc420
leaq -0x2f8(%rbp), %rdi
callq 0xc6b8
movb $0x1, -0x265(%rbp)
jmp 0x3802b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
leaq -0x2d8(%rbp), %rdi
callq 0xc6b8
jmp 0x38478
xorl %eax, %eax
movl %eax, %esi
leaq -0x298(%rbp), %rdi
movl $0xa, %edx
callq 0x3a920
movl %eax, -0x3b8(%rbp)
jmp 0x38048
movl -0x3b8(%rbp), %eax
movl %eax, -0x258(%rbp)
movw -0x24e(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x24e(%rbp)
jmp 0x383f7
leaq -0x298(%rbp), %rdi
leaq 0x2231ff(%rip), %rsi    # 0x25b278
xorl %eax, %eax
movl %eax, %edx
callq 0xc680
cmpq $-0x1, %rax
je 0x3808a
jmp 0x380a9
leaq 0x218fc2(%rip), %rdi    # 0x251053
leaq 0x218921(%rip), %rsi    # 0x2509b9
movl $0x194, %edx            # imm = 0x194
leaq 0x218f2a(%rip), %rcx    # 0x250fce
callq 0xc280
movw -0x24e(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x24e(%rbp)
jmp 0x383f7
testb $0x1, -0x265(%rbp)
je 0x38352
leaq 0x219225(%rip), %rsi    # 0x2512f9
xorl %eax, %eax
movl %eax, %edx
movq %rdx, -0x3c0(%rbp)
leaq -0x298(%rbp), %rdi
movq %rdi, -0x3c8(%rbp)
callq 0xc680
movq -0x3c8(%rbp), %rsi
movq -0x3c0(%rbp), %rdx
movq %rax, -0x2a8(%rbp)
movq -0x2a8(%rbp), %rcx
leaq -0x318(%rbp), %rdi
callq 0xc380
jmp 0x3811c
xorl %eax, %eax
movl %eax, %esi
leaq -0x318(%rbp), %rdi
movl $0xa, %edx
callq 0x3a920
movl %eax, -0x3cc(%rbp)
jmp 0x38139
movl -0x3cc(%rbp), %eax
movl %eax, -0x260(%rbp)
leaq -0x318(%rbp), %rdi
callq 0xc6b8
movq -0x2a8(%rbp), %rdx
incq %rdx
leaq -0x338(%rbp), %rdi
leaq -0x298(%rbp), %rsi
movq $-0x1, %rcx
callq 0xc380
jmp 0x38177
xorl %eax, %eax
movl %eax, %esi
leaq -0x338(%rbp), %rdi
movl $0xa, %edx
callq 0x3a920
movl %eax, -0x3d0(%rbp)
jmp 0x38194
movl -0x3d0(%rbp), %eax
movl %eax, -0x264(%rbp)
leaq -0x338(%rbp), %rdi
callq 0xc6b8
movl -0x264(%rbp), %eax
subl -0x260(%rbp), %eax
movl -0x25c(%rbp), %ecx
subl -0x258(%rbp), %ecx
cmpl %ecx, %eax
jne 0x381ca
jmp 0x3822f
leaq 0x218ea1(%rip), %rdi    # 0x251072
leaq 0x2187e1(%rip), %rsi    # 0x2509b9
movl $0x19d, %edx            # imm = 0x19D
leaq 0x218dea(%rip), %rcx    # 0x250fce
callq 0xc280
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
leaq -0x318(%rbp), %rdi
callq 0xc6b8
jmp 0x38478
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
leaq -0x338(%rbp), %rdi
callq 0xc6b8
jmp 0x38478
leaq -0x260(%rbp), %rdi
leaq -0x264(%rbp), %rsi
callq 0x3a960
movq %rax, -0x3d8(%rbp)
jmp 0x3824b
movq -0x3d8(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x2ac(%rbp)
leaq -0x258(%rbp), %rdi
leaq -0x25c(%rbp), %rsi
callq 0x3a960
movq %rax, -0x3e0(%rbp)
jmp 0x38276
movq -0x3e0(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x2b0(%rbp)
leaq -0x258(%rbp), %rdi
leaq -0x25c(%rbp), %rsi
callq 0x26070
movl (%rax), %eax
movl %eax, -0x2b4(%rbp)
movl -0x2b0(%rbp), %eax
cmpl -0x2b4(%rbp), %eax
jg 0x3834d
movq -0x380(%rbp), %rdi
addq $0xb50, %rdi            # imm = 0xB50
movl -0x254(%rbp), %eax
movl %eax, -0x33c(%rbp)
leaq -0x33c(%rbp), %rsi
callq 0x3a9a0
movq %rax, -0x3e8(%rbp)
jmp 0x382e1
movq -0x3e8(%rbp), %rdi
movl -0x2b0(%rbp), %eax
movl %eax, -0x340(%rbp)
leaq -0x340(%rbp), %rsi
callq 0x3aa80
movq %rax, -0x3f0(%rbp)
jmp 0x38309
movq -0x3f0(%rbp), %rdi
movl -0x2ac(%rbp), %eax
movl %eax, -0x344(%rbp)
leaq -0x344(%rbp), %rsi
callq 0x3ab60
jmp 0x3832a
movl -0x2ac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2ac(%rbp)
movl -0x2b0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2b0(%rbp)
jmp 0x382a0
jmp 0x383f5
xorl %eax, %eax
movl %eax, %esi
leaq -0x298(%rbp), %rdi
movl $0xa, %edx
callq 0x3a920
movl %eax, -0x3f4(%rbp)
jmp 0x3836f
movq -0x380(%rbp), %rdi
movl -0x3f4(%rbp), %eax
movl %eax, -0x260(%rbp)
addq $0xb50, %rdi            # imm = 0xB50
movl -0x254(%rbp), %eax
movl %eax, -0x348(%rbp)
leaq -0x348(%rbp), %rsi
callq 0x3a9a0
movq %rax, -0x400(%rbp)
jmp 0x383aa
movq -0x400(%rbp), %rdi
movl -0x258(%rbp), %eax
movl %eax, -0x34c(%rbp)
leaq -0x34c(%rbp), %rsi
callq 0x3aa80
movq %rax, -0x408(%rbp)
jmp 0x383d2
movq -0x408(%rbp), %rdi
movl -0x260(%rbp), %eax
movl %eax, -0x350(%rbp)
leaq -0x350(%rbp), %rsi
callq 0x3ab60
jmp 0x383f3
jmp 0x383f5
jmp 0x383f7
cmpq $-0x1, -0x278(%rbp)
jne 0x3840d
movl $0x5, -0x29c(%rbp)
jmp 0x38459
movq -0x278(%rbp), %rdx
leaq -0x370(%rbp), %rdi
leaq -0x248(%rbp), %rsi
movq $-0x1, %rcx
callq 0xc380
jmp 0x38430
leaq -0x248(%rbp), %rdi
leaq -0x370(%rbp), %rsi
callq 0xc420
leaq -0x370(%rbp), %rdi
callq 0xc6b8
movl $0x0, -0x29c(%rbp)
leaq -0x298(%rbp), %rdi
callq 0xc6b8
movl -0x29c(%rbp), %eax
testl %eax, %eax
je 0x38473
jmp 0x38471
jmp 0x38486
jmp 0x37db3
leaq -0x298(%rbp), %rdi
callq 0xc6b8
jmp 0x384cc
jmp 0x37d28
movq -0x380(%rbp), %rax
testb $0x1, 0xc61(%rax)
je 0x384ab
movq -0x380(%rbp), %rdi
callq 0x3ab90
jmp 0x384a9
jmp 0x384ab
leaq -0x248(%rbp), %rdi
callq 0xc6b8
leaq -0x218(%rbp), %rdi
callq 0xc120
addq $0x410, %rsp            # imm = 0x410
popq %rbp
retq
leaq -0x248(%rbp), %rdi
callq 0xc6b8
leaq -0x218(%rbp), %rdi
callq 0xc120
movq -0x220(%rbp), %rdi
callq 0xc5d0