// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x110c0, %rsp          # imm = 0x110C0
movb 0x10(%rbp), %al
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movl %r8d, -0x2c(%rbp)
movl %r9d, -0x30(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x23f90(%rip), %rsi     # 0x32311
callq 0x23f0
movq %rax, -0x11098(%rbp)
cmpq $0x0, -0x11098(%rbp)
jne 0xe3a4
movq $0x0, -0x8(%rbp)
jmp 0xe784
movq -0x11098(%rbp), %rcx
leaq -0x80(%rbp), %rdi
movl $0x8, %esi
movl $0x1, %edx
callq 0x20f0
movl %eax, -0x3c(%rbp)
cmpl $0x1, -0x3c(%rbp)
je 0xe3ce
movb $0x0, %al
callq 0x2c00
movq -0x80(%rbp), %rdi
callq 0x2bc0
cmpl $0x0, %eax
jne 0xe408
movq 0x2ac15(%rip), %rax     # 0x38ff8
movq (%rax), %rdi
leaq 0x22e2a(%rip), %rsi     # 0x31217
leaq 0x232f2(%rip), %rdx     # 0x316e6
movb $0x0, %al
callq 0x22c0
movq $0x0, -0x8(%rbp)
jmp 0xe784
movq -0x11098(%rbp), %rdi
callq 0x1bac0
movq -0x11098(%rbp), %rcx
leaq -0x68(%rbp), %rdi
movl $0x8, %esi
movl $0x1, %edx
callq 0x20f0
movl %eax, -0x3c(%rbp)
cmpl $0x1, -0x3c(%rbp)
je 0xe43e
movb $0x0, %al
callq 0x2c00
cmpl $0x0, -0x30(%rbp)
je 0xe45c
movq -0x20(%rbp), %rdi
movl -0x2c(%rbp), %esi
movl -0x30(%rbp), %edx
callq 0xe080
movq %rax, -0x110a8(%rbp)
jmp 0xe476
movq -0x20(%rbp), %rax
movq 0x38(%rax), %rdi
movl -0x2c(%rbp), %esi
movl -0x30(%rbp), %edx
callq 0xe080
movq %rax, -0x110a8(%rbp)
movq -0x110a8(%rbp), %rax
movq %rax, -0x11088(%rbp)
movq $0x0, -0x70(%rbp)
movq $0x0, -0x78(%rbp)
movq -0x70(%rbp), %rax
cmpq -0x68(%rbp), %rax
jae 0xe72b
leaq -0x11080(%rbp), %rdi
movq -0x11098(%rbp), %rcx
movl $0x1, %esi
movl $0x11000, %edx          # imm = 0x11000
callq 0x20f0
movl %eax, -0x3c(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %ecx
xorl %eax, %eax
cmpl -0x3c(%rbp), %ecx
movb %al, -0x110a9(%rbp)
jae 0xe4ea
movq -0x70(%rbp), %rax
cmpq -0x68(%rbp), %rax
setb %al
movb %al, -0x110a9(%rbp)
movb -0x110a9(%rbp), %al
testb $0x1, %al
jne 0xe4f9
jmp 0xe726
movl -0x34(%rbp), %eax
movzbl -0x11070(%rbp,%rax), %eax
movzbl 0x10(%rbp), %ecx
cmpl %ecx, %eax
jge 0xe511
jmp 0xe70c
cmpl $0x0, -0x30(%rbp)
je 0xe587
movq -0x20(%rbp), %rdi
movl -0x34(%rbp), %eax
movl -0x11080(%rbp,%rax), %esi
movl -0x34(%rbp), %eax
movl -0x1107c(%rbp,%rax), %edx
leaq -0x40(%rbp), %rcx
leaq -0x58(%rbp), %r8
xorl %r9d, %r9d
callq 0x19980
movq -0x20(%rbp), %rdi
movl -0x34(%rbp), %eax
movl -0x11078(%rbp,%rax), %esi
movl -0x34(%rbp), %eax
movl -0x11074(%rbp,%rax), %edx
leaq -0x44(%rbp), %rcx
leaq -0x60(%rbp), %r8
xorl %r9d, %r9d
callq 0x19980
movq -0x58(%rbp), %rax
movl -0x2c(%rbp), %ecx
xorl %edx, %edx
divq %rcx
movl %eax, -0x48(%rbp)
movq -0x60(%rbp), %rax
movl -0x2c(%rbp), %ecx
xorl %edx, %edx
divq %rcx
movl %eax, -0x4c(%rbp)
jmp 0xe5c5
movl -0x34(%rbp), %eax
movl -0x11080(%rbp,%rax), %eax
movl %eax, -0x40(%rbp)
movl -0x34(%rbp), %eax
movl -0x11078(%rbp,%rax), %eax
movl %eax, -0x44(%rbp)
movl -0x34(%rbp), %eax
movl -0x1107c(%rbp,%rax), %eax
xorl %edx, %edx
divl -0x2c(%rbp)
movl %eax, -0x48(%rbp)
movl -0x34(%rbp), %eax
movl -0x11074(%rbp,%rax), %eax
xorl %edx, %edx
divl -0x2c(%rbp)
movl %eax, -0x4c(%rbp)
movl -0x40(%rbp), %eax
cmpl -0x44(%rbp), %eax
jne 0xe70a
cmpl $-0x1, -0x40(%rbp)
je 0xe70a
movq -0x11088(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x40(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x11090(%rbp)
movq -0x11090(%rbp), %rax
cmpl $0x0, 0x4(%rax)
je 0xe6fe
movl -0x48(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jbe 0xe628
movl -0x48(%rbp), %eax
movl %eax, -0x1109c(%rbp)
movl -0x4c(%rbp), %eax
movl %eax, -0x48(%rbp)
movl -0x1109c(%rbp), %eax
movl %eax, -0x4c(%rbp)
movq -0x11090(%rbp), %rax
movl 0x4(%rax), %eax
shll %eax
subl -0x4c(%rbp), %eax
addl -0x48(%rbp), %eax
subl $0x3, %eax
movl %eax, %eax
movl -0x4c(%rbp), %ecx
subl -0x48(%rbp), %ecx
movl %ecx, %ecx
imulq %rcx, %rax
movl $0x2, %ecx
cqto
idivq %rcx
movl -0x4c(%rbp), %ecx
addq %rcx, %rax
movl %eax, -0x38(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x34(%rbp), %ecx
movl -0x11080(%rbp,%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0x110c0(%rbp)
movl -0x34(%rbp), %eax
movl -0x1107c(%rbp,%rax), %eax
movq -0x18(%rbp), %rcx
xorl %edx, %edx
divl 0x8(%rcx)
movl %eax, %ecx
movq -0x110c0(%rbp), %rax
movl %ecx, %ecx
movsd (%rax,%rcx,8), %xmm0
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x34(%rbp), %ecx
movl -0x11078(%rbp,%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0x110b8(%rbp)
movl -0x34(%rbp), %eax
movl -0x11074(%rbp,%rax), %eax
movq -0x18(%rbp), %rcx
xorl %edx, %edx
divl 0x8(%rcx)
movl %eax, %ecx
movq -0x110b8(%rbp), %rax
movl %ecx, %ecx
movsd (%rax,%rcx,8), %xmm2
movq -0x11090(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x38(%rbp), %ecx
movsd (%rax,%rcx,8), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0xe70c
movl -0x34(%rbp), %eax
addl $0x11, %eax
movl %eax, -0x34(%rbp)
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0xe4c9
jmp 0xe494
movq -0x11098(%rbp), %rdi
callq 0x2150
cmpl $0x0, -0x30(%rbp)
je 0xe75a
movq -0x11088(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
movl -0x2c(%rbp), %ecx
movl -0x30(%rbp), %r8d
callq 0xe7a0
jmp 0xe779
movq -0x11088(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x38(%rax), %rsi
movq -0x28(%rbp), %rdx
movl -0x2c(%rbp), %ecx
movl -0x30(%rbp), %r8d
callq 0xe7a0
movq -0x11088(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x110c0, %rsp          # imm = 0x110C0
popq %rbp
retq
nopw %cs:(%rax,%rax)