// File: /workspace/github_c_makefile_star2_O2/c-zhou[P]yahs/juicer.c
// Function: kh_resize_str
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 $0x28, %rsp
decl %esi
movl %esi, %eax
shrl %eax
orl %esi, %eax
movl %eax, %ecx
shrl $0x2, %ecx
orl %eax, %ecx
movl %ecx, %eax
shrl $0x4, %eax
orl %ecx, %eax
movl %eax, %ecx
shrl $0x8, %ecx
orl %eax, %ecx
movl %ecx, %eax
shrl $0x10, %eax
orl %ecx, %eax
incl %eax
cmpl $0x5, %eax
pushq $0x4
popq %rcx
movl %ecx, %r13d
cmovael %eax, %r13d
cvtsi2sd %r13, %xmm0
mulsd 0x12e0(%rip), %xmm0     # 0xd418
addsd 0x12e0(%rip), %xmm0     # 0xd420
cvttsd2si %xmm0, %rdx
xorl %r12d, %r12d
cmpl %edx, 0x4(%rdi)
jae 0xc2e4
movq %rdi, %rbx
movq %rdx, 0x20(%rsp)
movl %r13d, %r15d
shrl $0x2, %r15d
andl $-0x4, %r15d
cmpl $0x10, %eax
cmovbl %ecx, %r15d
xorl %edi, %edi
movq %r15, %rsi
callq 0x3598
pushq $-0x1
popq %r12
testq %rax, %rax
je 0xc2e4
movq %rax, %r14
movq %rax, %rdi
movl $0xaa, %esi
movq %r15, %rdx
callq 0x21a0
movl (%rbx), %edx
movl %r13d, %r15d
cmpl %r13d, %edx
jae 0xc1c1
movq 0x18(%rbx), %rsi
leaq (,%r15,8), %rdx
xorl %edi, %edi
callq 0x36a0
testq %rax, %rax
je 0xc2f6
movq %rax, 0x18(%rbx)
movl (%rbx), %edx
movq %r15, 0x10(%rsp)
movq %r13, 0x18(%rsp)
leal -0x1(%r13), %r15d
xorl %r8d, %r8d
pushq $0x2
popq %r13
cmpl %edx, %r8d
je 0xc29f
movq 0x10(%rbx), %rbp
movl %r8d, %eax
shrl $0x4, %eax
movl (%rbp,%rax,4), %esi
leal (%r8,%r8), %ecx
andl $0x1e, %ecx
movl %esi, %edi
shrl %cl, %edi
testb $0x3, %dil
jne 0xc297
movq 0x18(%rbx), %rdx
movq %r8, 0x8(%rsp)
movl %r8d, %edi
movq (%rdx,%rdi,8), %r12
btsl %ecx, %esi
movl %esi, (%rbp,%rax,4)
movq %r12, %rdi
callq 0xc302
xorl %edx, %edx
andl %r15d, %eax
movl %eax, %esi
shrl $0x4, %esi
movl (%r14,%rsi,4), %edi
leal (%rax,%rax), %ecx
andl $0x1e, %ecx
movl %r13d, %r8d
shll %cl, %r8d
testl %edi, %r8d
jne 0xc24c
addl %edx, %eax
incl %eax
movl %edx, %ecx
incl %ecx
movl %ecx, %edx
jmp 0xc223
notl %r8d
andl %r8d, %edi
movl %edi, (%r14,%rsi,4)
movl (%rbx), %edx
movl %eax, %edi
cmpl %edx, %eax
jae 0xc28a
movl (%rbp,%rsi,4), %r8d
shrl %cl, %r8d
movq 0x18(%rbx), %rax
testb $0x3, %r8b
jne 0xc28e
movq (%rax,%rdi,8), %rdx
movq %r12, (%rax,%rdi,8)
pushq $0x1
popq %rax
shll %cl, %eax
movq 0x10(%rbx), %rbp
orl %eax, (%rbp,%rsi,4)
movq %rdx, %r12
jmp 0xc219
movq 0x18(%rbx), %rax
movq 0x8(%rsp), %r8
movq %r12, (%rax,%rdi,8)
incl %r8d
jmp 0xc1d6
movq 0x18(%rsp), %r15
cmpl %r15d, %edx
jbe 0xc2c1
movq 0x18(%rbx), %rsi
movq 0x10(%rsp), %rdx
shlq $0x3, %rdx
xorl %edi, %edi
callq 0x36a0
movq %rax, 0x18(%rbx)
movq 0x10(%rbx), %rsi
xorl %r12d, %r12d
xorl %edi, %edi
callq 0x34a3
movq %r14, 0x10(%rbx)
movl %r15d, (%rbx)
movl 0x4(%rbx), %eax
movl %eax, 0x8(%rbx)
movq 0x20(%rsp), %rax
movl %eax, 0xc(%rbx)
movl %r12d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %edi, %edi
movq %r14, %rsi
callq 0x34a3
jmp 0xc2e4