// File: bowenfan96[P]icl-mcs-enigma/enigma.cpp
// Function: Enigma::Reflector::Reflector(char const*)
Enigma::Reflector::Reflector(const char* rf_filename) 
{
    std::ifstream rf_file;
    rf_file.open(rf_filename);
    
    if(rf_file.is_open()) {
        
        std::string index;
        int rf_num;
        int i = 0;
        size_t sz;
        
        while(std::getline(rf_file, index, ' ')) {
            
            index.erase(std::remove_if(index.begin(), index.end(), ::isspace), index.end());
            
            if(!index.empty()) {
                if(is_numeric(index)) {
                    if(is_valid(index)) {
                        rf_num = std::stoi(index, &sz);
                        if(i % 2 == 0) {
                            map_from.push_back(rf_num);
                        }
                        else {
                            map_to.push_back(rf_num);
                        }
                        i++;
                    }
                    else {
                        std::cerr << "Index in reflector file " << rf_filename << " does not correspond to an alphabet letter" << "\n";
                        throw INVALID_INDEX;
                    }
                }
                else {
                    std::cerr << "Non-numeric character in reflector file " << rf_filename << "\n";
                    throw NON_NUMERIC_CHARACTER;
                }
            }
        }
        
        // check reflector is not empty or contain only 1 parameter
        if(map_from.size() == 0 || map_to.size() == 0) {
            std::cerr << "Insufficient number of mappings in reflector file: " << rf_filename << "\n";
            throw INCORRECT_NUMBER_OF_REFLECTOR_PARAMETERS;
        }
        
        // check correct config (1 to 1 mapping, no value to itself)
        if(!is_one_to_one(map_from, map_to)) {
            std::cerr << "Malformed contact points (not linked 1 to 1) in reflector file " << rf_filename << "\n";
            throw INVALID_REFLECTOR_MAPPING;
        }
        
        // check exactly 13 pairings
        if(map_from.size() != map_to.size()) {
            std::cerr << "Incorrect (odd) number of parameters in reflector file " << rf_filename << "\n";
            throw INCORRECT_NUMBER_OF_REFLECTOR_PARAMETERS;
        }
            
        if(map_from.size() < 13 || map_to.size() < 13) {
            std::cerr << "Insufficient number of mappings in reflector file: " << rf_filename << "\n";
            throw INCORRECT_NUMBER_OF_REFLECTOR_PARAMETERS;
        }
        
        // mirror the mapping vectors
        std::vector<int> swap = map_from;
        map_from.insert(map_from.end(), map_to.begin(), map_to.end());
        map_to.insert(map_to.end(), swap.begin(), swap.end());
    }
    
    // check if file open failed
    else {
        std::cerr << "Reflector file " << rf_filename << " supplied cannot be opened" << "\n";
        throw ERROR_OPENING_CONFIGURATION_FILE;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x410, %rsp            # imm = 0x410
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x308(%rbp)
movq %rdi, %rax
movq %rax, -0x310(%rbp)
callq 0x5240
movq -0x308(%rbp), %rdi
addq $0x18, %rdi
movq %rdi, -0x300(%rbp)
callq 0x5240
leaq -0x218(%rbp), %rdi
callq 0x2210
jmp 0x3cc2
movq -0x10(%rbp), %rsi
leaq -0x218(%rbp), %rdi
movl $0x8, %edx
callq 0x2120
jmp 0x3cd9
leaq -0x218(%rbp), %rdi
callq 0x22d0
movb %al, -0x311(%rbp)
jmp 0x3ced
movb -0x311(%rbp), %al
testb $0x1, %al
jne 0x3cfc
jmp 0x4453
leaq -0x248(%rbp), %rdi
callq 0x2280
movl $0x0, -0x250(%rbp)
leaq -0x218(%rbp), %rdi
leaq -0x248(%rbp), %rsi
movl $0x20, %edx
callq 0x2360
movq %rax, -0x320(%rbp)
jmp 0x3d33
movq -0x320(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x2230
movb %al, -0x321(%rbp)
jmp 0x3d51
movb -0x321(%rbp), %al
testb $0x1, %al
jne 0x3d60
jmp 0x405c
leaq -0x248(%rbp), %rdi
movq %rdi, -0x338(%rbp)
callq 0x2170
movq -0x338(%rbp), %rdi
movq %rax, -0x270(%rbp)
callq 0x20e0
movq %rax, -0x278(%rbp)
movq -0x270(%rbp), %rdi
movq -0x278(%rbp), %rsi
movq 0xe1f6(%rip), %rdx      # 0x11f98
callq 0x5260
movq %rax, -0x330(%rbp)
jmp 0x3db0
movq -0x330(%rbp), %rax
movq %rax, -0x268(%rbp)
leaq -0x260(%rbp), %rdi
leaq -0x268(%rbp), %rsi
callq 0x52b0
leaq -0x248(%rbp), %rdi
movq %rdi, -0x348(%rbp)
callq 0x20e0
movq %rax, -0x288(%rbp)
leaq -0x280(%rbp), %rdi
leaq -0x288(%rbp), %rsi
callq 0x52b0
movq -0x348(%rbp), %rdi
movq -0x260(%rbp), %rsi
movq -0x280(%rbp), %rdx
callq 0x2040
movq %rax, -0x340(%rbp)
jmp 0x3e21
movq -0x340(%rbp), %rax
movq %rax, -0x290(%rbp)
leaq -0x248(%rbp), %rdi
callq 0x2300
testb $0x1, %al
jne 0x4057
leaq -0x248(%rbp), %rdi
callq 0x2e80
movb %al, -0x349(%rbp)
jmp 0x3e57
movb -0x349(%rbp), %al
testb $0x1, %al
jne 0x3e66
jmp 0x3fe3
leaq -0x248(%rbp), %rdi
callq 0x2f00
movb %al, -0x34a(%rbp)
jmp 0x3e7a
movb -0x34a(%rbp), %al
testb $0x1, %al
jne 0x3e89
jmp 0x3f53
leaq -0x248(%rbp), %rdi
leaq -0x258(%rbp), %rsi
movl $0xa, %edx
callq 0x52f0
movl %eax, -0x350(%rbp)
jmp 0x3ea9
movl -0x350(%rbp), %eax
movl %eax, -0x24c(%rbp)
movl -0x250(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x3f24
movq -0x310(%rbp), %rdi
leaq -0x24c(%rbp), %rsi
callq 0x5330
jmp 0x3edd
jmp 0x3f3f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
jmp 0x44ff
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
jmp 0x44f3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
jmp 0x4442
movq -0x310(%rbp), %rdi
addq $0x18, %rdi
leaq -0x24c(%rbp), %rsi
callq 0x5330
jmp 0x3f3d
jmp 0x3f3f
movl -0x250(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x250(%rbp)
jmp 0x3fe1
movq 0xe096(%rip), %rdi      # 0x11ff0
leaq 0x82bf(%rip), %rsi      # 0xc220
callq 0x21d0
movq %rax, -0x358(%rbp)
jmp 0x3f6f
movq -0x358(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x21d0
movq %rax, -0x360(%rbp)
jmp 0x3f88
movq -0x360(%rbp), %rdi
leaq 0x80bb(%rip), %rsi      # 0xc051
callq 0x21d0
movq %rax, -0x368(%rbp)
jmp 0x3fa4
movq -0x368(%rbp), %rdi
leaq 0x80ca(%rip), %rsi      # 0xc07c
callq 0x21d0
jmp 0x3fb9
movl $0x4, %edi
callq 0x20b0
movq %rax, %rdi
movl $0x3, (%rdi)
movq 0xdfbd(%rip), %rsi      # 0x11f90
xorl %eax, %eax
movl %eax, %edx
callq 0x2310
jmp 0x4523
jmp 0x4055
movq 0xe006(%rip), %rdi      # 0x11ff0
leaq 0x8248(%rip), %rsi      # 0xc239
callq 0x21d0
movq %rax, -0x370(%rbp)
jmp 0x3fff
movq -0x370(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x21d0
movq %rax, -0x378(%rbp)
jmp 0x4018
movq -0x378(%rbp), %rdi
leaq 0x8056(%rip), %rsi      # 0xc07c
callq 0x21d0
jmp 0x402d
movl $0x4, %edi
callq 0x20b0
movq %rax, %rdi
movl $0x4, (%rdi)
movq 0xdf49(%rip), %rsi      # 0x11f90
xorl %eax, %eax
movl %eax, %edx
callq 0x2310
jmp 0x4523
jmp 0x4057
jmp 0x3d12
movq -0x310(%rbp), %rdi
callq 0x53a0
cmpq $0x0, %rax
je 0x4084
movq -0x310(%rbp), %rdi
addq $0x18, %rdi
callq 0x53a0
cmpq $0x0, %rax
jne 0x40f6
movq 0xdf65(%rip), %rdi      # 0x11ff0
leaq 0x81d0(%rip), %rsi      # 0xc262
callq 0x21d0
movq %rax, -0x380(%rbp)
jmp 0x40a0
movq -0x380(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x21d0
movq %rax, -0x388(%rbp)
jmp 0x40b9
movq -0x388(%rbp), %rdi
leaq 0x7fb5(%rip), %rsi      # 0xc07c
callq 0x21d0
jmp 0x40ce
movl $0x4, %edi
callq 0x20b0
movq %rax, %rdi
movl $0xa, (%rdi)
movq 0xdea8(%rip), %rsi      # 0x11f90
xorl %eax, %eax
movl %eax, %edx
callq 0x2310
jmp 0x4523
movq -0x310(%rbp), %rdi
movq %rdi, %rsi
addq $0x18, %rsi
callq 0x2f50
movb %al, -0x389(%rbp)
jmp 0x4111
movb -0x389(%rbp), %al
testb $0x1, %al
jne 0x418f
jmp 0x411d
movq 0xdecc(%rip), %rdi      # 0x11ff0
leaq 0x816b(%rip), %rsi      # 0xc296
callq 0x21d0
movq %rax, -0x398(%rbp)
jmp 0x4139
movq -0x398(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x21d0
movq %rax, -0x3a0(%rbp)
jmp 0x4152
movq -0x3a0(%rbp), %rdi
leaq 0x7f1c(%rip), %rsi      # 0xc07c
callq 0x21d0
jmp 0x4167
movl $0x4, %edi
callq 0x20b0
movq %rax, %rdi
movl $0x9, (%rdi)
movq 0xde0f(%rip), %rsi      # 0x11f90
xorl %eax, %eax
movl %eax, %edx
callq 0x2310
jmp 0x4523
movq -0x310(%rbp), %rdi
callq 0x53a0
movq -0x310(%rbp), %rdi
movq %rax, -0x3a8(%rbp)
addq $0x18, %rdi
callq 0x53a0
movq %rax, %rcx
movq -0x3a8(%rbp), %rax
cmpq %rcx, %rax
je 0x4233
movq 0xde28(%rip), %rdi      # 0x11ff0
leaq 0x8107(%rip), %rsi      # 0xc2d6
callq 0x21d0
movq %rax, -0x3b0(%rbp)
jmp 0x41dd
movq -0x3b0(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x21d0
movq %rax, -0x3b8(%rbp)
jmp 0x41f6
movq -0x3b8(%rbp), %rdi
leaq 0x7e78(%rip), %rsi      # 0xc07c
callq 0x21d0
jmp 0x420b
movl $0x4, %edi
callq 0x20b0
movq %rax, %rdi
movl $0xa, (%rdi)
movq 0xdd6b(%rip), %rsi      # 0x11f90
xorl %eax, %eax
movl %eax, %edx
callq 0x2310
jmp 0x4523
movq -0x310(%rbp), %rdi
callq 0x53a0
cmpq $0xd, %rax
jb 0x425b
movq -0x310(%rbp), %rdi
addq $0x18, %rdi
callq 0x53a0
cmpq $0xd, %rax
jae 0x42cd
movq 0xdd8e(%rip), %rdi      # 0x11ff0
leaq 0x7ff9(%rip), %rsi      # 0xc262
callq 0x21d0
movq %rax, -0x3c0(%rbp)
jmp 0x4277
movq -0x3c0(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x21d0
movq %rax, -0x3c8(%rbp)
jmp 0x4290
movq -0x3c8(%rbp), %rdi
leaq 0x7dde(%rip), %rsi      # 0xc07c
callq 0x21d0
jmp 0x42a5
movl $0x4, %edi
callq 0x20b0
movq %rax, %rdi
movl $0xa, (%rdi)
movq 0xdcd1(%rip), %rsi      # 0x11f90
xorl %eax, %eax
movl %eax, %edx
callq 0x2310
jmp 0x4523
movq -0x310(%rbp), %rsi
leaq -0x2a8(%rbp), %rdi
callq 0x53c0
jmp 0x42e2
movq -0x310(%rbp), %rdi
callq 0x5560
movq %rax, -0x2b8(%rbp)
leaq -0x2b0(%rbp), %rdi
leaq -0x2b8(%rbp), %rsi
callq 0x5590
movq -0x310(%rbp), %rdi
addq $0x18, %rdi
movq %rdi, -0x3d8(%rbp)
callq 0x55d0
movq -0x3d8(%rbp), %rdi
movq %rax, -0x2c0(%rbp)
callq 0x5560
movq -0x310(%rbp), %rdi
movq %rax, -0x2c8(%rbp)
movq -0x2b0(%rbp), %rsi
movq -0x2c0(%rbp), %rdx
movq -0x2c8(%rbp), %rcx
callq 0x54b0
movq %rax, -0x3d0(%rbp)
jmp 0x4363
movq -0x310(%rbp), %rdi
movq -0x3d0(%rbp), %rax
movq %rax, -0x2d0(%rbp)
addq $0x18, %rdi
movq %rdi, -0x3e8(%rbp)
callq 0x5560
movq %rax, -0x2e0(%rbp)
leaq -0x2d8(%rbp), %rdi
leaq -0x2e0(%rbp), %rsi
callq 0x5590
leaq -0x2a8(%rbp), %rdi
movq %rdi, -0x3f0(%rbp)
callq 0x55d0
movq -0x3f0(%rbp), %rdi
movq %rax, -0x2e8(%rbp)
callq 0x5560
movq -0x3e8(%rbp), %rdi
movq %rax, -0x2f0(%rbp)
movq -0x2d8(%rbp), %rsi
movq -0x2e8(%rbp), %rdx
movq -0x2f0(%rbp), %rcx
callq 0x54b0
movq %rax, -0x3e0(%rbp)
jmp 0x43f9
movq -0x3e0(%rbp), %rax
movq %rax, -0x2f8(%rbp)
leaq -0x2a8(%rbp), %rdi
callq 0x5600
leaq -0x248(%rbp), %rdi
callq 0x2110
jmp 0x44de
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
leaq -0x2a8(%rbp), %rdi
callq 0x5600
leaq -0x248(%rbp), %rdi
callq 0x2110
jmp 0x44f3
movq 0xdb96(%rip), %rdi      # 0x11ff0
leaq 0x7ead(%rip), %rsi      # 0xc30e
callq 0x21d0
movq %rax, -0x3f8(%rbp)
jmp 0x446f
movq -0x3f8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x21d0
movq %rax, -0x400(%rbp)
jmp 0x4488
movq -0x400(%rbp), %rdi
leaq 0x7c93(%rip), %rsi      # 0xc129
callq 0x21d0
movq %rax, -0x408(%rbp)
jmp 0x44a4
movq -0x408(%rbp), %rdi
leaq 0x7bca(%rip), %rsi      # 0xc07c
callq 0x21d0
jmp 0x44b9
movl $0x4, %edi
callq 0x20b0
movq %rax, %rdi
movl $0xb, (%rdi)
movq 0xdabd(%rip), %rsi      # 0x11f90
xorl %eax, %eax
movl %eax, %edx
callq 0x2310
jmp 0x4523
leaq -0x218(%rbp), %rdi
callq 0x2050
addq $0x410, %rsp            # imm = 0x410
popq %rbp
retq
leaq -0x218(%rbp), %rdi
callq 0x2050
movq -0x300(%rbp), %rdi
callq 0x5600
movq -0x308(%rbp), %rdi
callq 0x5600
movq -0x220(%rbp), %rdi
callq 0x2330
nopw %cs:(%rax,%rax)