// File: /workspace/github_c_makefile_star2_O1/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, %ebp
movl %edx, %r12d
movq %rsi, %r13
movq %rdi, %r14
leaq 0x1c8a5(%rip), %rsi     # 0x2240f
callq 0x23e0
testq %rax, %rax
je 0x5fcc
movq %rax, %r15
leaq 0x60(%rsp), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0x20f0
cmpl $0x1, %eax
je 0x5b9e
xorl %eax, %eax
callq 0x4c81
movq 0x60(%rsp), %rdi
callq 0x4c6e
testl %eax, %eax
je 0x5fd4
movl %r12d, 0x8(%rsp)
movl %ebx, 0x34(%rsp)
movq %r15, %rdi
callq 0x15804
leaq 0x68(%rsp), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %r15, 0x48(%rsp)
movq %r15, %rcx
callq 0x20f0
cmpl $0x1, %eax
je 0x5be9
xorl %eax, %eax
callq 0x4c81
movl $0x10, %edi
callq 0x2330
movq %rax, %r14
movl %ebp, (%rax)
movl (%r13), %r12d
movl %r12d, 0x4(%rax)
movq %r12, %rdi
shlq $0x4, %rdi
callq 0x2330
movq %r14, 0x50(%rsp)
movq %rax, (%rsp)
movq %rax, 0x8(%r14)
testq %r12, %r12
movl %ebp, %ebx
je 0x5c66
movq (%rsp), %rax
leaq 0x8(%rax), %r14
movl $0x8, %r15d
xorl %ebp, %ebp
movq 0x8(%r13), %rax
movq (%rax,%r15), %rdi
movl %ebx, %esi
callq 0x4bf1
movl %ebp, -0x8(%r14)
movl %eax, -0x4(%r14)
movl %eax, %edi
movl $0x8, %esi
callq 0x2290
movq %rax, (%r14)
incq %rbp
addq $0x10, %r14
addq $0x58, %r15
cmpq %rbp, %r12
jne 0x5c30
movl %ebx, 0x14(%rsp)
movq %r12, 0x40(%rsp)
movq 0x68(%rsp), %rbp
testq %rbp, %rbp
je 0x5da6
movl 0x8(%rsp), %eax
movq %rax, 0x8(%rsp)
movl 0x14(%rsp), %eax
movq %rax, 0x58(%rsp)
xorl %r15d, %r15d
movl $0x1, %esi
movl $0x11000, %edx          # imm = 0x11000
leaq 0x70(%rsp), %rdi
movq 0x48(%rsp), %rcx
callq 0x20f0
movq %rax, %rbx
testl %ebx, %ebx
je 0x5d9d
movl $0x11, %r14d
leal -0x11(%r14), %r12d
movl 0x20(%rsp), %eax
cmpb %al, 0x80(%rsp,%r12)
jb 0x5d88
movl 0x70(%rsp,%r12), %esi
movl 0x74(%rsp,%r12), %edx
movq %r13, %rdi
leaq 0x3c(%rsp), %rcx
leaq 0x28(%rsp), %r8
xorl %r9d, %r9d
callq 0x13d50
movl 0x78(%rsp,%r12), %esi
movl 0x7c(%rsp,%r12), %edx
movq %r13, %rdi
leaq 0x38(%rsp), %rcx
leaq 0x18(%rsp), %r8
xorl %r9d, %r9d
callq 0x13d50
movl 0x3c(%rsp), %edx
movl 0x38(%rsp), %ecx
cmpl %ecx, %edx
setne %al
cmpl $-0x1, %edx
sete %sil
orb %al, %sil
jne 0x5d88
movq 0x28(%rsp), %rax
movq 0x18(%rsp), %rsi
cmpq %rsi, %rax
jbe 0x5d42
movq %rsi, 0x28(%rsp)
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rsi
movq 0x28(%rsp), %rax
subq %rax, %rsi
cmpq 0x8(%rsp), %rsi
jae 0x5d88
shlq $0x4, %rdx
movq (%rsp), %rdi
movq 0x8(%rdi,%rdx), %rsi
xorl %edx, %edx
movq 0x58(%rsp), %r8
divq %r8
incq (%rsi,%rax,8)
shlq $0x4, %rcx
movq 0x8(%rdi,%rcx), %rcx
movq 0x18(%rsp), %rax
xorl %edx, %edx
divq %r8
decq (%rcx,%rax,8)
incq %r15
cmpl %ebx, %r14d
jae 0x5d9d
addl $0x11, %r14d
cmpq %rbp, %r15
jb 0x5cbc
cmpq %rbp, %r15
jb 0x5c92
movq 0x48(%rsp), %rdi
callq 0x2150
movq 0x40(%rsp), %r9
testq %r9, %r9
movl 0x14(%rsp), %r8d
movl 0x34(%rsp), %eax
je 0x5e04
xorl %r10d, %r10d
movq %r10, %rdx
shlq $0x4, %rdx
movq (%rsp), %rcx
movl 0x4(%rcx,%rdx), %ecx
cmpq $0x2, %rcx
jb 0x5dfc
movq (%rsp), %rsi
movq 0x8(%rsi,%rdx), %rdx
movq (%rdx), %rsi
movl $0x1, %edi
addq (%rdx,%rdi,8), %rsi
movq %rsi, (%rdx,%rdi,8)
incq %rdi
cmpq %rdi, %rcx
jne 0x5dec
incq %r10
cmpq %r9, %r10
jne 0x5dc6
xorl %edx, %edx
divl %r8d
movl %eax, 0x8(%rsp)
cmpl $0x2, %eax
setl %al
testq %r9, %r9
sete %cl
orb %al, %cl
jne 0x5f6b
xorl %eax, %eax
movq %rax, 0x20(%rsp)
shlq $0x4, %rax
movq (%rsp), %rcx
movq 0x8(%rcx,%rax), %r15
movslq 0x4(%rcx,%rax), %r12
movl 0x8(%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
testq %r12, %r12
jle 0x5ea2
movl %ebp, %esi
leaq (,%rsi,8), %rax
movq %r15, %r8
subq %rax, %r8
xorl %r9d, %r9d
xorl %ecx, %ecx
movq (%r15,%r9,8), %rax
movq %rax, (%rdi,%r9,8)
addq %rax, %rcx
cmpq %rsi, %r9
jb 0x5e98
leaq 0x1(%r9), %r10
movq %rcx, %rax
cqto
idivq %r10
movq %rax, (%r8,%r9,8)
incq %r9
cmpq %r9, %r14
jne 0x5e78
jmp 0x5ea4
xorl %ecx, %ecx
movl %r13d, %eax
notl %eax
andl $0x1, %eax
movl %r12d, %r8d
subl %ebp, %r8d
addl %eax, %r8d
xorl %esi, %esi
cmpl %r8d, %ebp
jge 0x5f13
movl %r12d, %eax
subl %r13d, %eax
addl %ebp, %eax
movl %r13d, %r9d
subl %ebp, %r9d
movslq %ebp, %r10
movslq %eax, %r11
movslq %r8d, %rbx
xorl %esi, %esi
movq %rcx, %rax
cqto
idivq %r14
movq %rax, (%r15,%r10,8)
cmpq %r11, %r10
jge 0x5f0b
movslq %esi, %rsi
subq (%rdi,%rsi,8), %rcx
leal (%r9,%r10), %eax
cltq
movq (%r15,%rax,8), %rax
movq %rax, (%rdi,%rsi,8)
addq %rax, %rcx
incl %esi
cmpl %r13d, %esi
movl $0x0, %eax
cmovel %eax, %esi
incq %r10
cmpq %r10, %rbx
jne 0x5ed5
cmpl %r12d, %r8d
movl $0x0, %r10d
jge 0x5f50
addl %r12d, %ebp
movslq %r8d, %r8
movslq %esi, %rsi
subq (%rdi,%rsi,8), %rcx
movl %ebp, %eax
subl %r8d, %eax
movslq %eax, %r9
movq %rcx, %rax
cqto
idivq %r9
movq %rax, (%r15,%r8,8)
incl %esi
cmpl %r13d, %esi
cmovel %r10d, %esi
incq %r8
cmpq %r12, %r8
jne 0x5f24
callq 0x2060
movq 0x20(%rsp), %rax
incq %rax
movq 0x40(%rsp), %r9
cmpq %r9, %rax
jne 0x5e23
testq %r9, %r9
je 0x5fb5
xorl %eax, %eax
movabsq $0x100000000, %rcx      # imm = 0x100000000
movq %rax, %rsi
shlq $0x4, %rsi
movq (%rsp), %rdx
movl 0x4(%rdx,%rsi), %edx
testq %rdx, %rdx
je 0x5fad
movq (%rsp), %rdi
movq 0x8(%rdi,%rsi), %rsi
xorl %edi, %edi
xorl %r8d, %r8d
orq %rdi, (%rsi,%r8,8)
incq %r8
addq %rcx, %rdi
cmpq %r8, %rdx
jne 0x5f9e
incq %rax
cmpq %r9, %rax
jne 0x5f7c
movq 0x50(%rsp), %rax
addq $0x11078, %rsp          # imm = 0x11078
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x2799
callq 0x2771