// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x298, %rsp            # imm = 0x298
movq %rsi, %rbx
movq %rdi, %r14
leaq 0xd0(%rdi), %rax
movq %rax, -0x40(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, 0xd0(%rdi)
andq $0x0, 0xe0(%rdi)
leaq -0x2b8(%rbp), %rdi
callq 0x21d0
leaq -0x2b8(%rbp), %rdi
pushq $0x8
popq %rdx
movq %rbx, %rsi
callq 0x2100
movq %rbx, -0x30(%rbp)
leaq -0x240(%rbp), %rdi
callq 0x2050
testb %al, %al
je 0x2d44
leaq -0x58(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq %r14, -0x48(%rbp)
leaq 0x68(%r14), %rax
movq %rax, -0x70(%rbp)
xorl %r15d, %r15d
leaq -0x2b8(%rbp), %r13
leaq -0x68(%rbp), %rbx
pushq $0x20
popq %r14
movq 0x5484(%rip), %r12      # 0x7fa8
movq %r13, %rdi
movq %rbx, %rsi
movl %r14d, %edx
callq 0x22b0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x2bd1
movq -0x68(%rbp), %rdi
movq -0x60(%rbp), %rsi
addq %rdi, %rsi
movq %r12, %rdx
callq 0x3dce
movq -0x68(%rbp), %rdx
addq -0x60(%rbp), %rdx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2040
cmpq $0x0, -0x60(%rbp)
je 0x2b24
movq %rbx, %rdi
callq 0x2888
testb %al, %al
je 0x2e13
movq %rbx, %rdi
callq 0x28b6
testb %al, %al
je 0x2dd2
movq %rbx, %rdi
leaq -0x78(%rbp), %rsi
pushq $0xa
popq %rdx
callq 0x3a55
movl %eax, -0x34(%rbp)
cmpl $0x19, %r15d
ja 0x2bbc
movl %r15d, %ecx
movq -0x48(%rbp), %rdx
movl %eax, (%rdx,%rcx,4)
cltq
movl %r15d, 0x68(%rdx,%rax,4)
jmp 0x2bc9
movq -0x40(%rbp), %rdi
leaq -0x34(%rbp), %rsi
callq 0x3a76
incl %r15d
jmp 0x2b24
cmpl $0x19, %r15d
jbe 0x2da0
leaq -0x80(%rbp), %rax
movq %rax, -0x30(%rax)
movq $0x1, -0x28(%rax)
xorps %xmm0, %xmm0
movups %xmm0, -0x20(%rax)
movl $0x3f800000, -0x10(%rax) # imm = 0x3F800000
movups %xmm0, -0x8(%rax)
xorl %r12d, %r12d
leaq -0xb0(%rbp), %r14
movq -0x48(%rbp), %r13
movq %r13, %rbx
cmpq $-0x1a, %r12
je 0x2c2d
movq %r14, %rdi
movq %rbx, %rsi
callq 0x42dc
testb $0x1, %dl
je 0x2c70
decq %r12
addq $0x4, %rbx
jmp 0x2c0e
andl $0x0, 0xe8(%r13)
movb $0x0, 0xec(%r13)
leaq -0xb0(%rbp), %rdi
callq 0x3d0a
leaq -0x68(%rbp), %rdi
callq 0x20b0
leaq -0x2b8(%rbp), %rdi
callq 0x2060
addq $0x298, %rsp            # imm = 0x298
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r13, %rdi
movq -0x70(%rbp), %rsi
movq %rbx, %rdx
callq 0x46a8
movq -0x30(%rbp), %r15
movq %rax, %r14
movq 0x5363(%rip), %rdi      # 0x7ff0
leaq 0x24e8(%rip), %rsi      # 0x517c
callq 0x21b0
negq %r12
movq %rax, %rdi
movl %r12d, %esi
callq 0x2290
leaq 0x24e8(%rip), %rsi      # 0x5196
movq %rax, %rdi
callq 0x21b0
movl (%rbx), %esi
movq %rax, %rdi
callq 0x2290
leaq 0x24db(%rip), %rsi      # 0x51a2
movq %rax, %rdi
callq 0x21b0
movl (%rbx), %esi
movq %rax, %rdi
callq 0x2290
leaq 0x24cc(%rip), %rsi      # 0x51ac
movq %rax, %rdi
callq 0x21b0
subq %r13, %r14
sarq $0x2, %r14
movq %rax, %rdi
movq %r14, %rsi
callq 0x21a0
leaq 0x24cd(%rip), %rsi      # 0x51ce
movq %rax, %rdi
callq 0x21b0
movq %rax, %rdi
movq %r15, %rsi
callq 0x21b0
leaq 0x232d(%rip), %rsi      # 0x5048
movq %rax, %rdi
callq 0x21b0
pushq $0x4
popq %rdi
callq 0x20c0
movl $0x7, (%rax)
movq 0x5268(%rip), %rsi      # 0x7fa0
movq %rax, %rdi
xorl %edx, %edx
callq 0x2280
jmp 0x2da0
movq 0x52a5(%rip), %rdi      # 0x7ff0
leaq 0x248e(%rip), %rsi      # 0x51e0
callq 0x21b0
movq -0x30(%rbp), %rsi
movq %rax, %rdi
callq 0x21b0
leaq 0x238b(%rip), %rsi      # 0x50f5
movq %rax, %rdi
callq 0x21b0
leaq 0x22cf(%rip), %rsi      # 0x5048
movq %rax, %rdi
callq 0x21b0
pushq $0x4
popq %rdi
callq 0x20c0
movl $0xb, (%rax)
movq 0x520a(%rip), %rsi      # 0x7fa0
movq %rax, %rdi
xorl %edx, %edx
callq 0x2280
movq 0x5249(%rip), %rdi      # 0x7ff0
leaq 0x23a8(%rip), %rsi      # 0x5156
callq 0x21b0
movq %rax, %rdi
movq -0x30(%rbp), %rsi
callq 0x21b0
leaq 0x2282(%rip), %rsi      # 0x5048
movq %rax, %rdi
callq 0x21b0
pushq $0x7
jmp 0x2e43
movq 0x5217(%rip), %rdi      # 0x7ff0
leaq 0x2330(%rip), %rsi      # 0x5110
callq 0x21b0
movq %rax, %rdi
movq -0x30(%rbp), %rsi
callq 0x21b0
leaq 0x2225(%rip), %rsi      # 0x501d
movq %rax, %rdi
callq 0x21b0
leaq 0x2241(%rip), %rsi      # 0x5048
movq %rax, %rdi
callq 0x21b0
pushq $0x3
jmp 0x2e43
movq 0x51d6(%rip), %rdi      # 0x7ff0
leaq 0x2304(%rip), %rsi      # 0x5125
callq 0x21b0
movq %rax, %rdi
movq -0x30(%rbp), %rsi
callq 0x21b0
leaq 0x220f(%rip), %rsi      # 0x5048
movq %rax, %rdi
callq 0x21b0
pushq $0x4
popq %rbx
pushq $0x4
popq %rdi
callq 0x20c0
movl %ebx, (%rax)
movq 0x514b(%rip), %rsi      # 0x7fa0
movq %rax, %rdi
xorl %edx, %edx
callq 0x2280
movq %rax, %rbx
jmp 0x2e9c
movq %rax, %rbx
jmp 0x2e90
jmp 0x2e84
jmp 0x2e84
jmp 0x2e6f
movq %rax, %rbx
leaq -0xb0(%rbp), %rdi
callq 0x3d0a
jmp 0x2e87
jmp 0x2e84
jmp 0x2e84
movq %rax, %rbx
leaq -0x68(%rbp), %rdi
callq 0x20b0
leaq -0x2b8(%rbp), %rdi
callq 0x2060
movq -0x40(%rbp), %rdi
callq 0x3dba
movq %rbx, %rdi
callq 0x22a0
nop