// File: /workspace/github_cpp_makefile_star2_O1/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, -0x30(%rbp)
cmpl $0x3, %esi
jle 0x4266
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, -0x38(%rbp)
movl $0x0, (%r15)
cmpl $0x4, -0x30(%rbp)
je 0x3da2
movq -0x30(%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 0x50c6
movq -0x3d0(%rbp), %rdi
leaq -0x3c0(%rbp), %rax
cmpq %rax, %rdi
je 0x3d94
callq 0x2180
movq -0x38(%rbp), %rax
incl (%rax)
incq %rbx
cmpq %rbx, %r15
jne 0x3d5e
movl -0x30(%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 -0x38(%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 0x42b9
leaq -0x1b8(%rbp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
xorl %r14d, %r14d
movl %r14d, -0x30(%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 0x408d
movq -0x1c8(%rbp), %r13
movq -0x1c0(%rbp), %r15
leaq (%r15,%r13), %rbx
movq %r15, %r14
sarq $0x2, %r14
testq %r14, %r14
jle 0x3eef
andq $-0x4, %r15
addq %r13, %r15
incq %r14
addq $0x3, %r13
movq %r13, %r12
movsbl -0x3(%r12), %edi
callq 0x2040
testl %eax, %eax
jne 0x3f41
movsbl -0x2(%r12), %edi
callq 0x2040
testl %eax, %eax
jne 0x3f47
movsbl -0x1(%r12), %edi
callq 0x2040
testl %eax, %eax
jne 0x3f4d
movsbl (%r12), %edi
callq 0x2040
testl %eax, %eax
jne 0x3f50
decq %r14
addq $0x4, %r12
cmpq $0x1, %r14
jg 0x3e9c
movq %r15, %r13
movq %rbx, %rax
subq %r13, %rax
cmpq $0x1, %rax
je 0x3f2c
cmpq $0x2, %rax
je 0x3f1b
movq %rbx, %r12
cmpq $0x3, %rax
jne 0x3f50
movsbl (%r13), %edi
callq 0x2040
testl %eax, %eax
jne 0x3f3c
incq %r13
movsbl (%r13), %edi
callq 0x2040
testl %eax, %eax
jne 0x3f3c
incq %r13
movsbl (%r13), %edi
callq 0x2040
testl %eax, %eax
cmoveq %rbx, %r13
movq %r13, %r12
jmp 0x3f50
addq $-0x3, %r12
jmp 0x3f50
addq $-0x2, %r12
jmp 0x3f50
decq %r12
cmpq %rbx, %r12
sete %al
leaq 0x1(%r12), %r14
cmpq %rbx, %r14
sete %cl
orb %al, %cl
jne 0x3f84
movb (%r14), %r15b
movsbl %r15b, %edi
callq 0x2040
testl %eax, %eax
jne 0x3f7c
movb %r15b, (%r12)
incq %r12
incq %r14
cmpq %rbx, %r14
jne 0x3f65
movq %r12, %rax
subq -0x1c8(%rbp), %rax
movq %rax, -0x1c0(%rbp)
movb $0x0, (%r12)
movq -0x1c0(%rbp), %rax
testq %rax, %rax
movq -0x38(%rbp), %r15
movl -0x30(%rbp), %r14d
je 0x3e43
movq -0x1c8(%rbp), %rcx
movsbl (%rcx), %edx
addl $-0x30, %edx
cmpl $0x9, %edx
ja 0x421b
movl $0x1, %esi
cmpq %rsi, %rax
je 0x3fed
movq %rsi, %rdx
movsbl (%rcx,%rsi), %edi
addl $-0x30, %edi
incq %rsi
cmpl $0xa, %edi
jb 0x3fcd
cmpq %rdx, %rax
jne 0x421b
leaq -0x1c8(%rbp), %rdi
callq 0x2b7e
testb %al, %al
je 0x439d
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 0x4418
movslq %eax, %rcx
cmpq %rax, %rcx
jne 0x4424
movl (%r12), %ecx
cmpl $0x22, %ecx
je 0x4424
testl %ecx, %ecx
jne 0x405b
movl %r15d, (%r12)
movl %eax, -0x3c(%rbp)
movq -0x38(%rbp), %r15
movq 0x28(%r15), %rsi
cmpq 0x30(%r15), %rsi
je 0x4078
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x28(%r15)
jmp 0x4085
movq -0x68(%rbp), %rdi
leaq -0x3c(%rbp), %rdx
callq 0x4978
incl %r14d
jmp 0x3e3f
cmpl (%r15), %r14d
jl 0x4331
movq -0x1c8(%rbp), %rdi
leaq -0x1b8(%rbp), %rax
cmpq %rax, %rdi
je 0x40ae
callq 0x2180
cmpl $0x0, (%r15)
jle 0x416d
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 0x420a
movq (%rax,%r13), %rsi
movq %r12, %rdi
callq 0x2e5e
movq -0x70(%rbp), %rdi
movq %r12, %rsi
callq 0x5330
movq -0xf8(%rbp), %rdi
testq %rdi, %rdi
je 0x4111
callq 0x2180
movq 0x20(%r15), %rax
movq 0x28(%r15), %rdx
subq %rax, %rdx
sarq $0x2, %rdx
cmpq %r14, %rdx
jbe 0x41f9
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 0x41f9
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 0x40ca
movl $0x30, %edi
callq 0x21b0
movq %rax, %rbx
movq -0xb8(%rbp), %rsi
movq %rax, %rdi
callq 0x360c
movq %rbx, 0x40(%r15)
leaq -0x3d0(%rbp), %rdi
callq 0x2060
movq -0x60(%rbp), %rdi
leaq -0x50(%rbp), %rax
cmpq %rax, %rdi
je 0x41ab
callq 0x2180
leaq -0x90(%rbp), %rdi
callq 0x4854
movq -0xb8(%rbp), %rdi
leaq -0xa8(%rbp), %rax
cmpq %rax, %rdi
je 0x41cf
callq 0x2180
movq -0xd8(%rbp), %rdi
leaq -0xc8(%rbp), %rax
cmpq %rax, %rdi
je 0x41e7
callq 0x2180
addq $0x3a8, %rsp            # imm = 0x3A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x227a(%rip), %rdi      # 0x647a
movq %r14, %rsi
xorl %eax, %eax
callq 0x2240
leaq 0x2269(%rip), %rdi      # 0x647a
movq %r14, %rsi
xorl %eax, %eax
callq 0x2240
movq 0x4dce(%rip), %rdi      # 0x8ff0
leaq 0x2148(%rip), %rsi      # 0x6371
movl $0x2e, %edx
callq 0x21d0
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq 0x4dae(%rip), %rdi      # 0x8ff0
callq 0x21d0
movl $0x4, %r14d
leaq 0x1e10(%rip), %rsi      # 0x6064
movl $0x1, %edx
movq %rax, %rdi
callq 0x21d0
jmp 0x43fa
movq 0x4d83(%rip), %rdi      # 0x8ff0
leaq 0x2092(%rip), %rsi      # 0x6306
movl $0x4b, %edx
callq 0x21d0
movq 0x4d6b(%rip), %rdi      # 0x8ff0
leaq 0x1dd8(%rip), %rsi      # 0x6064
movl $0x1, %edx
callq 0x21d0
movl $0x4, %edi
callq 0x20c0
movl $0x1, (%rax)
movq 0x4d03(%rip), %rsi      # 0x8fb0
movq %rax, %rdi
xorl %edx, %edx
callq 0x2290
jmp 0x4331
movq 0x4d30(%rip), %rdi      # 0x8ff0
leaq 0x2113(%rip), %rsi      # 0x63da
movl $0x15, %edx
callq 0x21d0
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq 0x4d10(%rip), %rdi      # 0x8ff0
callq 0x21d0
movq %rax, %rbx
leaq 0x1e22(%rip), %rsi      # 0x6111
movl $0x1a, %edx
movq %rax, %rdi
callq 0x21d0
leaq 0x1d61(%rip), %rsi      # 0x6064
movl $0x1, %edx
movq %rbx, %rdi
callq 0x21d0
movl $0x4, %edi
callq 0x20c0
movl $0xb, (%rax)
movq 0x4c89(%rip), %rsi      # 0x8fb0
movq %rax, %rdi
xorl %edx, %edx
callq 0x2290
movl %r14d, %ebx
movq 0x4cb5(%rip), %rdi      # 0x8ff0
leaq 0x205e(%rip), %rsi      # 0x63a0
movl $0x1f, %edx
callq 0x21d0
movq 0x4c9d(%rip), %rdi      # 0x8ff0
movl %ebx, %esi
callq 0x22a0
movq %rax, %rbx
leaq 0x205c(%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 0x1cd6(%rip), %rsi      # 0x6064
movl $0x1, %edx
movq %rax, %rdi
callq 0x21d0
jmp 0x43fa
movq 0x4c4c(%rip), %rdi      # 0x8ff0
leaq 0x1fa7(%rip), %rsi      # 0x6352
movl $0x1e, %edx
callq 0x21d0
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq 0x4c2c(%rip), %rdi      # 0x8ff0
callq 0x21d0
movq %rax, %rbx
leaq 0x1c66(%rip), %rsi      # 0x6039
movl $0x2a, %edx
movq %rax, %rdi
callq 0x21d0
movl $0x3, %r14d
leaq 0x1c77(%rip), %rsi      # 0x6064
movl $0x1, %edx
movq %rbx, %rdi
callq 0x21d0
movl $0x4, %edi
callq 0x20c0
movl %r14d, (%rax)
movq 0x4ba2(%rip), %rsi      # 0x8fb0
movq %rax, %rdi
xorl %edx, %edx
callq 0x2290
leaq 0x2024(%rip), %rdi      # 0x6443
callq 0x20f0
leaq 0x2018(%rip), %rdi      # 0x6443
callq 0x2220
movq %rax, %r14
movq %rbx, %rdi
jmp 0x44af
jmp 0x4462
movq %rax, %r14
movq %rbx, %rdi
jmp 0x447d
movq %rax, %r14
jmp 0x4513
jmp 0x4499
movq %rax, %r14
cmpl $0x0, (%r12)
jne 0x449c
movl %r15d, (%r12)
jmp 0x449c
jmp 0x44b6
jmp 0x4499
jmp 0x4462
movq %rax, %r14
jmp 0x44c5
movq %rax, %r14
movq -0x3d0(%rbp), %rdi
leaq -0x3c0(%rbp), %rax
cmpq %rax, %rdi
je 0x44c5
callq 0x2180
jmp 0x44c5
jmp 0x44b6
jmp 0x44b6
movq %rax, %r14
movq -0xf8(%rbp), %rdi
testq %rdi, %rdi
jne 0x44af
jmp 0x44b9
movq %rax, %r14
movq -0x1c8(%rbp), %rdi
leaq -0x1b8(%rbp), %rax
cmpq %rax, %rdi
je 0x44b9
callq 0x2180
jmp 0x44b9
movq %rax, %r14
leaq -0x3d0(%rbp), %rdi
callq 0x2060
movq -0x60(%rbp), %rdi
leaq -0x50(%rbp), %rax
cmpq %rax, %rdi
je 0x44d7
callq 0x2180
leaq -0x90(%rbp), %rdi
callq 0x4854
movq -0xb8(%rbp), %rdi
leaq -0xa8(%rbp), %rax
cmpq %rax, %rdi
je 0x44fb
callq 0x2180
movq -0xd8(%rbp), %rdi
leaq -0xc8(%rbp), %rax
cmpq %rax, %rdi
je 0x4513
callq 0x2180
movq -0x68(%rbp), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x4524
callq 0x2180
movq -0x70(%rbp), %rdi
callq 0x48a2
movq %r14, %rdi
callq 0x22b0
nop