// File: /workspace/github_c_makefile_star2_O3/jogolden[P]ps4flashtool/southbridge.c
// Function: decrypt_decompress_eap_kernel
int decrypt_decompress_eap_kernel(unsigned char *eapkernel, unsigned int eaplength, unsigned char **data, unsigned int *length) {
    struct eap_kernel_storage_hdr *shdr = (struct eap_kernel_storage_hdr *)eapkernel;
    struct eap_kernel_hdr *hdr;
    unsigned char *ptr, *output;
    unsigned int len, outlen;

    if(shdr->magic != EAP_KERNEL_STORAGE_HEADER_MAGIC) {
        printf("error: invalid EAP kernel storage header magic\n");
        return 1;
    }

    ptr = (unsigned char *)malloc(eaplength);
    if(!ptr) {
        printf("error: could not allocate memory for EAP kernel\n");
        return 1;
    }

    memcpy(ptr, eapkernel, eaplength);

    memset(random_seeds, 0, sizeof(random_seeds));
    init_eap_keys();

    // decrypt
    len = 512 - 0x2C;
    len -= len % 0x10;
    aes128_cbc_decrypt(eap_keys[1], ptr + 0x2C, ptr + 0x2C, len, shdr->iv);
    hdr = (struct eap_kernel_hdr *)(ptr + 0x2C);

    if(hdr->magic != EAP_KERNEL_HEADER_MAGIC) {
        printf("error: invalid EAP kernel header magic\n");
        return 1;
    }

    // decrypt
    len = hdr->length;
    len -= hdr->length % 0x10;
    aes128_cbc_decrypt(eap_keys[1], ptr + 0x2C, ptr + 0x2C, len, shdr->iv);

    printf("warning: EAP kernel decryption and decompression will remove the decompressor part of the EAP kernel loading\n");

    // decompress
    output = (unsigned char *)malloc(len);
    outlen = len;
    do {
        outlen *= 2; // resize buffer
        output = realloc(output, outlen);
        len = arzl_decompress(output, outlen, ptr + 0x1D30, NULL);
    } while (len == 0x80560201); // out of space

    arzl_deobfuscate(output, len, 2);

    free(ptr);

    if(data) {
        *data = output;
    } else {
        free(output);
    }

    if(length) {
        *length = len;
    }

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
cmpl $0x12ebc95c, (%rdi)     # imm = 0x12EBC95C
jne 0x72b4
movq %rcx, %r13
movq %rdx, %r14
movq %rdi, %r12
movl %esi, %r15d
movq %r15, %rdi
callq 0x2150
testq %rax, %rax
je 0x72bd
movq %rax, %rbx
movq %rax, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x2140
leaq 0x51fc(%rip), %rdi      # 0xc3e0
movl $0x9c4, %edx            # imm = 0x9C4
xorl %esi, %esi
callq 0x20d0
callq 0x6fe5
leaq 0x2c(%rbx), %r15
addq $0x8, %r12
leaq 0x516c(%rip), %rdi      # 0xc370
movq %r15, %rsi
movq %r15, %rdx
movl $0x1d0, %ecx            # imm = 0x1D0
movq %r12, %r8
callq 0x2c4c
cmpl $0x4b726e00, 0x2c(%rbx) # imm = 0x4B726E00
jne 0x72c6
movq %r13, (%rsp)
movl 0x30(%rbx), %r13d
andl $-0x10, %r13d
leaq 0x5139(%rip), %rdi      # 0xc370
movq %r15, %rsi
movq %r15, %rdx
movl %r13d, %ecx
movq %r12, %r8
callq 0x2c4c
leaq 0x2483(%rip), %rdi      # 0x96d2
callq 0x2060
movq %r13, %rdi
callq 0x2150
movq %rax, %r12
movq %rbx, %r15
addq $0x1d30, %r15           # imm = 0x1D30
addl %r13d, %r13d
movq %r12, %rdi
movq %r13, %rsi
callq 0x2170
movq %rax, %r12
movq %rax, %rdi
movl %r13d, %esi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x2d0c
cmpl $0x80560201, %eax       # imm = 0x80560201
je 0x7269
movl %eax, %ebp
movq %r12, %rdi
movl %eax, %esi
movl $0x2, %edx
callq 0x35de
movq %rbx, %rdi
callq 0x2030
testq %r14, %r14
je 0x72e6
movq %r12, (%r14)
jmp 0x72ee
leaq 0x24ab(%rip), %rdi      # 0x9766
jmp 0x72cd
leaq 0x23de(%rip), %rdi      # 0x96a2
jmp 0x72cd
leaq 0x2472(%rip), %rdi      # 0x973f
callq 0x2060
movl $0x1, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, %rdi
callq 0x2030
movq (%rsp), %rcx
xorl %eax, %eax
testq %rcx, %rcx
je 0x72d7
movl %ebp, (%rcx)
jmp 0x72d7