// File: /workspace/github_c_makefile_star2_O3/c-zhou[P]yahs/juicer.c
// Function: make_juicer_pre_file_from_bed
static int make_juicer_pre_file_from_bed(char *f, char *agp, char *fai, uint8_t mq, int scale, int count_gap, FILE *fo)
{
    iostream_t *fp;
    char *line;
    char cname0[4096], cname1[4096], rname0[4096], rname1[4096];
    uint32_t s0, s1, e0, e1, i0, i1;
    uint64_t p0, p1, rec_c, pair_c;
    uint8_t q0, q1;
    int8_t buff;
    CC_ERR_t err0, err1;

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

    sdict_t *sdict = make_sdict_from_index(fai, 0);
    asm_dict_t *dict = agp? make_asm_dict_from_agp(sdict, agp, 1) : make_asm_dict_from_sdict(sdict);

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

    rec_c = pair_c = 0;
    buff = 0;
    while ((line = iostream_getline(fp)) != NULL) {
        if (is_empty_line(line))
            continue;
        
        if (++rec_c % 1000000 == 0)
            fprintf(stderr, "[I::%s] %lu million records processed, %lu read pairs \n", __func__, rec_c / 1000000, pair_c);
    
        if (buff == 0) {
            cname0[0] = rname0[0] = '\0';
            s0 = e0 = UINT32_MAX;
            q0 = 255;
            sscanf(line, "%s %u %u %s %hhu", cname0, &s0, &e0, rname0, &q0);
            ++buff;
        } else if (buff == 1) {
            cname1[0] = rname1[0] = '\0';
            s1 = e1 = UINT32_MAX;
            q1 = 255;
            sscanf(line, "%s %u %u %s %hhu", cname1, &s1, &e1, rname1, &q1);
            if (is_read_pair(rname0, rname1)) {
                buff = 0;

                if (q0 < mq || q1 < mq)
                    continue;
                
                err0 = sd_coordinate_conversion(dict, sd_get(sdict, cname0), s0 / 2 + e0 / 2 + (s0 & 1 && e0 & 1), &i0, &p0, count_gap);
                err1 = sd_coordinate_conversion(dict, sd_get(sdict, cname1), s1 / 2 + e1 / 2 + (s1 & 1 && e1 & 1), &i1, &p1, count_gap);
                
                if (err0 != CC_SUCCESS) {
                    if (err0 == SEQ_NOT_FOUND) {
                        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 (err0 == POS_NOT_IN_RANGE) {
                        fprintf(stderr, "[W::%s] sequence position \"%s:%u\" not in range \n", __func__, 
                                cname0, s0 / 2 + e0 / 2 + (s0 & 1 && e0 & 1));
                    }
                } else if (err1 != CC_SUCCESS) {
                    if (err1 == SEQ_NOT_FOUND) {
                        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 (err1 == POS_NOT_IN_RANGE) {
                        fprintf(stderr, "[W::%s] sequence position \"%s:%u\" not in range \n", __func__,
                                cname1, s1 / 2 + e1 / 2 + (s1 & 1 && e1 & 1));
                    }
                } else {
                    if (strcmp(dict->s[i0].name, dict->s[i1].name) <= 0)
                        fprintf(fo, "0\t%s\t%lu\t0\t1\t%s\t%lu\t1\n", dict->s[i0].name, p0 >> scale, dict->s[i1].name, p1 >> scale);
                    else
                        fprintf(fo, "0\t%s\t%lu\t1\t1\t%s\t%lu\t0\n", dict->s[i1].name, p1 >> scale, dict->s[i0].name, p0 >> scale);
                    
                    ++pair_c;
                }
            } else {
                buff = 1;
            
                strcpy(cname0, cname1);
                s0 = s1;
                e0 = e1;
                q0 = q1;
                strcpy(rname0, rname1);
            }
        }
    }

    fprintf(stderr, "[I::%s] %ld read pairs processed\n", __func__, pair_c);
    
    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);
    asm_destroy(dict);
    sd_destroy(sdict);

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4078, %rsp           # imm = 0x4078
movl %r9d, 0x24(%rsp)
movl %r8d, %ebx
movl %ecx, %r13d
movq %rdx, %rbp
movq %rsi, %r12
movq %rdi, %r15
movl $0x1, %esi
movl $0x28, %edx
xorl %edi, %edi
callq 0x4040
movq %rax, %r14
movq %rbp, %rdi
xorl %esi, %esi
callq 0x5d57
testq %r12, %r12
movq %rax, 0x58(%rsp)
movq %rax, %rdi
jne 0xcfd0
callq 0x62db
jmp 0xcfdd
movq %r12, %rsi
movl $0x1, %edx
callq 0x6768
movq %rax, 0x30(%rsp)
movq %r15, %rdi
callq 0x35d7
testq %rax, %rax
je 0xd49b
movq %rax, %r12
movq %r14, %rax
addq $0x18, %rax
movq %rax, 0x48(%rsp)
movl %ebx, %eax
movq %rax, 0x50(%rsp)
movb $0x1, %r15b
movq $0x0, 0x38(%rsp)
xorl %ebx, %ebx
movq %r12, %rdi
callq 0x3707
testq %rax, %rax
je 0xd3db
movq %rax, %rbp
movq %rax, %rdi
callq 0x35b4
testl %eax, %eax
jne 0xd017
incq %rbx
movq %rbx, %rax
movabsq $0x431bde82d7b634db, %rcx # imm = 0x431BDE82D7B634DB
mulq %rcx
movq %rdx, %rcx
movq %rbx, %rax
movabsq $0x790fb65668c26139, %rdx # imm = 0x790FB65668C26139
imulq %rdx, %rax
rorq $0x6, %rax
movabsq $0x10c6f7a0b5ed, %rdx   # imm = 0x10C6F7A0B5ED
cmpq %rdx, %rax
jbe 0xd1cb
testb $0x1, %r15b
jne 0xd1f8
xorl %eax, %eax
movb %al, 0x3070(%rsp)
movb %al, 0x70(%rsp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, 0x10(%rsp)
movl %eax, 0x18(%rsp)
movb $-0x1, 0xe(%rsp)
leaq 0xe(%rsp), %rax
movq %rax, (%rsp)
movq %rbp, %rdi
leaq 0x39da(%rip), %rsi      # 0x10a8b
leaq 0x70(%rsp), %rdx
leaq 0x18(%rsp), %rcx
leaq 0x10(%rsp), %r8
leaq 0x3070(%rsp), %r15
movq %r15, %r9
xorl %eax, %eax
callq 0x22b0
leaq 0x2070(%rsp), %rdi
movq %r15, %rsi
callq 0x342b
testb %al, %al
je 0xd256
movb $0x1, %r15b
cmpb %r13b, 0xf(%rsp)
jb 0xd017
cmpb %r13b, 0xe(%rsp)
jb 0xd017
movq 0x58(%rsp), %r15
movq %r15, %rdi
leaq 0x1070(%rsp), %rsi
callq 0x5733
movl 0x1c(%rsp), %edx
movl %edx, %ecx
shrl %ecx
movl 0x14(%rsp), %esi
andl %esi, %edx
shrl %esi
addl %ecx, %esi
andl $0x1, %edx
addl %esi, %edx
movq 0x30(%rsp), %rbp
movq %rbp, %rdi
movl %eax, %esi
leaq 0x44(%rsp), %rcx
leaq 0x68(%rsp), %r8
movl 0x24(%rsp), %r9d
callq 0x7bb9
movl %eax, 0x28(%rsp)
movq %r15, %rdi
leaq 0x70(%rsp), %rsi
callq 0x5733
movl 0x18(%rsp), %edx
movl %edx, %ecx
shrl %ecx
movl 0x10(%rsp), %esi
andl %esi, %edx
shrl %esi
addl %ecx, %esi
andl $0x1, %edx
addl %esi, %edx
movq %rbp, %rdi
movl %eax, %esi
leaq 0x40(%rsp), %rcx
leaq 0x60(%rsp), %r8
movl 0x24(%rsp), %r9d
callq 0x7bb9
movl 0x28(%rsp), %ecx
movb $0x1, %r15b
testl %ecx, %ecx
je 0xd29d
cmpl $0x1, %ecx
je 0xd2cc
cmpl $0x2, %ecx
jne 0xd017
leaq 0x1c(%rsp), %rdi
leaq 0x14(%rsp), %rsi
leaq 0x1070(%rsp), %rdx
callq 0x2e7f
jmp 0xd017
shrq $0x12, %rcx
movq 0x7e22(%rip), %rax      # 0x14ff8
movq (%rax), %rdi
leaq 0x3756(%rip), %rsi      # 0x10936
leaq 0x3886(%rip), %rdx      # 0x10a6d
movq 0x38(%rsp), %r8
xorl %eax, %eax
callq 0x2230
jmp 0xd075
xorl %r15d, %r15d
movb %r15b, 0x2070(%rsp)
movb %r15b, 0x1070(%rsp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, 0x14(%rsp)
movl %eax, 0x1c(%rsp)
movb $-0x1, 0xf(%rsp)
leaq 0xf(%rsp), %rax
movq %rax, (%rsp)
movq %rbp, %rdi
leaq 0x385b(%rip), %rsi      # 0x10a8b
leaq 0x1070(%rsp), %rdx
leaq 0x1c(%rsp), %rcx
leaq 0x14(%rsp), %r8
leaq 0x2070(%rsp), %r9
xorl %eax, %eax
callq 0x22b0
jmp 0xd017
leaq 0x1070(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x20b0
movl 0x18(%rsp), %eax
movl %eax, 0x1c(%rsp)
movl 0x10(%rsp), %eax
movl %eax, 0x14(%rsp)
movb 0xe(%rsp), %al
movb %al, 0xf(%rsp)
leaq 0x2070(%rsp), %rdi
leaq 0x3070(%rsp), %rsi
callq 0x20b0
xorl %r15d, %r15d
jmp 0xd017
testl %eax, %eax
je 0xd305
cmpl $0x1, %eax
je 0xd368
cmpl $0x2, %eax
jne 0xd017
leaq 0x18(%rsp), %rdi
leaq 0x10(%rsp), %rsi
leaq 0x70(%rsp), %rdx
callq 0x2f03
jmp 0xd017
movq %r14, %rdi
leaq 0x1070(%rsp), %rsi
leaq 0x20(%rsp), %rdx
callq 0xdd00
cmpl $0x0, 0x20(%rsp)
je 0xd017
leaq 0x1070(%rsp), %rdi
movq 0x48(%rsp), %rsi
movl %eax, %edx
callq 0x2ec2
jmp 0xd017
movq 0x30(%rsp), %rax
movq 0x8(%rax), %rax
movl 0x44(%rsp), %ecx
imulq $0x58, %rcx, %rcx
movq (%rax,%rcx), %rdi
movl 0x40(%rsp), %ecx
imulq $0x58, %rcx, %rcx
movq (%rax,%rcx), %rbp
movq %rdi, 0x28(%rsp)
movq %rbp, %rsi
callq 0x2210
testl %eax, %eax
jg 0xd39b
movq 0x68(%rsp), %rax
movq 0x50(%rsp), %rcx
shrq %cl, %rax
movq 0x60(%rsp), %r9
shrq %cl, %r9
movq 0x40b0(%rsp), %rdi
leaq 0x3666(%rip), %rsi      # 0x109c1
movq 0x28(%rsp), %rdx
movq %rax, %rcx
movq %rbp, %r8
jmp 0xd3ca
movq %r14, %rdi
leaq 0x70(%rsp), %rsi
leaq 0x20(%rsp), %rdx
callq 0xdd00
cmpl $0x0, 0x20(%rsp)
je 0xd017
leaq 0x70(%rsp), %rdi
movq 0x48(%rsp), %rsi
movl %eax, %edx
callq 0x2f46
jmp 0xd017
movq 0x60(%rsp), %rax
movq 0x50(%rsp), %rcx
shrq %cl, %rax
movq 0x68(%rsp), %r9
shrq %cl, %r9
movq 0x40b0(%rsp), %rdi
leaq 0x3619(%rip), %rsi      # 0x109d8
movq %rbp, %rdx
movq %rax, %rcx
movq 0x28(%rsp), %r8
xorl %eax, %eax
callq 0x2230
incq 0x38(%rsp)
jmp 0xd017
movq 0x7c16(%rip), %rax      # 0x14ff8
movq (%rax), %rdi
leaq 0x36b0(%rip), %rsi      # 0x10a9c
leaq 0x367a(%rip), %rdx      # 0x10a6d
xorl %ebx, %ebx
movq 0x38(%rsp), %rcx
xorl %eax, %eax
callq 0x2230
movl (%r14), %eax
testl %eax, %eax
je 0xd442
xorl %r15d, %r15d
movq 0x10(%r14), %rcx
movq %r15, %rdx
shrq $0x4, %rdx
movl (%rcx,%rdx,4), %edx
movl %ebx, %ecx
andb $0x1e, %cl
shrl %cl, %edx
testb $0x3, %dl
jne 0xd435
movq 0x18(%r14), %rax
movq (%rax,%r15,8), %rdi
callq 0x2060
movl (%r14), %eax
incq %r15
movl %eax, %ecx
addl $0x2, %ebx
cmpq %rcx, %r15
jb 0xd40b
movq 0x18(%r14), %rsi
xorl %edi, %edi
callq 0x40db
movq 0x10(%r14), %rsi
xorl %edi, %edi
callq 0x40db
movq 0x20(%r14), %rsi
xorl %edi, %edi
callq 0x40db
xorl %edi, %edi
movq %r14, %rsi
callq 0x40db
movq %r12, %rdi
callq 0x36a5
movq 0x30(%rsp), %rdi
callq 0x52fd
movq 0x58(%rsp), %rdi
callq 0x525f
addq $0x4078, %rsp           # imm = 0x4078
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %rdi
callq 0x2f87