// File: /workspace/github_c_makefile_star2_O3/zecoxao[P]ps3_decrypt_tools/lib/aes.c
// Function: aes_setkey_dec
int aes_setkey_dec(aes_context *ctx, const unsigned char *key, unsigned int keysize) {
    int i, j;
    aes_context cty;
    unsigned long *RK;
    unsigned long *SK;
    int ret;

    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;

    ret = aes_setkey_enc(&cty, key, keysize);
    if (ret != 0)
        return ( ret);

    SK = cty.rk + cty.nr * 4;

    *RK++ = *SK++;
    *RK++ = *SK++;
    *RK++ = *SK++;
    *RK++ = *SK++;

    for (i = ctx->nr - 1, SK -= 8; i > 0; i--, SK -= 8) {
        for (j = 0; j < 4; j++, SK++) {
            *RK++ = RT0[ FSb[ (*SK) & 0xFF ] ] ^
                    RT1[ FSb[ (*SK >> 8) & 0xFF ] ] ^
                    RT2[ FSb[ (*SK >> 16) & 0xFF ] ] ^
                    RT3[ FSb[ (*SK >> 24) & 0xFF ] ];
        }
    }

    *RK++ = *SK++;
    *RK++ = *SK++;
    *RK++ = *SK++;
    *RK++ = *SK++;

    memset(&cty, 0, sizeof ( aes_context));

    return ( 0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x238, %rsp            # imm = 0x238
movq %rdi, %rbx
cmpl $0x80, %edx
je 0x1590
cmpl $0x100, %edx            # imm = 0x100
je 0x1589
movl $0xffffffe0, %eax       # imm = 0xFFFFFFE0
cmpl $0xc0, %edx
jne 0x16c2
movl $0xc, %eax
jmp 0x1595
movl $0xe, %eax
jmp 0x1595
movl $0xa, %eax
movl %eax, (%rbx)
leaq 0x10(%rbx), %rax
movq %rax, 0x8(%rbx)
leaq 0x8(%rsp), %rdi
callq 0x125c
testl %eax, %eax
jne 0x16c2
movq 0x10(%rsp), %rax
movslq 0x8(%rsp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
addq $-0x20, %rax
movq 0x20(%rax), %rcx
movq %rcx, 0x10(%rbx)
movq 0x28(%rax), %rcx
movq %rcx, 0x18(%rbx)
movq 0x30(%rax), %rcx
movq %rcx, 0x20(%rbx)
movq 0x38(%rax), %rcx
leaq 0x30(%rbx), %r14
movq %rcx, 0x28(%rbx)
movl (%rbx), %ecx
cmpl $0x2, %ecx
jl 0x16a2
leaq 0x6a84(%rip), %rdx      # 0x8080
leaq 0x6b7d(%rip), %rsi      # 0x8180
movl $0xff, %edi
leaq 0x7371(%rip), %r8       # 0x8980
leaq 0x7b6a(%rip), %r9       # 0x9180
leaq 0x8363(%rip), %r11      # 0x9980
movq %r14, %r10
xorl %r14d, %r14d
xorl %ebx, %ebx
movq (%rax,%r14,8), %r15
movzbl %r15b, %r12d
movzbl (%r12,%rdx), %r12d
movl %r15d, %r13d
shrl $0x8, %r13d
andl %edi, %r13d
movzbl (%r13,%rdx), %r13d
movq (%r8,%r13,8), %r13
xorq (%rsi,%r12,8), %r13
movl %r15d, %r12d
shrl $0x10, %r12d
andl %edi, %r12d
movzbl (%r12,%rdx), %r12d
xorq (%r9,%r12,8), %r13
shrl $0x18, %r15d
movzbl (%r15,%rdx), %r15d
xorq (%r11,%r15,8), %r13
movq %r13, (%r10,%r14,8)
addq $-0x8, %rbx
incq %r14
cmpl $0x4, %r14d
jne 0x1625
leal -0x1(%rcx), %ebp
movq %rax, %r15
subq %rbx, %r15
leaq -0x40(%r15), %rax
movq %r10, %r14
subq %rbx, %r14
cmpl $0x2, %ecx
movl %ebp, %ecx
jg 0x161d
addq $-0x40, %r15
subq %rbx, %r10
movq %r10, %r14
movq %r15, %rax
movq (%rax), %rcx
movq %rcx, (%r14)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%r14)
movq 0x10(%rax), %rcx
movq %rcx, 0x10(%r14)
movq 0x18(%rax), %rax
movq %rax, 0x18(%r14)
xorl %eax, %eax
addq $0x238, %rsp            # imm = 0x238
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq