// File: /workspace/github_c_makefile_star2_O3/c-zhou[P]yahs/juicer.c
// Function: make_juicer_pre_file_from_bin
KHASH_SET_INIT_STR(str)

static int make_juicer_pre_file_from_bin(char *f, char *agp, char *fai, uint8_t mq, int scale, int count_gap, FILE *fo)
{
    int ret;
    FILE *fp;
    uint32_t i, i0, i1, m;
    uint64_t p0, p1, pair_n, pair_c, pair_u;
    int64_t magic_number;
    uint8_t buffer[BUFF_SIZE * 17];
    CC_ERR_t err0, err1;

    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);
    
    // check BIN file header consistency
    if ((ret = file_sdict_match(f, sdict))) {
        fprintf(stderr, "[E::%s] Not a valid BIN file or BIN file header does not match sequence dictionary: %d\n", __func__, ret);
        fprintf(stderr, "[E::%s] Make sure no contig length threshold (YaHS option '-l') applied for the BIN file\n", __func__);
        fprintf(stderr, "[E::%s] Consider using a BAM or BED file instead\n", __func__);
        exit(EXIT_FAILURE);
    }

    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();

    pair_c = pair_u = 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;

            err0 = sd_coordinate_conversion(dict, *(uint32_t *) (buffer + i),     *(uint32_t *) (buffer + i + 4),  &i0, &p0, count_gap);
            err1 = sd_coordinate_conversion(dict, *(uint32_t *) (buffer + i + 8), *(uint32_t *) (buffer + i + 12), &i1, &p1, count_gap);
            
            if (err0 != CC_SUCCESS || err1 != CC_SUCCESS) {
                // fprintf(stderr, "[W::%s] sequence not found \n", __func__);
                ++pair_u;
            } 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);
            }
        }
    }

    fprintf(stderr, "[I::%s] %lu read pairs processed: %lu unmapped\n", __func__, pair_c, pair_u);
    fclose(fp);
    asm_destroy(dict);
    sd_destroy(sdict);

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x11078, %rsp          # imm = 0x11078
movl %r9d, 0x2c(%rsp)
movl %r8d, %ebx
movl %ecx, %ebp
movq %rsi, %r14
movq %rdi, %r12
movq %rdx, %rdi
xorl %esi, %esi
callq 0x5d57
movq %rax, %r15
movq %rax, %rdi
testq %r14, %r14
jne 0xd94c
callq 0x62db
jmp 0xd959
movq %r14, %rsi
movl $0x1, %edx
callq 0x6768
movq %rax, %r14
movq %r12, %rdi
movq %r15, %rsi
callq 0x9760
testl %eax, %eax
jne 0xdbe2
leaq 0x22e2(%rip), %rsi      # 0xfc58
movq %r12, %rdi
callq 0x2330
testq %rax, %rax
je 0xdbe9
leaq 0x60(%rsp), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %rax, 0x8(%rsp)
movq %rax, %rcx
callq 0x20e0
cmpl $0x1, %eax
je 0xd9af
xorl %eax, %eax
callq 0x3541
movq 0x60(%rsp), %rdi
callq 0x352e
testl %eax, %eax
je 0xdbf1
movq %r15, 0x58(%rsp)
movq 0x8(%rsp), %r15
movq %r15, %rdi
callq 0x96e4
leaq 0x68(%rsp), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x20e0
cmpl $0x1, %eax
je 0xd9f6
xorl %eax, %eax
callq 0x3541
movq %r14, 0x40(%rsp)
movq 0x68(%rsp), %r15
testq %r15, %r15
jne 0xda1a
movq $0x0, 0x10(%rsp)
xorl %ebx, %ebx
movq 0x8(%rsp), %r14
jmp 0xdb8d
movl %ebx, %eax
movq %rax, 0x38(%rsp)
xorl %ebx, %ebx
movq $0x0, 0x10(%rsp)
movq 0x8(%rsp), %r14
movl %ebp, 0x4(%rsp)
movq %r15, 0x18(%rsp)
movl 0x2c(%rsp), %r13d
movl $0x1, %esi
movl $0x11000, %edx          # imm = 0x11000
leaq 0x70(%rsp), %rdi
movq %r14, %rcx
callq 0x20e0
testl %eax, %eax
je 0xdb7f
movl $0x11, %r14d
movq %rax, 0x20(%rsp)
leal -0x11(%r14), %r12d
cmpb %bpl, 0x80(%rsp,%r12)
jb 0xdb6a
movl 0x70(%rsp,%r12), %esi
movl 0x74(%rsp,%r12), %edx
movq 0x40(%rsp), %r15
movq %r15, %rdi
leaq 0x34(%rsp), %rcx
leaq 0x50(%rsp), %r8
movl %r13d, %r9d
callq 0x7bb9
movl %eax, %ebp
movl 0x78(%rsp,%r12), %esi
movl 0x7c(%rsp,%r12), %edx
movq %r15, %rdi
leaq 0x30(%rsp), %rcx
leaq 0x48(%rsp), %r8
movl %r13d, %r9d
callq 0x7bb9
orl %ebp, %eax
je 0xdace
incq 0x10(%rsp)
jmp 0xdb5c
movq 0x8(%r15), %rax
movl 0x34(%rsp), %ecx
imulq $0x58, %rcx, %rcx
movq (%rax,%rcx), %r12
movl 0x30(%rsp), %ecx
imulq $0x58, %rcx, %rcx
movq (%rax,%rcx), %rbp
movq %r12, %rdi
movq %rbp, %rsi
callq 0x2210
testl %eax, %eax
jg 0xdb28
movq 0x50(%rsp), %rax
movq 0x38(%rsp), %rcx
shrq %cl, %rax
movq 0x48(%rsp), %r9
shrq %cl, %r9
movq 0x110b0(%rsp), %rdi
leaq 0x2ea4(%rip), %rsi      # 0x109c1
movq %r12, %rdx
movq %rax, %rcx
movq %rbp, %r8
jmp 0xdb55
movq 0x48(%rsp), %rax
movq 0x38(%rsp), %rcx
shrq %cl, %rax
movq 0x50(%rsp), %r9
shrq %cl, %r9
movq 0x110b0(%rsp), %rdi
leaq 0x2e8c(%rip), %rsi      # 0x109d8
movq %rbp, %rdx
movq %rax, %rcx
movq %r12, %r8
xorl %eax, %eax
callq 0x2230
movl 0x4(%rsp), %ebp
movq 0x18(%rsp), %r15
movq 0x20(%rsp), %rax
incq %rbx
cmpl %eax, %r14d
jae 0xdb7f
addl $0x11, %r14d
cmpq %r15, %rbx
jb 0xda69
cmpq %r15, %rbx
movq 0x8(%rsp), %r14
jb 0xda3f
movq 0x7464(%rip), %rax      # 0x14ff8
movq (%rax), %rdi
leaq 0x3078(%rip), %rsi      # 0x10c16
leaq 0x2fa9(%rip), %rdx      # 0x10b4e
movq %rbx, %rcx
movq 0x10(%rsp), %r8
xorl %eax, %eax
callq 0x2230
movq %r14, %rdi
callq 0x2120
movq 0x40(%rsp), %rdi
callq 0x52fd
movq 0x58(%rsp), %rdi
callq 0x525f
addq $0x11078, %rsp          # imm = 0x11078
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, %edi
callq 0x305f
movq %r12, %rdi
callq 0x30df
callq 0x30b7