// File: DiplIngFrankGerlach[P]MST/./aes/aes.c
// Function: aes_key_setup
void aes_key_setup(const BYTE key[], WORD w[], int keysize)
{
	int Nb=4,Nr,Nk,idx;
	WORD temp,Rcon[]={0x01000000,0x02000000,0x04000000,0x08000000,0x10000000,0x20000000,
	                  0x40000000,0x80000000,0x1b000000,0x36000000,0x6c000000,0xd8000000,
	                  0xab000000,0x4d000000,0x9a000000};

	switch (keysize) {
		case 128: Nr = 10; Nk = 4; break;
		case 192: Nr = 12; Nk = 6; break;
		case 256: Nr = 14; Nk = 8; break;
		default: return;
	}

	for (idx=0; idx < Nk; ++idx) {
		w[idx] = ((key[4 * idx]) << 24) | ((key[4 * idx + 1]) << 16) |
				   ((key[4 * idx + 2]) << 8) | ((key[4 * idx + 3]));
	}

	for (idx = Nk; idx < Nb * (Nr+1); ++idx) {
		temp = w[idx - 1];
		if ((idx % Nk) == 0)
			temp = SubWord(KE_ROTWORD(temp)) ^ Rcon[(idx-1)/Nk];
		else if (Nk > 6 && (idx % Nk) == 4)
			temp = SubWord(temp);
		w[idx] = w[idx-Nk] ^ temp;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl $0x4, -0x18(%rbp)
movups 0x4cc8(%rip), %xmm0     # 0x813c
movups %xmm0, -0x44(%rbp)
movaps 0x4cb1(%rip), %xmm0     # 0x8130
movaps %xmm0, -0x50(%rbp)
movaps 0x4c96(%rip), %xmm0     # 0x8120
movaps %xmm0, -0x60(%rbp)
movaps 0x4c7b(%rip), %xmm0     # 0x8110
movaps %xmm0, -0x70(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x74(%rbp)
subl $0x80, %eax
je 0x34c0
jmp 0x34a8
movl -0x74(%rbp), %eax
subl $0xc0, %eax
je 0x34d0
jmp 0x34b4
movl -0x74(%rbp), %eax
subl $0x100, %eax            # imm = 0x100
je 0x34e0
jmp 0x34f0
movl $0xa, -0x1c(%rbp)
movl $0x4, -0x20(%rbp)
jmp 0x34f5
movl $0xc, -0x1c(%rbp)
movl $0x6, -0x20(%rbp)
jmp 0x34f5
movl $0xe, -0x1c(%rbp)
movl $0x8, -0x20(%rbp)
jmp 0x34f5
jmp 0x362e
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0x3576
movq -0x8(%rbp), %rax
movl -0x24(%rbp), %ecx
shll $0x2, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %edx
shll $0x18, %edx
movq -0x8(%rbp), %rax
movl -0x24(%rbp), %ecx
shll $0x2, %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
shll $0x10, %eax
orl %eax, %edx
movq -0x8(%rbp), %rax
movl -0x24(%rbp), %ecx
shll $0x2, %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
shll $0x8, %eax
orl %eax, %edx
movq -0x8(%rbp), %rax
movl -0x24(%rbp), %ecx
shll $0x2, %ecx
addl $0x3, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
orl %eax, %edx
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x34fc
movl -0x20(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
movl -0x18(%rbp), %ecx
movl -0x1c(%rbp), %edx
addl $0x1, %edx
imull %edx, %ecx
cmpl %ecx, %eax
jge 0x362e
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x28(%rbp)
movl -0x24(%rbp), %eax
cltd
idivl -0x20(%rbp)
cmpl $0x0, %edx
jne 0x35e3
movl -0x28(%rbp), %edi
shll $0x8, %edi
movl -0x28(%rbp), %eax
shrl $0x18, %eax
orl %eax, %edi
callq 0x3b40
movl %eax, -0x78(%rbp)
movl -0x24(%rbp), %eax
subl $0x1, %eax
cltd
idivl -0x20(%rbp)
movl %eax, %ecx
movl -0x78(%rbp), %eax
movslq %ecx, %rcx
xorl -0x70(%rbp,%rcx,4), %eax
movl %eax, -0x28(%rbp)
jmp 0x3602
cmpl $0x6, -0x20(%rbp)
jle 0x3600
movl -0x24(%rbp), %eax
cltd
idivl -0x20(%rbp)
cmpl $0x4, %edx
jne 0x3600
movl -0x28(%rbp), %edi
callq 0x3b40
movl %eax, -0x28(%rbp)
jmp 0x3602
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
subl -0x20(%rbp), %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edx
xorl -0x28(%rbp), %edx
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x357c
addq $0x80, %rsp
popq %rbp
retq
nopw (%rax,%rax)