// File: lmclaramunt[P]AES-Crypto/Cipher.cpp
// Function: Cipher::keyExpansion(unsigned char*, unsigned char**)
void Cipher::keyExpansion(unsigned char* key, unsigned char** keyExpanded) {
    unsigned char temp[4];
    int i = 0;
    
    while(i < Nk) {
        for(int j=0; j<4; j++) keyExpanded[i][j] = key[4*i+j];
        i++;
    }
    i = Nk;
    while (i < 4*(Nr+1)) {
        for(int j=0; j<4; j++) temp[j] = keyExpanded[i-1][j];
        if (i % Nk == 0) {
            unsigned char rcon[4];
            Rcon(i/Nk, rcon);
            RotWord(temp);
            subWord(temp);
            for(int j=0; j<4; j++) temp[j] ^= rcon[j];
        } else if (Nk > 6 && i % Nk == 4) {
            subWord(temp);
        }
        for(int j=0; j<4; j++) 
            keyExpanded[i][j] = keyExpanded[i-Nk][j] ^ temp[j];
        i++;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x40(%rbp)
movl $0x0, -0x20(%rbp)
movq -0x40(%rbp), %rcx
movl -0x20(%rbp), %eax
cmpl 0x18(%rcx), %eax
jge 0x8da8
movl $0x0, -0x24(%rbp)
cmpl $0x4, -0x24(%rbp)
jge 0x8d9d
movq -0x10(%rbp), %rax
movl -0x20(%rbp), %ecx
shll $0x2, %ecx
addl -0x24(%rbp), %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %dl
movq -0x18(%rbp), %rax
movslq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x24(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x8d66
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x8d53
movq -0x40(%rbp), %rax
movl 0x18(%rax), %eax
movl %eax, -0x20(%rbp)
movq -0x40(%rbp), %rcx
movl -0x20(%rbp), %eax
movl 0x1c(%rcx), %ecx
addl $0x1, %ecx
shll $0x2, %ecx
cmpl %ecx, %eax
jge 0x8f00
movl $0x0, -0x28(%rbp)
cmpl $0x4, -0x28(%rbp)
jge 0x8e02
movq -0x18(%rbp), %rax
movl -0x20(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movslq -0x28(%rbp), %rcx
movb (%rax,%rcx), %cl
movslq -0x28(%rbp), %rax
movb %cl, -0x1c(%rbp,%rax)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x8dd1
movq -0x40(%rbp), %rcx
movl -0x20(%rbp), %eax
cltd
idivl 0x18(%rcx)
cmpl $0x0, %edx
jne 0x8e74
movq -0x40(%rbp), %rdi
movl -0x20(%rbp), %eax
cltd
idivl 0x18(%rdi)
movl %eax, %esi
leaq -0x2c(%rbp), %rdx
callq 0x9490
movq -0x40(%rbp), %rdi
leaq -0x1c(%rbp), %rsi
callq 0x9440
movq -0x40(%rbp), %rdi
leaq -0x1c(%rbp), %rsi
callq 0x8f10
movl $0x0, -0x30(%rbp)
cmpl $0x4, -0x30(%rbp)
jge 0x8e72
movslq -0x30(%rbp), %rax
movzbl -0x2c(%rbp,%rax), %edx
movslq -0x30(%rbp), %rax
movzbl -0x1c(%rbp,%rax), %ecx
xorl %edx, %ecx
movb %cl, -0x1c(%rbp,%rax)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x8e49
jmp 0x8e9d
movq -0x40(%rbp), %rax
cmpl $0x6, 0x18(%rax)
jle 0x8e9b
movq -0x40(%rbp), %rcx
movl -0x20(%rbp), %eax
cltd
idivl 0x18(%rcx)
cmpl $0x4, %edx
jne 0x8e9b
movq -0x40(%rbp), %rdi
leaq -0x1c(%rbp), %rsi
callq 0x8f10
jmp 0x8e9d
movl $0x0, -0x34(%rbp)
cmpl $0x4, -0x34(%rbp)
jge 0x8ef2
movq -0x40(%rbp), %rdx
movq -0x18(%rbp), %rax
movl -0x20(%rbp), %ecx
subl 0x18(%rdx), %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movslq -0x34(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movslq -0x34(%rbp), %rcx
movzbl -0x1c(%rbp,%rcx), %ecx
xorl %ecx, %eax
movb %al, %dl
movq -0x18(%rbp), %rax
movslq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x34(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x8ea4
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x8db2
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)