// File: /workspace/github_c_makefile_star2_O3/bloomsource[P]dns_update/aes.c
// Function: aes_setkey_enc
int aes_setkey_enc( aes_context *ctx, const unsigned char *key, int keysize )
{
    int i;
    unsigned long *RK;

#if !defined(AES_ROM_TABLES)
    if( aes_init_done == 0 )
    {
        aes_gen_tables();
        aes_init_done = 1;
    }
#endif

    switch( keysize )
    {
        case 128: ctx->nr = 10; break;
        case 192: ctx->nr = 12; break;
        case 256: ctx->nr = 14; break;
        default : return( ERR_AES_INVALID_KEY_LENGTH );
    }


    ctx->rk = RK = ctx->buf;

    for( i = 0; i < (keysize >> 5); i++ )
    {
        GET_ULONG_LE( RK[i], key, i << 2 );
    }

    switch( ctx->nr )
    {
        case 10:

            for( i = 0; i < 10; i++, RK += 4 )
            {
                RK[4]  = RK[0] ^ RCON[i] ^
                ( (unsigned long) FSb[ ( RK[3] >>  8 ) & 0xFF ]       ) ^
                ( (unsigned long) FSb[ ( RK[3] >> 16 ) & 0xFF ] <<  8 ) ^
                ( (unsigned long) FSb[ ( RK[3] >> 24 ) & 0xFF ] << 16 ) ^
                ( (unsigned long) FSb[ ( RK[3]       ) & 0xFF ] << 24 );

                RK[5]  = RK[1] ^ RK[4];
                RK[6]  = RK[2] ^ RK[5];
                RK[7]  = RK[3] ^ RK[6];
            }
            break;

        case 12:

            for( i = 0; i < 8; i++, RK += 6 )
            {
                RK[6]  = RK[0] ^ RCON[i] ^
                ( (unsigned long) FSb[ ( RK[5] >>  8 ) & 0xFF ]       ) ^
                ( (unsigned long) FSb[ ( RK[5] >> 16 ) & 0xFF ] <<  8 ) ^
                ( (unsigned long) FSb[ ( RK[5] >> 24 ) & 0xFF ] << 16 ) ^
                ( (unsigned long) FSb[ ( RK[5]       ) & 0xFF ] << 24 );

                RK[7]  = RK[1] ^ RK[6];
                RK[8]  = RK[2] ^ RK[7];
                RK[9]  = RK[3] ^ RK[8];
                RK[10] = RK[4] ^ RK[9];
                RK[11] = RK[5] ^ RK[10];
            }
            break;

        case 14:

            for( i = 0; i < 7; i++, RK += 8 )
            {
                RK[8]  = RK[0] ^ RCON[i] ^
                ( (unsigned long) FSb[ ( RK[7] >>  8 ) & 0xFF ]       ) ^
                ( (unsigned long) FSb[ ( RK[7] >> 16 ) & 0xFF ] <<  8 ) ^
                ( (unsigned long) FSb[ ( RK[7] >> 24 ) & 0xFF ] << 16 ) ^
                ( (unsigned long) FSb[ ( RK[7]       ) & 0xFF ] << 24 );

                RK[9]  = RK[1] ^ RK[8];
                RK[10] = RK[2] ^ RK[9];
                RK[11] = RK[3] ^ RK[10];

                RK[12] = RK[4] ^
                ( (unsigned long) FSb[ ( RK[11]       ) & 0xFF ]       ) ^
                ( (unsigned long) FSb[ ( RK[11] >>  8 ) & 0xFF ] <<  8 ) ^
                ( (unsigned long) FSb[ ( RK[11] >> 16 ) & 0xFF ] << 16 ) ^
                ( (unsigned long) FSb[ ( RK[11] >> 24 ) & 0xFF ] << 24 );

                RK[13] = RK[5] ^ RK[12];
                RK[14] = RK[6] ^ RK[13];
                RK[15] = RK[7] ^ RK[14];
            }
            break;

        default:

            break;
    }

    return( 0 );
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
cmpl $0x80, %edx
je 0x6796
cmpl $0x100, %edx            # imm = 0x100
je 0x678f
movl $0xfffff800, %eax       # imm = 0xFFFFF800
cmpl $0xc0, %edx
jne 0x6a49
movl $0xc, %eax
jmp 0x679b
movl $0xe, %eax
jmp 0x679b
movl $0xa, %eax
movl %eax, (%rdi)
leaq 0x10(%rdi), %rcx
movq %rcx, 0x8(%rdi)
shrl $0x5, %edx
xorl %r8d, %r8d
movl (%rsi,%r8,4), %r9d
movq %r9, (%rcx,%r8,8)
incq %r8
cmpq %r8, %rdx
jne 0x67ab
addl $-0x8, %eax
leaq 0x1d7b(%rip), %rcx      # 0x8540
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
xorl %eax, %eax
jmp 0x6a49
movq 0x10(%rdi), %r10
xorl %eax, %eax
leaq 0x1d7e(%rip), %rcx      # 0x8560
movl $0xff, %edx
leaq 0x1dc2(%rip), %rsi      # 0x85b0
movl $0xff, %r8d
xorl %r9d, %r9d
xorq (%r9,%rcx), %r10
movq 0x48(%rdi,%r9,8), %r11
movl %r11d, %ebx
shrl $0x8, %ebx
andl %edx, %ebx
movzbl (%rbx,%rsi), %ebx
xorq %r10, %rbx
movl %r11d, %r10d
shrl $0x10, %r10d
andl %edx, %r10d
movzbl (%r10,%rsi), %r14d
shll $0x8, %r14d
xorq %rbx, %r14
movl %r11d, %r10d
shrl $0x18, %r10d
movzbl (%r10,%rsi), %ebx
shll $0x10, %ebx
movq %r11, %r10
andq %r8, %r10
movzbl (%r10,%rsi), %r10d
shll $0x18, %r10d
xorq %rbx, %r10
xorq %r14, %r10
movq %r10, 0x50(%rdi,%r9,8)
movq 0x18(%rdi,%r9,8), %rbx
xorq %r10, %rbx
movq %rbx, 0x58(%rdi,%r9,8)
xorq 0x20(%rdi,%r9,8), %rbx
movq %rbx, 0x60(%rdi,%r9,8)
xorq 0x28(%rdi,%r9,8), %rbx
movq %rbx, 0x68(%rdi,%r9,8)
movq %rbx, %r14
andq %r8, %r14
movzbl (%r14,%rsi), %r14d
xorq 0x30(%rdi,%r9,8), %r14
movl %ebx, %r15d
shrl $0x8, %r15d
andl %edx, %r15d
movzbl (%r15,%rsi), %r15d
shll $0x8, %r15d
movl %ebx, %r12d
shrl $0x10, %r12d
andl %edx, %r12d
movzbl (%r12,%rsi), %r12d
shll $0x10, %r12d
xorq %r15, %r12
shrl $0x18, %ebx
movzbl (%rbx,%rsi), %ebx
shll $0x18, %ebx
xorq %r12, %rbx
xorq %r14, %rbx
movq %rbx, 0x70(%rdi,%r9,8)
xorq 0x38(%rdi,%r9,8), %rbx
movq %rbx, 0x78(%rdi,%r9,8)
xorq 0x40(%rdi,%r9,8), %rbx
movq %rbx, 0x80(%rdi,%r9,8)
xorq %r11, %rbx
movq %rbx, 0x88(%rdi,%r9,8)
addq $0x8, %r9
cmpq $0x38, %r9
jne 0x67f7
jmp 0x6a49
movq 0x10(%rdi), %r10
xorl %eax, %eax
leaq 0x1c61(%rip), %rcx      # 0x8560
movl $0xff, %edx
leaq 0x1ca5(%rip), %rsi      # 0x85b0
xorl %r8d, %r8d
xorq (%r8,%rcx), %r10
movq 0x28(%rdi,%r8,4), %r9
movl %r9d, %r11d
shrl $0x8, %r11d
andl %edx, %r11d
movzbl (%r11,%rsi), %r11d
xorq %r10, %r11
movl %r9d, %r10d
shrl $0x10, %r10d
andl %edx, %r10d
movzbl (%r10,%rsi), %ebx
shll $0x8, %ebx
xorq %r11, %rbx
movl %r9d, %r10d
shrl $0x18, %r10d
movzbl (%r10,%rsi), %r11d
shll $0x10, %r11d
movzbl %r9b, %r10d
movzbl (%r10,%rsi), %r10d
shll $0x18, %r10d
xorq %r11, %r10
xorq %rbx, %r10
movq %r10, 0x30(%rdi,%r8,4)
movq 0x18(%rdi,%r8,4), %r11
xorq %r10, %r11
movq %r11, 0x38(%rdi,%r8,4)
xorq 0x20(%rdi,%r8,4), %r11
movq %r11, 0x40(%rdi,%r8,4)
xorq %r9, %r11
movq %r11, 0x48(%rdi,%r8,4)
addq $0x8, %r8
cmpq $0x50, %r8
jne 0x690e
jmp 0x6a49
movq 0x10(%rdi), %r9
addq $0x68, %rdi
xorl %eax, %eax
leaq 0x1bb7(%rip), %rcx      # 0x8560
movl $0xff, %edx
leaq 0x1bfb(%rip), %rsi      # 0x85b0
xorl %r8d, %r8d
xorq (%r8,%rcx), %r9
movq -0x30(%rdi), %r10
movl %r10d, %r11d
shrl $0x8, %r11d
andl %edx, %r11d
movzbl (%r11,%rsi), %r11d
xorq %r9, %r11
movl %r10d, %r9d
shrl $0x10, %r9d
andl %edx, %r9d
movzbl (%r9,%rsi), %ebx
shll $0x8, %ebx
xorq %r11, %rbx
movl %r10d, %r9d
shrl $0x18, %r9d
movzbl (%r9,%rsi), %r11d
shll $0x10, %r11d
movzbl %r10b, %r9d
movzbl (%r9,%rsi), %r9d
shll $0x18, %r9d
xorq %r11, %r9
xorq %rbx, %r9
movq %r9, -0x28(%rdi)
movq -0x50(%rdi), %r11
xorq %r9, %r11
movq %r11, -0x20(%rdi)
xorq -0x48(%rdi), %r11
movq %r11, -0x18(%rdi)
xorq -0x40(%rdi), %r11
movq %r11, -0x10(%rdi)
xorq -0x38(%rdi), %r11
movq %r11, -0x8(%rdi)
xorq %r10, %r11
movq %r11, (%rdi)
addq $0x8, %r8
addq $0x30, %rdi
cmpq $0x40, %r8
jne 0x69b8
popq %rbx
popq %r12
popq %r14
popq %r15
retq