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

    switch (keysize) {
        case 128: ctx->nr = 10;
            break;
        case 192: ctx->nr = 12;
            break;
        case 256: ctx->nr = 14;
            break;
        default: return ( POLARSSL_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 0x1297
cmpl $0x100, %edx            # imm = 0x100
je 0x1290
cmpl $0xc0, %edx
jne 0x12d4
movl $0xc, %eax
jmp 0x129c
movl $0xe, %eax
jmp 0x129c
movl $0xa, %eax
movl %eax, (%rdi)
leaq 0x10(%rdi), %rcx
movq %rcx, 0x8(%rdi)
shrl $0x5, %edx
xorl %edi, %edi
cmpq %rdi, %rdx
je 0x12bd
movl (%rsi,%rdi,4), %r8d
movq %r8, (%rcx,%rdi,8)
incq %rdi
jmp 0x12ab
addl $-0x8, %eax
leaq 0x5d49(%rip), %rdx      # 0x7010
movslq (%rdx,%rax,4), %rax
addq %rdx, %rax
jmpq *%rax
xorl %eax, %eax
jmp 0x12d9
movl $0xffffffe0, %eax       # imm = 0xFFFFFFE0
popq %rbx
popq %r14
popq %r15
retq
xorl %eax, %eax
leaq 0x5d48(%rip), %rdx      # 0x7030
movl $0xff, %esi
leaq 0x5d8c(%rip), %rdi      # 0x7080
movl $0xff, %r8d
xorl %r9d, %r9d
cmpq $0x38, %r9
je 0x12d9
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
jmp 0x12fd
xorl %eax, %eax
leaq 0x5c36(%rip), %rdx      # 0x7030
movl $0xff, %esi
leaq 0x5c7a(%rip), %rdi      # 0x7080
xorl %r8d, %r8d
cmpq $0x50, %r8
je 0x12d9
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
jmp 0x1409
xorl %eax, %eax
leaq 0x5b94(%rip), %rdx      # 0x7030
movl $0xff, %esi
leaq 0x5bd8(%rip), %rdi      # 0x7080
xorl %r8d, %r8d
cmpq $0x40, %r8
je 0x12d9
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
jmp 0x14ab