// File: /workspace/github_c_makefile_star2_O2/c-zhou[P]yahs/link.c
// Function: dump_links_from_pa5_file
void dump_links_from_pa5_file(const char *f, const char *fai, uint32_t ml, uint8_t mq, uint32_t rl, double q_drop, const char *out)
{
    FILE *fo;
    iostream_t *fp;
    char *line;
    char cname0[4096], cname1[4096];
    uint32_t s0, s1, e0, e1, i0, i1, p0, p1;
    uint8_t q, q0, q1;
    uint64_t rec_c, pair_c, inter_c, intra_c, sd_l, n, m, max_m;
    cov_t *cov;

    khash_t(str) *hmseq; // for absent sequences
    khint_t k;
    int absent;
    hmseq = kh_init(str);

    sdict_t *dict = make_sdict_from_index(fai, ml);

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

    fo = fopen(out, "w");
    if (fo == NULL) {
        fprintf(stderr, "[E::%s] cannot open file %s for writing\n", __func__, out);
        exit(EXIT_FAILURE);
    }
    write_bin_header(fo);
    sd_l = write_sequence_dictionary(fo, dict);

    rl >>= 1;
    rec_c = pair_c = inter_c = intra_c = 0;
    cov = cov_init(dict->n);
    n = 0;
    max_m = 0x7FFFFFFULL; // 128MB - ~1GB mem for covs

    fwrite(&pair_c, sizeof(uint64_t), 1, fo);

    while ((line = iostream_getline(fp)) != NULL) {
        if (is_empty_line(line))
            continue;

        cname0[0] = cname1[0] = '\0';
        p0 = p1 = UINT32_MAX;
        q0 = q1 = 255;
        sscanf(line, "%*s %s %u %s %u %hhu %hhu", cname0, &p0, cname1, &p1, &q0, &q1);

        i0 = sd_get(dict, cname0);
        i1 = sd_get(dict, cname1);
        q = MIN(q0, q1);

        if (i0 == UINT32_MAX) {
            k = kh_put(str, hmseq, cname0, &absent);
            if (absent) {
                kh_key(hmseq, k) = strdup(cname0);
                fprintf(stderr, "[W::%s] sequence \"%s\" not found \n", __func__, cname0);
            }
        } else if (i1 == UINT32_MAX) {
            k = kh_put(str, hmseq, cname1, &absent);
            if (absent) {
                kh_key(hmseq, k) = strdup(cname1);
                fprintf(stderr, "[W::%s] sequence \"%s\" not found \n", __func__, cname1);
            }
        } else if (q >= mq) {
            // update read coverage
            // fix bad alignments
            s0 = MIN_MAX(p0 - MIN(p0, rl), 0, dict->s[i0].len - 1);
            e0 = MIN_MAX(p0 + rl, 0, dict->s[i0].len - 1);
            s1 = MIN_MAX(p1 - MIN(p1, rl), 0, dict->s[i1].len - 1);
            e1 = MIN_MAX(p1 + rl, 0, dict->s[i1].len - 1);
            kv_push(uint64_t, cov->p[i0], (uint64_t) s0 << 32 | (uint32_t) 1);
            kv_push(uint64_t, cov->p[i0], (uint64_t) e0 << 32 | (uint32_t) -1);
            kv_push(uint64_t, cov->p[i1], (uint64_t) s1 << 32 | (uint32_t) 1);
            kv_push(uint64_t, cov->p[i1], (uint64_t) e1 << 32 | (uint32_t) -1);
            n += 4;
            if (n > max_m) {
                m = pos_compression(cov); // m is the position size after compression
                fprintf(stderr, "[I::%s] position compression n = %lu, m = %lu, max_m = %lu\n", __func__, n, m, max_m);
                if (m > n>>1) {
                    max_m <<= 1; // increase m limit if compression ratio smaller than 0.5
                    fprintf(stderr, "[I::%s] position memory buffer expanded max_m = %lu\n", __func__, max_m);
                }
                n = m;
            }

            p0 = MIN(p0, dict->s[i0].len - 1);
            p1 = MIN(p1, dict->s[i1].len - 1);
            if (i0 > i1) {
                SWAP(uint32_t, i0, i1);
                SWAP(uint32_t, p0, p1);
            }
            fwrite(&i0, sizeof(uint32_t), 1, fo);
            fwrite(&p0, sizeof(uint32_t), 1, fo);
            fwrite(&i1, sizeof(uint32_t), 1, fo);
            fwrite(&p1, sizeof(uint32_t), 1, fo);
            fwrite(&q, sizeof(uint8_t), 1, fo);

            if (i0 == i1)
                ++intra_c;
            else
                ++inter_c;

            ++pair_c;
        }

        if (++rec_c % 1000000 == 0)
            fprintf(stderr, "[I::%s] %lu million records processed, %lu read pairs \n", __func__, rec_c / 1000000, pair_c);
    }

    m = pos_compression(cov);
    fprintf(stderr, "[I::%s] position compression n = %lu, m = %lu, max_m = %lu\n", __func__, n, m, max_m);

    for (k = 0; k < kh_end(hmseq); ++k)
        if (kh_exist(hmseq, k))
            free((char *) kh_key(hmseq, k));
    kh_destroy(str, hmseq);

    iostream_close(fp);
    // write pair number
    fseek(fo, sizeof(uint64_t) + sd_l, SEEK_SET);
    fwrite(&pair_c, sizeof(uint64_t), 1, fo);
    fseek(fo, pair_c * 17, SEEK_CUR);

    fprintf(stderr, "[I::%s] dumped %lu read pairs from %lu records: %lu intra links + %lu inter links \n", __func__, pair_c, rec_c, intra_c, inter_c);

    // cov_t *cov = bed_cstats(f, dict);
    cov_norm_t *cov_norm = calc_cov_norms(cov, dict, q_drop);
    fwrite(&cov_norm->n, sizeof(uint64_t), 1, fo);
    fwrite(cov_norm->norm[0], sizeof(double), cov_norm->n, fo);
    cov_destroy(cov);
    cov_norm_destroy(cov_norm);

    sd_destroy(dict);
    fclose(fo);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x20b8, %rsp           # imm = 0x20B8
movq %r9, %r14
movsd %xmm0, 0xa8(%rsp)
movl %r8d, 0x54(%rsp)
movl %ecx, 0x7c(%rsp)
movl %edx, %ebx
movq %rsi, %r13
movq %rdi, %r15
callq 0xcb76
movq %rax, %r12
movq %r13, %rdi
movl %ebx, %esi
callq 0xeb03
movq %rax, %rbp
movq %r15, %rdi
callq 0x2a3e
testq %rax, %rax
je 0xdef0
movq %rax, %r13
leaq 0x108cf(%rip), %rsi     # 0x1e010
movq %r14, %rdi
callq 0x23e0
testq %rax, %rax
je 0xdf0d
movq %rax, %rbx
movq %rax, %rdi
callq 0x2952
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x1185a
movq %rax, 0xa0(%rsp)
shrl 0x54(%rsp)
leaq 0x98(%rsp), %r14
andq $0x0, (%r14)
movl (%rbp), %edi
callq 0x121fc
movq %rax, 0x20(%rsp)
pushq $0x8
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rdi
movq %rbx, 0x88(%rsp)
movq %rbx, %rcx
callq 0x24d0
movl $0x7ffffff, %eax        # imm = 0x7FFFFFF
movq %rax, 0x48(%rsp)
movq (%r14), %rax
movq %rax, 0x58(%rsp)
leaq 0xb0(%rsp), %rbx
movq $0x0, 0x68(%rsp)
movq $0x0, 0x80(%rsp)
movq $0x0, 0x38(%rsp)
movq $0x0, 0x18(%rsp)
movq %rbp, 0x60(%rsp)
movq %r13, %rdi
callq 0x2b6c
testq %rax, %rax
je 0xdd5e
movq %rax, %r14
movq %rax, %rdi
callq 0x2a1b
testl %eax, %eax
jne 0xd7ec
orl $-0x1, 0x10(%rsp)
orl $-0x1, 0x14(%rsp)
xorl %eax, %eax
movb %al, 0x10b0(%rsp)
movb %al, 0xb0(%rsp)
movb $-0x1, %al
movb %al, 0xe(%rsp)
movb %al, 0xf(%rsp)
movq %r14, %rdi
leaq 0x1130b(%rip), %rsi     # 0x1eb45
movq %rbx, %rdx
leaq 0x14(%rsp), %rcx
leaq 0x10b0(%rsp), %r14
movq %r14, %r8
leaq 0x10(%rsp), %r9
xorl %eax, %eax
leaq 0xe(%rsp), %r10
pushq %r10
leaq 0x17(%rsp), %r10
pushq %r10
callq 0x2350
popq %rcx
popq %rdx
movq %rbp, %rdi
movq %rbx, %rsi
callq 0xe65a
movl %eax, %r15d
movl %eax, 0x34(%rsp)
movq %rbp, %rdi
movq %r14, %rsi
callq 0xe65a
movl %eax, %edi
movl %eax, 0x30(%rsp)
movzbl 0xf(%rsp), %eax
movzbl 0xe(%rsp), %r8d
cmpb %r8b, %al
cmovbl %eax, %r8d
pushq $-0x1
popq %rax
cmpl %eax, %r15d
je 0xd8c0
cmpl $-0x1, %edi
je 0xd90e
cmpb 0x7c(%rsp), %r8b
jae 0xd940
movq 0x18(%rsp), %rax
jmp 0xdc4d
movq %r12, %rdi
movq %rbx, %rsi
leaq 0x2c(%rsp), %rdx
callq 0xcbe1
cmpl $0x0, 0x2c(%rsp)
je 0xd936
movl %eax, %r14d
movq %rbx, %rdi
callq 0x2510
movq 0x18(%r12), %rcx
movl %r14d, %edx
movq %rax, (%rcx,%rdx,8)
movq 0x1a703(%rip), %rax     # 0x27ff8
movq (%rax), %rdi
leaq 0x11088(%rip), %rsi     # 0x1e987
leaq 0x11226(%rip), %rdx     # 0x1eb2c
movq %rbx, %rcx
jmp 0xdcd6
movq %r12, %rdi
leaq 0x10b0(%rsp), %rsi
leaq 0x2c(%rsp), %rdx
callq 0xcbe1
cmpl $0x0, 0x2c(%rsp)
leaq 0xb0(%rsp), %rbx
jne 0xdc9c
movq 0x18(%rsp), %rax
jmp 0xdc55
movl %r15d, %ecx
movq %rbp, %rax
movl 0x14(%rsp), %ebp
movl %ebp, %r15d
movl 0x54(%rsp), %edx
subl %edx, %r15d
movl $0x0, %esi
cmovbl %esi, %r15d
cmovbel %esi, %r15d
movq 0x8(%rax), %rax
movl %ecx, 0x74(%rsp)
movl %ecx, %ecx
imulq $0x18, %rcx, %rbx
movl 0x10(%rax,%rbx), %ecx
decl %ecx
cmpl %ecx, %r15d
cmovael %ecx, %r15d
addl %edx, %ebp
cmpl %ecx, %ebp
cmovael %ecx, %ebp
movl 0x10(%rsp), %r9d
movl %r9d, %r14d
subl %edx, %r14d
cmovbl %esi, %r14d
cmovbel %esi, %r14d
movl %edi, 0x70(%rsp)
movl %edi, %ecx
imulq $0x18, %rcx, %rcx
movq %rcx, 0x40(%rsp)
movl 0x10(%rax,%rcx), %eax
decl %eax
cmpl %eax, %r14d
cmovael %eax, %r14d
addl %edx, %r9d
cmpl %eax, %r9d
cmovael %eax, %r9d
movq 0x20(%rsp), %rdi
movq 0x8(%rdi), %rcx
movq (%rcx,%rbx), %rdx
cmpq 0x8(%rcx,%rbx), %rdx
movl %r8d, 0x78(%rsp)
movq %r9, 0x90(%rsp)
jne 0xda13
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
pushq $0x2
popq %rax
cmoveq %rax, %rsi
leaq (%rcx,%rbx), %rax
movq %rsi, 0x8(%rcx,%rbx)
movq 0x10(%rax), %rdi
shlq $0x3, %rsi
callq 0x2390
movq 0x20(%rsp), %rdi
movq 0x8(%rdi), %rcx
movq %rax, 0x10(%rcx,%rbx)
movq (%rcx,%rbx), %rdx
shlq $0x20, %r15
incq %r15
movq 0x10(%rcx,%rbx), %rax
leaq 0x1(%rdx), %rsi
movq %rsi, (%rcx,%rbx)
movq %r15, (%rax,%rdx,8)
movq (%rcx,%rbx), %rdx
cmpq 0x8(%rcx,%rbx), %rdx
jne 0xda67
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
pushq $0x2
popq %rdx
cmoveq %rdx, %rsi
movq %rsi, 0x8(%rcx,%rbx)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2390
movq 0x20(%rsp), %rdi
movq 0x8(%rdi), %rcx
movq %rax, 0x10(%rcx,%rbx)
movq (%rcx,%rbx), %rdx
movq 0x40(%rsp), %r9
shlq $0x20, %rbp
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
orq %rsi, %rbp
leaq 0x1(%rdx), %rsi
movq %rsi, (%rcx,%rbx)
movq %rbp, (%rax,%rdx,8)
movq (%rcx,%r9), %rdx
cmpq 0x8(%rcx,%r9), %rdx
jne 0xdaca
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
pushq $0x2
popq %rax
cmoveq %rax, %rsi
leaq (%rcx,%r9), %rax
movq %rsi, 0x8(%rcx,%r9)
movq 0x10(%rax), %rdi
shlq $0x3, %rsi
callq 0x2390
movq 0x40(%rsp), %r9
movq 0x20(%rsp), %rdi
movq 0x8(%rdi), %rcx
movq %rax, 0x10(%rcx,%r9)
movq (%rcx,%r9), %rdx
movq 0x60(%rsp), %rbp
movq %rbx, %r15
shlq $0x20, %r14
incq %r14
movq 0x10(%rcx,%r9), %rax
leaq 0x1(%rdx), %rsi
movq %rsi, (%rcx,%r9)
movq %r14, (%rax,%rdx,8)
movq (%rcx,%r9), %rdx
cmpq 0x8(%rcx,%r9), %rdx
jne 0xdb2b
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
pushq $0x2
popq %rdx
cmoveq %rdx, %rsi
movq %rsi, 0x8(%rcx,%r9)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2390
movq 0x40(%rsp), %r9
movq 0x20(%rsp), %rdi
movq 0x8(%rdi), %rcx
movq %rax, 0x10(%rcx,%r9)
movq (%rcx,%r9), %rdx
movq 0x38(%rsp), %rbx
movq 0x90(%rsp), %r8
shlq $0x20, %r8
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
orq %rsi, %r8
leaq 0x1(%rdx), %rsi
movq %rsi, (%rcx,%r9)
movq %r8, (%rax,%rdx,8)
movq 0x68(%rsp), %r14
addq $0x4, %r14
cmpq 0x48(%rsp), %r14
ja 0xdce2
movq %r14, 0x68(%rsp)
movq 0x88(%rsp), %r8
movl 0x14(%rsp), %ecx
movq 0x8(%rbp), %rdx
movl 0x10(%rdx,%r15), %eax
decl %eax
cmpl %eax, %ecx
cmovbl %ecx, %eax
movl %eax, 0x14(%rsp)
movl 0x10(%rsp), %esi
movl 0x10(%rdx,%r9), %ecx
decl %ecx
cmpl %ecx, %esi
cmovbl %esi, %ecx
movl %ecx, 0x10(%rsp)
movl 0x74(%rsp), %edx
movl 0x70(%rsp), %esi
cmpl %esi, %edx
jbe 0xdbb9
movl %esi, 0x34(%rsp)
movl %edx, 0x30(%rsp)
movl %ecx, 0x14(%rsp)
movl %eax, 0x10(%rsp)
leaq 0x34(%rsp), %rdi
pushq $0x4
popq %r14
movq %r14, %rsi
pushq $0x1
popq %rbp
movq %rbp, %rdx
movq %r8, %r15
movq %r8, %rcx
callq 0x24d0
leaq 0x14(%rsp), %rdi
movq %r14, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x24d0
leaq 0x30(%rsp), %rdi
movq %r14, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x24d0
leaq 0x10(%rsp), %rdi
movq %r14, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x24d0
movsbl 0x78(%rsp), %edi
movq %r15, %rsi
callq 0x2260
movl 0x34(%rsp), %eax
xorl %ecx, %ecx
xorl %edx, %edx
cmpl 0x30(%rsp), %eax
setne %cl
sete %dl
addq %rdx, 0x80(%rsp)
addq %rcx, %rbx
movq %rbx, 0x38(%rsp)
incq 0x58(%rsp)
movq 0x18(%rsp), %rax
movq 0x60(%rsp), %rbp
leaq 0xb0(%rsp), %rbx
incq %rax
movq %rax, 0x18(%rsp)
xorl %edx, %edx
movl $0xf4240, %ecx          # imm = 0xF4240
divq %rcx
testq %rdx, %rdx
jne 0xd7ec
movq 0x1a381(%rip), %rcx     # 0x27ff8
movq (%rcx), %rdi
leaq 0x10d99(%rip), %rsi     # 0x1ea1a
leaq 0x10ea4(%rip), %rdx     # 0x1eb2c
movq %rax, %rcx
movq 0x58(%rsp), %r8
xorl %eax, %eax
callq 0x22c0
jmp 0xd7ec
movl %eax, %r14d
leaq 0x10b0(%rsp), %r15
movq %r15, %rdi
callq 0x2510
movq 0x18(%r12), %rcx
movl %r14d, %edx
movq %rax, (%rcx,%rdx,8)
movq 0x1a336(%rip), %rax     # 0x27ff8
movq (%rax), %rdi
leaq 0x10cbb(%rip), %rsi     # 0x1e987
leaq 0x10e59(%rip), %rdx     # 0x1eb2c
movq %r15, %rcx
xorl %eax, %eax
callq 0x22c0
jmp 0xd936
callq 0x122af
movq %rax, %r8
movq %rax, 0x38(%rsp)
movq 0x1a302(%rip), %rax     # 0x27ff8
movq (%rax), %rdi
leaq 0x10ca9(%rip), %rsi     # 0x1e9a9
leaq 0x10e25(%rip), %rdx     # 0x1eb2c
movq %r14, %rcx
movq 0x48(%rsp), %r9
xorl %eax, %eax
callq 0x22c0
shrq %r14
movq 0x38(%rsp), %rax
cmpq %r14, %rax
movq %rax, %r14
ja 0xdd30
movq 0x40(%rsp), %r9
jmp 0xdb64
movq 0x48(%rsp), %rcx
addq %rcx, %rcx
movq 0x1a2b9(%rip), %rax     # 0x27ff8
movq (%rax), %rdi
leaq 0x10c9c(%rip), %rsi     # 0x1e9e5
leaq 0x10ddc(%rip), %rdx     # 0x1eb2c
movq %rcx, 0x48(%rsp)
xorl %eax, %eax
callq 0x22c0
jmp 0xdd26
movq 0x58(%rsp), %rax
movq %rax, 0x98(%rsp)
movq 0x20(%rsp), %rdi
callq 0x122af
movq 0x1a27c(%rip), %rcx     # 0x27ff8
movq (%rcx), %rdi
leaq 0x10c23(%rip), %rsi     # 0x1e9a9
leaq 0x10d9f(%rip), %rdx     # 0x1eb2c
xorl %ebx, %ebx
movq 0x68(%rsp), %rcx
movq %rax, %r8
movq 0x48(%rsp), %r9
xorl %eax, %eax
callq 0x22c0
xorl %r14d, %r14d
movl (%r12), %eax
cmpq %rax, %r14
jae 0xdddf
movq 0x10(%r12), %rax
movq %r14, %rcx
shrq $0x4, %rcx
movl (%rax,%rcx,4), %eax
movl %ebx, %ecx
andb $0x1e, %cl
shrl %cl, %eax
testb $0x3, %al
jne 0xddd7
movq 0x18(%r12), %rax
movq (%rax,%r14,8), %rdi
callq 0x2060
incq %r14
addl $0x2, %ebx
jmp 0xdda6
movq %r12, %rdi
callq 0xcdc7
movq %r13, %rdi
callq 0x2b0a
movq 0xa0(%rsp), %rsi
addq $0x8, %rsi
movq 0x88(%rsp), %rbx
movq %rbx, %rdi
xorl %edx, %edx
callq 0x2370
leaq 0x98(%rsp), %r12
pushq $0x8
popq %r14
pushq $0x1
popq %r15
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x24d0
movq (%r12), %r12
imulq $0x11, %r12, %rsi
pushq $0x1
popq %rdx
movq %rbx, %rdi
callq 0x2370
movq 0x1a1b0(%rip), %rax     # 0x27ff8
movq (%rax), %rdi
subq $0x8, %rsp
leaq 0x10c58(%rip), %rsi     # 0x1eaae
leaq 0x10ccf(%rip), %rdx     # 0x1eb2c
movq %r12, %rcx
movq 0x20(%rsp), %r8
movq 0x88(%rsp), %r9
xorl %eax, %eax
pushq 0x40(%rsp)
callq 0x22c0
popq %rcx
popq %rdx
movq 0x20(%rsp), %r13
movq %r13, %rdi
movq %rbp, %rsi
movsd 0xa8(%rsp), %xmm0
callq 0x132a3
movq %rax, %r12
movq %rax, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x24d0
movq (%r12), %rdx
movq 0x10(%r12), %rax
movq (%rax), %rdi
movq %r14, %rsi
movq %rbx, %rcx
callq 0x24d0
movq %r13, %rdi
callq 0x12243
movq %r12, %rdi
callq 0x12287
movq %rbp, %rdi
callq 0xe20a
movq %rbx, %rdi
callq 0x2150
addq $0x20b8, %rsp           # imm = 0x20B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1a101(%rip), %rax     # 0x27ff8
movq (%rax), %rdi
leaq 0x102ef(%rip), %rsi     # 0x1e1f0
leaq 0x10c24(%rip), %rdx     # 0x1eb2c
movq %r15, %rcx
jmp 0xdf28
movq 0x1a0e4(%rip), %rax     # 0x27ff8
movq (%rax), %rdi
leaq 0x10a40(%rip), %rsi     # 0x1e95e
leaq 0x10c07(%rip), %rdx     # 0x1eb2c
movq %r14, %rcx
xorl %eax, %eax
callq 0x22c0
pushq $0x1
popq %rdi
callq 0x24b0