// File: /workspace/github_c_makefile_star2_O3/xiongxu[P]bam2bedGraph2/samtools-0.1.19/misc/bamcheck.c
// Function: read_ref_seq
void read_ref_seq(stats_t *stats,int32_t tid,int32_t pos)
{
    khash_t(kh_faidx) *h;
    khiter_t iter;
    faidx1_t val;
    char *chr, c;
    faidx_t *fai = stats->fai;

    h = fai->hash;
    chr = stats->sam->header->target_name[tid];

    // ID of the sequence name
    iter = kh_get(kh_faidx, h, chr);
    if (iter == kh_end(h)) 
        error("No such reference sequence [%s]?\n", chr);
    val = kh_value(h, iter);

    // Check the boundaries
    if (pos >= val.len)
        error("Was the bam file mapped with the reference sequence supplied?"
              " A read mapped beyond the end of the chromosome (%s:%d, chromosome length %d).\n", chr,pos,val.len);
    int size = stats->mrseq_buf;
    // The buffer extends beyond the chromosome end. Later the rest will be filled with N's.
    if (size+pos > val.len) size = val.len-pos;

    // Position the razf reader
    razf_seek(fai->rz, val.offset + pos / val.line_blen * val.line_len + pos % val.line_blen, SEEK_SET);

    uint8_t *ptr = stats->rseq_buf;
    int nread = 0;
    while ( nread<size && razf_read(fai->rz,&c,1) && !fai->rz->z_err )
    {
        if ( !isgraph(c) )
            continue;

        // Conversion between uint8_t coding and ACGT
        //      -12-4---8-------
        //      =ACMGRSVTWYHKDBN
        if ( c=='A' || c=='a' )
            *ptr = 1;
        else if ( c=='C' || c=='c' )
            *ptr = 2;
        else if ( c=='G' || c=='g' )
            *ptr = 4;
        else if ( c=='T' || c=='t' )
            *ptr = 8;
        else
            *ptr = 0;
        ptr++;
        nread++;
    }
    if ( nread < stats->mrseq_buf )
    {
        memset(ptr,0, stats->mrseq_buf - nread);
        nread = stats->mrseq_buf;
    }
    stats->nrseq_buf = nread;
    stats->rseq_pos  = pos;
    stats->tid       = tid;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, 0x10(%rsp)
movq 0x1c8(%rdi), %rax
movq 0x1d8(%rdi), %rcx
movq %rcx, 0x20(%rsp)
movq 0x18(%rcx), %rdi
movq 0x10(%rax), %rax
movq 0x8(%rax), %rax
movl %esi, 0x2c(%rsp)
movslq %esi, %rcx
movq (%rax,%rcx,8), %rsi
movq %rdi, 0x18(%rsp)
movl (%rdi), %r10d
testl %r10d, %r10d
movq %rdx, 0x30(%rsp)
je 0x50de
movsbl (%rsi), %edi
xorl %r8d, %r8d
movl $0x0, %ecx
testl %edi, %edi
je 0x50e8
movb 0x1(%rsi), %al
testb %al, %al
je 0x50e6
leaq 0x2(%rsi), %rdx
movl %edi, %ecx
movl %ecx, %edi
shll $0x5, %edi
subl %ecx, %edi
movsbl %al, %ecx
addl %edi, %ecx
movb (%rdx), %al
incq %rdx
testb %al, %al
jne 0x50c7
jmp 0x50e8
xorl %r15d, %r15d
jmp 0x5192
movl %edi, %ecx
movl %ecx, %eax
xorl %edx, %edx
divl %r10d
movl %edx, %r13d
leal -0x1(%r10), %edi
movl %ecx, %eax
xorl %edx, %edx
divl %edi
movl %edx, %ebx
incl %ebx
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rdi
movl %r13d, %r15d
movq %r10, 0x40(%rsp)
movq %rdi, 0x38(%rsp)
movl %r15d, %eax
shrl $0x4, %eax
movl (%rdi,%rax,4), %r12d
leal (%r15,%r15), %ebp
andl $0x1e, %ebp
movl %r12d, %eax
movl %ebp, %ecx
shrl %cl, %eax
testb $0x2, %al
jne 0x5185
testb $0x1, %al
jne 0x5162
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rax
movl %r15d, %ecx
movq (%rax,%rcx,8), %rdi
movq %rsi, %r14
callq 0x3300
movq 0x38(%rsp), %rdi
movq 0x40(%rsp), %r10
xorl %r8d, %r8d
movq %r14, %rsi
testl %eax, %eax
je 0x5185
addl %ebx, %r15d
cmpl %r10d, %r15d
movl %r10d, %eax
cmovbl %r8d, %eax
subl %eax, %r15d
cmpl %r13d, %r15d
jne 0x5116
leaq 0x1525b(%rip), %rdi     # 0x1a3d9
xorl %eax, %eax
callq 0x4926
movl %ebp, %ecx
shrl %cl, %r12d
testb $0x3, %r12b
cmovnel %r10d, %r15d
cmpl %r10d, %r15d
je 0x5177
movq 0x18(%rsp), %rax
movq 0x20(%rax), %rax
movl %r15d, %ecx
leaq (%rcx,%rcx,2), %rdx
movq 0x8(%rax,%rdx,8), %rcx
movq 0x30(%rsp), %r14
movslq %r14d, %rdi
cmpq %rdi, %rcx
jle 0x52f8
leaq (%rax,%rdx,8), %rsi
movslq (%rsi), %r8
movq 0x10(%rsp), %r12
movl 0x188(%r12), %eax
leal (%rax,%r14), %edx
movslq %edx, %rdx
movl %ecx, %ebp
subl %r14d, %ebp
cmpq %rdx, %rcx
cmovgel %eax, %ebp
movq 0x20(%rsp), %rax
movq (%rax), %rdi
movl %r14d, %eax
cltd
idivl 0x4(%rsi)
cltq
imulq %r8, %rax
addq 0x10(%rsi), %rax
movslq %edx, %rsi
addq %rax, %rsi
xorl %ebx, %ebx
xorl %edx, %edx
callq 0x135ef
movq 0x180(%r12), %r15
testl %ebp, %ebp
cmovlel %ebx, %ebp
jle 0x52ac
leaq 0xf(%rsp), %r12
movq 0x20(%rsp), %rax
movq (%rax), %r13
movq %r13, %rdi
movq %r12, %rsi
movl $0x1, %edx
callq 0x131e9
testl %eax, %eax
je 0x52a5
movq 0x20(%rsp), %rax
movq (%rax), %r13
cmpl $0x0, 0x88(%r13)
jne 0x52a5
callq 0x35f0
movq (%rax), %rcx
movsbq 0xf(%rsp), %rax
cmpw $0x0, (%rcx,%rax,2)
jns 0x522b
movzbl %al, %eax
andl $-0x21, %eax
cmpl $0x46, %eax
jg 0x527f
cmpl $0x41, %eax
je 0x5291
cmpl $0x43, %eax
jne 0x528d
movb $0x2, %al
jmp 0x5297
cmpl $0x47, %eax
je 0x5295
cmpl $0x54, %eax
jne 0x528d
movb $0x8, %al
jmp 0x5297
xorl %eax, %eax
jmp 0x5297
movb $0x1, %al
jmp 0x5297
movb $0x4, %al
movb %al, (%r15)
incq %r15
incl %ebx
cmpl %ebp, %ebx
jne 0x5223
jmp 0x52a7
movl %ebx, %ebp
movq 0x10(%rsp), %r12
movl 0x188(%r12), %eax
subl %ebp, %eax
jle 0x52cd
movslq %eax, %rdx
movq %r15, %rdi
xorl %esi, %esi
callq 0x3260
movl 0x188(%r12), %ebp
movl %ebp, 0x190(%r12)
movl %r14d, 0x18c(%r12)
movl 0x2c(%rsp), %eax
movl %eax, 0x140(%r12)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x150fc(%rip), %rdi     # 0x1a3fb
movl %r14d, %edx
xorl %eax, %eax
callq 0x4926