// File: /workspace/github_cpp_makefile_star2_O2/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, -0x50(%rbp)
leaq 0x20(%rdi), %rax
movq %rax, -0x40(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, 0x38(%rdi)
movups %xmm0, 0x28(%rdi)
movups %xmm0, 0x18(%rdi)
movups %xmm0, 0x8(%rdi)
cmpl $0x3, %esi
jle 0x3695
movq %rdx, %rbx
movl %esi, %r15d
movq %rdi, %r14
leaq -0xc0(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
xorl %ecx, %ecx
movb %cl, (%rax)
leaq -0xa0(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
andq $0x0, -0x80(%rbp)
leaq -0x68(%rbp), %rdx
movq %rdx, -0x10(%rdx)
andq $0x0, -0x8(%rdx)
movb %cl, (%rax)
movaps %xmm0, -0x90(%rbp)
movb %cl, (%rdx)
movq 0x8(%rbx), %rsi
leaq -0xd0(%rbp), %rdi
callq 0x22c0
movq 0x10(%rbx), %rsi
leaq -0xb0(%rbp), %rdi
callq 0x22c0
movq %r14, -0x48(%rbp)
andl $0x0, (%r14)
movq %r15, -0x38(%rbp)
cmpl $0x4, %r15d
jne 0x3641
movl -0x38(%rbp), %eax
movq -0x8(%rbx,%rax,8), %rsi
leaq -0x78(%rbp), %rdi
callq 0x22c0
movq -0x48(%rbp), %r15
pushq $0x30
popq %rdi
callq 0x21c0
movq %rax, %rbx
movq -0xd0(%rbp), %rsi
movq %rax, %rdi
callq 0x2500
movq %rbx, 0x38(%r15)
leaq -0x3d0(%rbp), %rdi
callq 0x21d0
leaq -0x3d0(%rbp), %rdi
leaq -0x78(%rbp), %rsi
pushq $0x8
popq %rbx
movl %ebx, %edx
callq 0x2160
movl %ebx, -0x2c(%rbp)
leaq -0x358(%rbp), %rdi
callq 0x2050
testb %al, %al
je 0x36d8
leaq -0x1b8(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movl $0x0, -0x38(%rbp)
leaq -0x3d0(%rbp), %r12
leaq -0x1c8(%rbp), %r14
pushq $0x20
popq %r13
movq 0x4b0c(%rip), %rbx      # 0x7fa8
movq %r12, %rdi
movq %r14, %rsi
movl %r13d, %edx
callq 0x22b0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x3542
movq -0x1c8(%rbp), %rdi
movq -0x1c0(%rbp), %rsi
addq %rdi, %rsi
movq %rbx, %rdx
callq 0x3dce
movq -0x1c8(%rbp), %rdx
addq -0x1c0(%rbp), %rdx
movq %r14, %rdi
movq %rax, %rsi
callq 0x2040
cmpq $0x0, -0x1c0(%rbp)
je 0x349c
movq %r14, %rdi
callq 0x2888
testb %al, %al
je 0x377e
movq %r14, %rdi
callq 0x28b6
testb %al, %al
je 0x37b0
movq %r14, %rdi
leaq -0xd8(%rbp), %rsi
pushq $0xa
popq %rdx
callq 0x3a55
movl %eax, -0x54(%rbp)
movq -0x40(%rbp), %rdi
leaq -0x54(%rbp), %rsi
callq 0x3a76
incl -0x38(%rbp)
jmp 0x349c
movl -0x38(%rbp), %eax
cmpl (%r15), %eax
jl 0x3734
leaq -0x1c8(%rbp), %rdi
callq 0x20b0
movl (%r15), %eax
testl %eax, %eax
jle 0x35d8
leaq -0xf8(%rbp), %rbx
xorl %r14d, %r14d
leaq -0x90(%rbp), %r12
leaq -0x1c8(%rbp), %r13
cltq
cmpq %rax, %r14
jge 0x35d8
movq %r12, %rdi
movq %r14, %rsi
callq 0x3b36
movq (%rax), %rsi
movq %r13, %rdi
callq 0x2a82
movq -0x50(%rbp), %rdi
movq %r13, %rsi
callq 0x49b0
movq %rbx, %rdi
callq 0x3dba
movq -0x40(%rbp), %rdi
movq %r14, %rsi
callq 0x3b14
movl (%rax), %r15d
movq -0x50(%rbp), %rdi
movq %r14, %rsi
callq 0x3b58
movl %r15d, 0xe8(%rax)
incq %r14
movq -0x48(%rbp), %r15
movl (%r15), %eax
jmp 0x3579
pushq $0x30
popq %rdi
callq 0x21c0
movq %rax, %rbx
movq -0xb0(%rbp), %rsi
movq %rax, %rdi
callq 0x2f54
movq %rbx, 0x40(%r15)
leaq -0x3d0(%rbp), %rdi
callq 0x2060
leaq -0x78(%rbp), %rdi
callq 0x20b0
leaq -0x90(%rbp), %rdi
callq 0x3b7a
leaq -0xb0(%rbp), %rdi
callq 0x20b0
leaq -0xd0(%rbp), %rdi
callq 0x20b0
addq $0x3a8, %rsp            # imm = 0x3A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x38(%rbp), %rax
leal -0x1(%rax), %r15d
pushq $0x3
popq %r13
leaq -0x3d0(%rbp), %r12
leaq -0x90(%rbp), %r14
cmpq %r13, %r15
je 0x33fe
movq (%rbx,%r13,8), %rsi
movq %r12, %rdi
leaq -0x1c8(%rbp), %rdx
callq 0x2090
movq %r14, %rdi
movq %r12, %rsi
callq 0x47ae
movq %r12, %rdi
callq 0x20b0
movq -0x48(%rbp), %rax
incl (%rax)
incq %r13
jmp 0x365b
movq 0x4954(%rip), %rdi      # 0x7ff0
leaq 0x1c47(%rip), %rsi      # 0x52ea
callq 0x21b0
leaq 0x1999(%rip), %rsi      # 0x5048
movq %rax, %rdi
callq 0x21b0
pushq $0x4
popq %rdi
callq 0x20c0
movl $0x1, (%rax)
movq 0x48d4(%rip), %rsi      # 0x7fa0
movq %rax, %rdi
xorl %edx, %edx
callq 0x2280
jmp 0x3734
movq 0x4911(%rip), %rdi      # 0x7ff0
leaq 0x1cd8(%rip), %rsi      # 0x53be
callq 0x21b0
leaq -0x78(%rbp), %rsi
movq %rax, %rdi
callq 0x2180
leaq 0x19f7(%rip), %rsi      # 0x50f5
movq %rax, %rdi
callq 0x21b0
leaq 0x193b(%rip), %rsi      # 0x5048
movq %rax, %rdi
callq 0x21b0
pushq $0x4
popq %rdi
callq 0x20c0
movl $0xb, (%rax)
movq 0x4876(%rip), %rsi      # 0x7fa0
movq %rax, %rdi
xorl %edx, %edx
callq 0x2280
movq 0x48b5(%rip), %rdi      # 0x7ff0
leaq 0x1c42(%rip), %rsi      # 0x5384
callq 0x21b0
movq %rax, %rdi
movl -0x38(%rbp), %esi
callq 0x2290
leaq 0x1c4b(%rip), %rsi      # 0x53a4
movq %rax, %rdi
callq 0x21b0
leaq -0x78(%rbp), %rsi
movq %rax, %rdi
callq 0x2180
leaq 0x18d4(%rip), %rsi      # 0x5048
movq %rax, %rdi
callq 0x21b0
jmp 0x37f3
movq 0x486b(%rip), %rdi      # 0x7ff0
leaq 0x1bc9(%rip), %rsi      # 0x5355
callq 0x21b0
leaq -0x78(%rbp), %rsi
movq %rax, %rdi
callq 0x2180
leaq 0x18a4(%rip), %rsi      # 0x5048
movq %rax, %rdi
callq 0x21b0
pushq $0x4
jmp 0x37ef
movq 0x4839(%rip), %rdi      # 0x7ff0
leaq 0x1b78(%rip), %rsi      # 0x5336
callq 0x21b0
leaq -0x78(%rbp), %rsi
movq %rax, %rdi
callq 0x2180
leaq 0x1847(%rip), %rsi      # 0x501d
movq %rax, %rdi
callq 0x21b0
leaq 0x1863(%rip), %rsi      # 0x5048
movq %rax, %rdi
callq 0x21b0
pushq $0x3
popq %rax
movl %eax, -0x2c(%rbp)
pushq $0x4
popq %rdi
callq 0x20c0
movl -0x2c(%rbp), %ecx
movl %ecx, (%rax)
movq 0x4799(%rip), %rsi      # 0x7fa0
movq %rax, %rdi
xorl %edx, %edx
callq 0x2280
movq %rax, %r15
movq %rbx, %rdi
callq 0x2190
jmp 0x3877
jmp 0x383a
movq %rax, %r15
movq %rbx, %rdi
callq 0x2190
jmp 0x3883
movq %rax, %r15
jmp 0x38b0
jmp 0x3861
jmp 0x3861
jmp 0x3874
jmp 0x383a
movq %rax, %r15
jmp 0x3883
movq %rax, %r15
leaq -0x3d0(%rbp), %rdi
callq 0x20b0
jmp 0x3883
movq %rax, %r15
movq %rbx, %rdi
callq 0x3dba
jmp 0x3877
jmp 0x3861
jmp 0x3861
movq %rax, %r15
leaq -0x1c8(%rbp), %rdi
callq 0x20b0
jmp 0x3877
jmp 0x3874
movq %rax, %r15
leaq -0x3d0(%rbp), %rdi
callq 0x2060
leaq -0x78(%rbp), %rdi
callq 0x20b0
leaq -0x90(%rbp), %rdi
callq 0x3b7a
leaq -0xb0(%rbp), %rdi
callq 0x20b0
leaq -0xd0(%rbp), %rdi
callq 0x20b0
movq -0x40(%rbp), %rdi
callq 0x3dba
movq -0x50(%rbp), %rdi
callq 0x3ba6
movq %r15, %rdi
callq 0x22a0