// File: /workspace/github_cpp_makefile_star2_O2/bowenfan96[P]icl-mcs-enigma/enigma.cpp
// Function: Enigma::Plugboard::Plugboard(char const*)
Enigma::Plugboard::Plugboard(const char* pb_filename) 
{
    std::ifstream pb_file;
    pb_file.open(pb_filename);
    
    if(pb_file.is_open()) {
        
        std::string index;
        int pb_num;
        int i = 0;
        size_t sz;
        
        while(std::getline(pb_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)) {
                        pb_num = std::stoi(index, &sz);
                        if(i % 2 == 0) {
                            map_from.push_back(pb_num);
                        }
                        else {
                            map_to.push_back(pb_num);
                        }
                        i++;
                    }
                    else {
                        std::cerr << "Index in plugboard file " << pb_filename << " does not correspond to an alphabet letter" << "\n";
                        throw INVALID_INDEX;
                    }
                }
                else {
                    std::cerr << "Non-numeric character in plugboard file " << pb_filename << "\n";
                    throw NON_NUMERIC_CHARACTER;
                }
            }
        }
        
        // check correct config (1 to 1 mapping, no value to itself)
        if(map_from.size() != 0) {
            if(map_to.size() == 0) {
                std::cerr << "Incorrect number of parameters in plugboard file " << pb_filename << "\n";
                throw INCORRECT_NUMBER_OF_PLUGBOARD_PARAMETERS;
            }
            if(!is_one_to_one(map_from, map_to)) {
                std::cerr << "Malformed contact points (not linked 1 to 1) in plugboard file " << pb_filename << "\n";
                throw IMPOSSIBLE_PLUGBOARD_CONFIGURATION;
            }
        }
        
        // check odd number of parameters or more than 13 pairings
        if(map_from.size() != map_to.size() || map_from.size() > 13) {
            std::cerr << "Incorrect number of parameters in plugboard file " << pb_filename << "\n";
            throw INCORRECT_NUMBER_OF_PLUGBOARD_PARAMETERS;
        }
        
        // mirror the mapping vectors
        std::vector<int> swap = map_from;
        map_from.insert(map_from.end(), map_to.begin(), map_to.end());
        map_to.insert(map_to.end(), swap.begin(), swap.end());
    }
    
    // check if file open failed
    else {
        std::cerr << "Plugboard file " << pb_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 $0x268, %rsp            # imm = 0x268
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x18(%rdi), %rax
movq %rax, -0x40(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%rdi)
movups %xmm0, 0x10(%rdi)
movups %xmm0, (%rdi)
leaq -0x290(%rbp), %rdi
callq 0x21d0
leaq -0x290(%rbp), %rdi
pushq $0x8
popq %rdx
movq %r14, %rsi
callq 0x2100
movq %r14, -0x30(%rbp)
leaq -0x218(%rbp), %rdi
callq 0x2050
testb %al, %al
je 0x2715
leaq -0x58(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movl $0x0, -0x34(%rbp)
leaq -0x290(%rbp), %r12
leaq -0x68(%rbp), %r13
pushq $0x20
popq %r14
movq 0x5a15(%rip), %r15      # 0x7fa8
movq %r12, %rdi
movq %r13, %rsi
movl %r14d, %edx
callq 0x22b0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x262e
movq -0x68(%rbp), %rdi
movq -0x60(%rbp), %rsi
addq %rdi, %rsi
movq %r15, %rdx
callq 0x3dce
movq -0x68(%rbp), %rdx
addq -0x60(%rbp), %rdx
movq %r13, %rdi
movq %rax, %rsi
callq 0x2040
cmpq $0x0, -0x60(%rbp)
je 0x2593
movq %r13, %rdi
callq 0x2888
testb %al, %al
je 0x2771
movq %r13, %rdi
callq 0x28b6
testb %al, %al
je 0x27a3
movq %r13, %rdi
leaq -0x88(%rbp), %rsi
pushq $0xa
popq %rdx
callq 0x3a55
movl %eax, -0x44(%rbp)
testb $0x1, -0x34(%rbp)
movq -0x40(%rbp), %rdi
cmoveq %rbx, %rdi
leaq -0x44(%rbp), %rsi
callq 0x3a76
incl -0x34(%rbp)
jmp 0x2593
movq 0x8(%rbx), %rcx
movq %rcx, %rax
cmpq (%rbx), %rcx
movq -0x40(%rbp), %r14
je 0x2666
movq 0x20(%rbx), %rax
cmpq 0x18(%rbx), %rax
je 0x26e0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x28d6
testb %al, %al
je 0x27e4
movq (%rbx), %rcx
movq 0x8(%rbx), %rax
subq %rcx, %rax
cmpq $0x34, %rax
ja 0x26e0
movq 0x20(%rbx), %rcx
subq 0x18(%rbx), %rcx
cmpq %rcx, %rax
jne 0x26e0
leaq -0x80(%rbp), %rdi
movq %rbx, %rsi
callq 0x3a9c
movq 0x8(%rbx), %rsi
movq 0x18(%rbx), %rdx
movq 0x20(%rbx), %rcx
movq %rbx, %rdi
callq 0x3af4
movq 0x20(%rbx), %rsi
movq -0x80(%rbp), %rdx
movq -0x78(%rbp), %rcx
movq %r14, %rdi
callq 0x3af4
leaq -0x80(%rbp), %rdi
callq 0x3dba
leaq -0x68(%rbp), %rdi
callq 0x20b0
leaq -0x290(%rbp), %rdi
callq 0x2060
addq $0x268, %rsp            # imm = 0x268
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x5909(%rip), %rdi      # 0x7ff0
leaq 0x2985(%rip), %rsi      # 0x5073
callq 0x21b0
movq %rax, %rdi
movq -0x30(%rbp), %rsi
callq 0x21b0
leaq 0x2942(%rip), %rsi      # 0x5048
movq %rax, %rdi
callq 0x21b0
pushq $0x6
jmp 0x2814
movq 0x58d4(%rip), %rdi      # 0x7ff0
leaq 0x29c2(%rip), %rsi      # 0x50e5
callq 0x21b0
movq -0x30(%rbp), %rsi
movq %rax, %rdi
callq 0x21b0
leaq 0x29ba(%rip), %rsi      # 0x50f5
movq %rax, %rdi
callq 0x21b0
leaq 0x28fe(%rip), %rsi      # 0x5048
movq %rax, %rdi
callq 0x21b0
pushq $0x4
popq %rdi
callq 0x20c0
movl $0xb, (%rax)
movq 0x5839(%rip), %rsi      # 0x7fa0
movq %rax, %rdi
xorl %edx, %edx
callq 0x2280
movq 0x5878(%rip), %rdi      # 0x7ff0
leaq 0x28cb(%rip), %rsi      # 0x504a
callq 0x21b0
movq %rax, %rdi
movq -0x30(%rbp), %rsi
callq 0x21b0
leaq 0x28b1(%rip), %rsi      # 0x5048
movq %rax, %rdi
callq 0x21b0
pushq $0x4
jmp 0x2814
movq 0x5846(%rip), %rdi      # 0x7ff0
leaq 0x2853(%rip), %rsi      # 0x5004
callq 0x21b0
movq %rax, %rdi
movq -0x30(%rbp), %rsi
callq 0x21b0
leaq 0x2854(%rip), %rsi      # 0x501d
movq %rax, %rdi
callq 0x21b0
leaq 0x2870(%rip), %rsi      # 0x5048
movq %rax, %rdi
callq 0x21b0
pushq $0x3
jmp 0x2814
movq 0x5805(%rip), %rdi      # 0x7ff0
leaq 0x28b3(%rip), %rsi      # 0x50a5
callq 0x21b0
movq %rax, %rdi
movq -0x30(%rbp), %rsi
callq 0x21b0
leaq 0x283e(%rip), %rsi      # 0x5048
movq %rax, %rdi
callq 0x21b0
pushq $0x5
popq %r14
pushq $0x4
popq %rdi
callq 0x20c0
movl %r14d, (%rax)
movq 0x5778(%rip), %rsi      # 0x7fa0
movq %rax, %rdi
xorl %edx, %edx
callq 0x2280
jmp 0x2834
movq %rax, %r14
leaq -0x80(%rbp), %rdi
callq 0x3dba
jmp 0x2859
jmp 0x2856
movq %rax, %r14
jmp 0x286e
movq %rax, %r14
jmp 0x2862
jmp 0x2856
jmp 0x2856
jmp 0x2856
jmp 0x2856
movq %rax, %r14
leaq -0x68(%rbp), %rdi
callq 0x20b0
leaq -0x290(%rbp), %rdi
callq 0x2060
movq -0x40(%rbp), %rdi
callq 0x3dba
movq %rbx, %rdi
callq 0x3dba
movq %r14, %rdi
callq 0x22a0
nop