// File: /workspace/github_c_makefile_star2_O1/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 $0x38, %rsp
movl %edx, %ebx
movl %esi, %r13d
movq 0x1c8(%rdi), %rax
movq 0x1d8(%rdi), %rcx
movq %rcx, 0x18(%rsp)
movq 0x18(%rcx), %rdx
movq 0x10(%rax), %rax
movq 0x8(%rax), %rax
movslq %esi, %rcx
movq (%rax,%rcx,8), %rsi
movq %rdx, 0x10(%rsp)
movl (%rdx), %r10d
testl %r10d, %r10d
movl %r13d, (%rsp)
movq %rdi, 0x8(%rsp)
je 0x4e17
movsbl (%rsi), %edi
xorl %r8d, %r8d
movl $0x0, %ecx
testl %edi, %edi
je 0x4e24
movb 0x1(%rsi), %al
testb %al, %al
je 0x4e22
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 0x4e00
jmp 0x4e24
movq %rdi, %r12
xorl %r15d, %r15d
jmp 0x4eda
movl %edi, %ecx
movq %rbx, 0x20(%rsp)
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, %r14d
incl %r14d
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rdi
movl %r13d, %r15d
movq %r10, 0x30(%rsp)
movq %rdi, 0x28(%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 0x4ebf
testb $0x1, %al
jne 0x4ea5
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rax
movl %r15d, %ecx
movq (%rax,%rcx,8), %rdi
movq %rsi, %rbx
callq 0x3300
movq 0x28(%rsp), %rdi
movq 0x30(%rsp), %r10
xorl %r8d, %r8d
movq %rbx, %rsi
testl %eax, %eax
je 0x4ebf
addl %r14d, %r15d
cmpl %r10d, %r15d
movl %r10d, %eax
cmovbl %r8d, %eax
subl %eax, %r15d
cmpl %r13d, %r15d
jne 0x4e59
movl %r10d, %r15d
jmp 0x4ecc
movl %ebp, %ecx
shrl %cl, %r12d
testb $0x3, %r12b
cmovnel %r10d, %r15d
movq 0x20(%rsp), %rbx
movl (%rsp), %r13d
movq 0x8(%rsp), %r12
cmpl %r10d, %r15d
je 0x5051
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rax
movl %r15d, %ecx
leaq (%rcx,%rcx,2), %rdx
movq 0x8(%rax,%rdx,8), %rcx
movslq %ebx, %rdi
cmpq %rdi, %rcx
jle 0x505f
leaq (%rax,%rdx,8), %rsi
movslq (%rsi), %r8
movl 0x188(%r12), %eax
leal (%rax,%rbx), %edx
movslq %edx, %rdx
movl %ecx, %ebp
subl %ebx, %ebp
cmpq %rdx, %rcx
cmovgel %eax, %ebp
movq 0x18(%rsp), %rax
movq (%rax), %rdi
movl %ebx, %eax
cltd
idivl 0x4(%rsi)
cltq
imulq %r8, %rax
addq 0x10(%rsi), %rax
movslq %edx, %rsi
addq %rax, %rsi
xorl %r14d, %r14d
xorl %edx, %edx
callq 0x12f74
movq 0x180(%r12), %r15
testl %ebp, %ebp
cmovlel %r14d, %ebp
jle 0x5009
leaq 0x7(%rsp), %r12
movq 0x18(%rsp), %rcx
movq (%rcx), %rdi
movq %r12, %rsi
movl $0x1, %edx
movq %rcx, %r13
callq 0x12b53
testl %eax, %eax
je 0x4ffd
movq (%r13), %rax
cmpl $0x0, 0x88(%rax)
jne 0x4ffd
callq 0x35f0
movq (%rax), %rcx
movsbq 0x7(%rsp), %rax
cmpw $0x0, (%rcx,%rax,2)
movq 0x18(%rsp), %rcx
jns 0x4f69
movzbl %al, %eax
andl $-0x21, %eax
cmpl $0x46, %eax
jg 0x4fc4
cmpl $0x41, %eax
je 0x4fda
cmpl $0x43, %eax
movl (%rsp), %r13d
jne 0x4fd6
movb $0x2, %al
jmp 0x4fe4
cmpl $0x47, %eax
je 0x4fde
cmpl $0x54, %eax
movl (%rsp), %r13d
jne 0x4fd6
movb $0x8, %al
jmp 0x4fe4
xorl %eax, %eax
jmp 0x4fe4
movb $0x1, %al
jmp 0x4fe0
movb $0x4, %al
movl (%rsp), %r13d
movb %al, (%r15)
incq %r15
incl %r14d
cmpl %ebp, %r14d
jne 0x4f69
movq 0x8(%rsp), %r12
jmp 0x5009
movl %r14d, %ebp
movq 0x8(%rsp), %r12
movl (%rsp), %r13d
movl 0x188(%r12), %eax
subl %ebp, %eax
jle 0x502a
movslq %eax, %rdx
movq %r15, %rdi
xorl %esi, %esi
callq 0x3260
movl 0x188(%r12), %ebp
movl %ebp, 0x190(%r12)
movl %ebx, 0x18c(%r12)
movl %r13d, 0x140(%r12)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x142d1(%rip), %rdi     # 0x19329
xorl %eax, %eax
callq 0x465a
leaq 0x142e5(%rip), %rdi     # 0x1934b
movl %ebx, %edx
xorl %eax, %eax
callq 0x465a