// File: /workspace/github_c_makefile_star2_O2/c-zhou[P]yahs/link.c
// Function: intra_link_mat_from_file
intra_link_mat_t *intra_link_mat_from_file(const char *f, cov_norm_t *cov_norm, asm_dict_t *dict, re_cuts_t *re_cuts, uint32_t resolution, int use_gap_seq, uint8_t mq)
{
    uint32_t i, k, m, i0, i1, b0, b1;
    uint64_t p0, p1, pair_n, pair_c, intra_c;
    int64_t magic_number;
    uint8_t buffer[BUFF_SIZE * 17];
    intra_link_mat_t *link_mat;
    intra_link_t *link;
    FILE *fp;

    fp = fopen(f, "r");
    if (fp == NULL)
        return 0;

    m = fread(&magic_number, sizeof(int64_t), 1, fp);
    if (m != 1) bin_fread_error();
    if (!is_valid_bin_header(magic_number)) {
        fprintf(stderr, "[E::%s] not a valid BIN file\n", __func__);
        return 0;
    }
    file_seek_skip_sdict(fp);
    m = fread(&pair_n, sizeof(uint64_t), 1, fp);
    if (m != 1) bin_fread_error();

    link_mat = use_gap_seq? intra_link_mat_init(dict, resolution, use_gap_seq) : intra_link_mat_init(dict->sdict, resolution, use_gap_seq);

    pair_c = 0;
    intra_c = 0;

    while (pair_c < pair_n) {
        m = fread(buffer, sizeof(uint8_t), BUFF_SIZE * 17, fp);

        for (i = 0; i < m && pair_c < pair_n; i += 17, ++pair_c) {
            if (*(uint8_t *) (buffer + i + 16) < mq)
                continue;

            if (use_gap_seq) {
                sd_coordinate_conversion(dict, *(uint32_t *) (buffer + i),     *(uint32_t *) (buffer + i + 4),  &i0, &p0, 0);
                sd_coordinate_conversion(dict, *(uint32_t *) (buffer + i + 8), *(uint32_t *) (buffer + i + 12), &i1, &p1, 0);
                b0 = p0 / resolution;
                b1 = p1 / resolution;
            } else {
                i0 = *(uint32_t *) (buffer + i);
                i1 = *(uint32_t *) (buffer + i + 8);
                b0 = *(uint32_t *) (buffer + i + 4) / resolution;
                b1 = *(uint32_t *) (buffer + i + 12) / resolution;
            }

            if (i0 == i1 && i0 != UINT32_MAX) {
                link = &link_mat->links[i0];
                if (link->n) {
                    if (b0 > b1)
                        SWAP(uint32_t, b0, b1);
                    k = (long) (link->n * 2 - b1 + b0 - 3) * (b1 - b0) / 2 + b1;
                    // link->link[k] += 1.;
                    link->link[k] += cov_norm->norm[*(uint32_t *) (buffer + i)][*(uint32_t *) (buffer + i + 4) / cov_norm->w] *
                        cov_norm->norm[*(uint32_t *) (buffer + i + 8)][*(uint32_t *) (buffer + i + 12) / cov_norm->w];
                }

                ++intra_c;
            }
        }
    }
#ifdef DEBUG
    fprintf(stderr, "[DEBUG::%s] %lu read pairs processed, %lu intra links \n", __func__, pair_c, intra_c);
#endif
    fclose(fp);
    
    // TODO normalise links by cell size
    if (use_gap_seq)
        intra_link_mat_normalise(link_mat, dict, re_cuts, resolution, use_gap_seq);
    else
        intra_link_mat_normalise(link_mat, dict->sdict, re_cuts, resolution, use_gap_seq);

    return link_mat;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x11088, %rsp          # imm = 0x11088
movl %r9d, 0x8(%rsp)
movl %r8d, %ebx
movq %rcx, %r14
movq %rdx, 0x10(%rsp)
movq %rsi, 0x50(%rsp)
leaq 0x157d3(%rip), %rsi     # 0x1f311
callq 0x23e0
testq %rax, %rax
je 0x9bb9
movq %rax, %r12
leaq 0x60(%rsp), %rdi
pushq $0x8
popq %rsi
pushq $0x1
popq %rdx
movq %rax, %rcx
callq 0x20f0
cmpl $0x1, %eax
je 0x9b6a
xorl %eax, %eax
callq 0x2986
movq 0x60(%rsp), %rdi
callq 0x2973
testl %eax, %eax
je 0x9e46
movq %r12, %rdi
callq 0x11940
leaq 0x68(%rsp), %rdi
pushq $0x8
popq %rsi
pushq $0x1
popq %rdx
movq %r12, %rcx
callq 0x20f0
cmpl $0x1, %eax
je 0x9ba3
xorl %eax, %eax
callq 0x2986
movl 0x8(%rsp), %edx
testl %edx, %edx
movq %r14, 0x38(%rsp)
je 0x9bc0
movq 0x10(%rsp), %rdi
movl %ebx, %esi
jmp 0x9bcd
xorl %ebx, %ebx
jmp 0x9e31
movq 0x10(%rsp), %rax
movq 0x38(%rax), %rdi
movl %ebx, %esi
xorl %edx, %edx
callq 0x9975
movq %rax, 0x28(%rsp)
movq 0x68(%rsp), %rax
movq %rax, 0x30(%rsp)
movl %ebx, 0xc(%rsp)
movl %ebx, %eax
movq %rax, 0x58(%rsp)
xorl %r15d, %r15d
movq %r12, 0x40(%rsp)
cmpq 0x30(%rsp), %r15
jae 0x9de5
movl $0x11000, %edx          # imm = 0x11000
leaq 0x80(%rsp), %rdi
pushq $0x1
popq %rsi
movq %r12, %rcx
callq 0x20f0
movb 0x110c0(%rsp), %r11b
movq %rax, %r8
xorl %r13d, %r13d
movl 0x8(%rsp), %r10d
movq 0x30(%rsp), %r12
movq %rax, 0x48(%rsp)
cmpl %r8d, %r13d
jae 0x9ddb
cmpq %r12, %r15
jae 0x9ddb
movl %r13d, %ebp
cmpb %r11b, 0x90(%rsp,%rbp)
jb 0x9dcf
movl 0x80(%rsp,%rbp), %r14d
testl %r10d, %r10d
je 0x9cf4
movl 0x84(%rsp,%rbp), %edx
movq 0x10(%rsp), %r12
movq %r12, %rdi
movl %r14d, %esi
movl %edx, 0x1c(%rsp)
leaq 0x24(%rsp), %rcx
leaq 0x78(%rsp), %r8
xorl %r9d, %r9d
callq 0x100d2
movl 0x88(%rsp,%rbp), %ebx
movl 0x8c(%rsp,%rbp), %ebp
movq %r12, %rdi
movq 0x30(%rsp), %r12
movl %ebx, %esi
movl %ebp, %edx
leaq 0x20(%rsp), %rcx
leaq 0x70(%rsp), %r8
xorl %r9d, %r9d
callq 0x100d2
movq 0x48(%rsp), %r8
movb 0x110c0(%rsp), %r11b
movl 0x8(%rsp), %r10d
movq 0x78(%rsp), %rax
xorl %edx, %edx
movq 0x58(%rsp), %rsi
divq %rsi
movq %rax, %rcx
movq 0x70(%rsp), %rax
xorl %edx, %edx
divq %rsi
movl 0x24(%rsp), %esi
movl 0x20(%rsp), %edx
jmp 0x9d2b
movl %r14d, 0x24(%rsp)
movl 0x84(%rsp,%rbp), %eax
movl 0x88(%rsp,%rbp), %ebx
movl %ebx, 0x20(%rsp)
movl %eax, 0x1c(%rsp)
xorl %edx, %edx
movl 0xc(%rsp), %esi
divl %esi
movl %eax, %ecx
movl 0x8c(%rsp,%rbp), %ebp
movl %ebp, %eax
xorl %edx, %edx
divl %esi
movl %ebx, %edx
movl %r14d, %esi
cmpl %edx, %esi
setne %dil
cmpl $-0x1, %esi
sete %sil
orb %dil, %sil
jne 0x9dcf
movq 0x28(%rsp), %rsi
movq 0x8(%rsi), %rsi
movl %edx, %edi
shlq $0x4, %rdi
movl 0x4(%rsi,%rdi), %edx
testl %edx, %edx
je 0x9dcf
addq %rdi, %rsi
cmpl %eax, %ecx
movl %eax, %edi
cmoval %ecx, %edi
movl %eax, %r8d
cmovbl %ecx, %eax
subl %edi, %eax
leal (%rax,%rdx,2), %eax
addl $-0x3, %eax
movl %ecx, %ecx
subq %r8, %rcx
movq %rcx, %rdx
negq %rdx
cmovsq %rcx, %rdx
movl %edx, %ecx
imulq %rax, %rcx
shrq %rcx
addl %edi, %ecx
movq 0x50(%rsp), %rax
movq 0x10(%rax), %rdi
movq (%rdi,%r14,8), %r8
movl 0x8(%rax), %r9d
movl 0x1c(%rsp), %eax
xorl %edx, %edx
divl %r9d
movsd (%r8,%rax,8), %xmm0
movq 0x48(%rsp), %r8
movl %ebx, %eax
movq (%rdi,%rax,8), %rdi
movl %ebp, %eax
xorl %edx, %edx
divl %r9d
mulsd (%rdi,%rax,8), %xmm0
movq 0x8(%rsi), %rax
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
addl $0x11, %r13d
incq %r15
jmp 0x9c34
movq 0x40(%rsp), %r12
jmp 0x9bf4
movq %r12, %rdi
callq 0x2150
movl 0x8(%rsp), %r8d
testl %r8d, %r8d
je 0x9e0f
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
movq 0x38(%rsp), %rdx
movl 0xc(%rsp), %ecx
jmp 0x9e2c
movq 0x10(%rsp), %rax
movq 0x38(%rax), %rsi
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
movq 0x38(%rsp), %rdx
movl 0xc(%rsp), %ecx
xorl %r8d, %r8d
callq 0x9e69
movq %rbx, %rax
addq $0x11088, %rsp          # imm = 0x11088
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1e1ab(%rip), %rax     # 0x27ff8
movq (%rax), %rdi
leaq 0x143e0(%rip), %rsi     # 0x1e237
leaq 0x148a0(%rip), %rdx     # 0x1e6fe
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x22c0
jmp 0x9e31