// File: /workspace/github_cpp_makefile_star2_O3/lmclaramunt[P]AES-Crypto/Cipher.cpp
// Function: Cipher::getKey(bool, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, unsigned char***)
void Cipher::getKey(bool encrypt, string* keyPath, unsigned char*** keyExpanded){
    unsigned char* key = new unsigned char[4*Nk];
    ifstream input(*keyPath, ios::binary);
    if(input.is_open()){        //Read key, given path to it
        for(int i=0; i < 4*Nk; i++)
            key[i] = input.get();
        input.close(); 
    } else if(encrypt) {          //Create new key if we are encrypting
        generateKey(key, Nk);
        ofstream keyFile;
        keyFile.open(*keyPath, ios::binary);
        keyFile.write((const char*)key, 4*Nk);
        keyFile.close();       
    } else {
        throw "Missing key for decryption\n";   //User should have key to decrypt
    }

    /* MEM53-CPP. Explicitly construct and destruct objects when 
       manually managing object lifetime */
    *keyExpanded = new unsigned char*[4*(Nr+1)];    //Expand the key   
    for(int j=0; j<4*(Nr+1); j++)
        (*keyExpanded)[j] = new unsigned char[4];
    keyExpansion(key, *keyExpanded);
    
    delete[] key;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x408, %rsp            # imm = 0x408
movq %rcx, %r15
movq %rdx, %r12
movl %esi, %ebp
movq %rdi, %rbx
movslq 0x18(%rdi), %rdi
shlq $0x2, %rdi
callq 0x2030
movq %rax, %r14
leaq 0x200(%rsp), %rdi
movq %r12, %rsi
movl $0x4, %edx
callq 0x2380
leaq 0x278(%rsp), %rdi
callq 0x2060
testb %al, %al
je 0x433e
cmpl $0x0, 0x18(%rbx)
jle 0x432f
xorl %r13d, %r13d
leaq 0x200(%rsp), %r12
movq %r12, %rdi
callq 0x2280
movb %al, (%r14,%r13)
incq %r13
movslq 0x18(%rbx), %rax
shlq $0x2, %rax
cmpq %rax, %r13
jl 0x4313
leaq 0x200(%rsp), %rdi
callq 0x2050
jmp 0x43b6
testb %bpl, %bpl
je 0x443b
movl 0x18(%rbx), %edx
movq %r14, %rsi
callq 0x4bd2
movq %rsp, %rdi
callq 0x23c0
movq %rsp, %rdi
movq %r12, %rsi
movl $0x4, %edx
callq 0x2100
movslq 0x18(%rbx), %rdx
shlq $0x2, %rdx
movq %rsp, %rdi
movq %r14, %rsi
callq 0x22c0
movq %rsp, %rdi
callq 0x2300
movq 0x6c44(%rip), %rax      # 0xafd0
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq 0x8(%rsp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, (%rsp,%rcx)
callq 0x21d0
leaq 0xf8(%rsp), %rdi
callq 0x20b0
movl 0x1c(%rbx), %eax
leal 0x4(,%rax,4), %ebp
movslq %ebp, %rax
leaq (,%rax,8), %rcx
testl %eax, %eax
movq $-0x1, %rdi
cmovnsq %rcx, %rdi
callq 0x2030
movq %rax, (%r15)
testl %ebp, %ebp
jle 0x4406
movl %ebp, %r12d
xorl %r13d, %r13d
movl $0x4, %edi
callq 0x2030
movq (%r15), %rcx
movq %rax, (%rcx,%r13,8)
incq %r13
cmpq %r13, %r12
jne 0x43ea
movq (%r15), %rax
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x4dd0
movq %r14, %rdi
callq 0x22b0
leaq 0x200(%rsp), %rdi
callq 0x2070
addq $0x408, %rsp            # imm = 0x408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x8, %edi
callq 0x2120
leaq 0x330e(%rip), %rcx      # 0x775a
movq %rcx, (%rax)
movq 0x6b6a(%rip), %rsi      # 0xafc0
movq %rax, %rdi
xorl %edx, %edx
callq 0x23d0
jmp 0x4473
movq %rax, %rbx
movq %rsp, %rdi
callq 0x2320
jmp 0x4476
jmp 0x4473
jmp 0x4473
movq %rax, %rbx
leaq 0x200(%rsp), %rdi
callq 0x2070
movq %rbx, %rdi
callq 0x2400
nop