// File: /workspace/github_c_makefile_star2_O1/xiongxu[P]bam2bedGraph2/samtools-0.1.19/misc/bamcheck.c
// Function: main
int main(int argc, char *argv[])
{
    char *targets = NULL;
    char *bam_fname = NULL;
    char *group_id = NULL;
    samfile_t *sam = NULL;
    char in_mode[5];

    stats_t *stats = calloc(1,sizeof(stats_t));
    stats->ngc    = 200;
    stats->nquals = 256;
    stats->nbases = 300;
    stats->nisize = 8000;
    stats->max_len   = 30;
    stats->max_qual  = 40;
    stats->isize_main_bulk = 0.99;   // There are always outliers at the far end
    stats->gcd_bin_size = 20e3;
    stats->gcd_ref_size = 4.2e9;
    stats->rseq_pos     = -1;
    stats->tid = stats->gcd_pos = -1;
    stats->igcd = 0;
    stats->is_sorted = 1;
    stats->cov_min  = 1;
    stats->cov_max  = 1000;
    stats->cov_step = 1;
    stats->argc = argc;
    stats->argv = argv;
    stats->filter_readlen = -1;
    stats->nindels = stats->nbases;

    strcpy(in_mode, "rb");

    static struct option loptions[] = 
    {
        {"help",0,0,'h'},
        {"remove-dups",0,0,'d'},
        {"sam",0,0,'s'},
        {"ref-seq",1,0,'r'},
        {"coverage",1,0,'c'},
        {"read-length",1,0,'l'},
        {"insert-size",1,0,'i'},
        {"most-inserts",1,0,'m'},
        {"trim-quality",1,0,'q'},
        {"target-regions",0,0,'t'},
        {"required-flag",1,0,'f'},
        {"filtering-flag",0,0,'F'},
        {"id",1,0,'I'},
        {"GC-depth",1,0,1},
        {0,0,0,0}
    };
    int opt;
    while ( (opt=getopt_long(argc,argv,"?hdsr:c:l:i:t:m:q:f:F:I:1:",loptions,NULL))>0 )
    {
        switch (opt)
        {
            case 'f': stats->flag_require=strtol(optarg,0,0); break;
            case 'F': stats->flag_filter=strtol(optarg,0,0); break;
            case 'd': stats->flag_filter|=BAM_FDUP; break;
            case 's': strcpy(in_mode, "r"); break;
            case 'r': stats->fai = fai_load(optarg); 
                      if (stats->fai==0) 
                          error("Could not load faidx: %s\n", optarg); 
                      break;
            case  1 : {
                        float flen,fbin;
                        if ( sscanf(optarg,"%f,%f",&fbin,&flen)!= 2 ) 
                            error("Unable to parse --GC-depth %s\n", optarg); 
                        stats->gcd_bin_size = fbin;
                        stats->gcd_ref_size = flen;
                      }
                      break;
            case 'c': if ( sscanf(optarg,"%d,%d,%d",&stats->cov_min,&stats->cov_max,&stats->cov_step)!= 3 ) 
                          error("Unable to parse -c %s\n", optarg); 
                      break;
            case 'l': stats->filter_readlen = atoi(optarg); break;
            case 'i': stats->nisize = atoi(optarg); break;
            case 'm': stats->isize_main_bulk = atof(optarg); break;
            case 'q': stats->trim_qual = atoi(optarg); break;
            case 't': targets = optarg; break;
            case 'I': group_id = optarg; break;
            case '?': 
            case 'h': error(NULL);
            default: error("Unknown argument: %s\n", optarg);
        }
    }
    if ( optind<argc )
        bam_fname = argv[optind++];

    if ( !bam_fname )
    {
        if ( isatty(fileno((FILE *)stdin)) )
            error(NULL);
        bam_fname = "-";
    }

    // Init structures
    //  .. coverage bins and round buffer
    if ( stats->cov_step > stats->cov_max - stats->cov_min + 1 )
    {
        stats->cov_step = stats->cov_max - stats->cov_min;
        if ( stats->cov_step <= 0 )
            stats->cov_step = 1;
    }
    stats->ncov = 3 + (stats->cov_max-stats->cov_min) / stats->cov_step;
    stats->cov_max = stats->cov_min + ((stats->cov_max-stats->cov_min)/stats->cov_step +1)*stats->cov_step - 1;
    stats->cov = calloc(sizeof(uint64_t),stats->ncov);
    stats->cov_rbuf.size = stats->nbases*5;
    stats->cov_rbuf.buffer = calloc(sizeof(int32_t),stats->cov_rbuf.size);
    // .. bam
    if ((sam = samopen(bam_fname, in_mode, NULL)) == 0) 
        error("Failed to open: %s\n", bam_fname);
    stats->sam = sam;
    if ( group_id ) init_group_id(stats, group_id);
    bam1_t *bam_line = bam_init1();
    // .. arrays
    stats->quals_1st      = calloc(stats->nquals*stats->nbases,sizeof(uint64_t));
    stats->quals_2nd      = calloc(stats->nquals*stats->nbases,sizeof(uint64_t));
    stats->gc_1st         = calloc(stats->ngc,sizeof(uint64_t));
    stats->gc_2nd         = calloc(stats->ngc,sizeof(uint64_t));
    stats->isize_inward   = calloc(stats->nisize,sizeof(uint64_t));
    stats->isize_outward  = calloc(stats->nisize,sizeof(uint64_t));
    stats->isize_other    = calloc(stats->nisize,sizeof(uint64_t));
    stats->gcd            = calloc(stats->ngcd,sizeof(gc_depth_t));
    stats->mpc_buf        = stats->fai ? calloc(stats->nquals*stats->nbases,sizeof(uint64_t)) : NULL;
    stats->acgt_cycles    = calloc(4*stats->nbases,sizeof(uint64_t));
    stats->read_lengths   = calloc(stats->nbases,sizeof(uint64_t));
    stats->insertions     = calloc(stats->nbases,sizeof(uint64_t));
    stats->deletions      = calloc(stats->nbases,sizeof(uint64_t));
    stats->ins_cycles_1st = calloc(stats->nbases+1,sizeof(uint64_t));
    stats->ins_cycles_2nd = calloc(stats->nbases+1,sizeof(uint64_t));
    stats->del_cycles_1st = calloc(stats->nbases+1,sizeof(uint64_t));
    stats->del_cycles_2nd = calloc(stats->nbases+1,sizeof(uint64_t));
    realloc_rseq_buffer(stats);
    if ( targets )
        init_regions(stats, targets);

    // Collect statistics
    if ( optind<argc )
    {
        // Collect stats in selected regions only
        bam_index_t *bam_idx = bam_index_load(bam_fname);
        if (bam_idx == 0)
            error("Random alignment retrieval only works for indexed BAM files.\n");

        int i;
        for (i=optind; i<argc; i++) 
        {
            int tid, beg, end;
            bam_parse_region(stats->sam->header, argv[i], &tid, &beg, &end);
            if ( tid < 0 ) continue;
            reset_regions(stats);
            bam_fetch(stats->sam->x.bam, bam_idx, tid, beg, end, stats, fetch_read);
        }
        bam_index_destroy(bam_idx);
    }
    else
    {
        // Stream through the entire BAM ignoring off-target regions if -t is given
        while (samread(sam,bam_line) >= 0) 
            collect_stats(bam_line,stats);
    }
    round_buffer_flush(stats,-1);

    output_stats(stats);

    bam_destroy1(bam_line);
    samclose(stats->sam);
    if (stats->fai) fai_destroy(stats->fai);
    free(stats->cov_rbuf.buffer); free(stats->cov);
    free(stats->quals_1st); free(stats->quals_2nd); 
    free(stats->gc_1st); free(stats->gc_2nd);
    free(stats->isize_inward); free(stats->isize_outward); free(stats->isize_other);
    free(stats->gcd);
    free(stats->rseq_buf);
    free(stats->mpc_buf);
    free(stats->acgt_cycles);
    free(stats->read_lengths);
    free(stats->insertions);
    free(stats->deletions);
    free(stats->ins_cycles_1st);
    free(stats->ins_cycles_2nd);
    free(stats->del_cycles_1st);
    free(stats->del_cycles_2nd);
    destroy_regions(stats);
    free(stats);
    if ( stats->rg_hash ) kh_destroy(kh_rg, stats->rg_hash);

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %r14
movl %edi, %ebp
movl $0x1, %edi
movl $0x1f0, %esi            # imm = 0x1F0
callq 0x32e0
movq %rax, %rbx
movabsq $0x12c00000100, %rax    # imm = 0x12C00000100
movq %rax, 0x4(%rbx)
movabsq $0xc800001f40, %rax     # imm = 0xC800001F40
movq %rax, 0xc(%rbx)
movaps 0x1169e(%rip), %xmm0    # 0x19160
movups %xmm0, 0x90(%rbx)
movabsq $-0x5a915ffffffb1e0, %rax # imm = 0xFA56EA0000004E20
movq %rax, 0x138(%rbx)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, 0x18c(%rbx)
movq $-0x1, 0x140(%rbx)
movl $0x0, 0x12c(%rbx)
leaq 0x158(%rbx), %rcx
movq %rcx, 0x50(%rsp)
movabsq $0x3e800000001, %rcx    # imm = 0x3E800000001
movq %rcx, 0x158(%rbx)
leaq 0x15c(%rbx), %rcx
movq %rcx, 0x48(%rsp)
leaq 0x160(%rbx), %rcx
movq %rcx, 0x20(%rsp)
movl $0x1, 0x160(%rbx)
movl %ebp, 0x1e0(%rbx)
movq %r14, 0x1e8(%rbx)
movl %eax, 0x1a0(%rbx)
movl $0x12c, 0x14(%rbx)      # imm = 0x12C
movw $0x6272, 0x1a(%rsp)     # imm = 0x6272
movb $0x0, 0x1c(%rsp)
leaq 0x12280(%rip), %rdx     # 0x19de6
leaq 0x1a7c3(%rip), %rcx     # 0x22330
movq $0x0, 0x28(%rsp)
movl %ebp, %edi
movq %r14, 0x38(%rsp)
movq %r14, %rsi
xorl %r8d, %r8d
callq 0x31f0
movl $0x0, %ecx
movq %rcx, 0x30(%rsp)
testl %eax, %eax
jle 0x7d30
movq 0x1a437(%rip), %r14     # 0x21fd8
leaq 0x1223e(%rip), %r15     # 0x19de6
leaq 0x1a781(%rip), %r13     # 0x22330
leaq 0x114a6(%rip), %r12     # 0x1905c
movq $0x0, 0x30(%rsp)
movq $0x0, 0x28(%rsp)
cmpl $0x48, %eax
jle 0x7c10
leal -0x63(%rax), %ecx
cmpl $0x11, %ecx
ja 0x7c77
movslq (%r12,%rcx,4), %rax
addq %r12, %rax
jmpq *%rax
movq (%r14), %rdi
leaq 0x1414d(%rip), %rsi     # 0x1bd39
movq 0x50(%rsp), %rdx
movq 0x48(%rsp), %rcx
movq 0x20(%rsp), %r8
xorl %eax, %eax
callq 0x3410
cmpl $0x3, %eax
je 0x7d13
jmp 0x8250
cmpl $0x1, %eax
je 0x7c35
cmpl $0x46, %eax
jne 0x821e
movq (%r14), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3360
movl %eax, 0x1bc(%rbx)
jmp 0x7d13
movq (%r14), %rdi
leaq 0x121dc(%rip), %rsi     # 0x19e1b
leaq 0x14(%rsp), %rdx
leaq 0x10(%rsp), %rcx
xorl %eax, %eax
callq 0x3410
cmpl $0x2, %eax
jne 0x822c
cvttss2si 0x14(%rsp), %eax
movl %eax, 0x138(%rbx)
cvttss2si 0x10(%rsp), %rax
movl %eax, 0x13c(%rbx)
jmp 0x7d13
cmpl $0x49, %eax
jne 0x8238
movq (%r14), %rax
movq %rax, 0x28(%rsp)
jmp 0x7d13
movq (%r14), %rdi
callq 0x3170
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x98(%rbx)
jmp 0x7d13
movq (%r14), %rdi
callq 0x34f0
movl %eax, 0xc(%rbx)
jmp 0x7d13
movq (%r14), %rdi
callq 0x13f4a
movq %rax, 0x1d8(%rbx)
testq %rax, %rax
jne 0x7d13
jmp 0x8244
movq (%r14), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3360
movl %eax, 0x1b8(%rbx)
jmp 0x7d13
movq (%r14), %rdi
callq 0x34f0
movl %eax, 0x1a0(%rbx)
jmp 0x7d13
movw $0x72, 0x1a(%rsp)
jmp 0x7d13
orb $0x4, 0x1bd(%rbx)
jmp 0x7d13
movq (%r14), %rdi
callq 0x34f0
movl %eax, (%rbx)
jmp 0x7d13
movq (%r14), %rax
movq %rax, 0x30(%rsp)
movl %ebp, %edi
movq 0x38(%rsp), %rsi
movq %r15, %rdx
movq %r13, %rcx
xorl %r8d, %r8d
callq 0x31f0
testl %eax, %eax
jg 0x7bc8
movq 0x1a299(%rip), %rdx     # 0x21fd0
movslq (%rdx), %rax
cmpl %ebp, %eax
jge 0x7d4e
leal 0x1(%rax), %ecx
movl %ecx, (%rdx)
movq 0x38(%rsp), %rcx
movq (%rcx,%rax,8), %r15
jmp 0x7d51
xorl %r15d, %r15d
movq 0x20(%rsp), %r14
testq %r15, %r15
jne 0x7d80
movq 0x1a266(%rip), %rax     # 0x21fc8
movq (%rax), %rdi
callq 0x3390
movl %eax, %edi
callq 0x3120
testl %eax, %eax
jne 0x8223
leaq 0x120ed(%rip), %r15     # 0x19e6d
movq 0x48(%rsp), %rax
movl (%rax), %eax
movq 0x50(%rsp), %rcx
movl (%rcx), %ecx
subl %ecx, %eax
leal 0x1(%rax), %edx
cmpl %edx, (%r14)
jle 0x7da6
cmpl $0x2, %eax
movl $0x1, %edx
cmovgel %eax, %edx
movl %edx, (%r14)
movl 0x160(%rbx), %esi
cltd
idivl %esi
leal 0x3(%rax), %edx
movl %edx, 0x14c(%rbx)
incl %eax
imull %esi, %eax
addl %ecx, %eax
decl %eax
movl %eax, 0x15c(%rbx)
movslq %edx, %rsi
movl $0x8, %edi
callq 0x32e0
movq %rax, 0x150(%rbx)
movslq 0x8(%rbx), %rax
leaq (%rax,%rax,4), %rsi
movl %esi, 0x170(%rbx)
movl $0x4, %edi
callq 0x32e0
movq %rax, 0x178(%rbx)
leaq 0x1a(%rsp), %rsi
movq %r15, %rdi
xorl %edx, %edx
callq 0xe1d9
testq %rax, %rax
je 0x825c
movq %rax, %r13
movq %r15, 0x20(%rsp)
movq %rax, 0x1c8(%rbx)
movq 0x28(%rsp), %rsi
testq %rsi, %rsi
je 0x7e33
movq %rbx, %rdi
callq 0x7633
movl $0x1, %edi
movl $0x38, %esi
callq 0x32e0
movq %rax, %r12
movslq 0x4(%rbx), %r15
movslq 0x8(%rbx), %rax
movq %rax, 0x28(%rsp)
imulq %rax, %r15
movl $0x8, %esi
movq %r15, %rdi
callq 0x32e0
movq %rax, 0x18(%rbx)
movl $0x8, %esi
movq %r15, %rdi
callq 0x32e0
movq %rax, 0x20(%rbx)
movslq 0x10(%rbx), %r14
movl $0x8, %esi
movq %r14, %rdi
callq 0x32e0
movq %rax, 0x28(%rbx)
movl $0x8, %esi
movq %r14, %rdi
callq 0x32e0
movq %rax, 0x30(%rbx)
movslq 0xc(%rbx), %r14
movl $0x8, %esi
movq %r14, %rdi
callq 0x32e0
movq %rax, 0x38(%rbx)
movl $0x8, %esi
movq %r14, %rdi
callq 0x32e0
movq %rax, 0x40(%rbx)
movl $0x8, %esi
movq %r14, %rdi
callq 0x32e0
movq %rax, 0x48(%rbx)
movl 0x128(%rbx), %edi
movl $0x8, %esi
callq 0x32e0
movq %rax, 0x130(%rbx)
cmpq $0x0, 0x1d8(%rbx)
je 0x7f05
movl $0x8, %esi
movq %r15, %rdi
callq 0x32e0
jmp 0x7f07
xorl %eax, %eax
movq %rax, 0x198(%rbx)
movq 0x28(%rsp), %r14
movq %r14, %rdi
shlq $0x2, %rdi
movl $0x8, %esi
callq 0x32e0
movq %rax, 0x50(%rbx)
movl $0x8, %esi
movq %r14, %rdi
callq 0x32e0
movq %rax, 0x58(%rbx)
movl $0x8, %esi
movq %r14, %rdi
callq 0x32e0
movq %rax, 0x60(%rbx)
movl $0x8, %esi
movq %r14, %rdi
callq 0x32e0
movq %rax, 0x68(%rbx)
leal 0x1(%r14), %eax
movslq %eax, %r14
movl $0x8, %esi
movq %r14, %rdi
callq 0x32e0
movq %rax, 0x70(%rbx)
movl $0x8, %esi
movq %r14, %rdi
callq 0x32e0
movq %rax, 0x78(%rbx)
movl $0x8, %esi
movq %r14, %rdi
callq 0x32e0
movq %rax, 0x80(%rbx)
movl $0x8, %esi
movq %r14, %rdi
callq 0x32e0
movq %rax, 0x88(%rbx)
movq %rbx, %rdi
callq 0x5106
movq 0x30(%rsp), %rsi
testq %rsi, %rsi
je 0x7fc6
movq %rbx, %rdi
callq 0x7162
movq 0x1a003(%rip), %r14     # 0x21fd0
cmpl %ebp, (%r14)
jge 0x81fe
movq 0x20(%rsp), %rdi
callq 0x10380
testq %rax, %rax
je 0x826d
movq %rax, %r15
movslq (%r14), %r14
cmpl %ebp, %r14d
jge 0x8092
leaq 0x44(%rsp), %r13
movq 0x1c8(%rbx), %rax
movq 0x10(%rax), %rdi
movq 0x38(%rsp), %rax
movq (%rax,%r14,8), %rsi
leaq 0x10(%rsp), %rdx
leaq 0x14(%rsp), %rcx
movq %r13, %r8
callq 0x8d8e
cmpl $0x0, 0x10(%rsp)
js 0x8086
movslq 0x1a4(%rbx), %rcx
testq %rcx, %rcx
jle 0x8058
movq 0x1b0(%rbx), %rax
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rcx
xorl %edx, %edx
movl $0x0, 0x8(%rax,%rdx)
addq $0x18, %rdx
cmpq %rdx, %rcx
jne 0x8047
movq 0x1c8(%rbx), %rax
movq 0x8(%rax), %rdi
movl 0x10(%rsp), %edx
movl 0x14(%rsp), %ecx
movl 0x44(%rsp), %r8d
leaq 0x1ff(%rip), %rax       # 0x8276
movq %rax, (%rsp)
movq %r15, %rsi
movq %rbx, %r9
callq 0x10ded
incq %r14
cmpl %ebp, %r14d
jne 0x7ffd
movq %r15, %rdi
callq 0xfb05
movq %rbx, %rdi
movq $-0x1, %rsi
callq 0x44bc
movq %rbx, %rdi
callq 0x600e
testq %r12, %r12
je 0x80c8
movq 0x30(%r12), %rdi
callq 0x3060
movq %r12, %rdi
callq 0x3060
movq 0x1c8(%rbx), %rdi
callq 0xe62d
movq 0x1d8(%rbx), %rdi
testq %rdi, %rdi
je 0x80e5
callq 0x13a1c
movq 0x178(%rbx), %rdi
callq 0x3060
movq 0x150(%rbx), %rdi
callq 0x3060
movq 0x18(%rbx), %rdi
callq 0x3060
movq 0x20(%rbx), %rdi
callq 0x3060
movq 0x28(%rbx), %rdi
callq 0x3060
movq 0x30(%rbx), %rdi
callq 0x3060
movq 0x38(%rbx), %rdi
callq 0x3060
movq 0x40(%rbx), %rdi
callq 0x3060
movq 0x48(%rbx), %rdi
callq 0x3060
movq 0x130(%rbx), %rdi
callq 0x3060
movq 0x180(%rbx), %rdi
callq 0x3060
movq 0x198(%rbx), %rdi
callq 0x3060
movq 0x50(%rbx), %rdi
callq 0x3060
movq 0x58(%rbx), %rdi
callq 0x3060
movq 0x60(%rbx), %rdi
callq 0x3060
movq 0x68(%rbx), %rdi
callq 0x3060
movq 0x70(%rbx), %rdi
callq 0x3060
movq 0x78(%rbx), %rdi
callq 0x3060
movq 0x80(%rbx), %rdi
callq 0x3060
movq 0x88(%rbx), %rdi
callq 0x3060
movq %rbx, %rdi
callq 0x75a3
movq %rbx, %rdi
callq 0x3060
movq 0x1d0(%rbx), %rbx
testq %rbx, %rbx
je 0x81ed
movq 0x18(%rbx), %rdi
callq 0x3060
movq 0x10(%rbx), %rdi
callq 0x3060
movq 0x20(%rbx), %rdi
callq 0x3060
movq %rbx, %rdi
callq 0x3060
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r13, %rdi
movq %r12, %rsi
callq 0xe673
testl %eax, %eax
js 0x809a
movq %r12, %rdi
movq %rbx, %rsi
callq 0x55e8
jmp 0x81fe
cmpl $0x3f, %eax
jne 0x8238
xorl %edi, %edi
xorl %eax, %eax
callq 0x465a
movq (%r14), %rsi
leaq 0x11beb(%rip), %rdi     # 0x19e21
jmp 0x8266
movq (%r14), %rsi
leaq 0x11c15(%rip), %rdi     # 0x19e57
jmp 0x8266
movq (%r14), %rsi
leaq 0x11bb3(%rip), %rdi     # 0x19e01
jmp 0x8266
movq (%r14), %rsi
leaq 0x11be6(%rip), %rdi     # 0x19e40
jmp 0x8266
leaq 0x11c0c(%rip), %rdi     # 0x19e6f
movq %r15, %rsi
xorl %eax, %eax
callq 0x465a
leaq 0x11c0f(%rip), %rdi     # 0x19e83
jmp 0x8225