// File: /workspace/github_c_makefile_star2_O3/xiongxu[P]bam2bedGraph2/samtools-0.1.19/faidx.c
// Function: faidx_fetch_seq
char *faidx_fetch_seq(const faidx_t *fai, char *c_name, int p_beg_i, int p_end_i, int *len)
{
	int l;
	char c;
    khiter_t iter;
    faidx1_t val;
	char *seq=NULL;

    // Adjust position
    iter = kh_get(s, fai->hash, c_name);
    if(iter == kh_end(fai->hash)) return 0;
    val = kh_value(fai->hash, iter);
	if(p_end_i < p_beg_i) p_beg_i = p_end_i;
    if(p_beg_i < 0) p_beg_i = 0;
    else if(val.len <= p_beg_i) p_beg_i = val.len - 1;
    if(p_end_i < 0) p_end_i = 0;
    else if(val.len <= p_end_i) p_end_i = val.len - 1;

    // Now retrieve the sequence 
	l = 0;
	seq = (char*)malloc(p_end_i - p_beg_i + 2);
	razf_seek(fai->rz, val.offset + p_beg_i / val.line_blen * val.line_len + p_beg_i % val.line_blen, SEEK_SET);
	while (razf_read(fai->rz, &c, 1) == 1 && l < p_end_i - p_beg_i + 1)
		if (isgraph(c)) seq[l++] = c;
	seq[l] = '\0';
	*len = l;
	return seq;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r8, 0x28(%rsp)
movq 0x18(%rdi), %rax
movq %rax, 0x8(%rsp)
movl (%rax), %ebp
testl %ebp, %ebp
movq %rdi, 0x20(%rsp)
je 0x15134
movsbl (%rsi), %r10d
movl $0x0, %r8d
testl %r10d, %r10d
je 0x1513f
movb 0x1(%rsi), %al
testb %al, %al
je 0x1513c
leaq 0x2(%rsi), %r9
movl %r10d, %r8d
movl %r8d, %r10d
shll $0x5, %r10d
subl %r8d, %r10d
movsbl %al, %r8d
addl %r10d, %r8d
movb (%r9), %al
incq %r9
testb %al, %al
jne 0x15117
jmp 0x1513f
xorl %r13d, %r13d
jmp 0x151ea
movl %r10d, %r8d
movl %edx, 0x14(%rsp)
movl %ecx, 0x18(%rsp)
movl %r8d, %eax
xorl %edx, %edx
divl %ebp
movl %edx, %edi
leal -0x1(%rbp), %ecx
movl %r8d, %eax
xorl %edx, %edx
divl %ecx
movl %edx, %r14d
incl %r14d
movq 0x8(%rsp), %rax
movq 0x10(%rax), %r15
movl %edi, %r13d
movl %edi, 0x1c(%rsp)
movq %rsi, 0x30(%rsp)
movl %r13d, %eax
shrl $0x4, %eax
movl (%r15,%rax,4), %ebx
leal (,%r13,2), %r12d
andl $0x1e, %r12d
movl %ebx, %eax
movl %r12d, %ecx
shrl %cl, %eax
testb $0x2, %al
jne 0x151d6
testb $0x1, %al
jne 0x151bc
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rax
movl %r13d, %ecx
movq (%rax,%rcx,8), %rdi
callq 0x3300
movl 0x1c(%rsp), %edi
movq 0x30(%rsp), %rsi
testl %eax, %eax
je 0x151d6
addl %r14d, %r13d
cmpl %ebp, %r13d
movl %ebp, %eax
movl $0x0, %ecx
cmovbl %ecx, %eax
subl %eax, %r13d
cmpl %edi, %r13d
jne 0x15175
jmp 0x151f1
movl %r12d, %ecx
shrl %cl, %ebx
testb $0x3, %bl
cmovnel %ebp, %r13d
movl 0x18(%rsp), %ecx
movl 0x14(%rsp), %edx
xorl %ebx, %ebx
cmpl %ebp, %r13d
jne 0x151f9
xorl %r15d, %r15d
jmp 0x152fe
movq 0x8(%rsp), %rax
movq 0x20(%rax), %rdi
movl %r13d, %eax
leaq (%rax,%rax,2), %r8
movq 0x8(%rdi,%r8,8), %rax
cmpl %edx, %ecx
cmovll %ecx, %edx
testl %edx, %edx
js 0x15223
movl %edx, %esi
movl %edx, %ebx
cmpq %rsi, %rax
jg 0x15223
leal -0x1(%rax), %ebx
movslq (%rdi,%r8,8), %rdx
movq %rdx, 0x8(%rsp)
movl 0x4(%rdi,%r8,8), %r13d
movq 0x10(%rdi,%r8,8), %r12
xorl %ebp, %ebp
movl $0x0, %r14d
testl %ecx, %ecx
js 0x15251
movl %ecx, %edx
movl %ecx, %r14d
cmpq %rdx, %rax
jg 0x15251
decl %eax
movl %eax, %r14d
subl %ebx, %r14d
leal 0x2(%r14), %eax
movslq %eax, %rdi
callq 0x33e0
movq %rax, %r15
movq 0x20(%rsp), %rcx
movq (%rcx), %rdi
movl %ebx, %eax
cltd
idivl %r13d
movq %rcx, %r13
cltq
imulq 0x8(%rsp), %rax
movslq %edx, %rcx
addq %rcx, %r12
addq %rax, %r12
movq %r12, %rsi
xorl %edx, %edx
callq 0x135ef
movq (%r13), %rdi
leaq 0x7(%rsp), %rsi
movl $0x1, %edx
callq 0x131e9
cmpl $0x1, %eax
jne 0x152ef
testl %r14d, %r14d
js 0x152ef
xorl %ebp, %ebp
callq 0x35f0
movq %rax, %rbx
leaq 0x7(%rsp), %r12
movq (%rbx), %rcx
movsbq 0x7(%rsp), %rax
cmpw $0x0, (%rcx,%rax,2)
jns 0x152d4
movslq %ebp, %rcx
incl %ebp
movb %al, (%r15,%rcx)
movq (%r13), %rdi
movq %r12, %rsi
movl $0x1, %edx
callq 0x131e9
cmpl $0x1, %eax
jne 0x152ef
cmpl %r14d, %ebp
jle 0x152bb
movslq %ebp, %rax
movb $0x0, (%r15,%rax)
movq 0x28(%rsp), %rcx
movl %eax, (%rcx)
movq %r15, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq