// File: /workspace/github_c_makefile_star2_O1/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 %r12
pushq %rbx
testq %rdi, %rdi
sete %cl
testq %rdx, %rdx
sete %r8b
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
orb %cl, %r8b
jne 0x1345
cmpl $0x80, %esi
je 0x1230
cmpl $0x100, %esi            # imm = 0x100
je 0x1230
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
cmpl $0xc0, %esi
jne 0x1345
cmpl $0x80, %esi
jne 0x1244
movl $0xa, 0xf0(%rdx)
jmp 0x1262
cmpl $0xc0, %esi
jne 0x1258
movl $0xc, 0xf0(%rdx)
jmp 0x1262
movl $0xe, 0xf0(%rdx)
movl (%rdi), %eax
movbel %eax, (%rdx)
movl 0x4(%rdi), %eax
movbel %eax, 0x4(%rdx)
movl 0x8(%rdi), %eax
movbel %eax, 0x8(%rdx)
movl 0xc(%rdi), %eax
movbel %eax, 0xc(%rdx)
cmpl $0x80, %esi
jne 0x134e
xorl %eax, %eax
leaq 0x3d6b(%rip), %rcx      # 0x5000
movl $0xff, %esi
leaq 0x415f(%rip), %rdi      # 0x5400
leaq 0x4558(%rip), %r8       # 0x5800
leaq 0x4951(%rip), %r9       # 0x5c00
leaq 0x4d4a(%rip), %r10      # 0x6000
xorl %r11d, %r11d
movl 0xc(%rdx,%r11,4), %ebx
movl %ebx, %r14d
shrl $0xe, %r14d
andl $0x3fc, %r14d           # imm = 0x3FC
movzbl 0x3(%r14,%rcx), %ebp
shll $0x18, %ebp
xorl (%rdx,%r11,4), %ebp
movl %ebx, %r14d
shrl $0x8, %r14d
andl %esi, %r14d
movzbl 0x2(%rdi,%r14,4), %r14d
shll $0x10, %r14d
movl %ebx, %r15d
andl %esi, %r15d
movzbl 0x1(%r8,%r15,4), %r15d
shll $0x8, %r15d
xorl %r14d, %r15d
movl %ebx, %r14d
shrl $0x18, %r14d
movzbl (%r9,%r14,4), %r14d
xorl %r15d, %r14d
xorl %ebp, %r14d
xorl (%r11,%r10), %r14d
movl %r14d, 0x10(%rdx,%r11,4)
xorl 0x4(%rdx,%r11,4), %r14d
movl %r14d, 0x14(%rdx,%r11,4)
xorl 0x8(%rdx,%r11,4), %r14d
movl %r14d, 0x18(%rdx,%r11,4)
xorl %ebx, %r14d
movl %r14d, 0x1c(%rdx,%r11,4)
addq $0x4, %r11
cmpq $0x28, %r11
jne 0x12b9
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movl 0x10(%rdi), %eax
movbel %eax, 0x10(%rdx)
movl 0x14(%rdi), %eax
movbel %eax, 0x14(%rdx)
cmpl $0xc0, %esi
jne 0x142c
addq $0x2c, %rdx
xorl %eax, %eax
leaq 0x3c89(%rip), %rcx      # 0x5000
movl $0xff, %esi
leaq 0x407d(%rip), %rdi      # 0x5400
leaq 0x4476(%rip), %r8       # 0x5800
leaq 0x486f(%rip), %r9       # 0x5c00
leaq 0x4c68(%rip), %r10      # 0x6000
xorl %r11d, %r11d
movl -0x18(%rdx), %ebx
movl %ebx, %r14d
shrl $0xe, %r14d
andl $0x3fc, %r14d           # imm = 0x3FC
movzbl 0x3(%r14,%rcx), %r14d
shll $0x18, %r14d
xorl -0x2c(%rdx), %r14d
movl %ebx, %r15d
shrl $0x8, %r15d
andl %esi, %r15d
movzbl 0x2(%rdi,%r15,4), %ebp
shll $0x10, %ebp
movl %ebx, %r15d
andl %esi, %r15d
movzbl 0x1(%r8,%r15,4), %r15d
shll $0x8, %r15d
xorl %ebp, %r15d
movl %ebx, %r12d
shrl $0x18, %r12d
movzbl (%r9,%r12,4), %ebp
xorl %r15d, %ebp
xorl %r14d, %ebp
xorl (%r11,%r10), %ebp
movl %ebp, -0x14(%rdx)
xorl -0x28(%rdx), %ebp
movl %ebp, -0x10(%rdx)
xorl -0x24(%rdx), %ebp
movl %ebp, -0xc(%rdx)
xorl -0x20(%rdx), %ebp
movl %ebp, -0x8(%rdx)
cmpq $0x1c, %r11
je 0x1345
xorl -0x1c(%rdx), %ebp
movl %ebp, -0x4(%rdx)
xorl %ebx, %ebp
movl %ebp, (%rdx)
addq $0x4, %r11
addq $0x18, %rdx
jmp 0x139b
movl 0x18(%rdi), %eax
movbel %eax, 0x18(%rdx)
movl 0x1c(%rdi), %eax
movbel %eax, 0x1c(%rdx)
xorl %eax, %eax
cmpl $0x100, %esi            # imm = 0x100
jne 0x1345
xorl %eax, %eax
leaq 0x3bad(%rip), %rcx      # 0x5000
movl $0xff, %esi
leaq 0x3fa1(%rip), %rdi      # 0x5400
leaq 0x439a(%rip), %r8       # 0x5800
leaq 0x4793(%rip), %r9       # 0x5c00
leaq 0x4b8c(%rip), %r10      # 0x6000
xorl %r11d, %r11d
movl 0x1c(%rdx,%r11,8), %ebx
movl %ebx, %r14d
shrl $0xe, %r14d
andl $0x3fc, %r14d           # imm = 0x3FC
movzbl 0x3(%r14,%rcx), %ebp
shll $0x18, %ebp
xorl (%rdx,%r11,8), %ebp
movl %ebx, %r14d
shrl $0x8, %r14d
andl %esi, %r14d
movzbl 0x2(%rdi,%r14,4), %r14d
shll $0x10, %r14d
movl %ebx, %r15d
andl %esi, %r15d
movzbl 0x1(%r8,%r15,4), %r15d
shll $0x8, %r15d
xorl %r14d, %r15d
movl %ebx, %r14d
shrl $0x18, %r14d
movzbl (%r9,%r14,4), %r14d
xorl %r15d, %r14d
xorl %ebp, %r14d
xorl (%r11,%r10), %r14d
movl %r14d, 0x20(%rdx,%r11,8)
xorl 0x4(%rdx,%r11,8), %r14d
movl %r14d, 0x24(%rdx,%r11,8)
xorl 0x8(%rdx,%r11,8), %r14d
movl %r14d, 0x28(%rdx,%r11,8)
xorl 0xc(%rdx,%r11,8), %r14d
movl %r14d, 0x2c(%rdx,%r11,8)
cmpq $0x18, %r11
je 0x1345
movl %r14d, %r15d
shrl $0x18, %r15d
movzbl 0x3(%rcx,%r15,4), %ebp
shll $0x18, %ebp
xorl 0x10(%rdx,%r11,8), %ebp
movl %r14d, %r15d
shrl $0xe, %r15d
andl $0x3fc, %r15d           # imm = 0x3FC
movzbl 0x2(%r15,%rdi), %r15d
shll $0x10, %r15d
movl %r14d, %r12d
shrl $0x8, %r12d
andl %esi, %r12d
movzbl 0x1(%r8,%r12,4), %r12d
shll $0x8, %r12d
xorl %r15d, %r12d
andl %esi, %r14d
movzbl (%r9,%r14,4), %r14d
xorl %r12d, %r14d
xorl %ebp, %r14d
movl %r14d, 0x30(%rdx,%r11,8)
xorl 0x14(%rdx,%r11,8), %r14d
movl %r14d, 0x34(%rdx,%r11,8)
xorl 0x18(%rdx,%r11,8), %r14d
movl %r14d, 0x38(%rdx,%r11,8)
xorl %ebx, %r14d
movl %r14d, 0x3c(%rdx,%r11,8)
addq $0x4, %r11
jmp 0x1477