// File: bowenfan96[P]icl-mcs-enigma/enigma.cpp
// Function: Enigma::Rotor::Rotor(char const*)
Enigma::Rotor::Rotor(const char* rot_filename) 
{
    std::ifstream rot_file;
    rot_file.open(rot_filename);
    
    if(rot_file.is_open()) {
        
        std::string index;
        int rot_num;
        int i = 0;
        size_t sz;
        
        while(std::getline(rot_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)) {
                        rot_num = std::stoi(index, &sz);
                        if(i <= 25) {
                            map_rtl[i] = rot_num;
                            map_ltr[rot_num] = i;
                        }
                        else {
                            triggers.push_back(rot_num);
                        }
                        i++;
                    }
                    else {
                        std::cerr << "Index in rotor file " << rot_filename << " does not correspond to an alphabet letter" << "\n";
                        throw INVALID_INDEX;
                    }
                }
                else {
                    std::cerr << "Non-numeric character for mapping in rotor file " << rot_filename << "\n";
                    throw NON_NUMERIC_CHARACTER;
                }
            }
        }
        
        // check mapping is correct, 26 numbers given in rotor file
        if(i < 26) {
            std::cerr << "Not all inputs mapped in rotor file: " << rot_filename << "\n";
            throw INVALID_ROTOR_MAPPING;
        }
        // check all numbers are unique
        std::unordered_set<int> set;
        
        for(int j=0; j<26; j++) {
            if(!set.insert(map_rtl[j]).second) {
                auto repeat_val = std::find(map_rtl.cbegin(), map_rtl.cend(), map_rtl[j]);
                
                std::cerr << "Invalid mapping of input " << j << " to output " << map_rtl[j] << " (output " << map_rtl[j] << " is already mapped to from input " << std::distance(map_rtl.cbegin(), repeat_val) << ") in rotor file: " << rot_filename << "\n";
                
                throw INVALID_ROTOR_MAPPING;
            }
        }
        
        // set position to 0
        // Enigma constructor will turn the rotors after reading pos file
        position = 0;
        
        // rotor is triggered if it rotates to its notch
        triggered = false;
    }
    
    // check if file open failed
    else {
        std::cerr << "Rotor file " << rot_filename << " supplied cannot be opened" << "\n";
        throw ERROR_OPENING_CONFIGURATION_FILE;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x420, %rsp            # imm = 0x420
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x308(%rbp)
addq $0xd0, %rdi
movq %rdi, -0x300(%rbp)
callq 0x5240
leaq -0x218(%rbp), %rdi
callq 0x2210
jmp 0x32bf
movq -0x10(%rbp), %rsi
leaq -0x218(%rbp), %rdi
movl $0x8, %edx
callq 0x2120
jmp 0x32d6
leaq -0x218(%rbp), %rdi
callq 0x22d0
movb %al, -0x309(%rbp)
jmp 0x32ea
movb -0x309(%rbp), %al
testb $0x1, %al
jne 0x32f9
jmp 0x3a1a
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, -0x318(%rbp)
jmp 0x3330
movq -0x318(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x2230
movb %al, -0x319(%rbp)
jmp 0x334e
movb -0x319(%rbp), %al
testb $0x1, %al
jne 0x335d
jmp 0x3693
leaq -0x248(%rbp), %rdi
movq %rdi, -0x330(%rbp)
callq 0x2170
movq -0x330(%rbp), %rdi
movq %rax, -0x270(%rbp)
callq 0x20e0
movq %rax, -0x278(%rbp)
movq -0x270(%rbp), %rdi
movq -0x278(%rbp), %rsi
movq 0xebf9(%rip), %rdx      # 0x11f98
callq 0x5260
movq %rax, -0x328(%rbp)
jmp 0x33ad
movq -0x328(%rbp), %rax
movq %rax, -0x268(%rbp)
leaq -0x260(%rbp), %rdi
leaq -0x268(%rbp), %rsi
callq 0x52b0
leaq -0x248(%rbp), %rdi
movq %rdi, -0x340(%rbp)
callq 0x20e0
movq %rax, -0x288(%rbp)
leaq -0x280(%rbp), %rdi
leaq -0x288(%rbp), %rsi
callq 0x52b0
movq -0x340(%rbp), %rdi
movq -0x260(%rbp), %rsi
movq -0x280(%rbp), %rdx
callq 0x2040
movq %rax, -0x338(%rbp)
jmp 0x341e
movq -0x338(%rbp), %rax
movq %rax, -0x290(%rbp)
leaq -0x248(%rbp), %rdi
callq 0x2300
testb $0x1, %al
jne 0x368e
leaq -0x248(%rbp), %rdi
callq 0x2e80
movb %al, -0x341(%rbp)
jmp 0x3454
movb -0x341(%rbp), %al
testb $0x1, %al
jne 0x3463
jmp 0x361a
leaq -0x248(%rbp), %rdi
callq 0x2f00
movb %al, -0x342(%rbp)
jmp 0x3477
movb -0x342(%rbp), %al
testb $0x1, %al
jne 0x3486
jmp 0x358a
leaq -0x248(%rbp), %rdi
leaq -0x258(%rbp), %rsi
movl $0xa, %edx
callq 0x52f0
movl %eax, -0x348(%rbp)
jmp 0x34a6
movl -0x348(%rbp), %eax
movl %eax, -0x24c(%rbp)
cmpl $0x19, -0x250(%rbp)
jg 0x3558
movq -0x308(%rbp), %rdi
movl -0x24c(%rbp), %eax
movl %eax, -0x350(%rbp)
movslq -0x250(%rbp), %rsi
callq 0x57d0
movl -0x350(%rbp), %ecx
movq -0x308(%rbp), %rdi
movl %ecx, (%rax)
movl -0x250(%rbp), %eax
movl %eax, -0x34c(%rbp)
addq $0x68, %rdi
movslq -0x24c(%rbp), %rsi
callq 0x57d0
movl -0x34c(%rbp), %ecx
movl %ecx, (%rax)
jmp 0x3576
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
jmp 0x3ac6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
jmp 0x3aba
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
jmp 0x3a09
movq -0x308(%rbp), %rdi
addq $0xd0, %rdi
leaq -0x24c(%rbp), %rsi
callq 0x5330
jmp 0x3574
jmp 0x3576
movl -0x250(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x250(%rbp)
jmp 0x3618
movq 0xea5f(%rip), %rdi      # 0x11ff0
leaq 0x8bac(%rip), %rsi      # 0xc144
callq 0x21d0
movq %rax, -0x358(%rbp)
jmp 0x35a6
movq -0x358(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x21d0
movq %rax, -0x360(%rbp)
jmp 0x35bf
movq -0x360(%rbp), %rdi
leaq 0x8a84(%rip), %rsi      # 0xc051
callq 0x21d0
movq %rax, -0x368(%rbp)
jmp 0x35db
movq -0x368(%rbp), %rdi
leaq 0x8a93(%rip), %rsi      # 0xc07c
callq 0x21d0
jmp 0x35f0
movl $0x4, %edi
callq 0x20b0
movq %rax, %rdi
movl $0x3, (%rdi)
movq 0xe986(%rip), %rsi      # 0x11f90
xorl %eax, %eax
movl %eax, %edx
callq 0x2310
jmp 0x3ade
jmp 0x368c
movq 0xe9cf(%rip), %rdi      # 0x11ff0
leaq 0x8b31(%rip), %rsi      # 0xc159
callq 0x21d0
movq %rax, -0x370(%rbp)
jmp 0x3636
movq -0x370(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x21d0
movq %rax, -0x378(%rbp)
jmp 0x364f
movq -0x378(%rbp), %rdi
leaq 0x8a1f(%rip), %rsi      # 0xc07c
callq 0x21d0
jmp 0x3664
movl $0x4, %edi
callq 0x20b0
movq %rax, %rdi
movl $0x4, (%rdi)
movq 0xe912(%rip), %rsi      # 0x11f90
xorl %eax, %eax
movl %eax, %edx
callq 0x2310
jmp 0x3ade
jmp 0x368e
jmp 0x330f
cmpl $0x1a, -0x250(%rbp)
jge 0x370e
movq 0xe94d(%rip), %rdi      # 0x11ff0
leaq 0x8ae0(%rip), %rsi      # 0xc18a
callq 0x21d0
movq %rax, -0x380(%rbp)
jmp 0x36b8
movq -0x380(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x21d0
movq %rax, -0x388(%rbp)
jmp 0x36d1
movq -0x388(%rbp), %rdi
leaq 0x899d(%rip), %rsi      # 0xc07c
callq 0x21d0
jmp 0x36e6
movl $0x4, %edi
callq 0x20b0
movq %rax, %rdi
movl $0x7, (%rdi)
movq 0xe890(%rip), %rsi      # 0x11f90
xorl %eax, %eax
movl %eax, %edx
callq 0x2310
jmp 0x3ade
leaq -0x2c8(%rbp), %rdi
callq 0x5800
movl $0x0, -0x2cc(%rbp)
cmpl $0x1a, -0x2cc(%rbp)
jge 0x39d4
movq -0x308(%rbp), %rdi
movslq -0x2cc(%rbp), %rsi
callq 0x57d0
movq %rax, %rsi
leaq -0x2c8(%rbp), %rdi
callq 0x5820
movb %dl, -0x391(%rbp)
movq %rax, -0x390(%rbp)
jmp 0x3762
movb -0x391(%rbp), %al
movq -0x390(%rbp), %rcx
movq %rcx, -0x2f0(%rbp)
movb %al, -0x2e8(%rbp)
movq -0x2f0(%rbp), %rax
movq %rax, -0x2e0(%rbp)
movb -0x2e8(%rbp), %al
movb %al, -0x2d8(%rbp)
movb -0x2d8(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x37a7
jmp 0x39be
movq -0x308(%rbp), %rdi
callq 0x58b0
movq -0x308(%rbp), %rdi
movq %rax, -0x3b0(%rbp)
callq 0x58d0
movq -0x308(%rbp), %rdi
movq %rax, -0x3a8(%rbp)
movslq -0x2cc(%rbp), %rsi
callq 0x57d0
movq -0x3b0(%rbp), %rdi
movq -0x3a8(%rbp), %rsi
movq %rax, %rdx
callq 0x5860
movq %rax, -0x3a0(%rbp)
jmp 0x37ff
movq -0x3a0(%rbp), %rax
movq %rax, -0x2f8(%rbp)
movq 0xe7dc(%rip), %rdi      # 0x11ff0
leaq 0x8995(%rip), %rsi      # 0xc1b0
callq 0x21d0
movq %rax, -0x3b8(%rbp)
jmp 0x3829
movq -0x3b8(%rbp), %rdi
movl -0x2cc(%rbp), %esi
callq 0x2320
movq %rax, -0x3c0(%rbp)
jmp 0x3844
movq -0x3c0(%rbp), %rdi
leaq 0x8978(%rip), %rsi      # 0xc1ca
callq 0x21d0
movq %rax, -0x3c8(%rbp)
jmp 0x3860
movq -0x308(%rbp), %rdi
movslq -0x2cc(%rbp), %rsi
callq 0x57d0
movq -0x3c8(%rbp), %rdi
movl (%rax), %esi
callq 0x2320
movq %rax, -0x3d0(%rbp)
jmp 0x388a
movq -0x3d0(%rbp), %rdi
leaq 0x893e(%rip), %rsi      # 0xc1d6
callq 0x21d0
movq %rax, -0x3d8(%rbp)
jmp 0x38a6
movq -0x308(%rbp), %rdi
movslq -0x2cc(%rbp), %rsi
callq 0x57d0
movq -0x3d8(%rbp), %rdi
movl (%rax), %esi
callq 0x2320
movq %rax, -0x3e0(%rbp)
jmp 0x38d0
movq -0x3e0(%rbp), %rdi
leaq 0x8902(%rip), %rsi      # 0xc1e0
callq 0x21d0
movq %rax, -0x3e8(%rbp)
jmp 0x38ec
movq -0x308(%rbp), %rdi
callq 0x58b0
movq %rax, %rdi
movq -0x2f8(%rbp), %rsi
callq 0x58f0
movq %rax, -0x3f0(%rbp)
jmp 0x3910
movq -0x3f0(%rbp), %rsi
movq -0x3e8(%rbp), %rdi
callq 0x2350
movq %rax, -0x3f8(%rbp)
jmp 0x392c
movq -0x3f8(%rbp), %rdi
leaq 0x88c8(%rip), %rsi      # 0xc202
callq 0x21d0
movq %rax, -0x400(%rbp)
jmp 0x3948
movq -0x400(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x21d0
movq %rax, -0x408(%rbp)
jmp 0x3961
movq -0x408(%rbp), %rdi
leaq 0x870d(%rip), %rsi      # 0xc07c
callq 0x21d0
jmp 0x3976
movl $0x4, %edi
callq 0x20b0
movq %rax, %rdi
movl $0x7, (%rdi)
movq 0xe600(%rip), %rsi      # 0x11f90
xorl %eax, %eax
movl %eax, %edx
callq 0x2310
jmp 0x3ade
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
leaq -0x2c8(%rbp), %rdi
callq 0x5930
jmp 0x3a09
jmp 0x39c0
movl -0x2cc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2cc(%rbp)
jmp 0x3724
movq -0x308(%rbp), %rax
movl $0x0, 0xe8(%rax)
movb $0x0, 0xec(%rax)
leaq -0x2c8(%rbp), %rdi
callq 0x5930
leaq -0x248(%rbp), %rdi
callq 0x2110
jmp 0x3aa5
leaq -0x248(%rbp), %rdi
callq 0x2110
jmp 0x3aba
movq 0xe5cf(%rip), %rdi      # 0x11ff0
leaq 0x87ec(%rip), %rsi      # 0xc214
callq 0x21d0
movq %rax, -0x410(%rbp)
jmp 0x3a36
movq -0x410(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x21d0
movq %rax, -0x418(%rbp)
jmp 0x3a4f
movq -0x418(%rbp), %rdi
leaq 0x86cc(%rip), %rsi      # 0xc129
callq 0x21d0
movq %rax, -0x420(%rbp)
jmp 0x3a6b
movq -0x420(%rbp), %rdi
leaq 0x8603(%rip), %rsi      # 0xc07c
callq 0x21d0
jmp 0x3a80
movl $0x4, %edi
callq 0x20b0
movq %rax, %rdi
movl $0xb, (%rdi)
movq 0xe4f6(%rip), %rsi      # 0x11f90
xorl %eax, %eax
movl %eax, %edx
callq 0x2310
jmp 0x3ade
leaq -0x218(%rbp), %rdi
callq 0x2050
addq $0x420, %rsp            # imm = 0x420
popq %rbp
retq
leaq -0x218(%rbp), %rdi
callq 0x2050
movq -0x300(%rbp), %rdi
callq 0x5600
movq -0x220(%rbp), %rdi
callq 0x2330
nop