// File: /workspace/github_c_makefile_star2_O3/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 $0x11078, %rsp          # imm = 0x11078
movl %r9d, 0x4(%rsp)
movl %r8d, (%rsp)
movq %rcx, %rbx
movq %rdx, 0x8(%rsp)
movq %rsi, 0x38(%rsp)
leaq 0x15a9f(%rip), %rsi     # 0x23449
callq 0x23e0
testq %rax, %rax
je 0xdc87
movq %rax, %r14
leaq 0x50(%rsp), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0x20f0
cmpl $0x1, %eax
je 0xd9de
xorl %eax, %eax
callq 0x4b91
movq 0x50(%rsp), %rdi
callq 0x4b7e
testl %eax, %eax
je 0xdc82
movq %r14, %rdi
callq 0x166b8
leaq 0x58(%rsp), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x20f0
cmpl $0x1, %eax
je 0xda1b
xorl %eax, %eax
callq 0x4b91
movl 0x4(%rsp), %edx
testl %edx, %edx
movq %rbx, 0x28(%rsp)
je 0xda32
movq 0x8(%rsp), %rdi
movl (%rsp), %esi
jmp 0xda40
movq 0x8(%rsp), %rax
movq 0x38(%rax), %rdi
movl (%rsp), %esi
xorl %edx, %edx
callq 0xd7db
movq %rax, 0x20(%rsp)
movq 0x58(%rsp), %r12
testq %r12, %r12
je 0xdc36
movl (%rsp), %eax
movq %rax, 0x40(%rsp)
xorl %r15d, %r15d
movq %r14, 0x30(%rsp)
movl $0x1, %esi
movl $0x11000, %edx          # imm = 0x11000
leaq 0x70(%rsp), %rdi
movq %r14, %rcx
callq 0x20f0
movq %rax, %r14
testl %r14d, %r14d
je 0xdc28
movl $0x11, %r13d
movl 0x4(%rsp), %r10d
movb 0x110b0(%rsp), %r11b
leal -0x11(%r13), %ebp
cmpb %r11b, 0x80(%rsp,%rbp)
jb 0xdc13
movl 0x70(%rsp,%rbp), %esi
testl %r10d, %r10d
movq %rsi, 0x48(%rsp)
je 0xdb46
movl 0x74(%rsp,%rbp), %edx
movq %r14, %rbx
movq %r12, %r14
movq 0x8(%rsp), %r12
movq %r12, %rdi
movl %edx, 0x14(%rsp)
leaq 0x1c(%rsp), %rcx
leaq 0x68(%rsp), %r8
xorl %r9d, %r9d
callq 0x14b8d
movl 0x78(%rsp,%rbp), %eax
movl 0x7c(%rsp,%rbp), %ebp
movq %r12, %rdi
movq %r14, %r12
movq %rbx, %r14
movl %eax, %ebx
movl %eax, %esi
movl %ebp, %edx
leaq 0x18(%rsp), %rcx
leaq 0x60(%rsp), %r8
xorl %r9d, %r9d
callq 0x14b8d
movb 0x110b0(%rsp), %r11b
movl 0x4(%rsp), %r10d
movq 0x68(%rsp), %rax
xorl %edx, %edx
movq 0x40(%rsp), %rsi
divq %rsi
movq %rax, %rcx
movq 0x60(%rsp), %rax
xorl %edx, %edx
divq %rsi
movl 0x1c(%rsp), %esi
movl 0x18(%rsp), %edx
jmp 0xdb6f
movl %esi, 0x1c(%rsp)
movl 0x74(%rsp,%rbp), %eax
movl 0x78(%rsp,%rbp), %ebx
movl %ebx, 0x18(%rsp)
movl %eax, 0x14(%rsp)
xorl %edx, %edx
movl (%rsp), %edi
divl %edi
movl %eax, %ecx
movl 0x7c(%rsp,%rbp), %ebp
movl %ebp, %eax
xorl %edx, %edx
divl %edi
movl %ebx, %edx
cmpl %edx, %esi
setne %dil
cmpl $-0x1, %esi
sete %sil
orb %dil, %sil
jne 0xdc13
movq 0x20(%rsp), %rsi
movq 0x8(%rsi), %rsi
movl %edx, %edi
shlq $0x4, %rdi
movl 0x4(%rsi,%rdi), %edx
testl %edx, %edx
je 0xdc13
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 0x38(%rsp), %rax
movq 0x10(%rax), %rdi
movq 0x48(%rsp), %rdx
movq (%rdi,%rdx,8), %r8
movl 0x8(%rax), %r9d
movl 0x14(%rsp), %eax
xorl %edx, %edx
divl %r9d
movsd (%r8,%rax,8), %xmm0
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)
incq %r15
cmpl %r14d, %r13d
jae 0xdc28
addl $0x11, %r13d
cmpq %r12, %r15
jb 0xda9e
cmpq %r12, %r15
movq 0x30(%rsp), %r14
jb 0xda68
movq %r14, %rdi
callq 0x2150
movl 0x4(%rsp), %r8d
testl %r8d, %r8d
je 0xdc5f
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
movq 0x28(%rsp), %rdx
movl (%rsp), %ecx
jmp 0xdc7b
movq 0x8(%rsp), %rax
movq 0x38(%rax), %rsi
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
movq 0x28(%rsp), %rdx
movl (%rsp), %ecx
xorl %r8d, %r8d
callq 0xdc9e
jmp 0xdc89
callq 0x28d6
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x11078, %rsp          # imm = 0x11078
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq