// File: /workspace/github_c_makefile_star2_O3/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 0x222e
cmpl $0x100, %edi            # imm = 0x100
je 0x221e
cmpl $0xc0, %edi
jne 0x2371
movl $0x34, %edi
movl $0x6, %r8d
jmp 0x2239
movl $0x3c, %edi
movl $0x8, %r8d
movb $0x1, %r9b
jmp 0x223c
movl $0x2c, %edi
movl $0x4, %r8d
xorl %r9d, %r9d
cmpl $0x1, %esi
ja 0x2371
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movl %r8d, %ecx
leal (,%r8,4), %eax
xorl %r11d, %r11d
leaq 0x3fdc(%rip), %r10      # 0x6240
movl (%rdx,%r11), %ebx
movbel %ebx, (%r11,%r10)
addq $0x4, %r11
cmpq %r11, %rax
jne 0x2264
movl $0xff, %r11d
movq 0x2d6c(%rip), %rbx      # 0x4ff0
movq 0x2d45(%rip), %r14      # 0x4fd0
xorb $0x1, %r9b
movq %r10, %r15
movl %ecx, %eax
xorl %edx, %edx
divl %r8d
testl %edx, %edx
je 0x22f2
cmpl $0x4, %edx
setne %al
orb %r9b, %al
jne 0x2343
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 0x234b
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 0x234b
movl -0x4(%r10,%rcx,4), %edx
xorl (%r15), %edx
movl %edx, (%r10,%rcx,4)
incq %rcx
addq $0x4, %r15
cmpq %rcx, %rdi
jne 0x2292
xorl %eax, %eax
cmpl $0x1, %esi
sete %al
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq