// File: /workspace/github_c_makefile_star2_O1/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, 0x24(%rsp)
movl %r8d, %ebx
movl %ecx, %r13d
movq %rsi, %r14
movq %rdi, %r12
movq %rdx, %rdi
xorl %esi, %esi
callq 0x5df2
movq %rax, %r15
movq %rax, %rdi
testq %r14, %r14
jne 0xd77f
callq 0x6398
jmp 0xd78c
movq %r14, %rsi
movl $0x1, %edx
callq 0x6814
movq %rax, %r14
movq %r12, %rdi
movq %r15, %rsi
callq 0x96f0
testl %eax, %eax
jne 0xda08
leaq 0x24bb(%rip), %rsi      # 0xfc64
movq %r12, %rdi
callq 0x2330
testq %rax, %rax
je 0xda0f
movq %rax, %rbp
leaq 0x60(%rsp), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0x20e0
cmpl $0x1, %eax
je 0xd7e0
xorl %eax, %eax
callq 0x3591
movq 0x60(%rsp), %rdi
callq 0x357e
testl %eax, %eax
je 0xda17
movq %r15, 0x58(%rsp)
movq %rbp, %rdi
callq 0x9674
leaq 0x68(%rsp), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %rbp, %rcx
callq 0x20e0
cmpl $0x1, %eax
je 0xd822
xorl %eax, %eax
callq 0x3591
movq %rbp, 0x30(%rsp)
movq %r14, 0x40(%rsp)
movq 0x68(%rsp), %r15
testq %r15, %r15
jne 0xd847
movq $0x0, 0x8(%rsp)
xorl %r12d, %r12d
jmp 0xd9b1
movl %ebx, %eax
movq %rax, 0x38(%rsp)
xorl %r12d, %r12d
movq $0x0, 0x8(%rsp)
movl %r13d, 0x4(%rsp)
movq %r15, 0x10(%rsp)
movl $0x1, %esi
movl $0x11000, %edx          # imm = 0x11000
leaq 0x70(%rsp), %rdi
movq 0x30(%rsp), %rcx
callq 0x20e0
testl %eax, %eax
je 0xd9a8
movl $0x11, %r14d
movq %rax, 0x18(%rsp)
leal -0x11(%r14), %ebp
cmpb %r13b, 0x80(%rsp,%rbp)
jb 0xd993
movl 0x70(%rsp,%rbp), %esi
movl 0x74(%rsp,%rbp), %edx
movq 0x40(%rsp), %r15
movq %r15, %rdi
leaq 0x2c(%rsp), %rcx
leaq 0x50(%rsp), %r8
movl 0x24(%rsp), %r13d
movl %r13d, %r9d
callq 0x7bc0
movl %eax, %ebx
movl 0x78(%rsp,%rbp), %esi
movl 0x7c(%rsp,%rbp), %edx
movq %r15, %rdi
leaq 0x28(%rsp), %rcx
leaq 0x48(%rsp), %r8
movl %r13d, %r9d
callq 0x7bc0
orl %ebx, %eax
je 0xd8f6
incq 0x8(%rsp)
jmp 0xd984
movq 0x8(%r15), %rax
movl 0x2c(%rsp), %ecx
imulq $0x58, %rcx, %rcx
movq (%rax,%rcx), %rbx
movl 0x28(%rsp), %ecx
imulq $0x58, %rcx, %rcx
movq (%rax,%rcx), %rbp
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x2210
testl %eax, %eax
jg 0xd950
movq 0x50(%rsp), %rax
movq 0x38(%rsp), %rcx
shrq %cl, %rax
movq 0x48(%rsp), %r9
shrq %cl, %r9
movq 0x110b0(%rsp), %rdi
leaq 0x3092(%rip), %rsi      # 0x109d7
movq %rbx, %rdx
movq %rax, %rcx
movq %rbp, %r8
jmp 0xd97d
movq 0x48(%rsp), %rax
movq 0x38(%rsp), %rcx
shrq %cl, %rax
movq 0x50(%rsp), %r9
shrq %cl, %r9
movq 0x110b0(%rsp), %rdi
leaq 0x307a(%rip), %rsi      # 0x109ee
movq %rbp, %rdx
movq %rax, %rcx
movq %rbx, %r8
xorl %eax, %eax
callq 0x2230
movl 0x4(%rsp), %r13d
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %rax
incq %r12
cmpl %eax, %r14d
jae 0xd9a8
addl $0x11, %r14d
cmpq %r15, %r12
jb 0xd890
cmpq %r15, %r12
jb 0xd864
movq 0x7640(%rip), %rax      # 0x14ff8
movq (%rax), %rdi
leaq 0x326a(%rip), %rsi      # 0x10c2c
leaq 0x319b(%rip), %rdx      # 0x10b64
movq %r12, %rcx
movq 0x8(%rsp), %r8
xorl %eax, %eax
callq 0x2230
movq 0x30(%rsp), %rdi
callq 0x2120
movq 0x40(%rsp), %rdi
callq 0x5398
movq 0x58(%rsp), %rdi
callq 0x52fc
addq $0x11078, %rsp          # imm = 0x11078
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, %edi
callq 0x30a9
movq %r12, %rdi
callq 0x3129
callq 0x3101