// File: /workspace/github_cpp_makefile_star2_O1/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, %r12
movq %rdi, (%rsp)
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rdi
movaps %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movq %rdx, %rax
movq %rdx, 0x8(%rsp)
shll $0x4, %eax
addl $0x10, %eax
movslq %eax, %r15
movq %r15, %rsi
callq 0x7174
movl $0x10, %ebx
leaq 0x30(%rsp), %r13
leaq 0x10(%rsp), %rbp
movq %r13, %rdi
movq %r12, %rsi
callq 0x5eb6
movq %rbp, %rdi
movq %r13, %rsi
callq 0x7f88
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x616e
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
decl %ebx
jne 0x6141
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
sarq $0x5, %rax
cmpq %r15, %rax
jae 0x624a
movl $0x1, %ebp
leaq 0x30(%rsp), %r12
leaq 0x10(%rsp), %r13
xorl %ebx, %ebx
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
movq $-0xa0, %r14
movq 0x18(%rsp), %rsi
addq %r14, %rsi
movq %r12, %rdi
callq 0x6dee
addq $0x20, %r14
cmpq $-0x20, %r14
jne 0x61b2
testl %ebx, %ebx
jne 0x61da
movq %r12, %rdi
movl %ebp, %esi
callq 0x5f04
xorl %r14d, %r14d
movq 0x18(%rsp), %rax
leaq (%rax,%r14), %rsi
addq $-0x200, %rsi           # imm = 0xFE00
movq 0x30(%rsp), %rdi
addq %r14, %rdi
callq 0x464e
addq $0x20, %r14
cmpq $0x80, %r14
jne 0x61dd
movq 0x18(%rsp), %rsi
movq 0x30(%rsp), %rdx
movq 0x38(%rsp), %rcx
movq %r13, %rdi
callq 0x810c
movq %r12, %rdi
callq 0x6d8c
incl %ebx
cmpl $0x4, %ebx
jne 0x619a
incl %ebp
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
sarq $0x5, %rax
cmpq %r15, %rax
jb 0x6198
xorps %xmm0, %xmm0
movq (%rsp), %rax
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
cmpl $0x0, 0x8(%rsp)
js 0x6306
movq 0x8(%rsp), %rax
incl %eax
movq %rax, 0x8(%rsp)
xorl %ebp, %ebp
leaq 0x60(%rsp), %r12
leaq 0x30(%rsp), %r13
leaq 0x8b02(%rip), %rbx      # 0xed88
xorl %r14d, %r14d
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
movq $0x0, 0x70(%rsp)
xorl %r15d, %r15d
movq 0x10(%rsp), %rsi
addq %rbp, %rsi
addq %r15, %rsi
movq %r12, %rdi
callq 0x6dee
addq $0x20, %r15
cmpq $0x200, %r15            # imm = 0x200
jne 0x629d
movq %r13, %rdi
movl $0x4, %esi
movl $0x4, %edx
movq %r12, %rcx
callq 0x6c1e
movq (%rsp), %rdi
movq %r13, %rsi
callq 0x840e
movq %rbx, 0x30(%rsp)
leaq 0x38(%rsp), %rdi
callq 0x7498
movq %r12, %rdi
callq 0x6d8c
incq %r14
addq $0x200, %rbp            # imm = 0x200
cmpq 0x8(%rsp), %r14
jne 0x6289
leaq 0x10(%rsp), %rdi
callq 0x6d8c
movq (%rsp), %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x6356
movq %rax, %r14
leaq 0x38(%rsp), %rdi
movq %rbx, -0x8(%rdi)
callq 0x7498
jmp 0x6362
jmp 0x635f
movq %rax, %r14
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x6386
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
jmp 0x6386
movq %rax, %r14
jmp 0x6386
jmp 0x6379
jmp 0x6379
movq %rax, %r14
leaq 0x60(%rsp), %rdi
callq 0x6d8c
movq (%rsp), %rdi
callq 0x7230
jmp 0x6386
jmp 0x6379
movq %rax, %r14
leaq 0x30(%rsp), %rdi
callq 0x6d8c
leaq 0x10(%rsp), %rdi
callq 0x6d8c
movq %r14, %rdi
callq 0x21b0