// File: /workspace/github_cpp_makefile_star2_O1/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, %rbx
movq %rdx, %r12
movl %esi, %ebp
movq %rdi, %r14
movslq 0x18(%rdi), %rdi
shlq $0x2, %rdi
callq 0x2030
movq %rax, %r15
leaq 0x200(%rsp), %rdi
movq %r12, %rsi
movl $0x4, %edx
callq 0x2380
leaq 0x278(%rsp), %rdi
callq 0x2060
testb %al, %al
je 0x432f
cmpl $0x0, 0x18(%r14)
jle 0x4320
xorl %r13d, %r13d
leaq 0x200(%rsp), %r12
movq %r12, %rdi
callq 0x2280
movb %al, (%r15,%r13)
incq %r13
movslq 0x18(%r14), %rax
shlq $0x2, %rax
cmpq %rax, %r13
jl 0x4304
leaq 0x200(%rsp), %rdi
callq 0x2050
jmp 0x43a8
testb %bpl, %bpl
je 0x442b
movl 0x18(%r14), %edx
movq %r15, %rsi
callq 0x4bc2
movq %rsp, %rdi
callq 0x23c0
movq %rsp, %rdi
movq %r12, %rsi
movl $0x4, %edx
callq 0x2100
movslq 0x18(%r14), %rdx
shlq $0x2, %rdx
movq %rsp, %rdi
movq %r15, %rsi
callq 0x22c0
movq %rsp, %rdi
callq 0x2300
movq 0x6c52(%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(%r14), %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, (%rbx)
testl %ebp, %ebp
jle 0x43f6
movl %ebp, %r12d
xorl %r13d, %r13d
movl $0x4, %edi
callq 0x2030
movq (%rbx), %rcx
movq %rax, (%rcx,%r13,8)
incq %r13
cmpq %r13, %r12
jne 0x43dd
movq (%rbx), %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x4db6
movq %r15, %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 0x32be(%rip), %rcx      # 0x76fa
movq %rcx, (%rax)
movq 0x6b7a(%rip), %rsi      # 0xafc0
movq %rax, %rdi
xorl %edx, %edx
callq 0x23d0
jmp 0x4463
movq %rax, %rbx
movq %rsp, %rdi
callq 0x2320
jmp 0x4466
jmp 0x4463
jmp 0x4463
movq %rax, %rbx
leaq 0x200(%rsp), %rdi
callq 0x2070
movq %rbx, %rdi
callq 0x2400
nop