// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/aes256otrsv3/ref/t-aes_enc_only.c
// Function: KeySchedule
int KeySchedule(int skeylen, int mode, const unsigned char *skey)
{
	int				i;
	int				Nk, Nr;
	unsigned int	tmp;

	/* Set Nk, Nr from skeylen*/
	switch(skeylen)
	{
		case 128:
			Nk = 4;
			Nr = 10;
			break;
		case 192:
			Nk = 6;
			Nr = 12;
			break;
		case 256:
			Nk = 8;
			Nr = 14;
			break;
		default:
			/* key-length error */
			return INVALID_PARAMETER;
	}
	
	/* enc/dec mode error */
	if((mode != ENC) && (mode != DEC))
	{
		return INVALID_PARAMETER;
	}

	/* round-key generation for enc mode */
	for(i=0; i<Nk; i++)
	{
		ekey[i] = (skey[4*i] << 24) | (skey[4*i+1] << 16) | (skey[4*i+2] << 8) | skey[4*i+3];
	}

	for(i=Nk; i<4*(Nr+1); i++)
	{
		if((i % Nk) == 0)
		{
			tmp  = S[(ekey[i-1] >> 16) & 0xff] << 24;
			tmp |= S[(ekey[i-1] >>  8) & 0xff] << 16;
			tmp |= S[ ekey[i-1]        & 0xff] <<  8;
			tmp |= S[ ekey[i-1] >> 24        ];
			ekey[i] = ekey[i-Nk] ^ tmp ^ RC[i/Nk-1];
		}
		else if((Nk == 8) && ((i % Nk) == 4))
		{
			tmp  = S[ ekey[i-1] >> 24        ] << 24;
			tmp |= S[(ekey[i-1] >> 16) & 0xff] << 16;
			tmp |= S[(ekey[i-1] >>  8) & 0xff] <<  8;
			tmp |= S[ ekey[i-1]        & 0xff];
			ekey[i] = ekey[i-Nk] ^ tmp;
		}
		else
		{
			ekey[i] = ekey[i-Nk] ^ ekey[i-1];
		}
	}

	/* round-key generation for dec mode */
	if(mode == DEC)
	{ return INVALID_PARAMETER;
	}

	return SUCCESS;
}

// Assembly:
movl $0x1, %eax
cmpl $0x80, %edi
je 0x1e04
cmpl $0x100, %edi            # imm = 0x100
je 0x1def
cmpl $0xc0, %edi
jne 0x1f53
movabsq $0x34, %rdi
movl $0x6, %r8d
jmp 0x1e14
movabsq $0x3c, %rdi
movl $0x8, %r8d
movb $0x1, %r9b
jmp 0x1e17
movabsq $0x2c, %rdi
movl $0x4, %r8d
xorl %r9d, %r9d
cmpl $0x1, %esi
ja 0x1f53
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movl %r8d, %ecx
leal (,%r8,4), %eax
xorl %r11d, %r11d
leaq 0x4401(%rip), %r10      # 0x6240
cmpq %r11, %rax
je 0x1e54
movl (%rdx,%r11), %ebx
movbel %ebx, (%r11,%r10)
addq $0x4, %r11
jmp 0x1e3f
movl $0xff, %r11d
movq 0x318f(%rip), %rbx      # 0x4ff0
movq 0x3168(%rip), %r14      # 0x4fd0
xorb $0x1, %r9b
movq %r10, %r15
cmpq %rcx, %rdi
je 0x1f41
movl %ecx, %eax
xorl %edx, %edx
divl %r8d
testl %edx, %edx
je 0x1ed8
cmpl $0x4, %edx
setne %al
orb %r9b, %al
jne 0x1f29
movl -0x4(%r10,%rcx,4), %eax
movl %eax, %edx
movl %eax, %r12d
movzbl %al, %r13d
shrl $0x18, %eax
movzbl (%rbx,%rax), %eax
shll $0x18, %eax
shrl $0x10, %edx
andl %r11d, %edx
movzbl (%rbx,%rdx), %edx
shll $0x10, %edx
orl %eax, %edx
shrl $0x8, %r12d
andl %r11d, %r12d
movzbl (%rbx,%r12), %eax
shll $0x8, %eax
orl %edx, %eax
movzbl (%rbx,%r13), %edx
orl %eax, %edx
xorl -0x20(%r10,%rcx,4), %edx
jmp 0x1f31
movl -0x4(%r10,%rcx,4), %edx
movl %edx, %r12d
shrl $0x10, %r12d
andl %r11d, %r12d
movzbl (%rbx,%r12), %ebp
shll $0x18, %ebp
movl %edx, %r12d
shrl $0x8, %r12d
andl %r11d, %r12d
movzbl (%rbx,%r12), %r12d
shll $0x10, %r12d
orl %ebp, %r12d
movzbl %dl, %r13d
movzbl (%rbx,%r13), %ebp
shll $0x8, %ebp
orl %r12d, %ebp
shrl $0x18, %edx
movzbl (%rbx,%rdx), %edx
orl %ebp, %edx
xorl (%r15), %edx
cltq
xorl -0x4(%r14,%rax,4), %edx
jmp 0x1f31
movl -0x4(%r10,%rcx,4), %edx
xorl (%r15), %edx
movl %edx, (%r10,%rcx,4)
incq %rcx
addq $0x4, %r15
jmp 0x1e6f
xorl %eax, %eax
cmpl $0x1, %esi
sete %al
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq