// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/aes128poetv1aes4/ref/aes.c
// Function: AES_set_encrypt_key
int AES_set_encrypt_key (const unsigned char *userKey, const int bits,
                         AES_KEY *key)
{

    u32 *rk;
    int i = 0;
    u32 temp;

    if (!userKey || !key) {
        return -1;
    }
    if (bits != 128 && bits != 192 && bits != 256) {
        return -2;
    }

    rk = key->rd_key;

    if (bits == 128) {
        key->rounds = 10;
    } else if (bits == 192) {
        key->rounds = 12;
    } else{
        key->rounds = 14;
    }

    rk[0] = GETU32(userKey     );
    rk[1] = GETU32(userKey +  4);
    rk[2] = GETU32(userKey +  8);
    rk[3] = GETU32(userKey + 12);
    if (bits == 128) {
        while (1) {
            temp  = rk[3];
            rk[4] = rk[0] ^
                    (Te2[(temp >> 16) & 0xff] & 0xff000000) ^
                    (Te3[(temp >>  8) & 0xff] & 0x00ff0000) ^
                    (Te0[(temp      ) & 0xff] & 0x0000ff00) ^
                    (Te1[(temp >> 24)       ] & 0x000000ff) ^
                    rcon[i];
            rk[5] = rk[1] ^ rk[4];
            rk[6] = rk[2] ^ rk[5];
            rk[7] = rk[3] ^ rk[6];
            if (++i == 10) {
                return 0;
            }
            rk += 4;
        }
    }
    rk[4] = GETU32(userKey + 16);
    rk[5] = GETU32(userKey + 20);
    if (bits == 192) {
        while (1) {
            temp = rk[ 5];
            rk[ 6] = rk[ 0] ^
                     (Te2[(temp >> 16) & 0xff] & 0xff000000) ^
                     (Te3[(temp >>  8) & 0xff] & 0x00ff0000) ^
                     (Te0[(temp      ) & 0xff] & 0x0000ff00) ^
                     (Te1[(temp >> 24)       ] & 0x000000ff) ^
                     rcon[i];
            rk[ 7] = rk[ 1] ^ rk[ 6];
            rk[ 8] = rk[ 2] ^ rk[ 7];
            rk[ 9] = rk[ 3] ^ rk[ 8];
            if (++i == 8) {
                return 0;
            }
            rk[10] = rk[ 4] ^ rk[ 9];
            rk[11] = rk[ 5] ^ rk[10];
            rk += 6;
        }
    }
    rk[6] = GETU32(userKey + 24);
    rk[7] = GETU32(userKey + 28);
    if (bits == 256) {
        while (1) {
            temp = rk[ 7];
            rk[ 8] = rk[ 0] ^
                     (Te2[(temp >> 16) & 0xff] & 0xff000000) ^
                     (Te3[(temp >>  8) & 0xff] & 0x00ff0000) ^
                     (Te0[(temp      ) & 0xff] & 0x0000ff00) ^
                     (Te1[(temp >> 24)       ] & 0x000000ff) ^
                     rcon[i];
            rk[ 9] = rk[ 1] ^ rk[ 8];
            rk[10] = rk[ 2] ^ rk[ 9];
            rk[11] = rk[ 3] ^ rk[10];
            if (++i == 7) {
                return 0;
            }
            temp = rk[11];
            rk[12] = rk[ 4] ^
                     (Te2[(temp >> 24)       ] & 0xff000000) ^
                     (Te3[(temp >> 16) & 0xff] & 0x00ff0000) ^
                     (Te0[(temp >>  8) & 0xff] & 0x0000ff00) ^
                     (Te1[(temp      ) & 0xff] & 0x000000ff);
            rk[13] = rk[ 5] ^ rk[12];
            rk[14] = rk[ 6] ^ rk[13];
            rk[15] = rk[ 7] ^ rk[14];

            rk += 8;
        }
    }
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
testq %rdi, %rdi
sete %al
testq %rdx, %rdx
sete %cl
orb %al, %cl
jne 0x1242
cmpl $0x80, %esi
je 0x122e
cmpl $0x100, %esi            # imm = 0x100
je 0x122e
cmpl $0xc0, %esi
jne 0x1260
cmpl $0x80, %esi
jne 0x124c
movl $0xa, 0xf0(%rdx)
jmp 0x1274
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x156e
cmpl $0xc0, %esi
jne 0x126a
movl $0xc, 0xf0(%rdx)
jmp 0x1274
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
jmp 0x156e
movl $0xe, 0xf0(%rdx)
movl (%rdi), %ecx
movbel %ecx, (%rdx)
movl 0x4(%rdi), %r8d
movbel %r8d, 0x4(%rdx)
movl 0x8(%rdi), %r9d
movbel %r9d, 0x8(%rdx)
bswapl %ecx
movl 0xc(%rdi), %r10d
movbel %r10d, 0xc(%rdx)
cmpl $0x80, %esi
jne 0x135e
xorl %eax, %eax
leaq 0x3d51(%rip), %rsi      # 0x5000
movl $0xff, %edi
leaq 0x4145(%rip), %r8       # 0x5400
leaq 0x453e(%rip), %r9       # 0x5800
leaq 0x4937(%rip), %r10      # 0x5c00
leaq 0x4d30(%rip), %r11      # 0x6000
xorl %ebx, %ebx
movl 0xc(%rdx,%rbx,4), %ebp
movl %ebp, %r14d
shrl $0xe, %r14d
andl $0x3fc, %r14d           # imm = 0x3FC
movzbl 0x3(%r14,%rsi), %r14d
shll $0x18, %r14d
xorl %ecx, %r14d
movl %ebp, %ecx
shrl $0x8, %ecx
andl %edi, %ecx
movzbl 0x2(%r8,%rcx,4), %ecx
shll $0x10, %ecx
movl %ebp, %r15d
andl %edi, %r15d
movzbl 0x1(%r9,%r15,4), %r15d
shll $0x8, %r15d
xorl %ecx, %r15d
xorl %r14d, %r15d
movl %ebp, %ecx
shrl $0x18, %ecx
movzbl (%r10,%rcx,4), %ecx
xorl %r15d, %ecx
xorl (%rbx,%r11), %ecx
movl %ecx, 0x10(%rdx,%rbx,4)
movl 0x4(%rdx,%rbx,4), %r14d
xorl %ecx, %r14d
movl %r14d, 0x14(%rdx,%rbx,4)
xorl 0x8(%rdx,%rbx,4), %r14d
movl %r14d, 0x18(%rdx,%rbx,4)
xorl %ebp, %r14d
movl %r14d, 0x1c(%rdx,%rbx,4)
addq $0x4, %rbx
cmpq $0x28, %rbx
jne 0x12d2
jmp 0x156e
bswapl %r8d
bswapl %r9d
movl 0x10(%rdi), %eax
movbel %eax, 0x10(%rdx)
bswapl %r10d
movl 0x14(%rdi), %r11d
movbel %r11d, 0x14(%rdx)
cmpl $0xc0, %esi
jne 0x143f
bswapl %r11d
addq $0x2c, %rdx
leaq 0x3c6d(%rip), %rax      # 0x5000
movl $0xff, %edi
leaq 0x4061(%rip), %rbx      # 0x5400
leaq 0x445a(%rip), %r14      # 0x5800
leaq 0x4853(%rip), %r15      # 0x5c00
leaq 0x4c4c(%rip), %r12      # 0x6000
xorl %r13d, %r13d
movl %r11d, %ebp
shrl $0xe, %ebp
andl $0x3fc, %ebp            # imm = 0x3FC
movzbl 0x3(%rbp,%rax), %ebp
shll $0x18, %ebp
xorl %ecx, %ebp
movl %r11d, %ecx
shrl $0x8, %ecx
andl %edi, %ecx
movzbl 0x2(%rbx,%rcx,4), %ecx
shll $0x10, %ecx
movl %r11d, %esi
andl %edi, %esi
movzbl 0x1(%r14,%rsi,4), %esi
shll $0x8, %esi
xorl %ecx, %esi
xorl %ebp, %esi
movl %r11d, %ecx
shrl $0x18, %ecx
movzbl (%r15,%rcx,4), %ecx
xorl %esi, %ecx
xorl (%r13,%r12), %ecx
movl %ecx, -0x14(%rdx)
xorl %ecx, %r8d
movl %r8d, -0x10(%rdx)
xorl %r8d, %r9d
movl %r9d, -0xc(%rdx)
xorl %r9d, %r10d
movl %r10d, -0x8(%rdx)
cmpq $0x1c, %r13
je 0x156c
movl -0x1c(%rdx), %esi
xorl %r10d, %esi
movl %esi, -0x4(%rdx)
xorl %esi, %r11d
movl %r11d, (%rdx)
addq $0x4, %r13
addq $0x18, %rdx
jmp 0x13b7
movl 0x18(%rdi), %eax
movbel %eax, 0x18(%rdx)
movl 0x1c(%rdi), %edi
movbel %edi, 0x1c(%rdx)
xorl %eax, %eax
cmpl $0x100, %esi            # imm = 0x100
jne 0x156e
bswapl %edi
leaq 0x3b9a(%rip), %rsi      # 0x5000
movl $0xff, %r11d
leaq 0x3f8d(%rip), %rbx      # 0x5400
leaq 0x4386(%rip), %r14      # 0x5800
leaq 0x477f(%rip), %r15      # 0x5c00
xorl %r13d, %r13d
movl %edi, %eax
shrl $0xe, %eax
andl $0x3fc, %eax            # imm = 0x3FC
movzbl 0x3(%rax,%rsi), %eax
shll $0x18, %eax
xorl %ecx, %eax
movl %edi, %ecx
shrl $0x8, %ecx
andl %r11d, %ecx
movzbl 0x2(%rbx,%rcx,4), %ecx
shll $0x10, %ecx
movl %edi, %ebp
andl %r11d, %ebp
movzbl 0x1(%r14,%rbp,4), %ebp
shll $0x8, %ebp
xorl %ecx, %ebp
xorl %eax, %ebp
movl %edi, %eax
shrl $0x18, %eax
movzbl (%r15,%rax,4), %ecx
xorl %ebp, %ecx
leaq 0x4b33(%rip), %rax      # 0x6000
xorl (%r13,%rax), %ecx
movl %ecx, 0x20(%rdx,%r13,8)
xorl %ecx, %r8d
movl %r8d, 0x24(%rdx,%r13,8)
xorl %r8d, %r9d
movl %r9d, 0x28(%rdx,%r13,8)
xorl %r9d, %r10d
movl %r10d, 0x2c(%rdx,%r13,8)
cmpq $0x18, %r13
je 0x156c
movl %r10d, %eax
shrl $0x18, %eax
movzbl 0x3(%rsi,%rax,4), %eax
shll $0x18, %eax
xorl 0x10(%rdx,%r13,8), %eax
movl %r10d, %ebp
shrl $0xe, %ebp
andl $0x3fc, %ebp            # imm = 0x3FC
movzbl 0x2(%rbp,%rbx), %ebp
shll $0x10, %ebp
movl %r10d, %r12d
shrl $0x8, %r12d
andl %r11d, %r12d
movzbl 0x1(%r14,%r12,4), %r12d
shll $0x8, %r12d
xorl %ebp, %r12d
movl %r10d, %ebp
andl %r11d, %ebp
movzbl (%r15,%rbp,4), %ebp
xorl %r12d, %ebp
xorl %eax, %ebp
movl %ebp, 0x30(%rdx,%r13,8)
xorl 0x14(%rdx,%r13,8), %ebp
movl %ebp, 0x34(%rdx,%r13,8)
xorl 0x18(%rdx,%r13,8), %ebp
movl %ebp, 0x38(%rdx,%r13,8)
xorl %ebp, %edi
movl %edi, 0x3c(%rdx,%r13,8)
addq $0x4, %r13
jmp 0x1484
xorl %eax, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq