// File: /workspace/github_c_makefile_star2_O1/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, %r15
movq %rdi, %r14
movq 0x18(%rdi), %r12
movq %rsi, %rdi
callq 0x31e0
movq %rax, %rbx
incl %eax
movslq %eax, %rdi
callq 0x33e0
testl %ebx, %ebx
movq %rax, 0x28(%rsp)
jle 0x140bd
callq 0x35f0
movq 0x28(%rsp), %r9
movq (%rax), %rax
movl %ebx, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
xorl %edx, %edx
xorl %r8d, %r8d
movsbq (%r15,%rdx), %rsi
testb $0x20, 0x1(%rax,%rsi,2)
jne 0x140b3
movslq %r8d, %rdi
incl %r8d
movb %sil, (%r9,%rdi)
incq %rdx
cmpq %rdx, %rcx
jne 0x1409d
jmp 0x140c3
movq %rax, %r9
xorl %r8d, %r8d
movslq %r8d, %rdx
movb $0x0, (%r9,%rdx)
movl %edx, %eax
decq %rax
movl %r8d, %r13d
testl %r13d, %r13d
jle 0x140e9
movl %eax, %ecx
decl %r13d
decq %rax
cmpb $0x3a, (%r9,%rcx)
jne 0x140d3
jmp 0x140ec
movl %ebx, %r13d
cmpl %r8d, %r13d
movq %r14, 0x50(%rsp)
movl %r8d, 0x34(%rsp)
jge 0x141f7
leal 0x1(%r13), %eax
cmpl %r8d, %eax
setl %bl
movq %r15, 0x18(%rsp)
movq %r12, 0x10(%rsp)
jge 0x14199
movslq %r13d, %r12
addq $0x2, %r12
movl %r13d, %ebp
notl %ebp
addl %r8d, %ebp
xorl %eax, %eax
movsbq -0x1(%r9,%r12), %r15
cmpq $0x2d, %r15
jne 0x1413a
incl %eax
jmp 0x14175
movl %eax, 0x20(%rsp)
movq %rdx, %r14
callq 0x35f0
cmpb $0x2c, %r15b
je 0x14169
movq (%rax), %rax
movzwl (%rax,%r15,2), %eax
andl $0x800, %eax            # imm = 0x800
movq 0x28(%rsp), %r9
movq %r14, %rdx
movl 0x20(%rsp), %eax
jne 0x14175
jmp 0x14182
movq 0x28(%rsp), %r9
movq %r14, %rdx
movl 0x20(%rsp), %eax
cmpq %rdx, %r12
setl %bl
incq %r12
decl %ebp
jne 0x1412a
cmpl $0x2, %eax
setge %al
orb %al, %bl
movq 0x18(%rsp), %r15
movq 0x10(%rsp), %r12
movl 0x34(%rsp), %r8d
testb $0x1, %bl
cmovnel %r8d, %r13d
movslq %r13d, %rax
movq %rax, 0x48(%rsp)
movb $0x0, (%r9,%rax)
movl (%r12), %edi
testl %edi, %edi
movq %rdi, 0x20(%rsp)
je 0x14243
movsbl (%r9), %esi
xorl %r10d, %r10d
movl $0x0, %ecx
testl %esi, %esi
je 0x14253
movb 0x1(%r9), %al
testb %al, %al
je 0x14251
leaq 0x2(%r9), %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 0x141e0
jmp 0x14253
movl (%r12), %ebp
testl %ebp, %ebp
je 0x1424a
movq %r12, 0x10(%rsp)
movsbl (%r15), %esi
xorl %edi, %edi
movl $0x0, %ecx
testl %esi, %esi
je 0x14348
movb 0x1(%r15), %al
testb %al, %al
je 0x14346
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 0x14229
jmp 0x14348
xorl %ebp, %ebp
jmp 0x142eb
xorl %ebp, %ebp
jmp 0x144d1
movl %esi, %ecx
movl %ecx, %eax
xorl %edx, %edx
divl %edi
movl %edx, %r14d
leal -0x1(%rdi), %esi
movl %ecx, %eax
xorl %edx, %edx
divl %esi
incl %edx
movq 0x10(%rsp), %rax
movq 0x10(%rax), %r12
movl %r14d, %ebp
movl %edx, 0x38(%rsp)
movl %ebp, %eax
shrl $0x4, %eax
movl (%r12,%rax,4), %r15d
leal (,%rbp,2), %ebx
andl $0x1e, %ebx
movl %r15d, %eax
movl %ebx, %ecx
shrl %cl, %eax
testb $0x2, %al
jne 0x142da
testb $0x1, %al
jne 0x142c5
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rax
movl %ebp, %ecx
movq (%rax,%rcx,8), %rdi
movq %r9, %rsi
callq 0x3300
movl 0x38(%rsp), %edx
xorl %r10d, %r10d
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %r9
testl %eax, %eax
je 0x142da
addl %edx, %ebp
cmpl %edi, %ebp
movl %edi, %eax
cmovbl %r10d, %eax
subl %eax, %ebp
cmpl %r14d, %ebp
jne 0x14277
movl %edi, %ebp
jmp 0x142e6
movl %ebx, %ecx
shrl %cl, %r15d
testb $0x3, %r15b
cmovnel %edi, %ebp
movq 0x18(%rsp), %r15
movb $0x1, %cl
cmpl %edi, %ebp
jne 0x14335
testl %edi, %edi
je 0x1433f
movsbl (%r15), %esi
xorl %r8d, %r8d
movl $0x0, %ecx
testl %esi, %esi
je 0x143f7
movb 0x1(%r15), %al
testb %al, %al
je 0x143f5
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 0x1431b
jmp 0x143f7
movq 0x10(%rsp), %r12
jmp 0x144c9
xorl %ebp, %ebp
jmp 0x14493
movl %esi, %ecx
movq %r15, 0x18(%rsp)
movl %ecx, %eax
xorl %edx, %edx
divl %ebp
movl %edx, %r12d
leal -0x1(%rbp), %esi
movl %ecx, %eax
xorl %edx, %edx
divl %esi
incl %edx
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rsi
movl %r12d, %r14d
movl %edx, 0x20(%rsp)
movq %rsi, 0x38(%rsp)
movl %r14d, %eax
shrl $0x4, %eax
movl (%rsi,%rax,4), %r15d
leal (%r14,%r14), %ebx
andl $0x1e, %ebx
movl %r15d, %eax
movl %ebx, %ecx
shrl %cl, %eax
testb $0x2, %al
jne 0x143d9
testb $0x1, %al
jne 0x143c4
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rax
movl %r14d, %ecx
movq (%rax,%rcx,8), %rdi
movq 0x18(%rsp), %rsi
callq 0x3300
movq 0x38(%rsp), %rsi
movl 0x20(%rsp), %edx
xorl %edi, %edi
movq 0x28(%rsp), %r9
testl %eax, %eax
je 0x143d9
addl %edx, %r14d
cmpl %ebp, %r14d
movl %ebp, %eax
cmovbl %edi, %eax
subl %eax, %r14d
cmpl %r12d, %r14d
jne 0x14376
jmp 0x143e6
movl %ebx, %ecx
shrl %cl, %r15d
testb $0x3, %r15b
cmovel %r14d, %ebp
movq 0x18(%rsp), %r15
movq 0x10(%rsp), %r12
jmp 0x144d1
movl %esi, %ecx
movl %ecx, %eax
xorl %edx, %edx
divl %edi
movl %edx, %r14d
leal -0x1(%rdi), %esi
movl %ecx, %eax
xorl %edx, %edx
divl %esi
incl %edx
movq 0x10(%rsp), %rax
movq 0x10(%rax), %r12
movl %r14d, %ebp
movl %edx, 0x38(%rsp)
movl %ebp, %eax
shrl $0x4, %eax
movl (%r12,%rax,4), %r15d
leal (,%rbp,2), %ebx
andl $0x1e, %ebx
movl %r15d, %eax
movl %ebx, %ecx
shrl %cl, %eax
testb $0x2, %al
jne 0x14480
testb $0x1, %al
jne 0x1446b
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rax
movl %ebp, %ecx
movq (%rax,%rcx,8), %rdi
movq 0x18(%rsp), %rsi
callq 0x3300
movl 0x38(%rsp), %edx
xorl %r8d, %r8d
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %r9
testl %eax, %eax
je 0x14480
addl %edx, %ebp
cmpl %edi, %ebp
movl %edi, %eax
cmovbl %r8d, %eax
subl %eax, %ebp
cmpl %r14d, %ebp
jne 0x1441b
movl %edi, %ebp
jmp 0x1448c
movl %ebx, %ecx
shrl %cl, %r15d
testb $0x3, %r15b
cmovnel %edi, %ebp
movq 0x18(%rsp), %r15
movb $0x1, %cl
cmpl %edi, %ebp
movq 0x10(%rsp), %r12
jne 0x144b8
movq 0x40(%rsp), %rax
movl $0x0, (%rax)
movq %r9, %rdi
callq 0x3060
movq 0x28(%rsp), %r9
xorl %ecx, %ecx
jmp 0x144c9
movq 0x48(%rsp), %rax
movb $0x3a, (%r9,%rax)
movl 0x34(%rsp), %eax
movl %eax, %r13d
testb %cl, %cl
je 0x14575
cmpl (%r12), %ebp
je 0x146b3
movq 0x20(%r12), %rax
movl %ebp, %ecx
leaq (%rcx,%rcx,2), %rcx
movslq (%rax,%rcx,8), %rdx
movq %rdx, 0x10(%rsp)
movl 0x4(%rax,%rcx,8), %edx
movl %edx, 0x20(%rsp)
movq 0x8(%rax,%rcx,8), %r15
movq 0x10(%rax,%rcx,8), %r12
movl 0x34(%rsp), %edx
movl %edx, %eax
subl %r13d, %eax
jle 0x1456e
leal 0x1(%r13), %ebx
movslq %r13d, %rcx
movl %ebx, %r13d
cmpl %edx, %ebx
jge 0x14541
leaq (%r9,%rcx), %rdx
movl $0x1, %esi
movl %ebx, %r13d
movb (%rdx,%rsi), %dil
cmpb $0x2c, %dil
je 0x1453a
movslq %r13d, %r8
incl %r13d
movb %dil, (%r9,%r8)
incq %rsi
cmpl %esi, %eax
jne 0x14526
movq %r12, 0x18(%rsp)
movslq %r13d, %r14
movb $0x0, (%r9,%r14)
leaq (%r9,%rcx), %rdi
incq %rdi
movq %r9, %r12
callq 0x34f0
movl %eax, %ebp
cmpl %r14d, %ebx
jne 0x1457d
movq %r12, %r9
movq 0x18(%rsp), %r12
jmp 0x1459d
xorl %ebp, %ebp
movl %r15d, %eax
jmp 0x145c4
xorl %r13d, %r13d
jmp 0x146a1
movslq %ebx, %rax
addq %r12, %rax
movq %r12, %r9
movq 0x18(%rsp), %r12
cmpb $0x2d, (%rax)
je 0x1459d
incl %ebx
incq %rax
cmpl %ebx, %r13d
jne 0x1458b
movl %r13d, %ebx
cmpl %r13d, %ebx
jge 0x145b8
movslq %ebx, %rax
leaq (%r9,%rax), %rdi
incq %rdi
callq 0x34f0
movq 0x28(%rsp), %r9
jmp 0x145bb
movl %r15d, %eax
xorl %ecx, %ecx
testl %ebp, %ebp
setg %cl
subl %ecx, %ebp
movslq %ebp, %rbx
cmpq %rbx, %r15
cmovlel %r15d, %ebx
movslq %eax, %r14
cmpq %r14, %r15
cmovlel %r15d, %r14d
cmpl %r14d, %ebx
cmovgel %r14d, %ebx
movq %r9, %rdi
callq 0x3060
subl %ebx, %r14d
leal 0x2(%r14), %eax
movslq %eax, %rdi
callq 0x33e0
movq %rax, %r13
movq 0x50(%rsp), %rbp
movq (%rbp), %rdi
movl %ebx, %eax
cltd
idivl 0x20(%rsp)
cltq
imulq 0x10(%rsp), %rax
movslq %edx, %rcx
addq %rcx, %r12
addq %rax, %r12
xorl %ebx, %ebx
movq %r12, %rsi
xorl %edx, %edx
callq 0x12f74
movq (%rbp), %rdi
leaq 0xf(%rsp), %rsi
movl $0x1, %edx
callq 0x12b53
cmpl $0x1, %eax
jne 0x14691
testl %r14d, %r14d
jle 0x14691
xorl %ebx, %ebx
leaq 0xf(%rsp), %r15
movq (%rbp), %rax
cmpl $0x0, 0x88(%rax)
jne 0x14691
callq 0x35f0
movq (%rax), %rcx
movsbq 0xf(%rsp), %rax
cmpw $0x0, (%rcx,%rax,2)
jns 0x14676
movslq %ebx, %rcx
incl %ebx
movb %al, (%r13,%rcx)
movq (%rbp), %rdi
movq %r15, %rsi
movl $0x1, %edx
callq 0x12b53
cmpl $0x1, %eax
jne 0x14691
cmpl %r14d, %ebx
jl 0x1464a
movslq %ebx, %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 0xd93e(%rip), %rax      # 0x21ff8
movq (%rax), %rdi
leaq 0x73bb(%rip), %rsi      # 0x1ba7f
xorl %r13d, %r13d
movq %r15, %rdx
xorl %eax, %eax
movq %r9, %rbx
callq 0x3320
movq %rbx, %rdi
callq 0x3060
jmp 0x146a1