// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x110b0, %rsp          # imm = 0x110B0
movb %cl, %al
movq 0x10(%rbp), %rcx
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movb %al, -0x19(%rbp)
movl %r8d, -0x20(%rbp)
movl %r9d, -0x24(%rbp)
movq -0x18(%rbp), %rdi
xorl %esi, %esi
callq 0x72b0
movq %rax, -0x11080(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x10abf
movq -0x11080(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl $0x1, %edx
callq 0x81b0
movq %rax, -0x11090(%rbp)
jmp 0x10ad2
movq -0x11080(%rbp), %rdi
callq 0x7950
movq %rax, -0x11090(%rbp)
movq -0x11090(%rbp), %rax
movq %rax, -0x11088(%rbp)
movq -0x8(%rbp), %rdi
movq -0x11080(%rbp), %rsi
callq 0xb7a0
movl %eax, -0x28(%rbp)
cmpl $0x0, %eax
je 0x10b62
movq 0x74f9(%rip), %rax      # 0x17ff8
movq (%rax), %rdi
movl -0x28(%rbp), %ecx
leaq 0x3d80(%rip), %rsi      # 0x1488c
leaq 0x3dd1(%rip), %rdx      # 0x148e4
movb $0x0, %al
callq 0x2230
movq 0x74d7(%rip), %rax      # 0x17ff8
movq (%rax), %rdi
leaq 0x3dd7(%rip), %rsi      # 0x14902
leaq 0x3db2(%rip), %rdx      # 0x148e4
movb $0x0, %al
callq 0x2230
movq 0x74b8(%rip), %rax      # 0x17ff8
movq (%rax), %rdi
leaq 0x3e12(%rip), %rsi      # 0x1495c
leaq 0x3d93(%rip), %rdx      # 0x148e4
movb $0x0, %al
callq 0x2230
movl $0x1, %edi
callq 0x23e0
movq -0x8(%rbp), %rdi
leaq 0x3001(%rip), %rsi      # 0x13b6e
callq 0x2340
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x10baa
movq 0x7474(%rip), %rax      # 0x17ff8
movq (%rax), %rdi
movq -0x8(%rbp), %rcx
leaq 0x289e(%rip), %rsi      # 0x13430
leaq 0x3d4b(%rip), %rdx      # 0x148e4
movb $0x0, %al
callq 0x2230
movl $0x1, %edi
callq 0x23e0
movq -0x30(%rbp), %rcx
leaq -0x70(%rbp), %rdi
movl $0x8, %esi
movl $0x1, %edx
callq 0x20e0
movl %eax, -0x40(%rbp)
cmpl $0x1, -0x40(%rbp)
je 0x10bd1
movb $0x0, %al
callq 0x2b00
movq -0x70(%rbp), %rdi
callq 0x2ac0
cmpl $0x0, %eax
jne 0x10c08
movq 0x7412(%rip), %rax      # 0x17ff8
movq (%rax), %rdi
leaq 0x3d9e(%rip), %rsi      # 0x1498e
leaq 0x3ced(%rip), %rdx      # 0x148e4
movb $0x0, %al
callq 0x2230
movl $0x1, %edi
callq 0x23e0
movq -0x30(%rbp), %rdi
callq 0xb710
movq -0x30(%rbp), %rcx
leaq -0x58(%rbp), %rdi
movl $0x8, %esi
movl $0x1, %edx
callq 0x20e0
movl %eax, -0x40(%rbp)
cmpl $0x1, -0x40(%rbp)
je 0x10c38
movb $0x0, %al
callq 0x2b00
movq $0x0, -0x68(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x60(%rbp), %rax
cmpq -0x58(%rbp), %rax
jae 0x10e74
leaq -0x11070(%rbp), %rdi
movq -0x30(%rbp), %rcx
movl $0x1, %esi
movl $0x11000, %edx          # imm = 0x11000
callq 0x20e0
movl %eax, -0x40(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %ecx
xorl %eax, %eax
cmpl -0x40(%rbp), %ecx
movb %al, -0x11091(%rbp)
jae 0x10c9b
movq -0x60(%rbp), %rax
cmpq -0x58(%rbp), %rax
setb %al
movb %al, -0x11091(%rbp)
movb -0x11091(%rbp), %al
testb $0x1, %al
jne 0x10caa
jmp 0x10e6f
movl -0x34(%rbp), %eax
movzbl -0x11060(%rbp,%rax), %eax
movzbl -0x19(%rbp), %ecx
cmpl %ecx, %eax
jge 0x10cc2
jmp 0x10e55
movq -0x11088(%rbp), %rdi
movl -0x34(%rbp), %eax
movl -0x11070(%rbp,%rax), %esi
movl -0x34(%rbp), %eax
movl -0x1106c(%rbp,%rax), %edx
movl -0x24(%rbp), %r9d
leaq -0x38(%rbp), %rcx
leaq -0x48(%rbp), %r8
callq 0x95d0
movl %eax, -0x11074(%rbp)
movq -0x11088(%rbp), %rdi
movl -0x34(%rbp), %eax
movl -0x11068(%rbp,%rax), %esi
movl -0x34(%rbp), %eax
movl -0x11064(%rbp,%rax), %edx
movl -0x24(%rbp), %r9d
leaq -0x3c(%rbp), %rcx
leaq -0x50(%rbp), %r8
callq 0x95d0
movl %eax, -0x11078(%rbp)
cmpl $0x0, -0x11074(%rbp)
jne 0x10d38
cmpl $0x0, -0x11078(%rbp)
je 0x10d49
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x10e53
movq -0x11088(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x38(%rbp), %ecx
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rdi
movq -0x11088(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x3c(%rbp), %ecx
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rsi
callq 0x2210
cmpl $0x0, %eax
jg 0x10deb
movq 0x10(%rbp), %rdi
movq -0x11088(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x38(%rbp), %ecx
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rdx
movq -0x48(%rbp), %rax
movl -0x20(%rbp), %ecx
shrq %cl, %rax
movq %rax, -0x110a0(%rbp)
movq -0x11088(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x3c(%rbp), %ecx
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %r8
movq -0x50(%rbp), %r9
movl -0x20(%rbp), %eax
movl %eax, %ecx
shrq %cl, %r9
movq -0x110a0(%rbp), %rcx
leaq 0x3975(%rip), %rsi      # 0x14757
movb $0x0, %al
callq 0x2230
jmp 0x10e51
movq 0x10(%rbp), %rdi
movq -0x11088(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x3c(%rbp), %ecx
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rdx
movq -0x50(%rbp), %rax
movl -0x20(%rbp), %ecx
shrq %cl, %rax
movq %rax, -0x110a8(%rbp)
movq -0x11088(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x38(%rbp), %ecx
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %r8
movq -0x48(%rbp), %r9
movl -0x20(%rbp), %eax
movl %eax, %ecx
shrq %cl, %r9
movq -0x110a8(%rbp), %rcx
leaq 0x3924(%rip), %rsi      # 0x1476e
movb $0x0, %al
callq 0x2230
jmp 0x10e53
jmp 0x10e55
movl -0x34(%rbp), %eax
addl $0x11, %eax
movl %eax, -0x34(%rbp)
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x10c7a
jmp 0x10c48
movq 0x717d(%rip), %rax      # 0x17ff8
movq (%rax), %rdi
movq -0x60(%rbp), %rcx
movq -0x68(%rbp), %r8
leaq 0x3b1f(%rip), %rsi      # 0x149ac
leaq 0x3a50(%rip), %rdx      # 0x148e4
movb $0x0, %al
callq 0x2230
movq -0x30(%rbp), %rdi
callq 0x2120
movq -0x11088(%rbp), %rdi
callq 0x63d0
movq -0x11080(%rbp), %rdi
callq 0x62a0
xorl %eax, %eax
addq $0x110b0, %rsp          # imm = 0x110B0
popq %rbp
retq
nopw (%rax,%rax)