// File: /workspace/github_c_makefile_star2_O1/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 $0x20d8, %rsp           # imm = 0x20D8
movq %r9, %r14
movsd %xmm0, 0xb8(%rsp)
movl %r8d, 0x54(%rsp)
movl %ecx, 0x7c(%rsp)
movl %edx, %ebx
movq %rsi, %r13
movq %rdi, %r15
movl $0x1, %esi
movl $0x28, %edx
xorl %edi, %edi
callq 0xb260
movq %rax, %r12
movq %r13, %rdi
movl %ebx, %esi
callq 0x11f82
movq %rax, 0x48(%rsp)
movq %r15, %rdi
callq 0x4d17
testq %rax, %rax
je 0x11139
movq %rax, %r13
leaq 0x1073e(%rip), %rsi     # 0x21010
movq %r14, %rdi
callq 0x23e0
testq %rax, %rax
je 0x11141
movq %rax, %rbx
movq %rax, %rdi
callq 0x4c49
movq %rbx, %rdi
movq 0x48(%rsp), %r15
movq %r15, %rsi
callq 0x156fa
movq %rax, 0xb0(%rsp)
shrl 0x54(%rsp)
leaq 0xa8(%rsp), %r14
movq $0x0, (%r14)
movl (%r15), %edi
callq 0x16058
movq %rax, 0x20(%rsp)
movl $0x8, %esi
movl $0x1, %edx
movq %r14, %rdi
movq %rbx, 0x98(%rsp)
movq %rbx, %rcx
callq 0x24d0
movq (%r14), %rax
movq %rax, 0x60(%rsp)
movq %r12, %rax
addq $0x18, %rax
movq %rax, 0x80(%rsp)
movl $0x7ffffff, %eax        # imm = 0x7FFFFFF
movq %rax, 0x40(%rsp)
leaq 0xd0(%rsp), %rbx
movq $0x0, 0x38(%rsp)
movq $0x0, 0x88(%rsp)
movq $0x0, 0x90(%rsp)
movq $0x0, 0x18(%rsp)
movq %r13, %rdi
callq 0x4e4f
testq %rax, %rax
je 0x10f6c
movq %rax, %r14
movq %rax, %rdi
callq 0x4cf4
testl %eax, %eax
jne 0x10996
xorl %eax, %eax
movb %al, 0x10d0(%rsp)
movb %al, 0xd0(%rsp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, 0x10(%rsp)
movl %eax, 0x14(%rsp)
movb $-0x1, %al
movb %al, 0xe(%rsp)
movb %al, 0xf(%rsp)
movq %r14, %rdi
leaq 0x1116e(%rip), %rsi     # 0x21b55
movq %rbx, %rdx
leaq 0x14(%rsp), %rcx
leaq 0x10d0(%rsp), %r15
movq %r15, %r8
leaq 0x10(%rsp), %r9
xorl %eax, %eax
leaq 0xe(%rsp), %r10
pushq %r10
leaq 0x17(%rsp), %r10
pushq %r10
callq 0x2350
addq $0x10, %rsp
movq %rbx, %r14
movq 0x48(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x11945
movl %eax, %ebp
movl %eax, 0x34(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x11945
movl %eax, %edi
movl %eax, 0x30(%rsp)
movzbl 0xf(%rsp), %eax
movzbl 0xe(%rsp), %ebx
cmpb %bl, %al
cmovbl %eax, %ebx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %eax, %ebp
je 0x10e08
cmpl $-0x1, %edi
je 0x10e36
cmpb 0x7c(%rsp), %bl
jb 0x10df9
movl %ebp, %ecx
movl 0x14(%rsp), %r15d
movl %r15d, %ebp
movl 0x54(%rsp), %edx
subl %edx, %ebp
movl $0x0, %esi
cmovbl %esi, %ebp
cmovbel %esi, %ebp
movq 0x48(%rsp), %rax
movq 0x8(%rax), %rax
movl %ecx, 0x78(%rsp)
movl %ecx, %r10d
leaq (,%r10,8), %rcx
leaq (%rcx,%rcx,2), %r9
movl 0x10(%rax,%r9), %ecx
decl %ecx
cmpl %ecx, %ebp
cmovael %ecx, %ebp
addl %edx, %r15d
cmpl %ecx, %r15d
cmovael %ecx, %r15d
movl 0x10(%rsp), %r8d
movl %r8d, %r14d
subl %edx, %r14d
cmovbl %esi, %r14d
cmovbel %esi, %r14d
movl %edi, 0x74(%rsp)
movl %edi, %ecx
movq %rcx, 0xa0(%rsp)
leaq (%rcx,%rcx,2), %rcx
movq %rcx, 0xc8(%rsp)
movl 0x10(%rax,%rcx,8), %eax
decl %eax
cmpl %eax, %r14d
cmovael %eax, %r14d
addl %edx, %r8d
cmpl %eax, %r8d
cmovael %eax, %r8d
movq %r8, 0x68(%rsp)
movq 0x20(%rsp), %r8
movq 0x8(%r8), %rax
movq 0x8(%rax,%r9), %rcx
cmpq %rcx, (%rax,%r9)
movq %r9, 0x58(%rsp)
jne 0x10b6d
leaq (%rax,%r9), %rdx
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rax,%r9)
movq 0x8(%rdx), %rsi
movq 0x10(%rdx), %rdi
shlq $0x3, %rsi
movq %r10, 0xc0(%rsp)
callq 0x2390
movq 0xc0(%rsp), %r10
movq 0x58(%rsp), %r9
movq 0x20(%rsp), %r8
movq 0x8(%r8), %rcx
movq %rax, 0x10(%rcx,%r9)
shlq $0x20, %rbp
incq %rbp
movq 0x8(%r8), %rax
leaq (%r10,%r10,2), %rsi
movq (%rax,%rsi,8), %rcx
movq 0x10(%rax,%rsi,8), %rdi
leaq 0x1(%rcx), %rdx
movq %rdx, (%rax,%rsi,8)
movq %rbp, (%rdi,%rcx,8)
movq %rsi, %rbp
movq 0x8(%rax,%rsi,8), %rcx
cmpq %rcx, (%rax,%rsi,8)
jne 0x10bd8
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
leaq (%rax,%rbp,8), %rcx
movq %rdx, 0x8(%rax,%r9)
movq 0x8(%rcx), %rsi
shlq $0x3, %rsi
callq 0x2390
movq 0x20(%rsp), %r8
movq 0x8(%r8), %rcx
movq 0x58(%rsp), %rdx
movq %rax, 0x10(%rcx,%rdx)
shlq $0x20, %r15
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
orq %rax, %r15
movq 0x8(%r8), %rax
movq (%rax,%rbp,8), %rcx
movq 0x10(%rax,%rbp,8), %rdx
leaq 0x1(%rcx), %rsi
movq %rbp, 0x58(%rsp)
movq %rsi, (%rax,%rbp,8)
movq %r15, (%rdx,%rcx,8)
movq 0xc8(%rsp), %rbp
movq 0x8(%rax,%rbp,8), %rcx
cmpq %rcx, (%rax,%rbp,8)
jne 0x10c61
leaq (%rax,%rbp,8), %rdx
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq 0xa0(%rsp), %rcx
leaq (,%rcx,8), %rcx
leaq (%rcx,%rcx,2), %r15
movq %rsi, 0x8(%rax,%r15)
movq 0x8(%rdx), %rsi
movq 0x10(%rdx), %rdi
shlq $0x3, %rsi
callq 0x2390
movq 0x20(%rsp), %r8
movq 0x8(%r8), %rcx
movq %rax, 0x10(%rcx,%r15)
shlq $0x20, %r14
incq %r14
movq 0x8(%r8), %rax
movq (%rax,%rbp,8), %rcx
movq 0x10(%rax,%rbp,8), %rdi
leaq 0x1(%rcx), %rdx
movq %rdx, (%rax,%rbp,8)
movq %r14, (%rdi,%rcx,8)
movq 0x8(%rax,%rbp,8), %rcx
cmpq %rcx, (%rax,%rbp,8)
jne 0x10cd0
leaq (%rax,%rbp,8), %rdx
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq 0xa0(%rsp), %rcx
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %r14
movq %rsi, 0x8(%rax,%r14)
movq 0x8(%rdx), %rsi
shlq $0x3, %rsi
callq 0x2390
movq 0x20(%rsp), %r8
movq 0x8(%r8), %rcx
movq %rax, 0x10(%rcx,%r14)
movq 0x68(%rsp), %rdi
shlq $0x20, %rdi
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
orq %rax, %rdi
movq 0x8(%r8), %rax
movq (%rax,%rbp,8), %rcx
movq 0x10(%rax,%rbp,8), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%rax,%rbp,8)
movq %rdi, (%rdx,%rcx,8)
movq 0x38(%rsp), %rcx
addq $0x4, %rcx
movq %rcx, 0x38(%rsp)
cmpq 0x40(%rsp), %rcx
movq 0x98(%rsp), %r14
ja 0x10ee8
movl 0x14(%rsp), %ecx
movq 0x48(%rsp), %rax
movq 0x8(%rax), %rdx
movq 0x58(%rsp), %rax
movl 0x10(%rdx,%rax,8), %eax
decl %eax
cmpl %eax, %ecx
cmovbl %ecx, %eax
movl %eax, 0x14(%rsp)
movl 0x10(%rsp), %esi
movl 0x10(%rdx,%rbp,8), %ecx
decl %ecx
cmpl %ecx, %esi
cmovbl %esi, %ecx
movl %ecx, 0x10(%rsp)
movl 0x78(%rsp), %edx
movl 0x74(%rsp), %esi
cmpl %esi, %edx
jbe 0x10d6b
movl %esi, 0x34(%rsp)
movl %edx, 0x30(%rsp)
movl %ecx, 0x14(%rsp)
movl %eax, 0x10(%rsp)
movl $0x4, %esi
movl $0x1, %edx
leaq 0x34(%rsp), %rdi
movq %r14, %rcx
callq 0x24d0
movl $0x4, %esi
movl $0x1, %edx
leaq 0x14(%rsp), %rdi
movq %r14, %rcx
callq 0x24d0
movl $0x4, %esi
movl $0x1, %edx
leaq 0x30(%rsp), %rdi
movq %r14, %rcx
callq 0x24d0
movl $0x4, %esi
movl $0x1, %edx
leaq 0x10(%rsp), %rdi
movq %r14, %rcx
callq 0x24d0
movsbl %bl, %edi
movq %r14, %rsi
callq 0x2260
movl 0x34(%rsp), %eax
xorl %ecx, %ecx
xorl %edx, %edx
cmpl 0x30(%rsp), %eax
setne %cl
sete %dl
addq %rdx, 0x88(%rsp)
addq %rcx, 0x90(%rsp)
incq 0x60(%rsp)
movq 0x18(%rsp), %rsi
leaq 0xd0(%rsp), %rbx
jmp 0x10e5f
movq %r12, %rdi
movq %r14, %rsi
leaq 0x2c(%rsp), %rdx
callq 0xfd31
cmpl $0x0, 0x2c(%rsp)
movq %r14, %rbx
je 0x10e5a
movq %rbx, %rdi
movq 0x80(%rsp), %rsi
movl %eax, %edx
callq 0x2c3c
jmp 0x10e5a
movq %r12, %rdi
leaq 0x10d0(%rsp), %rsi
leaq 0x2c(%rsp), %rdx
callq 0xfd31
cmpl $0x0, 0x2c(%rsp)
leaq 0xd0(%rsp), %rbx
jne 0x10ecc
movq 0x18(%rsp), %rsi
incq %rsi
movq %rsi, %rax
movabsq $0x431bde82d7b634db, %rcx # imm = 0x431BDE82D7B634DB
mulq %rcx
movq %rdx, %rcx
movq %rsi, 0x18(%rsp)
movabsq $0x790fb65668c26139, %rdx # imm = 0x790FB65668C26139
imulq %rdx, %rsi
rorq $0x6, %rsi
movabsq $0x10c6f7a0b5ed, %rdx   # imm = 0x10C6F7A0B5ED
cmpq %rdx, %rsi
ja 0x10996
shrq $0x12, %rcx
movq 0x1a14e(%rip), %rax     # 0x2aff8
movq (%rax), %rdi
leaq 0x10b76(%rip), %rsi     # 0x21a2a
leaq 0x10c81(%rip), %rdx     # 0x21b3c
movq 0x60(%rsp), %r8
xorl %eax, %eax
callq 0x22c0
jmp 0x10996
leaq 0x10d0(%rsp), %rdi
movq 0x80(%rsp), %rsi
movl %eax, %edx
callq 0x2bfb
jmp 0x10e5a
movq %r8, %rdi
callq 0x16118
movq %rax, %r8
movq %rax, 0x68(%rsp)
movq 0x1a0f9(%rip), %rax     # 0x2aff8
movq (%rax), %rdi
leaq 0x10ab0(%rip), %rsi     # 0x219b9
leaq 0x10c2c(%rip), %rdx     # 0x21b3c
movq 0x38(%rsp), %r15
movq %r15, %rcx
movq 0x40(%rsp), %r9
xorl %eax, %eax
callq 0x22c0
shrq %r15
movq 0x68(%rsp), %rax
cmpq %r15, %rax
movq %rax, %r15
ja 0x10f3e
movq %r15, 0x38(%rsp)
jmp 0x10d1b
movq 0x40(%rsp), %rcx
addq %rcx, %rcx
movq 0x1a0ab(%rip), %rax     # 0x2aff8
movq (%rax), %rdi
leaq 0x10a9e(%rip), %rsi     # 0x219f5
leaq 0x10bde(%rip), %rdx     # 0x21b3c
movq %rcx, 0x40(%rsp)
xorl %eax, %eax
callq 0x22c0
jmp 0x10f34
movq 0x60(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq 0x20(%rsp), %rdi
callq 0x16118
movq 0x1a06e(%rip), %rcx     # 0x2aff8
movq (%rcx), %rdi
leaq 0x10a25(%rip), %rsi     # 0x219b9
leaq 0x10ba1(%rip), %rdx     # 0x21b3c
xorl %ebx, %ebx
movq 0x38(%rsp), %rcx
movq %rax, %r8
movq 0x40(%rsp), %r9
xorl %eax, %eax
callq 0x22c0
cmpl $0x0, (%r12)
je 0x10ff1
xorl %r14d, %r14d
movq 0x10(%r12), %rax
movl %r14d, %ecx
shrl $0x4, %ecx
movl (%rax,%rcx,4), %eax
movl %ebx, %ecx
andb $0x1e, %cl
shrl %cl, %eax
testb $0x3, %al
jne 0x10fe2
movq 0x18(%r12), %rax
movq (%rax,%r14,8), %rdi
callq 0x2060
incq %r14
movl (%r12), %eax
addl $0x2, %ebx
cmpq %rax, %r14
jb 0x10fbb
movq 0x18(%r12), %rsi
xorl %edi, %edi
callq 0xb302
movq 0x10(%r12), %rsi
xorl %edi, %edi
callq 0xb302
movq 0x20(%r12), %rsi
xorl %edi, %edi
callq 0xb302
xorl %edi, %edi
movq %r12, %rsi
callq 0xb302
movq %r13, %rdi
callq 0x4ded
movq 0xb0(%rsp), %rsi
addq $0x8, %rsi
movq 0x98(%rsp), %r15
movq %r15, %rdi
xorl %edx, %edx
callq 0x2370
leaq 0xa8(%rsp), %r14
movl $0x8, %esi
movl $0x1, %edx
movq %r14, %rdi
movq %r15, %rcx
callq 0x24d0
movq (%r14), %r14
movq %r14, %rsi
shlq $0x4, %rsi
addq %r14, %rsi
movq %r15, %rdi
movl $0x1, %edx
callq 0x2370
movq 0x19f75(%rip), %rax     # 0x2aff8
movq (%rax), %rdi
subq $0x8, %rsp
leaq 0x10a2d(%rip), %rsi     # 0x21abe
leaq 0x10aa4(%rip), %rdx     # 0x21b3c
movq %r14, %rcx
movq 0x20(%rsp), %r8
movq 0x90(%rsp), %r9
xorl %eax, %eax
pushq 0x98(%rsp)
callq 0x22c0
addq $0x10, %rsp
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
movq 0x48(%rsp), %r12
movq %r12, %rsi
movsd 0xb8(%rsp), %xmm0
callq 0x3338
movq %rax, %r14
movl $0x8, %esi
movl $0x1, %edx
movq %rax, %rdi
movq %r15, %rcx
callq 0x24d0
movq (%r14), %rdx
movq 0x10(%r14), %rax
movq (%rax), %rdi
movl $0x8, %esi
movq %r15, %rcx
callq 0x24d0
movq %rbx, %rdi
callq 0x160a3
movq %r14, %rdi
callq 0x160f0
movq %r12, %rdi
callq 0x1148c
movq %r15, %rdi
callq 0x2150
addq $0x20d8, %rsp           # imm = 0x20D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %rdi
callq 0x2ca8
movq %r14, %rdi
callq 0x2c7d