// File: /workspace/github_c_makefile_star2_O2/c-zhou[P]yahs/break.c
// Function: link_mat_from_file
link_mat_t *link_mat_from_file(const char *f, asm_dict_t *dict, uint32_t dist_thres, uint32_t resolution, double noise, uint32_t move_avg, uint8_t mq)
{
    FILE *fp;
    uint32_t i, j, m, n, i0, i1;
    uint64_t p0, p1, pair_n, pair_c, intra_c;
    int64_t magic_number;
    uint8_t buffer[BUFF_SIZE * 17];

    fp = fopen(f, "r");
    if (fp == NULL) {
        fprintf(stderr, "[E::%s] cannot open file %s for reading\n", __func__, f);
        exit(EXIT_FAILURE);
    }

    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__);
        exit(EXIT_FAILURE);
    }
    file_seek_skip_sdict(fp);
    m = fread(&pair_n, sizeof(uint64_t), 1, fp);
    if (m != 1) bin_fread_error();

    link_mat_t *link_mat = (link_mat_t *) malloc(sizeof(link_mat_t));
    link_mat->b = resolution;
    link_mat->n = dict->n;
    link_mat->link = (link_t *) malloc(link_mat->n * sizeof(link_t));
    for (i = 0; i < link_mat->n; ++i) {
        n = div_ceil(dict->s[i].len, resolution);
        link_mat->link[i].s = i;
        link_mat->link[i].n = n;
        link_mat->link[i].link = (int64_t *) calloc(n, sizeof(int64_t));
    }

    pair_c = 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;

            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);

            if (i0 == i1 && i0 != UINT32_MAX) {
                if (p0 > p1)
                    SWAP(uint64_t, p0, p1);
                if (p1 - p0 < dist_thres) {
                    link_mat->link[i0].link[p0 / resolution] += 1;
                    link_mat->link[i1].link[p1 / resolution] -= 1;
                    ++intra_c;
                }
            }
        }
    }
    fclose(fp);

#ifdef DEBUG
    fprintf(stderr, "[DEBUG::%s] %lu read pairs processed, intra links: %lu \n", __func__, pair_c, intra_c);
#endif
    
    int64_t *link;
    for (i = 0; i < link_mat->n; ++i) {
        link = link_mat->link[i].link;
        n = link_mat->link[i].n;
        for (j = 1; j < n; ++j)
            link[j] += link[j - 1];
    }

#ifdef REMOVE_NOISE
    double l;
    noise = noise * dist_thres * (dist_thres + resolution) / 2;
    for (i = 0; i < link_mat->n; ++i) {
        link = link_mat->link[i].link;
        n = link_mat->link[i].n;
        for (j = 0; j < n; ++j) {
            l = (double) link[j] - noise;
            link[j] = (uint32_t) MAX(l, .1);
        }
    }
#endif

    int32_t ma_k = move_avg / resolution;
    if (ma_k > 1)
        for (i = 0; i < link_mat->n; ++i)
            calc_moving_average(link_mat->link[i].link, link_mat->link[i].n, ma_k);

    for (i = 0; i < link_mat->n; ++i) {
        int64_t *link_c = link_mat->link[i].link;
        n = link_mat->link[i].n;
        for (j = 0; j < n; ++j)
            link_c[j] |= (int64_t) j << 32;
    }
    
    return link_mat;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x11078, %rsp          # imm = 0x11078
