LLM4Binary/decompile-bench-makefile huggingface dataset:
// File: /workspace/github_c_makefile_star2_O2/mijallen[P]SimpleHTTPServer/lib/libyaml/tests/run-dumper.c
// Function: compare_documents
int compare_documents(yaml_document_t *document1, yaml_document_t *document2)
{
    int k;

    if ((document1->version_directive && !document2->version_directive)
            || (!document1->version_directive && document2->version_directive)
            || (document1->version_directive && document2->version_directive
                && (document1->version_directive->major != document2->version_directive->major
                    || document1->version_directive->minor != document2->version_directive->minor)))
        return 0;

    if ((document1->tag_directives.end - document1->tag_directives.start) !=
            (document2->tag_directives.end - document2->tag_directives.start))
        return 0;
    for (k = 0; k < (document1->tag_directives.end - document1->tag_directives.start); k ++) {
        if ((strcmp((char *)document1->tag_directives.start[k].handle,
                        (char *)document2->tag_directives.start[k].handle) != 0)
                || (strcmp((char *)document1->tag_directives.start[k].prefix,
                    (char *)document2->tag_directives.start[k].prefix) != 0))
            return 0;
    }

    if ((document1->nodes.top - document1->nodes.start) !=
            (document2->nodes.top - document2->nodes.start))
        return 0;

    if (document1->nodes.top != document1->nodes.start) {
        if (!compare_nodes(document1, 1, document2, 1))
            return 0;
    }

    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq 0x18(%rdi), %rcx
movq 0x18(%rsi), %rax
testq %rcx, %rcx
je 0x16d7
xorl %ebp, %ebp
testq %rax, %rax
je 0x174e
movl (%rcx), %edx
cmpl (%rax), %edx
jne 0x174e
movl 0x4(%rcx), %ecx
cmpl 0x4(%rax), %ecx
je 0x16dc
jmp 0x174e
testq %rax, %rax
jne 0x174c
movq 0x20(%rdi), %r15
movq 0x28(%rdi), %rax
subq %r15, %rax
movq 0x20(%rsi), %r12
movq 0x28(%rsi), %rcx
subq %r12, %rcx
cmpq %rcx, %rax
jne 0x174c
movq %rdi, 0x8(%rsp)
movq %rsi, 0x10(%rsp)
sarq $0x4, %rax
xorl %r13d, %r13d
testq %rax, %rax
cmovgq %rax, %r13
shlq $0x4, %r13
pushq $-0x10
popq %rbx
xorl %ebp, %ebp
leaq 0x10(%rbx), %r14
cmpq %r14, %r13
je 0x175f
movq 0x10(%r15,%rbx), %rdi
movq 0x10(%r12,%rbx), %rsi
callq 0x1170
testl %eax, %eax
jne 0x174e
movq 0x18(%r15,%rbx), %rdi
movq 0x18(%r12,%rbx), %rsi
callq 0x1170
movq %r14, %rbx
testl %eax, %eax
je 0x1718
jmp 0x174e
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %rcx
movq (%rcx), %rax
movq 0x10(%rcx), %rcx
movq %rcx, %rdx
subq %rax, %rdx
movq 0x10(%rsp), %rdi
movq 0x10(%rdi), %rsi
subq (%rdi), %rsi
xorl %ebp, %ebp
cmpq %rsi, %rdx
jne 0x174e
cmpq %rax, %rcx
je 0x17a1
pushq $0x1
popq %rcx
movq 0x8(%rsp), %rdi
movl %ecx, %esi
movq 0x10(%rsp), %rdx
callq 0x14d4
testl %eax, %eax
je 0x174e
pushq $0x1
popq %rbp
jmp 0x174e
// File: /workspace/github_c_makefile_star2_O2/jogolden[P]ps4flashtool/aes.c
// Function: Cipher
static void Cipher(state_t* state, uint8_t* RoundKey)
{
  uint8_t round = 0;

  // Add the First round key to the state before starting the rounds.
  AddRoundKey(0, state, RoundKey); 
  
  // There will be Nr rounds.
  // The first Nr-1 rounds are identical.
  // These Nr-1 rounds are executed in the loop below.
  for (round = 1; round < Nr; ++round)
  {
    SubBytes(state);
    ShiftRows(state);
    MixColumns(state);
    AddRoundKey(round, state, RoundKey);
  }
  
  // The last round is given below.
  // The MixColumns function is not here in the last round.
  SubBytes(state);
  ShiftRows(state);
  AddRoundKey(Nr, state, RoundKey);
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
subq $0x10, %rsp
movq %rsi, %rbx
movq %rdi, %r14
xorl %edi, %edi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x273d
movb $0x1, %bpl
movq %r14, %rdi
callq 0x276c
movq %r14, %rdi
callq 0x279d
cmpb $0xa, %bpl
je 0x244a
xorl %eax, %eax
movdqa 0x4c46(%rip), %xmm4     # 0x7010
cmpq $0x4, %rax
je 0x2433
movd (%r14,%rax,4), %xmm0
movdqa %xmm0, %xmm1
psrld $0x8, %xmm1
movdqa %xmm0, %xmm2
pslld $0x18, %xmm2
por %xmm1, %xmm2
pxor %xmm0, %xmm2
movdqa %xmm2, (%rsp)
movb 0x2(%rsp), %cl
xorb (%rsp), %cl
pxor %xmm1, %xmm1
pcmpgtb %xmm2, %xmm1
paddb %xmm2, %xmm2
pand %xmm4, %xmm1
movzbl %cl, %ecx
movd %ecx, %xmm3
punpcklbw %xmm3, %xmm3    # xmm3 = xmm3[0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7]
pshuflw $0x0, %xmm3, %xmm3      # xmm3 = xmm3[0,0,0,0,4,5,6,7]
pxor %xmm2, %xmm3
pxor %xmm0, %xmm3
pxor %xmm1, %xmm3
movd %xmm3, (%r14,%rax,4)
incq %rax
jmp 0x23ca
movzbl %bpl, %edi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x273d
incb %bpl
jmp 0x23a6
pushq $0xa
popq %rdi
movq %r14, %rsi
movq %rbx, %rdx
addq $0x10, %rsp
popq %rbx
popq %r14
popq %rbp
jmp 0x273d
// File: /workspace/github_c_makefile_star2_O2/jogolden[P]ps4flashtool/aes.c
// Function: AES_CBC_encrypt_buffer
void AES_CBC_encrypt_buffer(struct AES_ctx *ctx,uint8_t* buf, uint32_t length)
{
  uintptr_t i;
  uint8_t *Iv = ctx->Iv;
  for (i = 0; i < length; i += AES_BLOCKLEN)
  {
    XorWithIv(buf, Iv);
    Cipher((state_t*)buf, ctx->RoundKey);
    Iv = buf;
    buf += AES_BLOCKLEN;
    //printf("Step %d - %d", i/16, i);
  }
  /* store Iv in ctx for next call */
  memcpy(ctx->Iv, Iv, AES_BLOCKLEN);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbx
movq %rdi, %r14
leaq 0xb0(%rdi), %r12
movl %edx, %r13d
xorl %ebp, %ebp
movq %r12, %rsi
cmpq %r13, %rbp
jae 0x26ad
leaq (%rbx,%rbp), %r15
movq %r15, %rdi
callq 0x26c4
movq %r15, %rdi
movq %r14, %rsi
callq 0x2388
addq $0x10, %rbp
movq %r15, %rsi
jmp 0x2688
movups (%rsi), %xmm0
movups %xmm0, (%r12)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
// File: /workspace/github_c_makefile_star2_O2/jogolden[P]ps4flashtool/aes.c
// Function: AES_CBC_decrypt_buffer
void AES_CBC_decrypt_buffer(struct AES_ctx* ctx, uint8_t* buf,  uint32_t length)
{
  uintptr_t i;
  uint8_t storeNextIv[AES_BLOCKLEN];
  for (i = 0; i < length; i += AES_BLOCKLEN)
  {
    memcpy(storeNextIv, buf, AES_BLOCKLEN);
    InvCipher((state_t*)buf, ctx->RoundKey);
    XorWithIv(buf, ctx->Iv);
    memcpy(ctx->Iv, storeNextIv, AES_BLOCKLEN);
    buf += AES_BLOCKLEN;
  }

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %rbx
movq %rdi, %r14
movl %edx, %r13d
leaq 0xb0(%rdi), %r15
xorl %ebp, %ebp
cmpq %r13, %rbp
jae 0x272e
leaq (%rbx,%rbp), %r12
movups (%r12), %xmm0
movaps %xmm0, (%rsp)
movq %r12, %rdi
movq %r14, %rsi
callq 0x246b
movq %r12, %rdi
movq %r15, %rsi
callq 0x26c4
movaps (%rsp), %xmm0
movups %xmm0, (%r15)
addq $0x10, %rbp
jmp 0x26f8
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
// File: /workspace/github_c_makefile_star2_O2/jogolden[P]ps4flashtool/aes.c
// Function: AddRoundKey
static void AddRoundKey(uint8_t round,state_t* state,uint8_t* RoundKey)
{
  uint8_t i,j;
  for (i = 0; i < 4; ++i)
  {
    for (j = 0; j < 4; ++j)
    {
      (*state)[i][j] ^= RoundKey[(round * Nb * 4) + (i * Nb) + j];
    }
  }
}

// Assembly:
shll $0x4, %edi
addq %rdx, %rdi
xorl %eax, %eax
cmpq $0x4, %rax
je 0x276b
xorl %ecx, %ecx
cmpq $0x4, %rcx
je 0x275e
movb (%rdi,%rcx), %dl
xorb %dl, (%rsi,%rcx)
incq %rcx
jmp 0x274d
incq %rax
addq $0x4, %rsi
addq $0x4, %rdi
jmp 0x2745
retq
// File: /workspace/github_c_makefile_star2_O2/jogolden[P]ps4flashtool/aes.c
// Function: SubBytes
static void SubBytes(state_t* state)
{
  uint8_t i, j;
  for (i = 0; i < 4; ++i)
  {
    for (j = 0; j < 4; ++j)
    {
      (*state)[j][i] = getSBoxValue((*state)[j][i]);
    }
  }
}

// Assembly:
xorl %eax, %eax
leaq 0x48ab(%rip), %rcx      # 0x7020
cmpq $0x4, %rax
je 0x279c
xorl %edx, %edx
cmpq $0x4, %rdx
je 0x2794
movzbl (%rdi,%rdx,4), %esi
movb (%rsi,%rcx), %sil
movb %sil, (%rdi,%rdx,4)
incq %rdx
jmp 0x277d
incq %rax
incq %rdi
jmp 0x2775
retq
// File: /workspace/github_c_makefile_star2_O2/jogolden[P]ps4flashtool/aes.c
// Function: InvSubBytes
static void InvSubBytes(state_t* state)
{
  uint8_t i, j;
  for (i = 0; i < 4; ++i)
  {
    for (j = 0; j < 4; ++j)
    {
      (*state)[j][i] = getSBoxInvert((*state)[j][i]);
    }
  }
}

// Assembly:
xorl %eax, %eax
leaq 0x48f8(%rip), %rcx      # 0x7130
cmpq $0x4, %rax
je 0x285f
xorl %edx, %edx
cmpq $0x4, %rdx
je 0x2857
movzbl (%rdi,%rdx,4), %esi
movb (%rsi,%rcx), %sil
movb %sil, (%rdi,%rdx,4)
incq %rdx
jmp 0x2840
incq %rax
incq %rdi
jmp 0x2838
retq
// File: /workspace/github_c_makefile_star2_O2/jogolden[P]ps4flashtool/crypto.c
// Function: aes128_cbc_encrypt
int aes128_cbc_encrypt(void *key, void *plaintext, void *ciphertext, int length, void *iv) {
    struct AES_ctx ctx;

    unsigned char *temp = (unsigned char *)malloc(length);
    if(!temp) {
        return 1;
    }

    memcpy(temp, plaintext, length);

    AES_init_ctx_iv(&ctx, key, iv);
    AES_CBC_encrypt_buffer(&ctx, temp, length);

    memcpy(ciphertext, temp, length);
    free(temp);

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %r8, %r12
movq %rdx, %rbx
movq %rsi, %rbp
movq %rdi, %r13
movslq %ecx, %r14
movq %r14, %rdi
callq 0x2150
testq %rax, %rax
je 0x28d9
movq %rax, %r15
movq %rax, %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x2140
leaq 0x8(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x2350
movq %rbp, %rdi
movq %r15, %rsi
movl %r14d, %edx
callq 0x2668
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x2140
movq %r15, %rdi
callq 0x2030
xorl %eax, %eax
jmp 0x28dc
pushq $0x1
popq %rax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
// File: /workspace/github_c_makefile_star2_O2/jogolden[P]ps4flashtool/crypto.c
// Function: aes128_cbc_decrypt
int aes128_cbc_decrypt(void *key, void *ciphertext, void *plaintext, int length, void *iv) {
    struct AES_ctx ctx;

    unsigned char *temp = (unsigned char *)malloc(length);
    if(!temp) {
        return 1;
    }

    memcpy(temp, ciphertext, length);

    AES_init_ctx_iv(&ctx, key, iv);
    AES_CBC_decrypt_buffer(&ctx, temp, length);

    memcpy(plaintext, temp, length);
    free(temp);

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %r8, %r12
movq %rdx, %rbx
movq %rsi, %rbp
movq %rdi, %r13
movslq %ecx, %r14
movq %r14, %rdi
callq 0x2150
testq %rax, %rax
je 0x2967
movq %rax, %r15
movq %rax, %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x2140
leaq 0x8(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x2350
movq %rbp, %rdi
movq %r15, %rsi
movl %r14d, %edx
callq 0x26d8
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x2140
movq %r15, %rdi
callq 0x2030
xorl %eax, %eax
jmp 0x296a
pushq $0x1
popq %rax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
// File: /workspace/github_c_makefile_star2_O2/jogolden[P]ps4flashtool/flashtool.c
// Function: find_consolemap
struct consolemap_entry *find_consolemap(char *model) {
    int i, k;
    int good;
    char *m;

    for(i = 0; i < NUM_CONSOLES; i++) {
        m = g_consolemap[i].model;

        // loop and take wildcard into account
        good = 0;
        for(k = 0; k < MIN(strlen(m), strlen(model)); k++) {
            if(m[k] == '?' || m[k] == model[k]) {
                good = 1;
            } else {
                good = 0;
                break;
            }
        }

        if(good) {
            return &g_consolemap[i];
        }
    }

    return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
xorl %r13d, %r13d
leaq 0x7c40(%rip), %rbp      # 0xb0e0
cmpq $0x6, %r13
je 0x34f7
movq %r13, %rax
shlq $0x4, %rax
leaq (%rax,%rbp), %r14
movq (%rax,%rbp), %r15
movq %r15, %rdi
callq 0x2090
movq %rax, %r12
movq %rbx, %rdi
callq 0x2090
cmpq %rax, %r12
cmovbq %r12, %rax
movb $0x1, %dl
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x34ed
movb (%r15,%rcx), %dl
cmpb $0x3f, %dl
je 0x34e6
cmpb (%rbx,%rcx), %dl
jne 0x34f2
incq %rcx
xorl %edx, %edx
jmp 0x34d3
testb $0x1, %dl
je 0x34fa
incq %r13
jmp 0x34a0
xorl %r14d, %r14d
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
// File: /workspace/github_c_makefile_star2_O2/jogolden[P]ps4flashtool/flashtool.c
// Function: find_codename
struct codename_entry *find_codename(char *codename) {
    int i;

    for(i = 0; i < NUM_CODENAMES; i++) {
        if(!strcmp(g_codenames[i].codename, codename)) {
            return &g_codenames[i];
        }
    }

    return NULL;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %r14
xorl %ebx, %ebx
leaq 0x7c20(%rip), %r15      # 0xb140
xorl %ecx, %ecx
leaq 0x18(%rcx), %r12
cmpq $0x1f8, %r12            # imm = 0x1F8
je 0x354a
movq (%rcx,%r15), %rdi
movq %r14, %rsi
callq 0x2110
movq %r12, %rcx
testl %eax, %eax
jne 0x3522
leaq (%r12,%r15), %rbx
addq $-0x18, %rbx
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
// File: /workspace/github_c_makefile_star2_O2/jogolden[P]ps4flashtool/flashtool.c
// Function: hexdump
void hexdump(unsigned char *data, int length, int newlines) {
    int i, k;

    for(i = 0; i < length; i++) {
        printf("%02X ", data[i]);

        if(newlines) {
            if(i != 0 && (i + 1) % 16 == 0) {
                printf("\n");
            }
        }
    }

    printf("\n");
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %edx, %ebx
movl %esi, %r14d
movq %rdi, %r15
xorl %eax, %eax
testl %esi, %esi
cmovlel %eax, %r14d
negq %r14
pushq $0x1
popq %r13
leaq 0x3f3f(%rip), %r12      # 0x74c1
leaq (%r14,%r13), %rax
cmpq $0x1, %rax
je 0x35bb
testl %ebx, %ebx
setne %bpl
movzbl -0x1(%r15,%r13), %esi
movq %r12, %rdi
xorl %eax, %eax
callq 0x20b0
testb $0xf, %r13b
sete %al
testb %al, %bpl
je 0x35b6
pushq $0xa
popq %rdi
callq 0x2040
incq %r13
jmp 0x3582
pushq $0xa
popq %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2040
// File: /workspace/github_c_makefile_star2_O2/jogolden[P]ps4flashtool/flashtool.c
// Function: dumpbin
int dumpbin(unsigned char *filename, unsigned char *data, int length) {
    FILE *fp;

    fp = fopen(filename, "wb");
    if(!fp) {
        return 1;
    }
    
    fwrite(data, 1, length, fp);
    fclose(fp);

    return 0;
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
movl %edx, %ebp
movq %rsi, %rbx
leaq 0x3ee6(%rip), %rsi      # 0x74c7
callq 0x2180
testq %rax, %rax
je 0x360b
movq %rax, %r14
movslq %ebp, %rdx
pushq $0x1
popq %rsi
movq %rbx, %rdi
movq %rax, %rcx
callq 0x2190
movq %r14, %rdi
callq 0x2080
xorl %eax, %eax
jmp 0x360e
pushq $0x1
popq %rax
popq %rbx
popq %r14
popq %rbp
retq
// File: /workspace/github_c_makefile_star2_O2/jogolden[P]ps4flashtool/flashtool.c
// Function: readbin
int readbin(unsigned char *filename, unsigned char **data, int *length) {
    FILE *fp;
    unsigned char *ptr;
    long size;

    fp = fopen(filename, "rb");
    if(!fp) {
        return 1;
    }

    fseek(fp, 0, SEEK_END);
    size = ftell(fp);
    fseek(fp, 0, SEEK_SET);
    
    ptr = (unsigned char *)malloc(size);
    if(!ptr) {
        return 1;
    }

    memset(ptr, 0, size);
    fread(ptr, 1, size, fp);
    fclose(fp);

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

    if(length) {
        *length = size;
    }

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %rbx
movq %rsi, %r14
leaq 0x3e9f(%rip), %rsi      # 0x74ca
callq 0x2180
pushq $0x1
popq %rbp
testq %rax, %rax
je 0x36a7
movq %rax, %r12
pushq $0x2
popq %rdx
movq %rax, %rdi
xorl %esi, %esi
callq 0x2160
movq %r12, %rdi
callq 0x2130
movq %rax, %r15
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2160
pushq $0x1
popq %rdi
movq %r15, %rsi
callq 0x2100
testq %rax, %rax
je 0x36a7
movq %rax, %r13
pushq $0x1
popq %rsi
movq %rax, %rdi
movq %r15, %rdx
movq %r12, %rcx
callq 0x2070
movq %r12, %rdi
callq 0x2080
testq %r14, %r14
je 0x3695
movq %r13, (%r14)
jmp 0x369d
movq %r13, %rdi
callq 0x2030
xorl %ebp, %ebp
testq %rbx, %rbx
je 0x36a7
movl %r15d, (%rbx)
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
// File: /workspace/github_c_makefile_star2_O2/jogolden[P]ps4flashtool/flashtool.c
// Function: load_flashdump
unsigned char *load_flashdump(char *flashfile) {
    FILE *fp;
    unsigned char *data;
    int length;

    printf("loading flash dump '%s' ...\n", flashfile);

    if(readbin(flashfile, &data, &length)) {
        return NULL;
    }

    if(length != FLASH_SIZE) {
        printf("warning: flash size mismatch. is your flash really 32MB?\n");
    }

    return data;
}

// Assembly:
pushq %r14
pushq %rbx
subq $0x18, %rsp
movq %rdi, %r14
leaq 0x3e04(%rip), %rdi      # 0x74cd
xorl %ebx, %ebx
movq %r14, %rsi
xorl %eax, %eax
callq 0x20b0
leaq 0x10(%rsp), %rsi
leaq 0xc(%rsp), %rdx
movq %r14, %rdi
callq 0x3613
testl %eax, %eax
jne 0x3706
cmpl $0x2000000, 0xc(%rsp)   # imm = 0x2000000
je 0x3701
leaq 0x4211(%rip), %rdi      # 0x790d
callq 0x2060
movq 0x10(%rsp), %rbx
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r14
retq
// File: /workspace/github_c_makefile_star2_O2/jogolden[P]ps4flashtool/flashtool.c
// Function: dump_console_info
int dump_console_info(unsigned char *flashdata, unsigned char *path) {
    struct nvs_console_info cinfo;
    char macaddress[6];
    FILE *fp = NULL;
    int i;

    // read console info and mac address
    nvs_read(flashdata, 0, 2, NVS_VAR_BLOCK2_CONSOLE_INF0, sizeof(cinfo), (unsigned char *)&cinfo);
    nvs_read(flashdata, 0, 0, NVS_VAR_BLOCK0_MAC_ADDRESS, sizeof(macaddress), macaddress);

    fp = fopen(path, "w");
    if(!fp) {
        return 1;
    }

    fprintf(fp, "motherboard serial: %.*s\n", (int)sizeof(cinfo.moboserial), cinfo.moboserial);
    fprintf(fp, "serial: %.*s\n", (int)sizeof(cinfo.serial), cinfo.serial);
    fprintf(fp, "model: %.*s\n", (int)sizeof(cinfo.model), cinfo.model);
    fprintf(fp, "mac address: ");
    for(i = 0; i < sizeof(macaddress); i++) {
        fprintf(fp, "%02X%c", macaddress[i] & 0xFF, (i == sizeof(macaddress) - 1) ? '\n' : ':');
    }

    fprintf(fp, "\n");

    fclose(fp);

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x60, %rsp
movq %rsi, %rbx
movq %rdi, %r14
pushq $0x2
popq %rdx
pushq $0x4e
popq %r8
leaq 0x12(%rsp), %r9
xorl %esi, %esi
xorl %ecx, %ecx
callq 0x49c4
pushq $0x21
popq %rcx
pushq $0x6
popq %r8
leaq 0xc(%rsp), %r9
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x49c4
leaq 0x3c63(%rip), %rsi      # 0x73ba
movq %rbx, %rdi
callq 0x2180
testq %rax, %rax
je 0x3816
movq %rax, %rbx
leaq 0x3d81(%rip), %rsi      # 0x74f3
pushq $0xe
popq %rdx
leaq 0x12(%rsp), %rcx
movq %rax, %rdi
xorl %eax, %eax
callq 0x2120
leaq 0x42(%rsp), %rcx
leaq 0x3d7f(%rip), %rsi      # 0x750f
pushq $0xa
popq %rbp
movq %rbx, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0x2120
leaq 0x53(%rsp), %rcx
leaq 0x3d74(%rip), %rsi      # 0x751f
pushq $0xd
popq %rdx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x2120
leaq 0x3d6f(%rip), %rdi      # 0x752e
pushq $0xd
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rcx
callq 0x2190
pushq $-0x6
popq %r15
pushq $0x3a
popq %r12
leaq 0x3d0e(%rip), %r14      # 0x74ea
testq %r15, %r15
je 0x37ff
movzbl 0x12(%rsp,%r15), %edx
incq %r15
movl %r12d, %ecx
cmovel %ebp, %ecx
movq %rbx, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x2120
jmp 0x37dc
pushq $0xa
popq %rdi
movq %rbx, %rsi
callq 0x20e0
movq %rbx, %rdi
callq 0x2080
xorl %eax, %eax
jmp 0x3819
pushq $0x1
popq %rax
addq $0x60, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
// File: /workspace/github_c_makefile_star2_O2/jogolden[P]ps4flashtool/flashtool.c
// Function: print_flashinfo
void print_flashinfo(unsigned char *flashdata) {
    struct nvs_console_info cinfo;
    char macaddress[6];
    int i;

    // read console info and mac address
    nvs_read(flashdata, 0, 2, NVS_VAR_BLOCK2_CONSOLE_INF0, sizeof(cinfo), (unsigned char *)&cinfo);
    nvs_read(flashdata, 0, 0, NVS_VAR_BLOCK0_MAC_ADDRESS, sizeof(macaddress), macaddress);

    printf("* motherboard serial: %.*s\n", (int)sizeof(cinfo.moboserial), cinfo.moboserial);
    printf("* serial: %.*s\n", (int)sizeof(cinfo.serial), cinfo.serial);
    printf("* model: %.*s\n", (int)sizeof(cinfo.model), cinfo.model);
    printf("* mac address: ");
    for(i = 0; i < sizeof(macaddress); i++) {
        printf("%02X%c", macaddress[i] & 0xFF, (i == sizeof(macaddress) - 1) ? '\n' : ':');
    }

    printf("\n");
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x58, %rsp
movq %rdi, %rbx
pushq $0x2
popq %rdx
pushq $0x4e
popq %r8
leaq 0xa(%rsp), %r14
xorl %esi, %esi
xorl %ecx, %ecx
movq %r14, %r9
callq 0x49c4
pushq $0x21
popq %rcx
pushq $0x6
popq %r8
leaq 0x4(%rsp), %r9
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x49c4
leaq 0x3c87(%rip), %rdi      # 0x74f1
pushq $0xe
popq %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x20b0
leaq 0x3a(%rsp), %rdx
leaq 0x3c8a(%rip), %rdi      # 0x750d
pushq $0xa
popq %rbx
movl %ebx, %esi
xorl %eax, %eax
callq 0x20b0
leaq 0x4b(%rsp), %rdx
leaq 0x3c82(%rip), %rdi      # 0x751d
pushq $0xd
popq %rsi
xorl %eax, %eax
callq 0x20b0
leaq 0x3c80(%rip), %rdi      # 0x752c
xorl %eax, %eax
callq 0x20b0
pushq $-0x6
popq %r15
pushq $0x3a
popq %rbp
leaq 0x3c29(%rip), %r14      # 0x74ea
testq %r15, %r15
je 0x38e0
movzbl 0xa(%rsp,%r15), %esi
incq %r15
movl %ebp, %edx
cmovel %ebx, %edx
movq %r14, %rdi
xorl %eax, %eax
callq 0x20b0
jmp 0x38c1
pushq $0xa
popq %rdi
callq 0x2040
addq $0x58, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
// File: /workspace/github_c_makefile_star2_O2/jogolden[P]ps4flashtool/flashtool.c
// Function: extract_flashdump
int extract_flashdump(unsigned char *flashdata, char *extractdir) {
    unsigned char *ptr;
    int len;
    char path[512];
    
    printf("extracting flash to '%s' directory ...\n", extractdir);
    
    mkdir(extractdir, S_IRWXU);

    snprintf(path, sizeof(path), "%s/%s", extractdir, "emcipl.bin");
    if(decrypt_emc_ipl(flashdata, &ptr, &len)) {
        return 1;
    }

    dumpbin(path, ptr, len);
    free(ptr);

    printf("extracted EMC IPL to '%s'!\n", path);

    snprintf(path, sizeof(path), "%s/%s", extractdir, "eapkbl.elf");
    if(decrypt_eap_kbl(flashdata, &ptr, &len)) {
        return 1;
    }

    dumpbin(path, ptr, len);
    free(ptr);
    
    printf("extracted EAP KBL to '%s'!\n", path);

    snprintf(path, sizeof(path), "%s/%s", extractdir, "consoleinfo.txt");
    dump_console_info(flashdata, path);
    
    printf("wrote console info to '%s'!\n", path);

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x218, %rsp            # imm = 0x218
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x3bf5(%rip), %rdi      # 0x753c
xorl %eax, %eax
callq 0x20b0
movq %r14, %rdi
movl $0x1c0, %esi            # imm = 0x1C0
callq 0x2050
leaq 0x3c02(%rip), %rdx      # 0x7564
leaq 0x3c01(%rip), %r8       # 0x756a
leaq 0x10(%rsp), %rdi
movl $0x200, %esi            # imm = 0x200
movq %r14, %rcx
xorl %eax, %eax
callq 0x20c0
leaq 0x8(%rsp), %rsi
leaq 0x4(%rsp), %rdx
movq %rbx, %rdi
callq 0x62c7
pushq $0x1
popq %rbp
testl %eax, %eax
jne 0x3a72
leaq 0x8(%rsp), %r15
movq (%r15), %rsi
leaq 0x4(%rsp), %r12
movl (%r12), %edx
leaq 0x10(%rsp), %r13
movq %r13, %rdi
callq 0x35d1
movq (%r15), %rdi
callq 0x2030
leaq 0x3bea(%rip), %rdi      # 0x75b1
movq %r13, %rsi
xorl %eax, %eax
callq 0x20b0
leaq 0x3b8c(%rip), %rdx      # 0x7564
leaq 0x3b96(%rip), %r8       # 0x7575
movl $0x200, %esi            # imm = 0x200
movq %r13, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x20c0
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x672d
testl %eax, %eax
jne 0x3a72
movq 0x8(%rsp), %rsi
movl 0x4(%rsp), %edx
leaq 0x10(%rsp), %r15
movq %r15, %rdi
callq 0x35d1
movq 0x8(%rsp), %rdi
callq 0x2030
leaq 0x3ba7(%rip), %rdi      # 0x75d1
movq %r15, %rsi
xorl %eax, %eax
callq 0x20b0
leaq 0x3b29(%rip), %rdx      # 0x7564
leaq 0x3b3e(%rip), %r8       # 0x7580
movl $0x200, %esi            # imm = 0x200
movq %r15, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x20c0
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3711
leaq 0x3b2a(%rip), %rdi      # 0x7590
movq %r15, %rsi
xorl %eax, %eax
callq 0x20b0
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
// File: /workspace/github_c_makefile_star2_O2/jogolden[P]ps4flashtool/flashtool.c
// Function: rawextract_flashdump
int rawextract_flashdump(unsigned char *flashdata, char *extractdir) {
    struct bldr_hdr *emc_blhdr = flash_locate_emc_ipl(flashdata, 0);
    struct bldr_hdr *eap_blhdr = flash_locate_eap_kbl(flashdata);
    char path[512];

    snprintf(path, sizeof(path), "%s/%s", extractdir, "emcipl.bin");
    dumpbin(path, (unsigned char *)emc_blhdr, emc_blhdr->hdr_len + emc_blhdr->body_len);

    printf("raw extracted EMC IPL to '%s'!\n", path);

    snprintf(path, sizeof(path), "%s/%s", extractdir, "eapkbl.elf");
    dumpbin(path, (unsigned char *)eap_blhdr, eap_blhdr->hdr_len + eap_blhdr->body_len);

    printf("raw extracted EAP KBL to '%s'!\n", path);

    snprintf(path, sizeof(path), "%s/%s", extractdir, "consoleinfo.txt");
    dump_console_info(flashdata, path);
    
    printf("wrote console info to '%s'!\n", path);

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x208, %rsp            # imm = 0x208
movq %rsi, %r14
movq %rdi, %rbx
xorl %esi, %esi
callq 0x5f48
movq %rax, %rbp
movq %rbx, %rdi
callq 0x6011
movq %rax, %r13
leaq 0x3aab(%rip), %r12      # 0x7564
leaq 0x3aaa(%rip), %r8       # 0x756a
movq %rsp, %r15
movl $0x200, %esi            # imm = 0x200
movq %r15, %rdi
movq %r12, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x20c0
movl 0xc(%rbp), %edx
addl 0x8(%rbp), %edx
movq %r15, %rdi
movq %rbp, %rsi
callq 0x35d1
leaq 0x3abd(%rip), %rdi      # 0x75ad
movq %r15, %rsi
xorl %eax, %eax
callq 0x20b0
leaq 0x3a74(%rip), %r8       # 0x7575
movl $0x200, %esi            # imm = 0x200
movq %r15, %rdi
movq %r12, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x20c0
movl 0xc(%r13), %edx
addl 0x8(%r13), %edx
movq %r15, %rdi
movq %r13, %rsi
callq 0x35d1
leaq 0x3a9d(%rip), %rdi      # 0x75cd
movq %r15, %rsi
xorl %eax, %eax
callq 0x20b0
leaq 0x3a3f(%rip), %r8       # 0x7580
movl $0x200, %esi            # imm = 0x200
movq %r15, %rdi
movq %r12, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x20c0
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3711
leaq 0x3a28(%rip), %rdi      # 0x7590
movq %r15, %rsi
xorl %eax, %eax
callq 0x20b0
xorl %eax, %eax
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
// File: /workspace/github_c_makefile_star2_O2/jogolden[P]ps4flashtool/hmac-sha1.c
// Function: hmac_sha1
int
hmac_sha1 (const void *key, size_t keylen,
	   const void *in, size_t inlen, void *resbuf)
{
  struct sha1_ctx inner;
  struct sha1_ctx outer;
  char optkeybuf[20];
  char block[64];
  char innerhash[20];

  /* Reduce the key's size, so that it becomes <= 64 bytes large.  */

  if (keylen > 64)
    {
      struct sha1_ctx keyhash;

      sha1_init_ctx (&keyhash);
      sha1_process_bytes (key, keylen, &keyhash);
      sha1_finish_ctx (&keyhash, optkeybuf);

      key = optkeybuf;
      keylen = 20;
    }

  /* Compute INNERHASH from KEY and IN.  */

  sha1_init_ctx (&inner);

  memset (block, IPAD, sizeof (block));
  memxor (block, key, keylen);

  sha1_process_block (block, 64, &inner);
  sha1_process_bytes (in, inlen, &inner);

  sha1_finish_ctx (&inner, innerhash);

  /* Compute result from KEY and INNERHASH.  */

  sha1_init_ctx (&outer);

  memset (block, OPAD, sizeof (block));
  memxor (block, key, keylen);

  sha1_process_block (block, 64, &outer);
  sha1_process_bytes (innerhash, 20, &outer);

  sha1_finish_ctx (&outer, resbuf);

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1d8, %rsp            # imm = 0x1D8
movq %r8, 0x8(%rsp)
movq %rcx, (%rsp)
movq %rdx, %r13
movq %rsi, %r15
movq %rdi, %r14
cmpq $0x41, %rsi
jb 0x46ca
pushq $0x14
popq %r12
leaq 0x98(%rsp), %rbx
movq %rbx, %rdi
callq 0x4a90
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x5ded
leaq 0x30(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4ad1
movq %r12, %r15
leaq 0x98(%rsp), %r12
movq %r12, %rdi
callq 0x4a90
movaps 0x395f(%rip), %xmm0     # 0x8040
leaq 0x50(%rsp), %rbp
movaps %xmm0, 0x30(%rbp)
movaps %xmm0, 0x20(%rbp)
movaps %xmm0, 0x10(%rbp)
movaps %xmm0, (%rbp)
movq %rbp, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x4654
pushq $0x40
popq %rbx
movq %rbp, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x4ba3
movq %r13, %rdi
movq (%rsp), %rsi
movq %r12, %rdx
callq 0x5ded
leaq 0x10(%rsp), %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x4ad1
leaq 0x138(%rsp), %r12
movq %r12, %rdi
callq 0x4a90
movaps 0x3905(%rip), %xmm0     # 0x8050
movaps %xmm0, 0x30(%rbp)
movaps %xmm0, 0x20(%rbp)
movaps %xmm0, 0x10(%rbp)
movaps %xmm0, (%rbp)
movq %rbp, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x4654
movq %rbp, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x4ba3
pushq $0x14
popq %rsi
movq %r13, %rdi
movq %r12, %rdx
callq 0x5ded
movq %r12, %rdi
movq 0x8(%rsp), %rsi
callq 0x4ad1
xorl %eax, %eax
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop
// File: /workspace/github_c_makefile_star2_O2/jogolden[P]ps4flashtool/keymgr.c
// Function: keymgr_loadkeys
int keymgr_loadkeys(const char *keyfile) {
    FILE *fkeyfile;
    long filesize;
    unsigned char *ptr;
    int i;
    int sz;

    fkeyfile = fopen(keyfile, "rb");
    if(!fkeyfile) {
        printf("keymgr: invalid key file!\n");
        return 1;
    }

    kmgrstate.keynames = (char **)malloc(MAXIMUM_KEYS * sizeof(char *));
    kmgrstate.keytypes = (int *)malloc(MAXIMUM_KEYS * sizeof(int));
    kmgrstate.keydata = (unsigned char **)malloc(MAXIMUM_KEYS * sizeof(char *));

    fseek(fkeyfile, 0, SEEK_END);
    filesize = ftell(fkeyfile);
    fseek(fkeyfile, 0, SEEK_SET);

    kmgrstate.filedata = (char *)malloc(filesize);
    fread(kmgrstate.filedata, 1, filesize, fkeyfile);
    fclose(fkeyfile);

    // parse all the keys
    ptr = kmgrstate.filedata;
    kmgrstate.numkeys = *(unsigned int *)ptr;
    ptr += 4;

    for(i = 0; i < kmgrstate.numkeys; i++) {
        // name
        sz = *(unsigned int *)ptr; ptr += 4;
        kmgrstate.keynames[i] = ptr; ptr += sz;

        // keytype
        kmgrstate.keytypes[i] = *(unsigned int *)ptr; ptr += 4;

        // keydat
        sz = *(unsigned int *)ptr; ptr += 4;
        kmgrstate.keydata[i] = ptr; ptr += sz;
    }

    return 0;
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
leaq 0x2d16(%rip), %rsi      # 0x74ca
callq 0x2180
testq %rax, %rax
je 0x48b1
movq %rax, %r14
movl $0x8000, %edi           # imm = 0x8000
callq 0x2150
movq %rax, 0x6b5a(%rip)      # 0xb330
movl $0x4000, %edi           # imm = 0x4000
callq 0x2150
movq %rax, 0x6b51(%rip)      # 0xb338
movl $0x8000, %edi           # imm = 0x8000
callq 0x2150
movq %rax, 0x6b48(%rip)      # 0xb340
xorl %ebx, %ebx
pushq $0x2
popq %rdx
movq %r14, %rdi
xorl %esi, %esi
callq 0x2160
movq %r14, %rdi
callq 0x2130
movq %rax, %r15
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2160
movq %r15, %rdi
callq 0x2150
movq %rax, 0x6b1b(%rip)      # 0xb348
pushq $0x1
popq %rsi
movq %rax, %rdi
movq %r15, %rdx
movq %r14, %rcx
callq 0x2070
movq %r14, %rdi
callq 0x2080
movq 0x6afb(%rip), %rcx      # 0xb348
movl (%rcx), %edx
movl %edx, 0x6afb(%rip)      # 0xb350
addq $0x4, %rcx
xorl %eax, %eax
movslq %edx, %rdx
cmpq %rdx, %rax
jge 0x48c0
movslq (%rcx), %rdx
leaq 0x4(%rcx), %rsi
movq 0x6abf(%rip), %rdi      # 0xb330
movq %rsi, (%rdi,%rax,8)
leaq (%rcx,%rdx), %rsi
addq $0x4, %rsi
leaq 0xc(%rcx,%rdx), %rcx
movl -0x8(%rcx), %edx
movq 0x6aac(%rip), %rdi      # 0xb338
movl %edx, (%rdi,%rax,4)
movslq -0x4(%rcx), %rdx
movq 0x6aa6(%rip), %rdi      # 0xb340
movq %rcx, (%rdi,%rax,8)
leaq (%rdx,%rsi), %rcx
addq $0x8, %rcx
incq %rax
movl 0x6aa1(%rip), %edx      # 0xb350
jmp 0x485b
leaq 0x37c3(%rip), %rdi      # 0x807b
callq 0x2060
pushq $0x1
popq %rbx
movl %ebx, %eax
popq %rbx
popq %r14
popq %r15
retq
// File: /workspace/github_c_makefile_star2_O2/jogolden[P]ps4flashtool/keymgr.c
// Function: keymgr_printkeys
void keymgr_printkeys() {
    int i;

    printf("\nkey manager state:\n");
    for(i = 0; i < kmgrstate.numkeys; i++) {
        printf("\tname: '%s' keytype: 0x%X\n", kmgrstate.keynames[i], kmgrstate.keytypes[i]);
        //hexdump(kmgrstate.keydata[i], 0x10);
    }
    printf("\n");
}

// Assembly:
pushq %r14
pushq %rbx
pushq %rax
leaq 0x3790(%rip), %rdi      # 0x8095
callq 0x2060
leaq 0x374f(%rip), %rbx      # 0x8060
xorl %r14d, %r14d
movslq 0x6a35(%rip), %rax      # 0xb350
cmpq %rax, %r14
jge 0x4945
movq 0x6a09(%rip), %rax      # 0xb330
movq (%rax,%r14,8), %rsi
movq 0x6a06(%rip), %rax      # 0xb338
movl (%rax,%r14,4), %edx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x20b0
incq %r14
jmp 0x4914
pushq $0xa
popq %rdi
addq $0x8, %rsp
popq %rbx
popq %r14
jmp 0x2040
// File: /workspace/github_c_makefile_star2_O2/jogolden[P]ps4flashtool/keymgr.c
// Function: keymgr_getkey
unsigned char *keymgr_getkey(const char *keyname, int keytype) {
    int i;

    for(i = 0; i < kmgrstate.numkeys; i++) {
        if(!strcmp(kmgrstate.keynames[i], keyname) && kmgrstate.keytypes[i] == keytype) {
            return kmgrstate.keydata[i];
        }
    }

    return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %esi, %ebp
movq %rdi, %r14
movl 0x69e5(%rip), %r15d     # 0xb350
movq 0x69be(%rip), %r12      # 0xb330
movq 0x69bf(%rip), %r13      # 0xb338
xorl %eax, %eax
testl %r15d, %r15d
cmovlel %eax, %r15d
xorl %ebx, %ebx
cmpq %rbx, %r15
je 0x49a5
movq (%r12,%rbx,8), %rdi
movq %r14, %rsi
callq 0x2110
testl %eax, %eax
jne 0x49a0
cmpl %ebp, (%r13,%rbx,4)
je 0x49a9
incq %rbx
jmp 0x4984
xorl %eax, %eax
jmp 0x49b4
movq 0x6990(%rip), %rax      # 0xb340
movq (%rax,%rbx,8), %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop
// File: /workspace/github_c_makefile_star2_O2/jogolden[P]ps4flashtool/nvs.c
// Function: nvs_read
int nvs_read(unsigned char *flashdata, unsigned int bank, unsigned int block, unsigned int offset, int size, unsigned char *buffer) {
    if(bank != 0) {
        printf("error: invalid NVS bank number %i\n", bank);
        return 1;
    }

    if(block > 4) {
        printf("error: invalid NVS block number %i\n", block);
        return 1;
    }

    if(offset >= g_nvs_block_sizes[block]) {
        printf("error: invalid NVS offset 0x%X\n", offset);
        return 1;
    }

    memcpy(buffer, flashdata + g_nvs_block_offsets[block] + offset, size);

    return 0;
}

// Assembly:
pushq %rax
testl %esi, %esi
je 0x49d2
leaq 0x36d9(%rip), %rdi      # 0x80a9
jmp 0x4a20
cmpl $0x5, %edx
jb 0x49e2
leaq 0x36ee(%rip), %rdi      # 0x80cc
movl %edx, %esi
jmp 0x4a20
movl %edx, %edx
leaq 0x3725(%rip), %rsi      # 0x8110
cmpl %ecx, (%rsi,%rdx,4)
jbe 0x4a17
movq %rdi, %rax
leaq 0x3736(%rip), %rsi      # 0x8130
movl (%rsi,%rdx,4), %edx
movl %ecx, %ecx
addq %rcx, %rax
addq %rdx, %rax
movslq %r8d, %rdx
movq %r9, %rdi
movq %rax, %rsi
callq 0x2140
xorl %eax, %eax
jmp 0x4a2a
leaq 0x36d2(%rip), %rdi      # 0x80f0
movl %ecx, %esi
xorl %eax, %eax
callq 0x20b0
pushq $0x1
popq %rax
popq %rcx
retq
// File: /workspace/github_c_makefile_star2_O2/jogolden[P]ps4flashtool/nvs.c
// Function: nvs_write
int nvs_write(unsigned char *flashdata, unsigned int bank, unsigned int block, unsigned int offset, int size, unsigned char *buffer) {
    if(bank != 0) {
        printf("error: invalid NVS bank number %i\n", bank);
        return 1;
    }

    if(block > 4) {
        printf("error: invalid NVS block number %i\n", block);
        return 1;
    }

    if(offset >= g_nvs_block_sizes[block]) {
        printf("error: invalid NVS offset 0x%X\n", offset);
        return 1;
    }

    memcpy(flashdata + g_nvs_block_offsets[block] + offset, buffer, size);

    return 0;
}

// Assembly:
pushq %rax
testl %esi, %esi
je 0x4a3a
leaq 0x3671(%rip), %rdi      # 0x80a9
jmp 0x4a82
cmpl $0x5, %edx
jb 0x4a4a
leaq 0x3686(%rip), %rdi      # 0x80cc
movl %edx, %esi
jmp 0x4a82
movl %edx, %eax
leaq 0x36bd(%rip), %rdx      # 0x8110
cmpl %ecx, (%rdx,%rax,4)
jbe 0x4a79
leaq 0x36d1(%rip), %rdx      # 0x8130
movl (%rdx,%rax,4), %eax
movl %ecx, %ecx
addq %rcx, %rdi
addq %rax, %rdi
movslq %r8d, %rdx
movq %r9, %rsi
callq 0x2140
xorl %eax, %eax
jmp 0x4a8c
leaq 0x3670(%rip), %rdi      # 0x80f0
movl %ecx, %esi
xorl %eax, %eax
callq 0x20b0
pushq $0x1
popq %rax
popq %rcx
retq
nop
// File: /workspace/github_c_makefile_star2_O2/jogolden[P]ps4flashtool/sha1.c
// Function: sha1_finish_ctx
void *
sha1_finish_ctx (struct sha1_ctx *ctx, void *resbuf)
{
  /* Take yet unprocessed bytes into account.  */
  uint32_t bytes = ctx->buflen;
  size_t size = (bytes < 56) ? 64 / 4 : 64 * 2 / 4;

  /* Now count remaining bytes.  */
  ctx->total[0] += bytes;
  if (ctx->total[0] < bytes)
    ++ctx->total[1];

  /* Put the 64-bit file length in *bits* at the end of the buffer.  */
  ctx->buffer[size - 2] = SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29));
  ctx->buffer[size - 1] = SWAP (ctx->total[0] << 3);

  memcpy (&((char *) ctx->buffer)[bytes], fillbuf, (size - 2) * 4 - bytes);

  /* Process last bytes.  */
  sha1_process_block (ctx->buffer, size * 4, ctx);

  return sha1_read_ctx (ctx, resbuf);
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbx
movq %rdi, %r14
movl 0x18(%rdi), %esi
movl 0x1c(%rdi), %eax
movl 0x14(%rdi), %ecx
addl %eax, %ecx
movl %ecx, 0x14(%rdi)
jae 0x4af5
incl %esi
movl %esi, 0x18(%r14)
xorl %r15d, %r15d
cmpl $0x38, %eax
setae %r15b
shldl $0x3, %ecx, %esi
movl %esi, %edx
shll $0x18, %edx
movl %esi, %edi
shll $0x8, %edi
movl $0xff0000, %r8d         # imm = 0xFF0000
andl %r8d, %edi
orl %edx, %edi
movl %esi, %edx
shrl $0x8, %edx
movl $0xff00, %r9d           # imm = 0xFF00
andl %r9d, %edx
orl %edi, %edx
shrl $0x18, %esi
orl %edx, %esi
leaq 0x20(%r14), %r12
shll $0x6, %r15d
leal 0x38(%r15), %edx
movl %esi, 0x20(%r14,%rdx)
movl %ecx, %esi
shll $0x1b, %esi
movl %ecx, %edi
shll $0xb, %edi
andl %r8d, %edi
orl %esi, %edi
movl %ecx, %esi
shrl $0x5, %esi
andl %r9d, %esi
orl %edi, %esi
shrl $0x15, %ecx
movzbl %cl, %ecx
orl %esi, %ecx
movl %ecx, 0x5c(%r15,%r14)
leaq (%r14,%rax), %rdi
addq $0x20, %rdi
subq %rax, %rdx
leaq 0x360a(%rip), %rsi      # 0x8180
callq 0x2140
addq $0x40, %r15
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x4ba3
movq %r14, %rdi
movq %rbx, %rsi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
jmp 0x4aa7
// File: /workspace/github_c_makefile_star2_O2/jogolden[P]ps4flashtool/sha1.c
// Function: sha1_buffer
void *
sha1_buffer (const char *buffer, size_t len, void *resblock)
{
  struct sha1_ctx ctx;

  /* Initialize the computation context.  */
  sha1_init_ctx (&ctx);

  /* Process whole buffer but last len % 64 bytes.  */
  sha1_process_bytes (buffer, len, &ctx);

  /* Put result in desired memory area.  */
  return sha1_finish_ctx (&ctx, resblock);
}

// Assembly:
pushq %r14
pushq %rbx
subq $0xa8, %rsp
movq %rdx, %rbx
movaps 0x2398(%rip), %xmm0     # 0x8150
movq %rsp, %r14
movaps %xmm0, (%r14)
movss 0x23a9(%rip), %xmm0     # 0x8170
movaps %xmm0, 0x10(%r14)
movq %r14, %rdx
callq 0x5ded
movq %r14, %rdi
movq %rbx, %rsi
callq 0x4ad1
movq %rbx, %rax
addq $0xa8, %rsp
popq %rbx
popq %r14
retq
// File: /workspace/github_c_makefile_star2_O2/jogolden[P]ps4flashtool/southbridge.c
// Function: flash_locate_emc_ipl
struct bldr_hdr *flash_locate_emc_ipl(unsigned char *flashdata, int forcehdr) {
    // reversed from EMC bootrom
    // I want to start calling the first header the meta header and then the second header the main header.
    // I cannot tell if the two slots for booting are for updates or for testkit/devkit vs retail?
    struct flashheader0 *hdr0 = NULL;
    struct flashheader1 *hdr1 = NULL;
    struct bldr_hdr *blhdr = NULL;
    unsigned char flag;
    int blocknum;

    hdr0 = (struct flashheader0 *)flashdata;

    if(verbose) {
        printf("* flash branding '%s'\n", hdr0->branding);
    }

    blocknum = hdr0->secondHeaderBlockNum2;
    flag = *(unsigned int *)(flashdata + (hdr0->headerInfoBlockNum * 0x200));
    if(!((flag >> 7) & 1)) {
        if(verbose && !forcehdr) {
            printf("* using header block num 1\n");
        }

        blocknum = hdr0->secondHeaderBlockNum1;
    } else if(verbose && !forcehdr) {
        printf("* using header block num 2\n");
    }

    hdr1 = (struct flashheader1 *)(flashdata + (blocknum * 0x200));
    blhdr = (struct bldr_hdr *)(flashdata + ((blocknum + hdr1->dataBlockNumber) * 0x200));
    
    if(blhdr->magic != BLDR_MAGIC) {
        printf("error: invalid EMC IPL bootloader header magic!\n");
        return NULL;
    }

    if(blhdr->type != BLDR_TYPE_EMC) {
        printf("error: invalid EMC IPL bootloader header type!\n");
        return NULL;
    }

    if(forcehdr == 1) {
        return (struct bldr_hdr *)(flashdata + ((hdr0->secondHeaderBlockNum1 + hdr1->dataBlockNumber) * 0x200));
    } else if(forcehdr == 2) {
        return (struct bldr_hdr *)(flashdata + ((hdr0->secondHeaderBlockNum2 + hdr1->dataBlockNumber) * 0x200));
    }

    return blhdr;
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
movl %esi, %ebp
movq %rdi, %rbx
leaq 0x53cc(%rip), %r14      # 0xb324
cmpl $0x0, (%r14)
je 0x5f6f
leaq 0x225b(%rip), %rdi      # 0x81c0
movq %rbx, %rsi
xorl %eax, %eax
callq 0x20b0
movl 0x34(%rbx), %ecx
shll $0x9, %ecx
cmpl $0x0, (%r14)
sete %dl
testl %ebp, %ebp
setne %al
orb %dl, %al
testb $-0x80, (%rbx,%rcx)
jne 0x5f9f
testb %al, %al
jne 0x5f99
leaq 0x2585(%rip), %rdi      # 0x8519
callq 0x2060
movl 0x24(%rbx), %r14d
jmp 0x5fb3
movl 0x28(%rbx), %r14d
testb %al, %al
jne 0x5fb3
leaq 0x2586(%rip), %rdi      # 0x8534
callq 0x2060
movslq %r14d, %rax
movq %rax, %rcx
shlq $0x9, %rcx
movl 0x30(%rbx,%rcx), %ecx
addl %ecx, %eax
shll $0x9, %eax
cmpl $0xd48ff9aa, (%rbx,%rax) # imm = 0xD48FF9AA
jne 0x5fe7
addq %rbx, %rax
cmpb $0x48, 0x7(%rax)
jne 0x5ff0
cmpl $0x2, %ebp
je 0x6003
cmpl $0x1, %ebp
jne 0x5ffe
addl 0x24(%rbx), %ecx
jmp 0x6006
leaq 0x2590(%rip), %rdi      # 0x857e
jmp 0x5ff7
leaq 0x2558(%rip), %rdi      # 0x854f
callq 0x2060
xorl %eax, %eax
popq %rbx
popq %r14
popq %rbp
retq
addl 0x28(%rbx), %ecx
shll $0x9, %ecx
addq %rbx, %rcx
movq %rcx, %rax
jmp 0x5ffe
// File: /workspace/github_c_makefile_star2_O2/jogolden[P]ps4flashtool/southbridge.c
// Function: flash_locate_eap_kbl
struct bldr_hdr *flash_locate_eap_kbl(unsigned char *flashdata) {
    struct bldr_hdr *blhdr = NULL;
    struct slb2_header *slbhdr = (struct slb2_header *)(flashdata + 0xC4000);
    struct slb2_entry *entry = NULL;
    int i;

    if(slbhdr->magic != SLB2_MAGIC) {
        printf("error: invalid KBL SLB2 magic!\n");
        return NULL;
    }

    for(i = 0; i < slbhdr->file_count; i++) {
        entry = &slbhdr->entry_list[i];

        if(!strcmp(entry->file_name, "C0010001") || !strcmp(entry->file_name, "eap_kbl")) {
            break;
        }
    }
    
    return (struct bldr_hdr *)(flashdata + 0xC4000 + (entry->block_offset << 9));
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
cmpl $0x32424c53, 0xc4000(%rdi) # imm = 0x32424C53
jne 0x6090
movq %rdi, %r15
leaq 0xc4000(%rdi), %rax
movq %rax, (%rsp)
movl 0xc400c(%rdi), %ebp
addq $0xc4020, %r15          # imm = 0xC4020
incq %rbp
xorl %ebx, %ebx
movq %r15, %r14
xorl %r12d, %r12d
movq %r12, %rcx
decq %rbp
je 0x60aa
movq %r14, %r12
leaq 0x10(%r14), %r13
movq %r13, %rdi
leaq 0x1405(%rip), %rsi      # 0x746c
callq 0x2110
testl %eax, %eax
je 0x60a0
leaq 0x30(%r12), %r14
incq %rbx
movq %r13, %rdi
leaq 0x2155(%rip), %rsi      # 0x81d7
callq 0x2110
movq %r12, %rcx
testl %eax, %eax
jne 0x604e
jmp 0x60aa
leaq 0x2517(%rip), %rdi      # 0x85ae
callq 0x2060
xorl %eax, %eax
jmp 0x60b9
imulq $0x30, %rbx, %rax
addq %rax, %r15
movq %r15, %rcx
movl (%rcx), %eax
shll $0x9, %eax
movq (%rsp), %rcx
addq %rax, %rcx
movq %rcx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
// File: /workspace/github_c_makefile_star2_O2/jogolden[P]ps4flashtool/southbridge.c
// Function: decrypt_emc_ipl
int decrypt_emc_ipl(unsigned char *flashdata, unsigned char **data, unsigned int *length) {
    struct bldr_hdr *blhdr = flash_locate_emc_ipl(flashdata, 0);
    unsigned char *hdraeskey = keymgr_getkey("emciplaes", KEY_TYPE_AES_128);
    
    if(verbose) {
        printf("* EMC IPL header aes key: ");
        hexdump(hdraeskey, 0x10, 0);
    }

    struct bldr_hdr dechdr;
    unsigned char *decbody = (unsigned char *)malloc(blhdr->body_len);

    // copy the header on to the stack, we dont want to modify anything
    memcpy(&dechdr, blhdr, blhdr->hdr_len);
    aes128_cbc_iv_zero_decrypt(hdraeskey, &blhdr->crypteddata, &dechdr.crypteddata, 0x50);
    aes128_cbc_iv_zero_decrypt(dechdr.crypteddata.iplbodyaeskey, &blhdr->bodystart, decbody, blhdr->body_len);

    if(data) {
        *data = decbody;
    }

    if(length) {
        *length = blhdr->body_len;
    }

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdx, %rbx
movq %rsi, %r14
xorl %esi, %esi
callq 0x5f48
movq %rax, %r15
leaq 0x1ef0(%rip), %rdi      # 0x81df
movl $0xa1, %esi
callq 0x4954
movq %rax, %r12
leaq 0x5021(%rip), %rax      # 0xb324
cmpl $0x0, (%rax)
je 0x6323
leaq 0x1ee5(%rip), %rdi      # 0x81f4
xorl %eax, %eax
callq 0x20b0
pushq $0x10
popq %rsi
movq %r12, %rdi
xorl %edx, %edx
callq 0x3559
movl 0xc(%r15), %edi
callq 0x2150
movq %rax, %r13
movl 0x8(%r15), %edx
leaq 0x7(%rsp), %rdi
movq %r15, %rsi
callq 0x2140
leaq 0x30(%r15), %rsi
leaq 0x37(%rsp), %rbp
pushq $0x50
popq %rcx
movq %r12, %rdi
movq %rbp, %rdx
callq 0x2994
movq %r15, %rsi
subq $-0x80, %rsi
movl 0xc(%r15), %ecx
movq %rbp, %rdi
movq %r13, %rdx
callq 0x2994
testq %r14, %r14
je 0x6375
movq %r13, (%r14)
testq %rbx, %rbx
je 0x6380
movl 0xc(%r15), %eax
movl %eax, (%rbx)
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
// File: /workspace/github_c_makefile_star2_O2/jogolden[P]ps4flashtool/southbridge.c
// Function: decrypt_eap_kbl
int decrypt_eap_kbl(unsigned char *flashdata, unsigned char **data, unsigned int *length) {
    struct bldr_hdr *blhdr = flash_locate_eap_kbl(flashdata);
    unsigned char *hdraeskey = keymgr_getkey("eapkblaes", KEY_TYPE_AES_128);
    
    if(verbose) {
        printf("* EAP KBL header aes key: ");
        hexdump(hdraeskey, 0x10, 0);
    }

    struct bldr_hdr dechdr;
    unsigned char *decbody = (unsigned char *)malloc(blhdr->body_len);

    // copy the header on to the stack, we dont want to modify anything
    memcpy(&dechdr, blhdr, blhdr->hdr_len);
    aes128_cbc_iv_zero_decrypt(hdraeskey, &blhdr->crypteddata, &dechdr.crypteddata, 0x50);
    aes128_cbc_iv_zero_decrypt(dechdr.crypteddata.iplbodyaeskey, &blhdr->bodystart, decbody, blhdr->body_len);

    if(data) {
        *data = decbody;
    }

    if(length) {
        *length = blhdr->body_len;
    }

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdx, %rbx
movq %rsi, %r15
callq 0x6011
movq %rax, %r14
leaq 0x1c29(%rip), %rdi      # 0x837c
movl $0xa1, %esi
callq 0x4954
movq %rax, %r12
leaq 0x4bbd(%rip), %rax      # 0xb324
cmpl $0x0, (%rax)
je 0x6787
leaq 0x1c1e(%rip), %rdi      # 0x8391
xorl %eax, %eax
callq 0x20b0
pushq $0x10
popq %rsi
movq %r12, %rdi
xorl %edx, %edx
callq 0x3559
movl 0xc(%r14), %edi
callq 0x2150
movq %rax, %r13
movl 0x8(%r14), %edx
leaq 0x7(%rsp), %rdi
movq %r14, %rsi
callq 0x2140
leaq 0x30(%r14), %rsi
leaq 0x37(%rsp), %rbp
pushq $0x50
popq %rcx
movq %r12, %rdi
movq %rbp, %rdx
callq 0x2994
movq %r14, %rsi
subq $-0x80, %rsi
movl 0xc(%r14), %ecx
movq %rbp, %rdi
movq %r13, %rdx
callq 0x2994
testq %r15, %r15
je 0x67d9
movq %r13, (%r15)
testq %rbx, %rbx
je 0x67e4
movl 0xc(%r14), %eax
movl %eax, (%rbx)
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq