// File: /workspace/github_c_makefile_star2_O1/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 %rbx
cmpl $0x80, %edx
je 0x6820
cmpl $0x100, %edx            # imm = 0x100
je 0x6819
movl $0xfffff800, %eax       # imm = 0xFFFFF800
cmpl $0xc0, %edx
jne 0x6ac6
movl $0xc, %eax
jmp 0x6825
movl $0xe, %eax
jmp 0x6825
movl $0xa, %eax
movl %eax, (%rdi)
leaq 0x10(%rdi), %rcx
movq %rcx, 0x8(%rdi)
sarl $0x5, %edx
testl %edx, %edx
jle 0x684a
movl %edx, %eax
xorl %edx, %edx
movl (%rsi,%rdx,4), %r8d
movq %r8, (%rcx,%rdx,8)
incq %rdx
cmpq %rdx, %rax
jne 0x683a
movl (%rdi), %edx
xorl %eax, %eax
cmpl $0xa, %edx
je 0x6a2b
cmpl $0xc, %edx
je 0x697f
cmpl $0xe, %edx
jne 0x6ac6
xorl %eax, %eax
leaq 0x1cce(%rip), %rdx      # 0x8540
movl $0xff, %esi
leaq 0x1d12(%rip), %rdi      # 0x8590
movl $0xff, %r8d
xorl %r9d, %r9d
movq (%r9,%rdx), %r11
xorq (%rcx,%r9,8), %r11
movq 0x38(%rcx,%r9,8), %r10
movl %r10d, %ebx
shrl $0x8, %ebx
andl %esi, %ebx
movzbl (%rbx,%rdi), %ebx
xorq %r11, %rbx
movl %r10d, %r11d
shrl $0x10, %r11d
andl %esi, %r11d
movzbl (%r11,%rdi), %r14d
shll $0x8, %r14d
xorq %rbx, %r14
movl %r10d, %r11d
shrl $0x18, %r11d
movzbl (%r11,%rdi), %ebx
shll $0x10, %ebx
movq %r10, %r11
andq %r8, %r11
movzbl (%r11,%rdi), %r11d
shll $0x18, %r11d
xorq %rbx, %r11
xorq %r14, %r11
movq %r11, 0x40(%rcx,%r9,8)
xorq 0x8(%rcx,%r9,8), %r11
movq %r11, 0x48(%rcx,%r9,8)
xorq 0x10(%rcx,%r9,8), %r11
movq %r11, 0x50(%rcx,%r9,8)
xorq 0x18(%rcx,%r9,8), %r11
movq %r11, 0x58(%rcx,%r9,8)
movq %r11, %rbx
andq %r8, %rbx
movzbl (%rbx,%rdi), %ebx
xorq 0x20(%rcx,%r9,8), %rbx
movl %r11d, %r14d
shrl $0x8, %r14d
andl %esi, %r14d
movzbl (%r14,%rdi), %r14d
shll $0x8, %r14d
movl %r11d, %r15d
shrl $0x10, %r15d
andl %esi, %r15d
movzbl (%r15,%rdi), %r15d
shll $0x10, %r15d
xorq %r14, %r15
shrl $0x18, %r11d
movzbl (%r11,%rdi), %r11d
shll $0x18, %r11d
xorq %r15, %r11
xorq %rbx, %r11
movq %r11, 0x60(%rcx,%r9,8)
xorq 0x28(%rcx,%r9,8), %r11
movq %r11, 0x68(%rcx,%r9,8)
xorq 0x30(%rcx,%r9,8), %r11
movq %r11, 0x70(%rcx,%r9,8)
xorq %r10, %r11
movq %r11, 0x78(%rcx,%r9,8)
addq $0x8, %r9
cmpq $0x38, %r9
jne 0x6887
jmp 0x6ac6
leaq 0x1bba(%rip), %rdx      # 0x8540
movl $0xff, %esi
leaq 0x1bfe(%rip), %rdi      # 0x8590
xorl %r8d, %r8d
movq (%r8,%rdx), %r10
xorq (%rcx), %r10
movq 0x28(%rcx), %r9
movl %r9d, %r11d
shrl $0x8, %r11d
andl %esi, %r11d
movzbl (%r11,%rdi), %r11d
xorq %r10, %r11
movl %r9d, %r10d
shrl $0x10, %r10d
andl %esi, %r10d
movzbl (%r10,%rdi), %r10d
shll $0x8, %r10d
xorq %r11, %r10
movl %r9d, %r11d
shrl $0x18, %r11d
movzbl (%r11,%rdi), %r11d
shll $0x10, %r11d
movzbl %r9b, %ebx
movzbl (%rbx,%rdi), %ebx
shll $0x18, %ebx
xorq %r11, %rbx
xorq %r10, %rbx
movq %rbx, 0x30(%rcx)
xorq 0x8(%rcx), %rbx
movq %rbx, 0x38(%rcx)
xorq 0x10(%rcx), %rbx
movq %rbx, 0x40(%rcx)
xorq 0x18(%rcx), %rbx
movq %rbx, 0x48(%rcx)
xorq 0x20(%rcx), %rbx
movq %rbx, 0x50(%rcx)
xorq %r9, %rbx
movq %rbx, 0x58(%rcx)
addq $0x30, %rcx
addq $0x8, %r8
cmpq $0x40, %r8
jne 0x6995
jmp 0x6ac6
leaq 0x1b0e(%rip), %rdx      # 0x8540
movl $0xff, %esi
leaq 0x1b52(%rip), %rdi      # 0x8590
xorl %r8d, %r8d
movq (%r8,%rdx), %r10
xorq (%rcx,%r8,4), %r10
movq 0x18(%rcx,%r8,4), %r9
movl %r9d, %r11d
shrl $0x8, %r11d
andl %esi, %r11d
movzbl (%r11,%rdi), %r11d
xorq %r10, %r11
movl %r9d, %r10d
shrl $0x10, %r10d
andl %esi, %r10d
movzbl (%r10,%rdi), %r10d
shll $0x8, %r10d
xorq %r11, %r10
movl %r9d, %r11d
shrl $0x18, %r11d
movzbl (%r11,%rdi), %r11d
shll $0x10, %r11d
movzbl %r9b, %ebx
movzbl (%rbx,%rdi), %ebx
shll $0x18, %ebx
xorq %r11, %rbx
xorq %r10, %rbx
movq %rbx, 0x20(%rcx,%r8,4)
xorq 0x8(%rcx,%r8,4), %rbx
movq %rbx, 0x28(%rcx,%r8,4)
xorq 0x10(%rcx,%r8,4), %rbx
movq %rbx, 0x30(%rcx,%r8,4)
xorq %r9, %rbx
movq %rbx, 0x38(%rcx,%r8,4)
addq $0x8, %r8
cmpq $0x50, %r8
jne 0x6a41
popq %rbx
popq %r14
popq %r15
retq