// File: /workspace/github_c_makefile_star2_O1/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 0x40c0
movq %rax, %r14
movq %rbp, %rdi
xorl %esi, %esi
callq 0x5df2
movq %rax, %rbp
movq %rax, %rdi
testq %r12, %r12
jne 0xce04
callq 0x6398
jmp 0xce11
movq %r12, %rsi
movl $0x1, %edx
callq 0x6814
movq %rax, 0x30(%rsp)
movq %rbp, 0x58(%rsp)
movq %r15, %rdi
callq 0x3627
testq %rax, %rax
je 0xd2d2
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 %ebp, %ebp
movq %r12, %rdi
callq 0x375f
testq %rax, %rax
je 0xd214
movq %rax, %rbx
movq %rax, %rdi
callq 0x3604
testl %eax, %eax
jne 0xce50
incq %rbp
movq %rbp, %rax
movabsq $0x431bde82d7b634db, %rcx # imm = 0x431BDE82D7B634DB
mulq %rcx
movq %rdx, %rcx
movq %rbp, %rax
movabsq $0x790fb65668c26139, %rdx # imm = 0x790FB65668C26139
imulq %rdx, %rax
rorq $0x6, %rax
movabsq $0x10c6f7a0b5ed, %rdx   # imm = 0x10C6F7A0B5ED
cmpq %rdx, %rax
jbe 0xd004
testb $0x1, %r15b
jne 0xd031
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 %rbx, %rdi
leaq 0x3bb7(%rip), %rsi      # 0x10aa1
leaq 0x70(%rsp), %rdx
leaq 0x18(%rsp), %rcx
leaq 0x10(%rsp), %r8
leaq 0x3070(%rsp), %rbx
movq %rbx, %r9
xorl %eax, %eax
callq 0x22b0
leaq 0x2070(%rsp), %rdi
movq %rbx, %rsi
callq 0x347b
testb %al, %al
je 0xd08f
movb $0x1, %r15b
cmpb %r13b, 0xf(%rsp)
jb 0xce50
cmpb %r13b, 0xe(%rsp)
jb 0xce50
movq 0x58(%rsp), %r15
movq %r15, %rdi
leaq 0x1070(%rsp), %rsi
callq 0x57b5
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), %rbx
movq %rbx, %rdi
movl %eax, %esi
leaq 0x44(%rsp), %rcx
leaq 0x68(%rsp), %r8
movl 0x24(%rsp), %r9d
callq 0x7bc0
movl %eax, 0x28(%rsp)
movq %r15, %rdi
leaq 0x70(%rsp), %rsi
callq 0x57b5
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 %rbx, %rdi
movl %eax, %esi
leaq 0x40(%rsp), %rcx
leaq 0x60(%rsp), %r8
movl 0x24(%rsp), %r9d
callq 0x7bc0
movl 0x28(%rsp), %ecx
movb $0x1, %r15b
testl %ecx, %ecx
je 0xd0d6
cmpl $0x1, %ecx
je 0xd105
cmpl $0x2, %ecx
jne 0xce50
leaq 0x1c(%rsp), %rdi
leaq 0x14(%rsp), %rsi
leaq 0x1070(%rsp), %rdx
callq 0x2ec9
jmp 0xce50
shrq $0x12, %rcx
movq 0x7fe9(%rip), %rax      # 0x14ff8
movq (%rax), %rdi
leaq 0x3933(%rip), %rsi      # 0x1094c
leaq 0x3a63(%rip), %rdx      # 0x10a83
movq 0x38(%rsp), %r8
xorl %eax, %eax
callq 0x2230
jmp 0xceae
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 %rbx, %rdi
leaq 0x3a38(%rip), %rsi      # 0x10aa1
leaq 0x1070(%rsp), %rdx
leaq 0x1c(%rsp), %rcx
leaq 0x14(%rsp), %r8
leaq 0x2070(%rsp), %r9
xorl %eax, %eax
callq 0x22b0
jmp 0xce50
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 0xce50
testl %eax, %eax
je 0xd13e
cmpl $0x1, %eax
je 0xd1a1
cmpl $0x2, %eax
jne 0xce50
leaq 0x18(%rsp), %rdi
leaq 0x10(%rsp), %rsi
leaq 0x70(%rsp), %rdx
callq 0x2f4d
jmp 0xce50
movq %r14, %rdi
leaq 0x1070(%rsp), %rsi
leaq 0x20(%rsp), %rdx
callq 0xdb26
cmpl $0x0, 0x20(%rsp)
je 0xce50
leaq 0x1070(%rsp), %rdi
movq 0x48(%rsp), %rsi
movl %eax, %edx
callq 0x2f0c
jmp 0xce50
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), %rbx
movq %rdi, 0x28(%rsp)
movq %rbx, %rsi
callq 0x2210
testl %eax, %eax
jg 0xd1d4
movq 0x68(%rsp), %rax
movq 0x50(%rsp), %rcx
shrq %cl, %rax
movq 0x60(%rsp), %r9
shrq %cl, %r9
movq 0x40b0(%rsp), %rdi
leaq 0x3843(%rip), %rsi      # 0x109d7
movq 0x28(%rsp), %rdx
movq %rax, %rcx
movq %rbx, %r8
jmp 0xd203
movq %r14, %rdi
leaq 0x70(%rsp), %rsi
leaq 0x20(%rsp), %rdx
callq 0xdb26
cmpl $0x0, 0x20(%rsp)
je 0xce50
leaq 0x70(%rsp), %rdi
movq 0x48(%rsp), %rsi
movl %eax, %edx
callq 0x2f90
jmp 0xce50
movq 0x60(%rsp), %rax
movq 0x50(%rsp), %rcx
shrq %cl, %rax
movq 0x68(%rsp), %r9
shrq %cl, %r9
movq 0x40b0(%rsp), %rdi
leaq 0x37f6(%rip), %rsi      # 0x109ee
movq %rbx, %rdx
movq %rax, %rcx
movq 0x28(%rsp), %r8
xorl %eax, %eax
callq 0x2230
incq 0x38(%rsp)
jmp 0xce50
movq 0x7ddd(%rip), %rax      # 0x14ff8
movq (%rax), %rdi
leaq 0x388d(%rip), %rsi      # 0x10ab2
leaq 0x3857(%rip), %rdx      # 0x10a83
xorl %ebx, %ebx
movq 0x38(%rsp), %rcx
xorl %eax, %eax
callq 0x2230
cmpl $0x0, (%r14)
movq 0x58(%rsp), %r13
je 0xd27b
xorl %r15d, %r15d
movq 0x10(%r14), %rax
movl %r15d, %ecx
shrl $0x4, %ecx
movl (%rax,%rcx,4), %eax
movl %ebx, %ecx
andb $0x1e, %cl
shrl %cl, %eax
testb $0x3, %al
jne 0xd26d
movq 0x18(%r14), %rax
movq (%rax,%r15,8), %rdi
callq 0x2060
incq %r15
movl (%r14), %eax
addl $0x2, %ebx
cmpq %rax, %r15
jb 0xd248
movq 0x18(%r14), %rsi
xorl %edi, %edi
callq 0x4162
movq 0x10(%r14), %rsi
xorl %edi, %edi
callq 0x4162
movq 0x20(%r14), %rsi
xorl %edi, %edi
callq 0x4162
xorl %edi, %edi
movq %r14, %rsi
callq 0x4162
movq %r12, %rdi
callq 0x36fd
movq 0x30(%rsp), %rdi
callq 0x5398
movq %r13, %rdi
callq 0x52fc
addq $0x4078, %rsp           # imm = 0x4078
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %rdi
callq 0x2fd1