// File: /workspace/github_cpp_makefile_star2_O3/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 0x713c
movl $0x10, %ebx
leaq 0x30(%rsp), %r13
leaq 0x10(%rsp), %rbp
movq %r13, %rdi
movq %r12, %rsi
callq 0x5df6
movq %rbp, %rdi
movq %r13, %rsi
callq 0x7ffc
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x60bf
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
decl %ebx
jne 0x6092
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
sarq $0x5, %rax
cmpq %r15, %rax
jae 0x619b
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 0x6da4
addq $0x20, %r14
cmpq $-0x20, %r14
jne 0x6103
testl %ebx, %ebx
jne 0x612b
movq %r12, %rdi
movl %ebp, %esi
callq 0x5e4b
xorl %r14d, %r14d
movq 0x18(%rsp), %rax
leaq (%rax,%r14), %rsi
addq $-0x200, %rsi           # imm = 0xFE00
movq 0x30(%rsp), %rdi
addq %r14, %rdi
callq 0x44e0
addq $0x20, %r14
cmpq $0x80, %r14
jne 0x612e
movq 0x18(%rsp), %rsi
movq 0x30(%rsp), %rdx
movq 0x38(%rsp), %rcx
movq %r13, %rdi
callq 0x81c2
movq %r12, %rdi
callq 0x6d3c
incl %ebx
cmpl $0x4, %ebx
jne 0x60eb
incl %ebp
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
sarq $0x5, %rax
cmpq %r15, %rax
jb 0x60e9
xorps %xmm0, %xmm0
movq (%rsp), %rax
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
cmpl $0x0, 0x8(%rsp)
js 0x6257
movq 0x8(%rsp), %rax
incl %eax
movq %rax, 0x8(%rsp)
xorl %ebx, %ebx
leaq 0x60(%rsp), %r12
leaq 0x30(%rsp), %r13
leaq 0x8bb1(%rip), %rbp      # 0xed88
xorl %r14d, %r14d
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
movq $0x0, 0x70(%rsp)
xorl %r15d, %r15d
movq 0x10(%rsp), %rsi
addq %rbx, %rsi
addq %r15, %rsi
movq %r12, %rdi
callq 0x6da4
addq $0x20, %r15
cmpq $0x200, %r15            # imm = 0x200
jne 0x61ee
movq %r13, %rdi
movl $0x4, %esi
movl $0x4, %edx
movq %r12, %rcx
callq 0x6bd2
movq (%rsp), %rdi
movq %r13, %rsi
callq 0x84ca
movq %rbp, 0x30(%rsp)
leaq 0x38(%rsp), %rdi
callq 0x746c
movq %r12, %rdi
callq 0x6d3c
incq %r14
addq $0x200, %rbx            # imm = 0x200
cmpq 0x8(%rsp), %r14
jne 0x61da
leaq 0x10(%rsp), %rdi
callq 0x6d3c
movq (%rsp), %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x62a8
movq %rax, %r14
movq %rbp, 0x30(%rsp)
leaq 0x38(%rsp), %rdi
callq 0x746c
jmp 0x62b4
jmp 0x62b1
movq %rax, %r14
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x62d8
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
jmp 0x62d8
movq %rax, %r14
jmp 0x62d8
jmp 0x62cb
jmp 0x62cb
movq %rax, %r14
leaq 0x60(%rsp), %rdi
callq 0x6d3c
movq (%rsp), %rdi
callq 0x71fe
jmp 0x62d8
jmp 0x62cb
movq %rax, %r14
leaq 0x30(%rsp), %rdi
callq 0x6d3c
leaq 0x10(%rsp), %rdi
callq 0x6d3c
movq %r14, %rdi
callq 0x21b0