// File: /workspace/github_cpp_makefile_star2_O2/permutationlock[P]aes_math/lib/aes.cpp
// Function: expandKey(std::vector<unsigned char, std::allocator<unsigned char>>, int)
vector<QSMatrix<GaloisPolynomial>> expandKey(vector<unsigned char> key, int rounds){
    vector<GaloisPolynomial> keyBytes;
    keyBytes.reserve(16*(rounds+1));    // Reserve space so inserts are faster
    
    // Just copy in key for first 16 bytes
    for(int i=0; i<16; i++){
        keyBytes.push_back(extractPoly(key));
    }
    
    int iteration = 1;
    
    // Continue until we have a 128 bit key for each round + an initial key
    while(keyBytes.size()<16*(rounds+1)){
        for(int w=0; w<4; w++){
            // Copy last 4 bytes
            vector<GaloisPolynomial> word;
            for(int i=0; i<4; i++){
                word.push_back(keyBytes[keyBytes.size()-5+i]);
            }
            
            // Apply core operations if first word
            if(w==0) keyExpandCore(word, iteration);
            
            // Add each byte with that 16 bytes back
            for(int i=0; i<4; i++){
                word[i] += keyBytes[keyBytes.size()-16+i];
            }
            
            // Append
            keyBytes.insert(keyBytes.end(), word.begin(), word.end());
        }
        
        iteration += 1;
    }
    
    // Convert to vector of matrices
    vector<QSMatrix<GaloisPolynomial>> keyMatrices;
    
    for(int i=0; i<rounds+1; i++){
        vector<GaloisPolynomial> v;
        for(int b=0; b<16; b++){
            v.push_back(keyBytes[i*16+b]);
        }
        keyMatrices.push_back(QSMatrix<GaloisPolynomial>(4, 4, v));
    }
    
    return keyMatrices;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rsi, %r15
movq %rdi, (%rsp)
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rdi
movaps %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
movl %edx, 0x8(%rsp)
movl %edx, %eax
shll $0x4, %eax
addl $0x10, %eax
movslq %eax, %r14
movq %r14, %rsi
callq 0x5d22
pushq $0x10
popq %rbp
leaq 0x30(%rsp), %r12
leaq 0x10(%rsp), %r13
subl $0x1, %ebp
jb 0x50de
movq %r12, %rdi
movq %r15, %rsi
callq 0x4ef2
movq %r13, %rdi
movq %r12, %rsi
callq 0x6ace
movq %r12, %rdi
callq 0x4360
jmp 0x50b9
pushq $0x1
popq %r15
leaq 0x30(%rsp), %r12
leaq 0x10(%rsp), %r13
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
sarq $0x5, %rax
cmpq %r14, %rax
jae 0x51a4
xorl %ebp, %ebp
cmpl $0x4, %ebp
je 0x519c
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
andq $0x0, 0x40(%rsp)
movq $-0xa0, %rbx
cmpq $-0x20, %rbx
je 0x513f
movq 0x18(%rsp), %rsi
addq %rbx, %rsi
movq %r12, %rdi
callq 0x59ee
addq $0x20, %rbx
jmp 0x5123
testl %ebp, %ebp
jne 0x514e
movq %r12, %rdi
movl %r15d, %esi
callq 0x4f2d
xorl %ebx, %ebx
movq 0x18(%rsp), %rsi
cmpq $0x80, %rbx
je 0x517b
addq %rbx, %rsi
addq $-0x200, %rsi           # imm = 0xFE00
movq 0x30(%rsp), %rdi
addq %rbx, %rdi
callq 0x3952
addq $0x20, %rbx
jmp 0x5150
movq 0x30(%rsp), %rdx
movq 0x38(%rsp), %rcx
movq %r13, %rdi
callq 0x5db4
movq %r12, %rdi
callq 0x59b8
incl %ebp
jmp 0x5105
incl %r15d
jmp 0x50ec
xorps %xmm0, %xmm0
movq (%rsp), %rax
movups %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movslq 0x8(%rsp), %rax
movq %rax, 0x8(%rsp)
xorl %ebx, %ebx
leaq 0x30(%rsp), %r14
pushq $0x4
popq %rbp
leaq 0x60(%rsp), %r15
cmpq 0x8(%rsp), %rbx
jg 0x5244
movl %ebx, %r13d
andl $0xfffffff, %r13d       # imm = 0xFFFFFFF
shlq $0x9, %r13
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
andq $0x0, 0x70(%rsp)
xorl %r12d, %r12d
cmpq $0x200, %r12            # imm = 0x200
je 0x5214
movq 0x10(%rsp), %rsi
addq %r13, %rsi
addq %r12, %rsi
movq %r15, %rdi
callq 0x59ee
addq $0x20, %r12
jmp 0x51f2
movq %r14, %rdi
movl %ebp, %esi
movl %ebp, %edx
movq %r15, %rcx
callq 0x58a4
movq (%rsp), %rdi
movq %r14, %rsi
callq 0x6f06
movq %r14, %rdi
callq 0x59da
movq %r15, %rdi
callq 0x59b8
incq %rbx
jmp 0x51cc
leaq 0x10(%rsp), %rdi
callq 0x59b8
movq (%rsp), %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5274
jmp 0x528c
movq %rax, %r14
leaq 0x30(%rsp), %rdi
callq 0x59da
jmp 0x528f
movq %rax, %r14
jmp 0x52b3
movq %rax, %r14
leaq 0x30(%rsp), %rdi
callq 0x4360
jmp 0x52b3
jmp 0x52a6
jmp 0x52a6
movq %rax, %r14
leaq 0x60(%rsp), %rdi
callq 0x59b8
movq (%rsp), %rdi
callq 0x5dd4
jmp 0x52b3
jmp 0x52a6
movq %rax, %r14
leaq 0x30(%rsp), %rdi
callq 0x59b8
leaq 0x10(%rsp), %rdi
callq 0x59b8
movq %r14, %rdi
callq 0x21c0