// File: /workspace/github_c_makefile_star2_O1/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 $0x11068, %rsp          # imm = 0x11068
movl %r9d, 0x4(%rsp)
movl %r8d, (%rsp)
movq %rcx, %rbx
movq %rdx, 0x8(%rsp)
movq %rsi, 0x30(%rsp)
leaq 0x158d8(%rip), %rsi     # 0x2240f
callq 0x23e0
testq %rax, %rax
je 0xcdf2
movq %rax, %r15
leaq 0x40(%rsp), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0x20f0
cmpl $0x1, %eax
je 0xcb6b
xorl %eax, %eax
callq 0x4c81
movq 0x40(%rsp), %rdi
callq 0x4c6e
testl %eax, %eax
je 0xcded
movq %r15, %rdi
callq 0x15804
leaq 0x48(%rsp), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x20f0
cmpl $0x1, %eax
je 0xcba8
xorl %eax, %eax
callq 0x4c81
movq %rbx, 0x20(%rsp)
movl 0x4(%rsp), %edx
testl %edx, %edx
je 0xcbbf
movq 0x8(%rsp), %rdi
movl (%rsp), %esi
jmp 0xcbcd
movq 0x8(%rsp), %rax
movq 0x38(%rax), %rdi
movl (%rsp), %esi
xorl %edx, %edx
callq 0xc954
movq %rax, 0x18(%rsp)
movq 0x48(%rsp), %r13
testq %r13, %r13
je 0xcda1
movb 0x110a0(%rsp), %bl
movl (%rsp), %eax
movq %rax, 0x38(%rsp)
xorl %ebp, %ebp
movq %r15, 0x28(%rsp)
movl $0x1, %esi
movl $0x11000, %edx          # imm = 0x11000
leaq 0x60(%rsp), %rdi
movq %r15, %rcx
callq 0x20f0
movq %rax, %r14
testl %r14d, %r14d
je 0xcd93
movl $0x11, %r15d
movl 0x4(%rsp), %r10d
leal -0x11(%r15), %r12d
cmpb %bl, 0x70(%rsp,%r12)
jb 0xcd7e
movl 0x60(%rsp,%r12), %esi
testl %r10d, %r10d
je 0xccaa
movl 0x64(%rsp,%r12), %edx
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
leaq 0x14(%rsp), %rcx
leaq 0x58(%rsp), %r8
xorl %r9d, %r9d
callq 0x13d50
movl 0x68(%rsp,%r12), %esi
movl 0x6c(%rsp,%r12), %edx
movq %rbx, %rdi
movb 0x110a0(%rsp), %bl
leaq 0x10(%rsp), %rcx
leaq 0x50(%rsp), %r8
xorl %r9d, %r9d
callq 0x13d50
movl 0x4(%rsp), %r10d
movq 0x58(%rsp), %rax
xorl %edx, %edx
movq 0x38(%rsp), %rsi
divq %rsi
movq %rax, %rcx
movq 0x50(%rsp), %rax
xorl %edx, %edx
divq %rsi
jmp 0xccce
movl %esi, 0x14(%rsp)
movl 0x64(%rsp,%r12), %eax
movl 0x68(%rsp,%r12), %ecx
movl %ecx, 0x10(%rsp)
xorl %edx, %edx
movl (%rsp), %esi
divl %esi
movl %eax, %ecx
movl 0x6c(%rsp,%r12), %eax
xorl %edx, %edx
divl %esi
movl 0x14(%rsp), %edi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
cmpq %rdx, %rdi
je 0xcd7e
cmpl 0x10(%rsp), %edi
jne 0xcd7e
movq 0x18(%rsp), %rdx
movq 0x8(%rdx), %rsi
shlq $0x4, %rdi
movl 0x4(%rsi,%rdi), %edx
testl %edx, %edx
je 0xcd7e
addq %rdi, %rsi
cmpl %eax, %ecx
movl %eax, %edi
cmoval %ecx, %edi
movl %eax, %r8d
cmovbl %ecx, %eax
subl %edi, %eax
movl %ecx, %ecx
subq %r8, %rcx
movq %rcx, %r8
negq %r8
cmovsq %rcx, %r8
leal (%rax,%rdx,2), %eax
addl $-0x3, %eax
movl %r8d, %ecx
imulq %rax, %rcx
shrq %rcx
addl %edi, %ecx
movq 0x30(%rsp), %r9
movq 0x10(%r9), %rdi
movl 0x60(%rsp,%r12), %edx
movl 0x64(%rsp,%r12), %eax
movq (%rdi,%rdx,8), %r8
movl 0x8(%r9), %r9d
xorl %edx, %edx
divl %r9d
movsd (%r8,%rax,8), %xmm0
movl 0x68(%rsp,%r12), %eax
movq (%rdi,%rax,8), %rdi
movl 0x6c(%rsp,%r12), %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 %rbp
cmpl %r14d, %r15d
jae 0xcd93
addl $0x11, %r15d
cmpq %r13, %rbp
jb 0xcc29
cmpq %r13, %rbp
movq 0x28(%rsp), %r15
jb 0xcbfb
movq %r15, %rdi
callq 0x2150
movl 0x4(%rsp), %r8d
testl %r8d, %r8d
je 0xcdca
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
movq 0x20(%rsp), %rdx
movl (%rsp), %ecx
jmp 0xcde6
movq 0x8(%rsp), %rax
movq 0x38(%rax), %rsi
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
movq 0x20(%rsp), %rdx
movl (%rsp), %ecx
xorl %r8d, %r8d
callq 0xce09
jmp 0xcdf4
callq 0x28d6
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x11068, %rsp          # imm = 0x11068
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq