// File: /workspace/github_c_makefile_star2_O1/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, %r13
movq 0x1c8(%rdi), %rax
movq 0x10(%rax), %rdi
callq 0x8a18
movq 0x1c8(%r13), %rax
movq 0x10(%rax), %rax
movq 0x20(%rax), %rax
movq %rax, 0x18(%rsp)
leaq 0x14665(%rip), %rsi     # 0x1b806
movq %rbx, 0x28(%rsp)
movq %rbx, %rdi
callq 0x34b0
testq %rax, %rax
je 0x7559
movq %rax, %r15
xorl %eax, %eax
movq %rsp, %r12
movq %rax, (%r12)
leaq 0x20(%rsp), %r14
movq %rax, (%r14)
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
movl $0xffffffff, 0x14(%rsp) # imm = 0xFFFFFFFF
movl $0x0, 0x10(%rsp)
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x7086
movq (%rsp), %rbp
cmpq $-0x1, %rax
je 0x7515
cmpb $0x23, (%rbp)
je 0x71e3
movq %rax, %rbx
testq %rax, %rax
setg %r14b
jle 0x7543
callq 0x35f0
movq (%rax), %rax
movsbq (%rbp), %rcx
testb $0x20, 0x1(%rax,%rcx,2)
jne 0x7252
leaq -0x1(%rbx), %rcx
xorl %r12d, %r12d
cmpq %r12, %rcx
je 0x7543
movsbq 0x1(%rbp,%r12), %rdx
incq %r12
testb $0x20, 0x1(%rax,%rdx,2)
je 0x7230
cmpq %r12, %rbx
setg %r14b
jmp 0x7255
xorl %r12d, %r12d
testb %r14b, %r14b
je 0x7543
movq %r15, 0x30(%rsp)
movq (%rsp), %rax
movb $0x0, (%rax,%r12)
movq (%rsp), %r8
movq 0x18(%rsp), %rax
movl (%rax), %r10d
testl %r10d, %r10d
movl $0x0, %edi
movq %r13, 0x38(%rsp)
je 0x72b2
movsbl (%r8), %ecx
testl %ecx, %ecx
je 0x72ba
movb 0x1(%r8), %al
testb %al, %al
je 0x72bc
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 0x729b
jmp 0x72bc
xorl %r13d, %r13d
jmp 0x7364
xorl %ecx, %ecx
movl %ecx, %eax
xorl %edx, %edx
divl %r10d
movl %edx, %r15d
leal -0x1(%r10), %esi
movl %ecx, %eax
xorl %edx, %edx
divl %esi
movl %edx, %r14d
incl %r14d
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rsi
movl %r15d, %r13d
movq %r10, 0x50(%rsp)
movq %r8, 0x48(%rsp)
movq %rsi, 0x40(%rsp)
movl %r13d, %eax
shrl $0x4, %eax
movl (%rsi,%rax,4), %ebx
leal (,%r13,2), %ebp
andl $0x1e, %ebp
movl %ebx, %eax
movl %ebp, %ecx
shrl %cl, %eax
testb $0x2, %al
jne 0x7359
testb $0x1, %al
jne 0x7340
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rax
movl %r13d, %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 0x7359
addl %r14d, %r13d
cmpl %r10d, %r13d
movl %r10d, %eax
cmovbl %edi, %eax
subl %eax, %r13d
cmpl %r15d, %r13d
jne 0x72f1
movl %r10d, %r13d
jmp 0x7364
movl %ebp, %ecx
shrl %cl, %ebx
testb $0x3, %bl
cmovnel %r10d, %r13d
leaq 0x20(%rsp), %r14
cmpl %r10d, %r13d
jne 0x73b6
cmpl $0x0, 0x10(%rsp)
movl $0x1, 0x10(%rsp)
movq 0x38(%rsp), %r13
movq 0x30(%rsp), %r15
movq %rsp, %r12
jne 0x71e3
movq 0x1ac63(%rip), %rax     # 0x21ff8
movq (%rax), %rdi
leaq 0x12816(%rip), %rsi     # 0x19bb5
movq %r8, %rdx
xorl %eax, %eax
callq 0x3320
movl $0x1, 0x10(%rsp)
jmp 0x71e3
movq 0x18(%rsp), %rax
movq 0x20(%rax), %rax
movl %r13d, %ecx
movl (%rax,%rcx,4), %ebx
movq 0x38(%rsp), %r13
movslq 0x1a4(%r13), %rax
cmpl %eax, %ebx
jl 0x7432
movq 0x1b0(%r13), %rdi
leaq (%rax,%rax,2), %rax
leaq 0x960(,%rax,8), %rsi
callq 0x3430
xorl %esi, %esi
movq %rax, 0x1b0(%r13)
movslq 0x1a4(%r13), %rdx
leal 0x64(%rdx), %ecx
leaq (%rdx,%rdx,2), %rdx
leaq (%rax,%rdx,8), %rax
movl $0x10, %edx
movl $0x0, -0x8(%rax,%rdx)
movq %rsi, -0x10(%rax,%rdx)
movq %rsi, (%rax,%rdx)
addq $0x18, %rdx
cmpq $0x970, %rdx            # imm = 0x970
jne 0x740d
movl %ecx, 0x1a4(%r13)
movslq %ebx, %rax
movq 0x1b0(%r13), %rcx
leaq (%rax,%rax,2), %r14
movslq (%rcx,%r14,8), %r15
movslq 0x4(%rcx,%r14,8), %rax
cmpl %eax, %r15d
jl 0x7479
leaq (%rcx,%r14,8), %rcx
leaq 0x3e8(%rax), %rdx
movl %edx, 0x4(%rcx)
movq 0x10(%rcx), %rdi
leaq 0x1f40(,%rax,8), %rsi
callq 0x3430
movq 0x1b0(%r13), %rcx
movq %rax, 0x10(%rcx,%r14,8)
movq (%rsp), %rax
leaq (%rax,%r12), %rdi
incq %rdi
movq 0x1b0(%r13), %rax
movq 0x10(%rax,%r14,8), %rax
leaq (%rax,%r15,8), %rdx
leaq (%rax,%r15,8), %rcx
addq $0x4, %rcx
leaq 0x12771(%rip), %rsi     # 0x19c14
xorl %eax, %eax
callq 0x3410
cmpl $0x2, %eax
jne 0x7584
movl 0x14(%rsp), %ecx
cmpl $-0x1, %ecx
setne %al
cmpl %ebx, %ecx
sete %cl
testb %cl, %al
movq %rsp, %r12
jne 0x74e1
movq 0x1b0(%r13), %rax
movq 0x10(%rax,%r14,8), %rax
movl (%rax,%r15,8), %eax
movl %eax, 0xc(%rsp)
movl %ebx, 0x14(%rsp)
movq 0x1b0(%r13), %rax
movq 0x10(%rax,%r14,8), %rax
movl (%rax,%r15,8), %edx
cmpl %edx, 0xc(%rsp)
ja 0x758d
movq 0x1b0(%r13), %rax
incl (%rax,%r14,8)
movq 0x30(%rsp), %r15
leaq 0x20(%rsp), %r14
jmp 0x71e3
testq %rbp, %rbp
je 0x7522
movq %rbp, %rdi
callq 0x3060
cmpq $0x0, 0x1b0(%r13)
je 0x7576
movq %r15, %rdi
callq 0x31c0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x12648(%rip), %rdi     # 0x19b92
movq 0x28(%rsp), %rsi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x465a
callq 0x3090
movl (%rax), %edi
callq 0x35a0
leaq 0x1261e(%rip), %rdi     # 0x19b8a
movq 0x28(%rsp), %rsi
movq %rax, %rdx
jmp 0x7552
leaq 0x12701(%rip), %rdi     # 0x19c7e
xorl %eax, %eax
callq 0x465a
leaq 0x1268f(%rip), %rdi     # 0x19c1a
jmp 0x757d
movq (%rsp), %rsi
leaq 0x126a3(%rip), %rdi     # 0x19c3b
movl 0xc(%rsp), %ecx
xorl %eax, %eax
callq 0x465a