// File: /workspace/github_c_makefile_star2_O2/genometools[P]genometools/src/external/samtools-0.1.18/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);
	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 $0x38, %rsp
movq %rdx, 0x20(%rsp)
movq %rdi, 0x28(%rsp)
movq 0x18(%rdi), %r13
movq %rsi, 0x8(%rsp)
movq %rsi, %rdi
callq 0x172d0
movq %rax, %rbx
incl %eax
movslq %eax, %rdi
callq 0x17f50
movq %rax, %r15
xorl %ebp, %ebp
testl %ebx, %ebx
movl $0x0, %r12d
movq %rbx, 0x10(%rsp)
cmovgl %ebx, %r12d
xorl %r14d, %r14d
movslq %r14d, %rbx
cmpq %rbp, %r12
je 0x1f9d07
callq 0x17820
movq (%rax), %rcx
movq 0x8(%rsp), %rax
movsbq (%rax,%rbp), %rax
testb $0x20, 0x1(%rcx,%rax,2)
jne 0x1f9d02
leal 0x1(%rbx), %r14d
movb %al, (%r15,%rbx)
incq %rbp
jmp 0x1f9cd9
movb $0x0, (%r15,%rbx)
movl %ebx, %eax
decq %rax
movl %r14d, %ebp
testl %ebp, %ebp
jle 0x1f9d28
movl %eax, %ecx
decl %ebp
decq %rax
cmpb $0x3a, (%r15,%rcx)
jne 0x1f9d14
jmp 0x1f9d2d
movq 0x10(%rsp), %rbp
cmpl %r14d, %ebp
jge 0x1f9d82
movq %r13, 0x18(%rsp)
movslq %ebp, %r12
movl $0x0, 0x10(%rsp)
incq %r12
movq %r12, %rcx
movq %rcx, %r12
cmpq %rbx, %rcx
jge 0x1f9d94
movsbq (%r15,%r12), %r13
cmpq $0x2d, %r13
je 0x1f9d7c
callq 0x17820
leaq 0x1(%r12), %rcx
cmpb $0x2c, %r13b
je 0x1f9d48
movq (%rax), %rax
movzwl (%rax,%r13,2), %eax
andl $0x800, %eax            # imm = 0x800
jne 0x1f9d48
jmp 0x1f9d94
incl 0x10(%rsp)
jmp 0x1f9d42
movq %r13, %rdi
movq 0x8(%rsp), %rsi
callq 0x1f98dc
movl %eax, %r12d
jmp 0x1f9df7
cmpl $0x2, 0x10(%rsp)
cmovael %r14d, %ebp
cmpq %rbx, %r12
cmovll %r14d, %ebp
movslq %ebp, %rax
movq %rax, 0x10(%rsp)
movb $0x0, (%r15,%rax)
movq 0x18(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
callq 0x1f98dc
movl %eax, %r12d
cmpl (%r13), %eax
jne 0x1f9df7
movq %r13, %rdi
movq 0x8(%rsp), %rsi
callq 0x1f98dc
cmpl %r12d, %eax
jne 0x1f9fb3
movq 0x20(%rsp), %rax
andl $0x0, (%rax)
movq %r15, %rdi
callq 0x17f48
xorl %ecx, %ecx
jmp 0x1f9fa1
movq 0x20(%r13), %rax
movl %r12d, %ecx
imulq $0x18, %rcx, %rcx
movslq (%rax,%rcx), %rdx
movq %rdx, 0x10(%rsp)
movl 0x4(%rax,%rcx), %edx
movl %edx, 0x18(%rsp)
movq 0x8(%rax,%rcx), %rdx
movq 0x10(%rax,%rcx), %r12
cmpl %r14d, %ebp
jge 0x1f9e4f
movq %r12, 0x30(%rsp)
leal 0x1(%rbp), %r13d
movslq %ebp, %r14
leaq 0x1(%r14), %rax
movslq %r13d, %r12
cmpq %rbx, %rax
jge 0x1f9e55
movb (%r15,%rax), %cl
cmpb $0x2c, %cl
je 0x1f9e4a
incl %r13d
movb %cl, (%r15,%r12)
incq %rax
jmp 0x1f9e32
xorl %esi, %esi
movl %edx, %eax
jmp 0x1f9ed2
movb $0x0, (%r15,%r12)
leaq (%r15,%r14), %rbx
leaq (%r15,%r14), %rdi
incq %rdi
movq %rdx, 0x8(%rsp)
callq 0x17b90
movq 0x8(%rsp), %rdx
movl %eax, %esi
subq %r14, %r12
shlq $0x20, %r14
movabsq $0x100000000, %rcx      # imm = 0x100000000
addq %rcx, %r14
pushq $0x1
popq %rax
cmpq %rax, %r12
je 0x1f9ec2
cmpb $0x2d, (%rbx,%rax)
je 0x1f9ea0
addq %rcx, %r14
incq %rax
jmp 0x1f9e8d
addl %eax, %ebp
cmpl %ebp, %r13d
jle 0x1f9ec2
sarq $0x20, %r14
leaq (%r15,%r14), %rdi
incq %rdi
movl %esi, %ebx
callq 0x17b90
movl %ebx, %esi
movq 0x8(%rsp), %rdx
jmp 0x1f9ec4
movl %edx, %eax
xorl %ecx, %ecx
testl %esi, %esi
setg %cl
subl %ecx, %esi
movq 0x30(%rsp), %r12
movslq %esi, %r13
cmpq %r13, %rdx
cmovlel %edx, %r13d
movslq %eax, %rbx
cmpq %rbx, %rdx
cmovlel %edx, %ebx
cmpl %ebx, %r13d
cmovgel %ebx, %r13d
movq %r15, %rdi
callq 0x17f48
subl %r13d, %ebx
leal 0x2(%rbx), %eax
movslq %eax, %rdi
callq 0x17f50
movq %rax, 0x8(%rsp)
movq 0x28(%rsp), %r15
movq (%r15), %rdi
movl %r13d, %eax
cltd
idivl 0x18(%rsp)
cltq
imulq 0x10(%rsp), %rax
movslq %edx, %rcx
addq %rcx, %r12
addq %rax, %r12
xorl %r14d, %r14d
movq %r12, %rsi
xorl %edx, %edx
callq 0x2d8e09
movq (%r15), %r13
leaq 0x7(%rsp), %r12
pushq $0x1
popq %rbp
movq %r13, %rdi
movq %r12, %rsi
movl %ebp, %edx
callq 0x2d8a3a
movl %r14d, %r15d
cmpl $0x1, %eax
jne 0x1f9f8f
cmpl %ebx, %r14d
jge 0x1f9f8f
movq 0x28(%rsp), %rax
movq (%rax), %r13
cmpl $0x0, 0x88(%r13)
jne 0x1f9f8f
callq 0x17820
movq (%rax), %rcx
movsbq 0x7(%rsp), %rax
cmpw $0x0, (%rcx,%rax,2)
jns 0x1f9f40
incl %r14d
movq 0x8(%rsp), %rcx
movb %al, (%rcx,%r15)
jmp 0x1f9f40
movq 0x8(%rsp), %rcx
movb $0x0, (%rcx,%r15)
movq 0x20(%rsp), %rax
movl %r14d, (%rax)
movq %rcx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%rsp), %rcx
movb $0x3a, (%r15,%rcx)
movl %r14d, %ebp
jmp 0x1f9d8f