// File: /workspace/github_c_makefile_star2_O3/xiongxu[P]bam2bedGraph2/samtools-0.1.19/misc/bamcheck.c
// Function: init_regions
void init_regions(stats_t *stats, char *file)
{
    khiter_t iter;
    khash_t(kh_bam_tid) *header_hash;

    bam_init_header_hash(stats->sam->header);
    header_hash = (khash_t(kh_bam_tid)*)stats->sam->header->hash;

    FILE *fp = fopen(file,"r");
    if ( !fp ) error("%s: %s\n",file,strerror(errno));

    char *line = NULL;
    size_t len = 0;
    ssize_t nread;
    int warned = 0;
    int prev_tid=-1, prev_pos=-1;
    while ((nread = mygetline(&line, &len, fp)) != -1) 
    {
        if ( line[0] == '#' ) continue;

        int i = 0;
        while ( i<nread && !isspace(line[i]) ) i++;
        if ( i>=nread ) error("Could not parse the file: %s [%s]\n", file,line);
        line[i] = 0;

        iter = kh_get(kh_bam_tid, header_hash, line);
        int tid = kh_val(header_hash, iter);
        if ( iter == kh_end(header_hash) )
        {
            if ( !warned )
                fprintf(stderr,"Warning: Some sequences not present in the BAM, e.g. \"%s\". This message is printed only once.\n", line);
            warned = 1;
            continue;
        }

        if ( tid >= stats->nregions )
        {
            stats->regions = realloc(stats->regions,sizeof(regions_t)*(stats->nregions+100));
            int j;
            for (j=stats->nregions; j<stats->nregions+100; j++)
            {
                stats->regions[j].npos = stats->regions[j].mpos = stats->regions[j].cpos = 0;
                stats->regions[j].pos = NULL;
            }
            stats->nregions += 100;
        }
        int npos = stats->regions[tid].npos;
        if ( npos >= stats->regions[tid].mpos )
        {
            stats->regions[tid].mpos += 1000;
            stats->regions[tid].pos = realloc(stats->regions[tid].pos,sizeof(pos_t)*stats->regions[tid].mpos);
        }

        if ( (sscanf(line+i+1,"%d %d",&stats->regions[tid].pos[npos].from,&stats->regions[tid].pos[npos].to))!=2 ) error("Could not parse the region [%s]\n");
        if ( prev_tid==-1 || prev_tid!=tid )
        {
            prev_tid = tid;
            prev_pos = stats->regions[tid].pos[npos].from;
        }
        if ( prev_pos>stats->regions[tid].pos[npos].from )
            error("The positions are not in chromosomal order (%s:%d comes after %d)\n", line,stats->regions[tid].pos[npos].from,prev_pos);
        stats->regions[tid].npos++;
    }
    if (line) free(line);
    if ( !stats->regions ) error("Unable to map the -t sequences to the BAM sequences.\n");
    fclose(fp);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %rbx
movq %rdi, %r12
movq 0x1c8(%rdi), %rax
movq 0x10(%rax), %rdi
callq 0x8e02
movq 0x1c8(%r12), %rax
movq 0x10(%rax), %rax
movq 0x20(%rax), %rax
movq %rax, 0x18(%rsp)
leaq 0x15503(%rip), %rsi     # 0x1c926
movq %rbx, 0x28(%rsp)
movq %rbx, %rdi
callq 0x34b0
testq %rax, %rax
je 0x77a7
movq %rax, %r15
xorl %eax, %eax
movq %rsp, %r13
movq %rax, (%r13)
leaq 0x38(%rsp), %rbp
movq %rax, (%rbp)
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
movl $0xffffffff, 0x14(%rsp) # imm = 0xFFFFFFFF
movl $0x0, 0x10(%rsp)
movq %r13, %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x7307
movq (%rsp), %rbx
cmpq $-0x1, %rax
je 0x7762
cmpb $0x23, (%rbx)
je 0x7466
movq %rax, %r14
testq %rax, %rax
jle 0x7791
callq 0x35f0
movq (%rax), %rax
negq %r14
movl $0x1, %ebp
movsbq -0x1(%rbx,%rbp), %rcx
testb $0x20, 0x1(%rax,%rcx,2)
jne 0x74c5
leaq (%r14,%rbp), %rcx
incq %rcx
incq %rbp
cmpq $0x1, %rcx
jne 0x74a3
jmp 0x7791
movb $0x0, -0x1(%rbx,%rbp)
movq (%rsp), %r8
movq 0x18(%rsp), %rax
movl (%rax), %r10d
testl %r10d, %r10d
movq %r12, 0x30(%rsp)
movq %r15, 0x20(%rsp)
je 0x7515
movsbl (%r8), %ecx
testl %ecx, %ecx
movl $0x0, %edi
je 0x751d
movb 0x1(%r8), %al
testb %al, %al
je 0x751f
leaq 0x2(%r8), %rdx
movl %ecx, %esi
shll $0x5, %esi
subl %ecx, %esi
movsbl %al, %ecx
addl %esi, %ecx
movb (%rdx), %al
incq %rdx
testb %al, %al
jne 0x74fe
jmp 0x751f
xorl %r15d, %r15d
jmp 0x75c5
xorl %ecx, %ecx
movl %ecx, %eax
xorl %edx, %edx
divl %r10d
movl %edx, %r13d
leal -0x1(%r10), %esi
movl %ecx, %eax
xorl %edx, %edx
divl %esi
movl %edx, %ebx
incl %ebx
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rsi
movl %r13d, %r15d
movq %r10, 0x50(%rsp)
movq %r8, 0x48(%rsp)
movq %rsi, 0x40(%rsp)
movl %r15d, %eax
shrl $0x4, %eax
movl (%rsi,%rax,4), %r14d
leal (%r15,%r15), %r12d
andl $0x1e, %r12d
movl %r14d, %eax
movl %r12d, %ecx
shrl %cl, %eax
testb $0x2, %al
jne 0x75b7
testb $0x1, %al
jne 0x75a1
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rax
movl %r15d, %ecx
movq (%rax,%rcx,8), %rdi
movq %r8, %rsi
callq 0x3300
movq 0x40(%rsp), %rsi
movq 0x50(%rsp), %r10
movq 0x48(%rsp), %r8
xorl %edi, %edi
testl %eax, %eax
je 0x75b7
addl %ebx, %r15d
cmpl %r10d, %r15d
movl %r10d, %eax
cmovbl %edi, %eax
subl %eax, %r15d
cmpl %r13d, %r15d
jne 0x7552
jmp 0x75ca
movl %r12d, %ecx
shrl %cl, %r14d
testb $0x3, %r14b
cmovnel %r10d, %r15d
cmpl %r10d, %r15d
jne 0x7617
cmpl $0x0, 0x10(%rsp)
movl $0x1, 0x10(%rsp)
movq 0x30(%rsp), %r12
movq 0x20(%rsp), %r15
movq %rsp, %r13
leaq 0x38(%rsp), %rbp
jne 0x7466
movq 0x1ba02(%rip), %rax     # 0x22ff8
movq (%rax), %rdi
leaq 0x13665(%rip), %rsi     # 0x1ac65
movq %r8, %rdx
xorl %eax, %eax
callq 0x3320
movl $0x1, 0x10(%rsp)
jmp 0x7466
movq 0x18(%rsp), %rax
movq 0x20(%rax), %rax
movl %r15d, %ecx
movl (%rax,%rcx,4), %ebx
movq 0x30(%rsp), %r12
movslq 0x1a4(%r12), %rcx
movq 0x1b0(%r12), %rax
cmpl %ecx, %ebx
jl 0x769b
leaq (%rcx,%rcx,2), %rcx
leaq 0x960(,%rcx,8), %rsi
movq %rax, %rdi
callq 0x3430
xorl %edi, %edi
movq %rax, 0x1b0(%r12)
movslq 0x1a4(%r12), %rdx
leal 0x64(%rdx), %ecx
leaq (%rdx,%rdx,2), %rdx
leaq (%rax,%rdx,8), %rdx
movl $0x10, %esi
movl $0x0, -0x8(%rdx,%rsi)
movq %rdi, -0x10(%rdx,%rsi)
movq %rdi, (%rdx,%rsi)
addq $0x18, %rsi
cmpq $0x970, %rsi            # imm = 0x970
jne 0x7675
movl %ecx, 0x1a4(%r12)
movslq %ebx, %rcx
leaq (%rcx,%rcx,2), %r14
movslq (%rax,%r14,8), %r15
movslq 0x4(%rax,%r14,8), %rcx
cmpl %ecx, %r15d
jge 0x76b7
movq 0x10(%rax,%r14,8), %rax
jmp 0x76e3
leaq (%rax,%r14,8), %rax
leaq 0x3e8(%rcx), %rdx
movl %edx, 0x4(%rax)
movq 0x10(%rax), %rdi
leaq 0x1f40(,%rcx,8), %rsi
callq 0x3430
movq 0x1b0(%r12), %rcx
movq %rax, 0x10(%rcx,%r14,8)
movq %rsp, %r13
addq (%rsp), %rbp
leaq (%rax,%r15,8), %rdx
leaq (%rax,%r15,8), %rcx
addq $0x4, %rcx
movq %rbp, %rdi
leaq 0x135c4(%rip), %rsi     # 0x1acc4
xorl %eax, %eax
callq 0x3410
cmpl $0x2, %eax
jne 0x77d2
movl 0x14(%rsp), %ecx
cmpl $-0x1, %ecx
setne %al
cmpl %ebx, %ecx
sete %cl
andb %al, %cl
movq 0x1b0(%r12), %rax
movq 0x10(%rax,%r14,8), %rdx
movl (%rdx,%r15,8), %edx
cmpb $0x1, %cl
leaq 0x38(%rsp), %rbp
jne 0x774c
cmpl %edx, 0xc(%rsp)
movq 0x20(%rsp), %r15
jbe 0x7759
jmp 0x77db
movl %edx, 0xc(%rsp)
movl %ebx, 0x14(%rsp)
movq 0x20(%rsp), %r15
incl (%rax,%r14,8)
jmp 0x7466
testq %rbx, %rbx
je 0x776f
movq %rbx, %rdi
callq 0x3060
cmpq $0x0, 0x1b0(%r12)
je 0x77c4
movq %r15, %rdi
callq 0x31c0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x134aa(%rip), %rdi     # 0x1ac42
movq 0x28(%rsp), %rsi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x4926
callq 0x3090
movl (%rax), %edi
callq 0x35a0
leaq 0x13480(%rip), %rdi     # 0x1ac3a
movq 0x28(%rsp), %rsi
movq %rax, %rdx
jmp 0x77a0
leaq 0x13563(%rip), %rdi     # 0x1ad2e
xorl %eax, %eax
callq 0x4926
leaq 0x134f1(%rip), %rdi     # 0x1acca
jmp 0x77cb
movq (%rsp), %rsi
leaq 0x13505(%rip), %rdi     # 0x1aceb
movl 0xc(%rsp), %ecx
xorl %eax, %eax
callq 0x4926