movl %r9d, 0x20(%rsp)
movl %r8d, %ebx
movl %ecx, 0x4(%rsp)
movl %edx, %r12d
movq %rsi, %r13
movq %rdi, %r14
leaq 0x1ba49(%rip), %rsi     # 0x1f311
callq 0x23e0
testq %rax, %rax
je 0x3cca
movq %rax, %r15
leaq 0x60(%rsp), %rdi
pushq $0x8
popq %rsi
pushq $0x1
popq %rdx
movq %rax, %rcx
callq 0x20f0
cmpl $0x1, %eax
je 0x38f8
xorl %eax, %eax
callq 0x2986
movq 0x60(%rsp), %rdi
callq 0x2973
testl %eax, %eax
je 0x3cee
movl %ebx, 0x34(%rsp)
movq %r15, %rdi
callq 0x11940
leaq 0x68(%rsp), %rdi
pushq $0x8
popq %rbx
pushq $0x1
popq %rdx
movq %rbx, %rsi
movq %r15, 0x40(%rsp)
movq %r15, %rcx
callq 0x20f0
cmpl $0x1, %eax
je 0x393d
xorl %eax, %eax
callq 0x2986
pushq $0x10
popq %rdi
callq 0x2330
movq %rax, %r14
movl 0x4(%rsp), %eax
movl %eax, (%r14)
movl (%r13), %edi
movl %edi, 0x4(%r14)
movq %rdi, 0x10(%rsp)
shlq $0x4, %rdi
callq 0x2330
movq %r14, 0x50(%rsp)
movq %rax, 0x8(%r14)
movq %rax, 0x8(%rsp)
movq %rax, %r15
addq $0x8, %r15
xorl %ebp, %ebp
movq %rbx, %r14
cmpq %rbp, 0x10(%rsp)
je 0x39b9
movq 0x8(%r13), %rax
movq (%rax,%r14), %rdi
movl 0x4(%rsp), %esi
callq 0x28ff
movl %ebp, -0x8(%r15)
movl %eax, -0x4(%r15)
movl %eax, %edi
movq %rbx, %rsi
callq 0x2290
movq %rax, (%r15)
incq %rbp
addq $0x10, %r15
addq $0x58, %r14
jmp 0x397f
movq 0x68(%rsp), %r15
movl %r12d, %eax
movq %rax, 0x18(%rsp)
movl 0x4(%rsp), %eax
movq %rax, 0x58(%rsp)
xorl %ebx, %ebx
pushq $0x1
popq %r12
cmpq %r15, %rbx
jae 0x3ad2
movl $0x11000, %edx          # imm = 0x11000
leaq 0x70(%rsp), %rdi
movq %r12, %rsi
movq 0x40(%rsp), %rcx
callq 0x20f0
movq %rax, %rbp
xorl %r14d, %r14d
cmpl %ebp, %r14d
jae 0x39d1
cmpq %r15, %rbx
jae 0x39d1
movl %r14d, %r12d
movl 0x20(%rsp), %eax
cmpb %al, 0x80(%rsp,%r12)
jae 0x3a1f
addl $0x11, %r14d
incq %rbx
jmp 0x39fb
movl 0x70(%rsp,%r12), %esi
movl 0x74(%rsp,%r12), %edx
movq %r13, %rdi
leaq 0x3c(%rsp), %rcx
leaq 0x48(%rsp), %r8
xorl %r9d, %r9d
callq 0x100d2
movl 0x78(%rsp,%r12), %esi
movl 0x7c(%rsp,%r12), %edx
movq %r13, %rdi
leaq 0x38(%rsp), %rcx
leaq 0x28(%rsp), %r8
xorl %r9d, %r9d
callq 0x100d2
movl 0x3c(%rsp), %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, %rcx
je 0x3a16
cmpl 0x38(%rsp), %ecx
jne 0x3a16
movq 0x48(%rsp), %rdx
movq 0x28(%rsp), %rsi
cmpq %rsi, %rdx
jbe 0x3a8f
movq %rsi, 0x48(%rsp)
movq %rdx, 0x28(%rsp)
movq %rsi, %rax
jmp 0x3a95
movq %rdx, %rax
movq %rsi, %rdx
subq %rax, %rdx
cmpq 0x18(%rsp), %rdx
jae 0x3a16
shlq $0x4, %rcx
movq 0x8(%rsp), %rdx
movq 0x8(%rdx,%rcx), %rcx
xorl %edx, %edx
movq 0x58(%rsp), %rsi
divq %rsi
incq (%rcx,%rax,8)
movq 0x28(%rsp), %rax
xorl %edx, %edx
divq %rsi
decq (%rcx,%rax,8)
jmp 0x3a16
movq 0x40(%rsp), %rdi
callq 0x2150
xorl %r8d, %r8d
movq 0x10(%rsp), %r14
movl 0x34(%rsp), %eax
cmpq %r14, %r8
je 0x3b1d
movq %r8, %rdx
shlq $0x4, %rdx
movq 0x8(%rsp), %rsi
movq 0x8(%rsi,%rdx), %rcx
movl 0x4(%rsi,%rdx), %edx
movq %r12, %rsi
cmpq %rdx, %rsi
jae 0x3b18
movq -0x8(%rcx,%rsi,8), %rdi
addq %rdi, (%rcx,%rsi,8)
incq %rsi
jmp 0x3b05
incq %r8
jmp 0x3ae8
xorl %ebx, %ebx
xorl %edx, %edx
divl 0x4(%rsp)
movl %eax, 0x18(%rsp)
cmpl $0x2, %eax
jl 0x3c72
xorl %eax, %eax
cmpq %r14, %rax
je 0x3c72
movq %rax, 0x20(%rsp)
shlq $0x4, %rax
movq 0x8(%rsp), %rcx
movq 0x8(%rcx,%rax), %r15
movslq 0x4(%rcx,%rax), %r12
movl 0x18(%rsp), %r13d
cmpl %r13d, %r12d
cmovll %r12d, %r13d
movl %r13d, %ebp
sarl %ebp
movslq %r13d, %r14
leaq (,%r14,8), %rdi
callq 0x2330
movq %rax, %rdi
movslq %ebp, %rsi
testl %r14d, %r14d
movl $0x0, %r8d
cmovgl %r14d, %r8d
leaq (,%rsi,8), %rax
movq %r15, %r9
subq %rax, %r9
xorl %r10d, %r10d
xorl %ecx, %ecx
cmpq %r10, %r8
je 0x3bc6
movq (%r15,%r10,8), %rax
movq %rax, (%rdi,%r10,8)
addq %rax, %rcx
leaq 0x1(%r10), %r11
cmpq %rsi, %r10
jl 0x3bc1
movq %rcx, %rax
cqto
idivq %r11
movq %rax, (%r9,%r10,8)
movq %r11, %r10
jmp 0x3b9c
movl %r13d, %eax
notl %eax
andl $0x1, %eax
movl %r12d, %edx
subl %ebp, %edx
addl %eax, %edx
movl %r12d, %eax
subl %r13d, %eax
addl %ebp, %eax
movl %r13d, %r10d
subl %ebp, %r10d
movslq %edx, %r8
movslq %eax, %r11
xorl %r9d, %r9d
cmpq %r8, %rsi
jge 0x3c29
movq %rcx, %rax
cqto
idivq %r14
movq %rax, (%r15,%rsi,8)
cmpq %r11, %rsi
jge 0x3c24
movslq %r9d, %r9
subq (%rdi,%r9,8), %rcx
leal (%r10,%rsi), %eax
cltq
movq (%r15,%rax,8), %rax
movq %rax, (%rdi,%r9,8)
addq %rax, %rcx
incl %r9d
cmpl %r13d, %r9d
cmovel %ebx, %r9d
incq %rsi
jmp 0x3bec
addl %r12d, %ebp
movq 0x10(%rsp), %r14
cmpq %r12, %r8
jge 0x3c60
movslq %r9d, %r9
subq (%rdi,%r9,8), %rcx
movl %ebp, %eax
subl %r8d, %eax
movslq %eax, %rsi
movq %rcx, %rax
cqto
idivq %rsi
movq %rax, (%r15,%r8,8)
incl %r9d
cmpl %r13d, %r9d
cmovel %ebx, %r9d
incq %r8
jmp 0x3c31
callq 0x2060
movq 0x20(%rsp), %rax
incq %rax
jmp 0x3b34
xorl %eax, %eax
movabsq $0x100000000, %rcx      # imm = 0x100000000
cmpq %r14, %rax
je 0x3cb3
movq %rax, %rsi
shlq $0x4, %rsi
movq 0x8(%rsp), %rdi
movq 0x8(%rdi,%rsi), %rdx
movl 0x4(%rdi,%rsi), %esi
xorl %edi, %edi
xorl %r8d, %r8d
cmpq %r8, %rsi
je 0x3cae
orq %rdi, (%rdx,%r8,8)
incq %r8
addq %rcx, %rdi
jmp 0x3c9d
incq %rax
jmp 0x3c7e
movq 0x50(%rsp), %rax
addq $0x11078, %rsp          # imm = 0x11078
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x24327(%rip), %rax     # 0x27ff8
movq (%rax), %rdi
leaq 0x1a515(%rip), %rsi     # 0x1e1f0
leaq 0x1aa22(%rip), %rdx     # 0x1e704
movq %r14, %rcx
xorl %eax, %eax
callq 0x22c0
jmp 0x3d0d
movq 0x24303(%rip), %rax     # 0x27ff8
movq (%rax), %rdi
leaq 0x1a538(%rip), %rsi     # 0x1e237
leaq 0x1a9fe(%rip), %rdx     # 0x1e704
xorl %eax, %eax
callq 0x22c0
pushq $0x1
popq %rdi
callq 0x24b0