// File: /workspace/github_cpp_makefile_star2_O3/bowenfan96[P]icl-mcs-enigma/enigma.cpp
// Function: Enigma::Enigma(int, char**)
Enigma::Enigma(int argc, char** argv) 
{    
    // Minimum 3 parameters: 0 rotors, 1 plugboard, 1 reflector, 1 position
    // Check if there are 3 or more parameters supplied: argc >= 4
    if(argc < 4) {
        std::cerr << "usage: enigma plugboard-file reflector-file (<rotor-file>)* rotor-positions" << "\n";
        throw INSUFFICIENT_NUMBER_OF_PARAMETERS;
    }
    
    std::string pb_filename;
    std::string rf_filename;
    std::vector<std::string> rot_filenames;
    std::string pos_filename;
        
    pb_filename = argv[1];
    rf_filename = argv[2];
    
    // check if there are rotor(s), then load them
    num_rotors = 0;
    if(argc > 4) {
        for(int i=3; i < (argc - 1); i++) {
            rot_filenames.push_back(argv[i]);
            num_rotors++;
        }
    }
    
    pos_filename = argv[argc - 1];
    
    // load Plugboard
    pb = new Plugboard(pb_filename.c_str());
    
    // load rotor positions
    std::ifstream pos_file;
    pos_file.open(pos_filename);
    
    if(pos_file.is_open()) {
        
        std::string index;
        int pos_num;
        int num_pos = 0;
        size_t sz;
        
        while(std::getline(pos_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)) {
                        pos_num = std::stoi(index, &sz);
                        vec_rot_posn.push_back(pos_num);
                        num_pos++;
                    }
                    else {
                        std::cerr << "Index in rotor positions file " << pos_filename << " does not correspond to an alphabet letter" << "\n";
                        throw INVALID_INDEX;
                    }
                }
                else {
                    std::cerr << "Non-numeric character in rotor positions file " << pos_filename << "\n";
                    throw NON_NUMERIC_CHARACTER;
                }
            }
        }
        
        // check every rotor has a starting position defined
        // excessive positions are ignored
        if(num_pos < num_rotors) {
            std::cerr << "No starting position for rotor " << num_pos << " in rotor position file: " << pos_filename << "\n";
            throw NO_ROTOR_STARTING_POSITION;
        }
    }
    
    // check if position file open failed
    else {
        std::cerr << "Rotor positions file " << pos_filename << " supplied cannot be opened" << "\n";
        throw ERROR_OPENING_CONFIGURATION_FILE;
    }
    
    // load rotors
    if(num_rotors > 0) {
        for(int i=0; i<num_rotors; i++) {
            vec_rotors.push_back(Rotor(rot_filenames.at(i).c_str()));
            vec_rotors.at(i).position = vec_rot_posn.at(i);
        }
    }
    
    // load Reflector
    rf = new Reflector(rf_filename.c_str());
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3a8, %rsp            # imm = 0x3A8
leaq 0x8(%rdi), %rax
movq %rax, -0x70(%rbp)
leaq 0x20(%rdi), %rax
movq %rax, -0x68(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, 0x38(%rdi)
movups %xmm0, 0x28(%rdi)
movups %xmm0, 0x18(%rdi)
movups %xmm0, 0x8(%rdi)
movq %rsi, -0x38(%rbp)
cmpl $0x3, %esi
jle 0x4250
movq %rdx, %r14
movq %rdi, %r15
leaq -0xc8(%rbp), %rcx
movq %rcx, -0x10(%rcx)
xorl %eax, %eax
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq -0xa8(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
movq %rax, -0x80(%rbp)
movaps %xmm0, -0x90(%rbp)
leaq -0x50(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
movq 0x8(%rdx), %rbx
movq %rbx, %rdi
callq 0x20b0
leaq -0xd8(%rbp), %rdi
xorl %esi, %esi
xorl %edx, %edx
movq %rbx, %rcx
movq %rax, %r8
callq 0x2260
movq 0x10(%r14), %rbx
leaq -0xb8(%rbp), %r12
movq 0x8(%r12), %r13
movq %rbx, %rdi
callq 0x20b0
movq %r12, %rdi
xorl %esi, %esi
movq %r13, %rdx
movq %rbx, %rcx
movq %rax, %r8
callq 0x2260
movq %r15, -0x30(%rbp)
movl $0x0, (%r15)
cmpl $0x4, -0x38(%rbp)
je 0x3d86
movq -0x38(%rbp), %rax
leal -0x1(%rax), %r15d
movl $0x3, %ebx
leaq -0x3d0(%rbp), %r13
leaq -0x90(%rbp), %r12
movq (%r14,%rbx,8), %rsi
movq %r13, %rdi
leaq -0x1c8(%rbp), %rdx
callq 0x2090
movq %r12, %rdi
movq %r13, %rsi
callq 0x50b8
movq -0x3d0(%rbp), %rdi
leaq -0x3c0(%rbp), %rax
cmpq %rax, %rdi
je 0x3d78
callq 0x2180
movq -0x30(%rbp), %rax
incl (%rax)
incq %rbx
cmpq %rbx, %r15
jne 0x3d42
movl -0x38(%rbp), %eax
movq -0x8(%r14,%rax,8), %rbx
leaq -0x60(%rbp), %r14
movq 0x8(%r14), %r12
movq %rbx, %rdi
callq 0x20b0
movq %r14, %rdi
xorl %esi, %esi
movq %r12, %rdx
movq %rbx, %rcx
movq %rax, %r8
callq 0x2260
movq -0x30(%rbp), %r15
movl $0x30, %edi
callq 0x21b0
movq %rax, %rbx
movq -0xd8(%rbp), %rsi
movq %rax, %rdi
callq 0x2560
movq %rbx, 0x38(%r15)
leaq -0x3d0(%rbp), %rdi
callq 0x21c0
leaq -0x3d0(%rbp), %rdi
leaq -0x60(%rbp), %rsi
movl $0x8, %edx
callq 0x2160
leaq -0x358(%rbp), %rdi
callq 0x2050
testb %al, %al
je 0x42a3
leaq -0x1b8(%rbp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
xorl %r14d, %r14d
movq %r14, -0x38(%rbp)
leaq -0x3d0(%rbp), %rdi
leaq -0x1c8(%rbp), %rsi
movl $0x20, %edx
callq 0x22c0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x405a
movq -0x1c8(%rbp), %r14
movq -0x1c0(%rbp), %rax
leaq (%r14,%rax), %r13
movq %rax, %r15
sarq $0x2, %r15
movq %r14, %r12
testq %r15, %r15
jle 0x3ecb
andq $-0x4, %rax
movq %rax, %r12
addq %r14, %r12
incq %r15
leaq 0x3(%r14), %rbx
movsbl -0x3(%rbx), %edi
callq 0x2040
testl %eax, %eax
jne 0x3f22
movsbl -0x2(%rbx), %edi
callq 0x2040
testl %eax, %eax
jne 0x3f28
movsbl -0x1(%rbx), %edi
callq 0x2040
testl %eax, %eax
jne 0x3f2e
movsbl (%rbx), %edi
callq 0x2040
testl %eax, %eax
jne 0x3f31
decq %r15
addq $0x4, %rbx
cmpq $0x1, %r15
jg 0x3e83
movq %r13, %rax
subq %r12, %rax
cmpq $0x1, %rax
je 0x3f0d
cmpq $0x2, %rax
movq -0x30(%rbp), %r15
je 0x3efc
cmpq $0x3, %rax
jne 0x3f94
movsbl (%r12), %edi
callq 0x2040
testl %eax, %eax
jne 0x3f1d
incq %r12
movsbl (%r12), %edi
callq 0x2040
testl %eax, %eax
jne 0x3f1d
incq %r12
movsbl (%r12), %edi
callq 0x2040
testl %eax, %eax
cmoveq %r13, %r12
movq %r12, %rbx
jmp 0x3f31
addq $-0x3, %rbx
jmp 0x3f31
addq $-0x2, %rbx
jmp 0x3f31
decq %rbx
cmpq %r13, %rbx
sete %al
leaq 0x1(%rbx), %r15
cmpq %r13, %r15
sete %cl
orb %al, %cl
jne 0x3f6a
movb (%r15), %r14b
movsbl %r14b, %edi
callq 0x2040
testl %eax, %eax
jne 0x3f5b
movb %r14b, (%rbx)
incq %rbx
incq %r15
cmpq %r13, %r15
jne 0x3f45
movq -0x1c8(%rbp), %r14
movq -0x30(%rbp), %r15
movq %rbx, %rax
subq %r14, %rax
movq %rax, -0x1c0(%rbp)
movb $0x0, (%rbx)
movq -0x1c0(%rbp), %rax
testq %rax, %rax
movq -0x38(%rbp), %r14
je 0x3e27
jmp 0x3f99
movq %r13, %rbx
jmp 0x3f6e
movq -0x1c8(%rbp), %rcx
xorl %edx, %edx
movsbl (%rcx,%rdx), %esi
addl $-0x30, %esi
cmpl $0xa, %esi
jae 0x41db
incq %rdx
cmpq %rdx, %rax
jne 0x3fa2
leaq -0x1c8(%rbp), %rdi
callq 0x2b96
testb %al, %al
je 0x438a
movq -0x1c8(%rbp), %rbx
callq 0x2030
movq %rax, %r12
movl (%rax), %r15d
movl $0x0, (%rax)
movq %rbx, %rdi
leaq -0x98(%rbp), %rsi
movl $0xa, %edx
callq 0x2210
cmpq %rbx, -0x98(%rbp)
je 0x43ec
movslq %eax, %rcx
cmpq %rax, %rcx
jne 0x43f8
movl (%r12), %ecx
cmpl $0x22, %ecx
je 0x43f8
testl %ecx, %ecx
jne 0x4028
movl %r15d, (%r12)
movl %eax, -0x3c(%rbp)
movq -0x30(%rbp), %r15
movq 0x28(%r15), %rsi
cmpq 0x30(%r15), %rsi
je 0x4045
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x28(%r15)
jmp 0x4052
movq -0x68(%rbp), %rdi
leaq -0x3c(%rbp), %rdx
callq 0x499e
incl %r14d
jmp 0x3e23
movl (%r15), %eax
cmpl %eax, %r14d
jl 0x431b
movq -0x1c8(%rbp), %rdi
leaq -0x1b8(%rbp), %rcx
cmpq %rcx, %rdi
je 0x4081
callq 0x2180
movl (%r15), %eax
testl %eax, %eax
jle 0x413e
movl $0x1d, %ebx
xorl %r13d, %r13d
leaq -0x1c8(%rbp), %r12
xorl %r14d, %r14d
movq -0x90(%rbp), %rax
movq -0x88(%rbp), %rdx
subq %rax, %rdx
sarq $0x5, %rdx
cmpq %r14, %rdx
jbe 0x423f
movq (%rax,%r13), %rsi
movq %r12, %rdi
callq 0x2e4c
movq -0x70(%rbp), %rdi
movq %r12, %rsi
callq 0x5322
movq -0xf8(%rbp), %rdi
testq %rdi, %rdi
je 0x40e2
callq 0x2180
movq 0x20(%r15), %rax
movq 0x28(%r15), %rdx
subq %rax, %rdx
sarq $0x2, %rdx
cmpq %r14, %rdx
jbe 0x41ca
movq 0x8(%r15), %rcx
movq 0x10(%r15), %rdx
subq %rcx, %rdx
sarq $0x4, %rdx
movabsq $-0x1111111111111111, %rsi # imm = 0xEEEEEEEEEEEEEEEF
imulq %rsi, %rdx
cmpq %r14, %rdx
jbe 0x41ca
movl (%rax,%r14,4), %eax
movl %eax, (%rcx,%rbx,8)
incq %r14
movslq (%r15), %rax
addq $0x20, %r13
addq $0x1e, %rbx
cmpq %rax, %r14
jl 0x409b
movl $0x30, %edi
callq 0x21b0
movq %rax, %rbx
movq -0xb8(%rbp), %rsi
movq %rax, %rdi
callq 0x35fc
movq %rbx, 0x40(%r15)
leaq -0x3d0(%rbp), %rdi
callq 0x2060
movq -0x60(%rbp), %rdi
leaq -0x50(%rbp), %rax
cmpq %rax, %rdi
je 0x417c
callq 0x2180
leaq -0x90(%rbp), %rdi
callq 0x4866
movq -0xb8(%rbp), %rdi
leaq -0xa8(%rbp), %rax
cmpq %rax, %rdi
je 0x41a0
callq 0x2180
movq -0xd8(%rbp), %rdi
leaq -0xc8(%rbp), %rax
cmpq %rax, %rdi
je 0x41b8
callq 0x2180
addq $0x3a8, %rsp            # imm = 0x3A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x22a9(%rip), %rdi      # 0x647a
movq %r14, %rsi
xorl %eax, %eax
callq 0x2240
movq 0x4e0e(%rip), %rdi      # 0x8ff0
leaq 0x2188(%rip), %rsi      # 0x6371
movl $0x2e, %edx
callq 0x21d0
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq 0x4dee(%rip), %rdi      # 0x8ff0
callq 0x21d0
movl $0x4, %r14d
leaq 0x1e50(%rip), %rsi      # 0x6064
movl $0x1, %edx
movq %rax, %rdi
callq 0x21d0
movl $0x4, %edi
callq 0x20c0
movl %r14d, (%rax)
movq 0x4d7b(%rip), %rsi      # 0x8fb0
movq %rax, %rdi
xorl %edx, %edx
callq 0x2290
leaq 0x2234(%rip), %rdi      # 0x647a
movq %r14, %rsi
xorl %eax, %eax
callq 0x2240
movq 0x4d99(%rip), %rdi      # 0x8ff0
leaq 0x20a8(%rip), %rsi      # 0x6306
movl $0x4b, %edx
callq 0x21d0
movq 0x4d81(%rip), %rdi      # 0x8ff0
leaq 0x1dee(%rip), %rsi      # 0x6064
movl $0x1, %edx
callq 0x21d0
movl $0x4, %edi
callq 0x20c0
movl $0x1, (%rax)
movq 0x4d19(%rip), %rsi      # 0x8fb0
movq %rax, %rdi
xorl %edx, %edx
callq 0x2290
jmp 0x431b
movq 0x4d46(%rip), %rdi      # 0x8ff0
leaq 0x2129(%rip), %rsi      # 0x63da
movl $0x15, %edx
callq 0x21d0
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq 0x4d26(%rip), %rdi      # 0x8ff0
callq 0x21d0
movq %rax, %rbx
leaq 0x1e38(%rip), %rsi      # 0x6111
movl $0x1a, %edx
movq %rax, %rdi
callq 0x21d0
leaq 0x1d77(%rip), %rsi      # 0x6064
movl $0x1, %edx
movq %rbx, %rdi
callq 0x21d0
movl $0x4, %edi
callq 0x20c0
movl $0xb, (%rax)
movq 0x4c9f(%rip), %rsi      # 0x8fb0
movq %rax, %rdi
xorl %edx, %edx
callq 0x2290
movq %r14, %rbx
movq 0x4ccb(%rip), %rdi      # 0x8ff0
leaq 0x2074(%rip), %rsi      # 0x63a0
movl $0x1f, %edx
callq 0x21d0
movq 0x4cb3(%rip), %rdi      # 0x8ff0
movl %ebx, %esi
callq 0x22a0
movq %rax, %rbx
leaq 0x2072(%rip), %rsi      # 0x63c0
movl $0x19, %edx
movq %rax, %rdi
callq 0x21d0
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq %rbx, %rdi
callq 0x21d0
movl $0x8, %r14d
leaq 0x1cec(%rip), %rsi      # 0x6064
movl $0x1, %edx
movq %rax, %rdi
callq 0x21d0
jmp 0x4221
movq 0x4c5f(%rip), %rdi      # 0x8ff0
leaq 0x1fba(%rip), %rsi      # 0x6352
movl $0x1e, %edx
callq 0x21d0
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq 0x4c3f(%rip), %rdi      # 0x8ff0
callq 0x21d0
movq %rax, %rbx
leaq 0x1c79(%rip), %rsi      # 0x6039
movl $0x2a, %edx
movq %rax, %rdi
callq 0x21d0
movl $0x3, %r14d
leaq 0x1c8a(%rip), %rsi      # 0x6064
movl $0x1, %edx
movq %rbx, %rdi
callq 0x21d0
jmp 0x4221
leaq 0x2050(%rip), %rdi      # 0x6443
callq 0x20f0
leaq 0x2044(%rip), %rdi      # 0x6443
callq 0x2220
movq %rax, %r14
movq %rbx, %rdi
jmp 0x4483
jmp 0x4434
movq %rax, %r14
movq %rbx, %rdi
jmp 0x444f
movq %rax, %r14
jmp 0x44e7
jmp 0x446d
movq %rax, %r14
cmpl $0x0, (%r12)
jne 0x4470
movl %r15d, (%r12)
jmp 0x4470
jmp 0x448a
jmp 0x4434
movq %rax, %r14
jmp 0x4499
movq %rax, %r14
movq -0x3d0(%rbp), %rdi
leaq -0x3c0(%rbp), %rax
cmpq %rax, %rdi
je 0x4499
callq 0x2180
jmp 0x4499
jmp 0x448a
jmp 0x448a
movq %rax, %r14
movq -0xf8(%rbp), %rdi
testq %rdi, %rdi
jne 0x4483
jmp 0x448d
jmp 0x446d
movq %rax, %r14
movq -0x1c8(%rbp), %rdi
leaq -0x1b8(%rbp), %rax
cmpq %rax, %rdi
je 0x448d
callq 0x2180
jmp 0x448d
movq %rax, %r14
leaq -0x3d0(%rbp), %rdi
callq 0x2060
movq -0x60(%rbp), %rdi
leaq -0x50(%rbp), %rax
cmpq %rax, %rdi
je 0x44ab
callq 0x2180
leaq -0x90(%rbp), %rdi
callq 0x4866
movq -0xb8(%rbp), %rdi
leaq -0xa8(%rbp), %rax
cmpq %rax, %rdi
je 0x44cf
callq 0x2180
movq -0xd8(%rbp), %rdi
leaq -0xc8(%rbp), %rax
cmpq %rax, %rdi
je 0x44e7
callq 0x2180
movq -0x68(%rbp), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x44f8
callq 0x2180
movq -0x70(%rbp), %rdi
callq 0x48be
movq %r14, %rdi
callq 0x22b0
nop