// File: /workspace/github_c_makefile_star2_O3/xiongxu[P]bam2bedGraph2/samtools-0.1.19/faidx.c
// Function: fai_fetch
char *fai_fetch(const faidx_t *fai, const char *str, int *len)
{
	char *s, c;
	int i, l, k, name_end;
	khiter_t iter;
	faidx1_t val;
	khash_t(s) *h;
	int beg, end;

	beg = end = -1;
	h = fai->hash;
	name_end = l = strlen(str);
	s = (char*)malloc(l+1);
	// remove space
	for (i = k = 0; i < l; ++i)
		if (!isspace(str[i])) s[k++] = str[i];
	s[k] = 0; l = k;
	// determine the sequence name
	for (i = l - 1; i >= 0; --i) if (s[i] == ':') break; // look for colon from the end
	if (i >= 0) name_end = i;
	if (name_end < l) { // check if this is really the end
		int n_hyphen = 0;
		for (i = name_end + 1; i < l; ++i) {
			if (s[i] == '-') ++n_hyphen;
			else if (!isdigit(s[i]) && s[i] != ',') break;
		}
		if (i < l || n_hyphen > 1) name_end = l; // malformated region string; then take str as the name
		s[name_end] = 0;
		iter = kh_get(s, h, s);
		if (iter == kh_end(h)) { // cannot find the sequence name
			iter = kh_get(s, h, str); // try str as the name
			if (iter == kh_end(h)) {
				*len = 0;
			free(s); return 0;
			} else s[name_end] = ':', name_end = l;
		}
	} else iter = kh_get(s, h, str);
	if(iter == kh_end(h)) {
		fprintf(stderr, "[fai_fetch] Warning - Reference %s not found in FASTA file, returning empty sequence\n", str);
		free(s);
		return 0;
	};
	val = kh_value(h, iter);
	// parse the interval
	if (name_end < l) {
		for (i = k = name_end + 1; i < l; ++i)
			if (s[i] != ',') s[k++] = s[i];
		s[k] = 0;
		beg = atoi(s + name_end + 1);
		for (i = name_end + 1; i != k; ++i) if (s[i] == '-') break;
		end = i < k? atoi(s + i + 1) : val.len;
		if (beg > 0) --beg;
	} else beg = 0, end = val.len;
	if (beg >= val.len) beg = val.len;
	if (end >= val.len) end = val.len;
	if (beg > end) beg = end;
	free(s);

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdx, 0x40(%rsp)
movq %rsi, %r12
movq %rdi, %rbx
movq 0x18(%rdi), %rax
movq %rax, 0x10(%rsp)
movq %rsi, %rdi
callq 0x31e0
movq %rax, %r13
incl %eax
movslq %eax, %rdi
callq 0x33e0
movq %rax, %r15
testl %r13d, %r13d
jle 0x149ec
callq 0x35f0
movq (%rax), %rax
movl %r13d, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
xorl %edx, %edx
xorl %r8d, %r8d
movsbq (%r12,%rdx), %rsi
testb $0x20, 0x1(%rax,%rsi,2)
jne 0x149e2
movslq %r8d, %rdi
incl %r8d
movb %sil, (%r15,%rdi)
incq %rdx
cmpq %rdx, %rcx
jne 0x149cc
jmp 0x149ef
xorl %r8d, %r8d
movslq %r8d, %rbp
movb $0x0, (%r15,%rbp)
movl %ebp, %eax
decq %rax
movl %r8d, %esi
testl %esi, %esi
jle 0x14a13
movl %eax, %ecx
decl %esi
decq %rax
cmpb $0x3a, (%r15,%rcx)
jne 0x149ff
jmp 0x14a16
movl %r13d, %esi
movq %r12, 0x30(%rsp)
cmpl %r8d, %esi
movq %r15, 0x20(%rsp)
movq %rsi, 0x28(%rsp)
movq %rbx, 0x50(%rsp)
movq %r8, 0x38(%rsp)
jge 0x14b2a
leal 0x1(%rsi), %eax
cmpl %r8d, %eax
jge 0x14ac4
movslq %esi, %r15
addq $0x2, %r15
leal -0x1(%r8), %r12d
xorl %ecx, %ecx
movb $0x1, %r13b
movq 0x20(%rsp), %rax
movsbq -0x1(%rax,%r15), %rbx
cmpq $0x2d, %rbx
jne 0x14a69
incl %ecx
jmp 0x14a99
movl %ecx, 0x18(%rsp)
callq 0x35f0
cmpb $0x2c, %bl
je 0x14a90
movq (%rax), %rax
movzwl (%rax,%rbx,2), %eax
andl $0x800, %eax            # imm = 0x800
movl 0x18(%rsp), %ecx
movq 0x28(%rsp), %rsi
jne 0x14a99
jmp 0x14aab
movl 0x18(%rsp), %ecx
movq 0x28(%rsp), %rsi
cmpq %rbp, %r15
setl %r13b
incq %r15
decl %r12d
cmpl %r12d, %esi
jne 0x14a54
cmpl $0x2, %ecx
movq 0x38(%rsp), %r8
cmovgel %r8d, %esi
testb $0x1, %r13b
cmovnel %r8d, %esi
movq 0x20(%rsp), %r15
movq %rsi, 0x28(%rsp)
movslq %esi, %rax
movq %rax, 0x48(%rsp)
movb $0x0, (%r15,%rax)
movq 0x10(%rsp), %rax
movl (%rax), %edi
testl %edi, %edi
movq %rdi, 0x18(%rsp)
je 0x14b71
movsbl (%r15), %esi
xorl %r8d, %r8d
movl $0x0, %ecx
testl %esi, %esi
je 0x14c15
movb 0x1(%r15), %al
testb %al, %al
je 0x14c13
leaq 0x2(%r15), %rdx
movl %esi, %ecx
movl %ecx, %esi
shll $0x5, %esi
subl %ecx, %esi
movsbl %al, %ecx
addl %esi, %ecx
movb (%rdx), %al
incq %rdx
testb %al, %al
jne 0x14b10
jmp 0x14c15
movq 0x10(%rsp), %rax
movl (%rax), %edi
testl %edi, %edi
je 0x14f90
movq 0x30(%rsp), %rdx
movsbl (%rdx), %esi
xorl %r8d, %r8d
movl $0x0, %ecx
testl %esi, %esi
je 0x14b7b
movb 0x1(%rdx), %al
testb %al, %al
je 0x14b79
addq $0x2, %rdx
movl %esi, %ecx
movl %ecx, %esi
shll $0x5, %esi
subl %ecx, %esi
movsbl %al, %ecx
addl %esi, %ecx
movb (%rdx), %al
incq %rdx
testb %al, %al
jne 0x14b5a
jmp 0x14b7b
xorl %r15d, %r15d
jmp 0x14ca1
movl %esi, %ecx
movl %ecx, %eax
xorl %edx, %edx
divl %edi
movl %edx, %r13d
leal -0x1(%rdi), %esi
movl %ecx, %eax
xorl %edx, %edx
divl %esi
movl %edx, %r14d
incl %r14d
movq 0x10(%rsp), %rax
movq 0x10(%rax), %r12
movl %r13d, %r15d
movq %rdi, 0x18(%rsp)
movl %r15d, %eax
shrl $0x4, %eax
movl (%r12,%rax,4), %ebp
leal (%r15,%r15), %ebx
andl $0x1e, %ebx
movl %ebp, %eax
movl %ebx, %ecx
shrl %cl, %eax
testb $0x2, %al
jne 0x14c02
testb $0x1, %al
jne 0x14be9
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rax
movl %r15d, %ecx
movq (%rax,%rcx,8), %rdi
movq 0x30(%rsp), %rsi
callq 0x3300
xorl %r8d, %r8d
movq 0x18(%rsp), %rdi
testl %eax, %eax
je 0x14c02
addl %r14d, %r15d
cmpl %edi, %r15d
movl %edi, %eax
cmovbl %r8d, %eax
subl %eax, %r15d
cmpl %r13d, %r15d
jne 0x14ba4
jmp 0x14f90
movl %ebx, %ecx
shrl %cl, %ebp
testb $0x3, %bpl
cmovnel %edi, %r15d
jmp 0x14db6
movl %esi, %ecx
movl %ecx, %eax
xorl %edx, %edx
divl %edi
movl %edx, %ebp
leal -0x1(%rdi), %esi
movl %ecx, %eax
xorl %edx, %edx
divl %esi
movl %edx, %r13d
incl %r13d
movq 0x10(%rsp), %rax
movq 0x10(%rax), %r14
movl %ebp, %r15d
movl %r15d, %eax
shrl $0x4, %eax
movl (%r14,%rax,4), %r12d
leal (%r15,%r15), %ebx
andl $0x1e, %ebx
movl %r12d, %eax
movl %ebx, %ecx
shrl %cl, %eax
testb $0x2, %al
jne 0x14c94
testb $0x1, %al
jne 0x14c7e
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rax
movl %r15d, %ecx
movq (%rax,%rcx,8), %rdi
movq 0x20(%rsp), %rsi
callq 0x3300
xorl %r8d, %r8d
movq 0x18(%rsp), %rdi
testl %eax, %eax
je 0x14c94
addl %r13d, %r15d
cmpl %edi, %r15d
movl %edi, %eax
cmovbl %r8d, %eax
subl %eax, %r15d
cmpl %ebp, %r15d
jne 0x14c38
jmp 0x14caa
movl %ebx, %ecx
shrl %cl, %r12d
testb $0x3, %r12b
cmovnel %edi, %r15d
cmpl %edi, %r15d
jne 0x14db6
testl %edi, %edi
je 0x14ce6
movq 0x30(%rsp), %rdx
movsbl (%rdx), %esi
xorl %r8d, %r8d
movl $0x0, %ecx
testl %esi, %esi
je 0x14cf0
movb 0x1(%rdx), %al
testb %al, %al
je 0x14cee
addq $0x2, %rdx
movl %esi, %ecx
movl %ecx, %esi
shll $0x5, %esi
subl %ecx, %esi
movsbl %al, %ecx
addl %esi, %ecx
movb (%rdx), %al
incq %rdx
testb %al, %al
jne 0x14ccf
jmp 0x14cf0
xorl %r15d, %r15d
jmp 0x14d7c
movl %esi, %ecx
movl %ecx, %eax
xorl %edx, %edx
divl %edi
movl %edx, %ebp
leal -0x1(%rdi), %esi
movl %ecx, %eax
xorl %edx, %edx
divl %esi
movl %edx, %r13d
incl %r13d
movq 0x10(%rsp), %rax
movq 0x10(%rax), %r14
movl %ebp, %r15d
movl %r15d, %eax
shrl $0x4, %eax
movl (%r14,%rax,4), %r12d
leal (%r15,%r15), %ebx
andl $0x1e, %ebx
movl %r12d, %eax
movl %ebx, %ecx
shrl %cl, %eax
testb $0x2, %al
jne 0x14d6f
testb $0x1, %al
jne 0x14d59
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rax
movl %r15d, %ecx
movq (%rax,%rcx,8), %rdi
movq 0x30(%rsp), %rsi
callq 0x3300
xorl %r8d, %r8d
movq 0x18(%rsp), %rdi
testl %eax, %eax
je 0x14d6f
addl %r13d, %r15d
cmpl %edi, %r15d
movl %edi, %eax
cmovbl %r8d, %eax
subl %eax, %r15d
cmpl %ebp, %r15d
jne 0x14d13
jmp 0x14d81
movl %ebx, %ecx
shrl %cl, %r12d
testb $0x3, %r12b
cmovnel %edi, %r15d
cmpl %edi, %r15d
jne 0x14d9e
movq 0x40(%rsp), %rax
movl $0x0, (%rax)
movq 0x20(%rsp), %rdi
callq 0x3060
xorl %r13d, %r13d
jmp 0x14f7e
movq 0x20(%rsp), %rax
movq 0x48(%rsp), %rcx
movb $0x3a, (%rax,%rcx)
movq 0x38(%rsp), %r14
movq %r14, 0x28(%rsp)
cmpl %edi, %r15d
je 0x14f90
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rax
movl %r15d, %ecx
leaq (%rcx,%rcx,2), %rcx
movslq (%rax,%rcx,8), %r15
movl 0x4(%rax,%rcx,8), %esi
movq 0x8(%rax,%rcx,8), %r13
movq 0x10(%rax,%rcx,8), %r12
movq 0x38(%rsp), %rdx
movl %edx, %eax
movq 0x28(%rsp), %rcx
subl %ecx, %eax
movl %esi, 0x10(%rsp)
jle 0x14e73
leal 0x1(%rcx), %ebx
movslq %ecx, %rcx
movl %ebx, %r14d
cmpl %edx, %ebx
movq 0x20(%rsp), %rbp
jge 0x14e2f
leaq (%rcx,%rbp), %rdx
movl $0x1, %esi
movl %ebx, %r14d
movb (%rdx,%rsi), %dil
cmpb $0x2c, %dil
je 0x14e28
movslq %r14d, %r8
incl %r14d
movb %dil, (%rbp,%r8)
incq %rsi
cmpl %esi, %eax
jne 0x14e13
movq %r15, 0x18(%rsp)
movslq %r14d, %r15
movb $0x0, (%rbp,%r15)
leaq (%rcx,%rbp), %rdi
incq %rdi
callq 0x34f0
movq %rbp, %rcx
movl %eax, %ebp
cmpl %r15d, %ebx
jne 0x14e7a
movq 0x18(%rsp), %r15
cmpl %r14d, %ebx
jge 0x14e94
movslq %ebx, %rax
movq 0x20(%rsp), %rcx
leaq (%rcx,%rax), %rdi
incq %rdi
callq 0x34f0
jmp 0x14e97
xorl %ebp, %ebp
movl %r13d, %eax
jmp 0x14ea0
movslq %ebx, %rax
addq %rcx, %rax
movq 0x18(%rsp), %r15
cmpb $0x2d, (%rax)
je 0x14e58
incl %ebx
incq %rax
cmpl %ebx, %r14d
jne 0x14e85
movl %r13d, %eax
xorl %ecx, %ecx
testl %ebp, %ebp
setg %cl
subl %ecx, %ebp
movslq %ebp, %rbx
cmpq %rbx, %r13
cmovlel %r13d, %ebx
movslq %eax, %r14
cmpq %r14, %r13
cmovlel %r13d, %r14d
cmpl %r14d, %ebx
cmovgel %r14d, %ebx
movq 0x20(%rsp), %rdi
callq 0x3060
subl %ebx, %r14d
leal 0x2(%r14), %eax
movslq %eax, %rdi
callq 0x33e0
movq %rax, %r13
movq 0x50(%rsp), %r8
movq (%r8), %rdi
movl %ebx, %eax
cltd
idivl 0x10(%rsp)
cltq
imulq %r15, %rax
movslq %edx, %rcx
addq %rcx, %r12
addq %rax, %r12
xorl %ebp, %ebp
movq %r12, %rsi
movq %r8, %r12
xorl %edx, %edx
callq 0x135ef
movq (%r12), %rdi
leaq 0xf(%rsp), %rsi
movl $0x1, %edx
callq 0x131e9
cmpl $0x1, %eax
jne 0x14f6e
testl %r14d, %r14d
jle 0x14f6e
xorl %ebp, %ebp
leaq 0xf(%rsp), %r15
movq (%r12), %rbx
cmpl $0x0, 0x88(%rbx)
jne 0x14f6e
callq 0x35f0
movq (%rax), %rcx
movsbq 0xf(%rsp), %rax
cmpw $0x0, (%rcx,%rax,2)
jns 0x14f54
movslq %ebp, %rcx
incl %ebp
movb %al, (%r13,%rcx)
movq %rbx, %rdi
movq %r15, %rsi
movl $0x1, %edx
callq 0x131e9
cmpl $0x1, %eax
jne 0x14f6e
cmpl %r14d, %ebp
jl 0x14f28
movslq %ebp, %rax
movb $0x0, (%r13,%rax)
movq 0x40(%rsp), %rcx
movl %eax, (%rcx)
movq %r13, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xe061(%rip), %rax      # 0x22ff8
movq (%rax), %rdi
leaq 0x7bfe(%rip), %rsi      # 0x1cb9f
xorl %r13d, %r13d
movq 0x30(%rsp), %rdx
xorl %eax, %eax
callq 0x3320
movq 0x20(%rsp), %rdi
callq 0x3060
jmp 0x14f7e