// File: 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
subq $0x400, %rsp            # imm = 0x400
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x308(%rbp)
movq %rdi, %rax
movq %rax, -0x310(%rbp)
callq 0x5240
movq -0x308(%rbp), %rdi
addq $0x18, %rdi
movq %rdi, -0x300(%rbp)
callq 0x5240
leaq -0x218(%rbp), %rdi
callq 0x2210
jmp 0x2692
movq -0x10(%rbp), %rsi
leaq -0x218(%rbp), %rdi
movl $0x8, %edx
callq 0x2120
jmp 0x26a9
leaq -0x218(%rbp), %rdi
callq 0x22d0
movb %al, -0x311(%rbp)
jmp 0x26bd
movb -0x311(%rbp), %al
testb $0x1, %al
jne 0x26cc
jmp 0x2da1
leaq -0x248(%rbp), %rdi
callq 0x2280
movl $0x0, -0x250(%rbp)
leaq -0x218(%rbp), %rdi
leaq -0x248(%rbp), %rsi
movl $0x20, %edx
callq 0x2360
movq %rax, -0x320(%rbp)
jmp 0x2703
movq -0x320(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x2230
movb %al, -0x321(%rbp)
jmp 0x2721
movb -0x321(%rbp), %al
testb $0x1, %al
jne 0x2730
jmp 0x2a2c
leaq -0x248(%rbp), %rdi
movq %rdi, -0x338(%rbp)
callq 0x2170
movq -0x338(%rbp), %rdi
movq %rax, -0x270(%rbp)
callq 0x20e0
movq %rax, -0x278(%rbp)
movq -0x270(%rbp), %rdi
movq -0x278(%rbp), %rsi
movq 0xf826(%rip), %rdx      # 0x11f98
callq 0x5260
movq %rax, -0x330(%rbp)
jmp 0x2780
movq -0x330(%rbp), %rax
movq %rax, -0x268(%rbp)
leaq -0x260(%rbp), %rdi
leaq -0x268(%rbp), %rsi
callq 0x52b0
leaq -0x248(%rbp), %rdi
movq %rdi, -0x348(%rbp)
callq 0x20e0
movq %rax, -0x288(%rbp)
leaq -0x280(%rbp), %rdi
leaq -0x288(%rbp), %rsi
callq 0x52b0
movq -0x348(%rbp), %rdi
movq -0x260(%rbp), %rsi
movq -0x280(%rbp), %rdx
callq 0x2040
movq %rax, -0x340(%rbp)
jmp 0x27f1
movq -0x340(%rbp), %rax
movq %rax, -0x290(%rbp)
leaq -0x248(%rbp), %rdi
callq 0x2300
testb $0x1, %al
jne 0x2a27
leaq -0x248(%rbp), %rdi
callq 0x2e80
movb %al, -0x349(%rbp)
jmp 0x2827
movb -0x349(%rbp), %al
testb $0x1, %al
jne 0x2836
jmp 0x29b3
leaq -0x248(%rbp), %rdi
callq 0x2f00
movb %al, -0x34a(%rbp)
jmp 0x284a
movb -0x34a(%rbp), %al
testb $0x1, %al
jne 0x2859
jmp 0x2923
leaq -0x248(%rbp), %rdi
leaq -0x258(%rbp), %rsi
movl $0xa, %edx
callq 0x52f0
movl %eax, -0x350(%rbp)
jmp 0x2879
movl -0x350(%rbp), %eax
movl %eax, -0x24c(%rbp)
movl -0x250(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x28f4
movq -0x310(%rbp), %rdi
leaq -0x24c(%rbp), %rsi
callq 0x5330
jmp 0x28ad
jmp 0x290f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
jmp 0x2e4d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
jmp 0x2e41
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
jmp 0x2d90
movq -0x310(%rbp), %rdi
addq $0x18, %rdi
leaq -0x24c(%rbp), %rsi
callq 0x5330
jmp 0x290d
jmp 0x290f
movl -0x250(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x250(%rbp)
jmp 0x29b1
movq 0xf6c6(%rip), %rdi      # 0x11ff0
leaq 0x9707(%rip), %rsi      # 0xc038
callq 0x21d0
movq %rax, -0x358(%rbp)
jmp 0x293f
movq -0x358(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x21d0
movq %rax, -0x360(%rbp)
jmp 0x2958
movq -0x360(%rbp), %rdi
leaq 0x96eb(%rip), %rsi      # 0xc051
callq 0x21d0
movq %rax, -0x368(%rbp)
jmp 0x2974
movq -0x368(%rbp), %rdi
leaq 0x96fa(%rip), %rsi      # 0xc07c
callq 0x21d0
jmp 0x2989
movl $0x4, %edi
callq 0x20b0
movq %rax, %rdi
movl $0x3, (%rdi)
movq 0xf5ed(%rip), %rsi      # 0x11f90
xorl %eax, %eax
movl %eax, %edx
callq 0x2310
jmp 0x2e71
jmp 0x2a25
movq 0xf636(%rip), %rdi      # 0x11ff0
leaq 0x96bd(%rip), %rsi      # 0xc07e
callq 0x21d0
movq %rax, -0x370(%rbp)
jmp 0x29cf
movq -0x370(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x21d0
movq %rax, -0x378(%rbp)
jmp 0x29e8
movq -0x378(%rbp), %rdi
leaq 0x9686(%rip), %rsi      # 0xc07c
callq 0x21d0
jmp 0x29fd
movl $0x4, %edi
callq 0x20b0
movq %rax, %rdi
movl $0x4, (%rdi)
movq 0xf579(%rip), %rsi      # 0x11f90
xorl %eax, %eax
movl %eax, %edx
callq 0x2310
jmp 0x2e71
jmp 0x2a27
jmp 0x26e2
movq -0x310(%rbp), %rdi
callq 0x53a0
cmpq $0x0, %rax
je 0x2b65
movq -0x310(%rbp), %rdi
addq $0x18, %rdi
callq 0x53a0
cmpq $0x0, %rax
jne 0x2aca
movq 0xf591(%rip), %rdi      # 0x11ff0
leaq 0x9641(%rip), %rsi      # 0xc0a7
callq 0x21d0
movq %rax, -0x380(%rbp)
jmp 0x2a74
movq -0x380(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x21d0
movq %rax, -0x388(%rbp)
jmp 0x2a8d
movq -0x388(%rbp), %rdi
leaq 0x95e1(%rip), %rsi      # 0xc07c
callq 0x21d0
jmp 0x2aa2
movl $0x4, %edi
callq 0x20b0
movq %rax, %rdi
movl $0x6, (%rdi)
movq 0xf4d4(%rip), %rsi      # 0x11f90
xorl %eax, %eax
movl %eax, %edx
callq 0x2310
jmp 0x2e71
movq -0x310(%rbp), %rdi
movq %rdi, %rsi
addq $0x18, %rsi
callq 0x2f50
movb %al, -0x389(%rbp)
jmp 0x2ae5
movb -0x389(%rbp), %al
testb $0x1, %al
jne 0x2b63
jmp 0x2af1
movq 0xf4f8(%rip), %rdi      # 0x11ff0
leaq 0x95da(%rip), %rsi      # 0xc0d9
callq 0x21d0
movq %rax, -0x398(%rbp)
jmp 0x2b0d
movq -0x398(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x21d0
movq %rax, -0x3a0(%rbp)
jmp 0x2b26
movq -0x3a0(%rbp), %rdi
leaq 0x9548(%rip), %rsi      # 0xc07c
callq 0x21d0
jmp 0x2b3b
movl $0x4, %edi
callq 0x20b0
movq %rax, %rdi
movl $0x5, (%rdi)
movq 0xf43b(%rip), %rsi      # 0x11f90
xorl %eax, %eax
movl %eax, %edx
callq 0x2310
jmp 0x2e71
jmp 0x2b65
movq -0x310(%rbp), %rdi
callq 0x53a0
movq -0x310(%rbp), %rdi
movq %rax, -0x3a8(%rbp)
addq $0x18, %rdi
callq 0x53a0
movq %rax, %rcx
movq -0x3a8(%rbp), %rax
cmpq %rcx, %rax
jne 0x2ba9
movq -0x310(%rbp), %rdi
callq 0x53a0
cmpq $0xd, %rax
jbe 0x2c1b
movq 0xf440(%rip), %rdi      # 0x11ff0
leaq 0x94f0(%rip), %rsi      # 0xc0a7
callq 0x21d0
movq %rax, -0x3b0(%rbp)
jmp 0x2bc5
movq -0x3b0(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x21d0
movq %rax, -0x3b8(%rbp)
jmp 0x2bde
movq -0x3b8(%rbp), %rdi
leaq 0x9490(%rip), %rsi      # 0xc07c
callq 0x21d0
jmp 0x2bf3
movl $0x4, %edi
callq 0x20b0
movq %rax, %rdi
movl $0x6, (%rdi)
movq 0xf383(%rip), %rsi      # 0x11f90
xorl %eax, %eax
movl %eax, %edx
callq 0x2310
jmp 0x2e71
movq -0x310(%rbp), %rsi
leaq -0x2a8(%rbp), %rdi
callq 0x53c0
jmp 0x2c30
movq -0x310(%rbp), %rdi
callq 0x5560
movq %rax, -0x2b8(%rbp)
leaq -0x2b0(%rbp), %rdi
leaq -0x2b8(%rbp), %rsi
callq 0x5590
movq -0x310(%rbp), %rdi
addq $0x18, %rdi
movq %rdi, -0x3c8(%rbp)
callq 0x55d0
movq -0x3c8(%rbp), %rdi
movq %rax, -0x2c0(%rbp)
callq 0x5560
movq -0x310(%rbp), %rdi
movq %rax, -0x2c8(%rbp)
movq -0x2b0(%rbp), %rsi
movq -0x2c0(%rbp), %rdx
movq -0x2c8(%rbp), %rcx
callq 0x54b0
movq %rax, -0x3c0(%rbp)
jmp 0x2cb1
movq -0x310(%rbp), %rdi
movq -0x3c0(%rbp), %rax
movq %rax, -0x2d0(%rbp)
addq $0x18, %rdi
movq %rdi, -0x3d8(%rbp)
callq 0x5560
movq %rax, -0x2e0(%rbp)
leaq -0x2d8(%rbp), %rdi
leaq -0x2e0(%rbp), %rsi
callq 0x5590
leaq -0x2a8(%rbp), %rdi
movq %rdi, -0x3e0(%rbp)
callq 0x55d0
movq -0x3e0(%rbp), %rdi
movq %rax, -0x2e8(%rbp)
callq 0x5560
movq -0x3d8(%rbp), %rdi
movq %rax, -0x2f0(%rbp)
movq -0x2d8(%rbp), %rsi
movq -0x2e8(%rbp), %rdx
movq -0x2f0(%rbp), %rcx
callq 0x54b0
movq %rax, -0x3d0(%rbp)
jmp 0x2d47
movq -0x3d0(%rbp), %rax
movq %rax, -0x2f8(%rbp)
leaq -0x2a8(%rbp), %rdi
callq 0x5600
leaq -0x248(%rbp), %rdi
callq 0x2110
jmp 0x2e2c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
leaq -0x2a8(%rbp), %rdi
callq 0x5600
leaq -0x248(%rbp), %rdi
callq 0x2110
jmp 0x2e41
movq 0xf248(%rip), %rdi      # 0x11ff0
leaq 0x936a(%rip), %rsi      # 0xc119
callq 0x21d0
movq %rax, -0x3e8(%rbp)
jmp 0x2dbd
movq -0x3e8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x21d0
movq %rax, -0x3f0(%rbp)
jmp 0x2dd6
movq -0x3f0(%rbp), %rdi
leaq 0x9345(%rip), %rsi      # 0xc129
callq 0x21d0
movq %rax, -0x3f8(%rbp)
jmp 0x2df2
movq -0x3f8(%rbp), %rdi
leaq 0x927c(%rip), %rsi      # 0xc07c
callq 0x21d0
jmp 0x2e07
movl $0x4, %edi
callq 0x20b0
movq %rax, %rdi
movl $0xb, (%rdi)
movq 0xf16f(%rip), %rsi      # 0x11f90
xorl %eax, %eax
movl %eax, %edx
callq 0x2310
jmp 0x2e71
leaq -0x218(%rbp), %rdi
callq 0x2050
addq $0x400, %rsp            # imm = 0x400
popq %rbp
retq
leaq -0x218(%rbp), %rdi
callq 0x2050
movq -0x300(%rbp), %rdi
callq 0x5600
movq -0x308(%rbp), %rdi
callq 0x5600
movq -0x220(%rbp), %rdi
callq 0x2330
nopw %cs:(%rax,%rax)