// File: /workspace/github_cpp_makefile_star2_O3/statgen[P]statgen/lib/samtools-0.1.7a-hybrid/faidx.c
// Function: fai_fetch
char *fai_fetch(const faidx_t *fai, const char *str, int *len)
{
	char *s, *p, c;
	int i, l, k;
	khiter_t iter;
	faidx1_t val;
	khash_t(s) *h;
	int beg, end;

	beg = end = -1;
	h = fai->hash;
	l = strlen(str);
	p = s = (char*)malloc(l+1);
	/* squeeze out "," */
	for (i = k = 0; i != l; ++i)
		if (str[i] != ',' && !isspace(str[i])) s[k++] = str[i];
	s[k] = 0;
	for (i = 0; i != k; ++i) if (s[i] == ':') break;
	s[i] = 0;
	iter = kh_get(s, h, s); /* get the ref_id */
	if (iter == kh_end(h)) {
		*len = 0;
		free(s); return 0;
	}
	val = kh_value(h, iter);
	if (i == k) { /* dump the whole sequence */
		beg = 0; end = val.len;
	} else {
		for (p = s + i + 1; i != k; ++i) if (s[i] == '-') break;
		beg = atoi(p);
		if (i < k) {
			p = s + i + 1;
			end = atoi(p);
		} else end = val.len;
	}
	if (beg > 0) --beg;
	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, 0x28(%rsp)
movq %rsi, %r12
movq %rdi, 0x20(%rsp)
movq 0x18(%rdi), %rax
movq %rax, 0x10(%rsp)
movq %rsi, %rdi
callq 0x32d0
movq %rax, %r13
incl %eax
movslq %eax, %rdi
callq 0x31d0
movq %rax, %rbp
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
andq %r13, %rbx
je 0x1b9b3
xorl %r14d, %r14d
xorl %r13d, %r13d
movsbq (%r12,%r14), %r15
cmpq $0x2c, %r15
je 0x1b984
callq 0x3310
movq (%rax), %rax
testb $0x20, 0x1(%rax,%r15,2)
jne 0x1b984
movslq %r13d, %rax
incl %r13d
movb %r15b, (%rbp,%rax)
incq %r14
cmpq %r14, %rbx
jne 0x1b95e
movslq %r13d, %rax
movb $0x0, (%rbp,%rax)
movb $0x1, %dil
testl %eax, %eax
je 0x1b9ba
movl %r13d, %ecx
xorl %eax, %eax
cmpb $0x3a, (%rbp,%rax)
je 0x1b9c1
incq %rax
cmpq %rax, %rcx
jne 0x1b9a0
movl %ecx, %eax
jmp 0x1b9c3
movb $0x0, (%rbp)
movb $0x1, %dil
xorl %r13d, %r13d
xorl %eax, %eax
jmp 0x1b9c3
xorl %edi, %edi
movl %eax, %r8d
movb $0x0, (%rbp,%r8)
movq 0x10(%rsp), %rax
movl (%rax), %r14d
testl %r14d, %r14d
movq %rbp, 0x8(%rsp)
movl %r13d, 0x4(%rsp)
movl %edi, 0x18(%rsp)
movq %r8, 0x30(%rsp)
je 0x1ba20
movsbl (%rbp), %esi
xorl %r9d, %r9d
movl $0x0, %ecx
testl %esi, %esi
je 0x1ba29
movb 0x1(%rbp), %al
testb %al, %al
je 0x1ba27
leaq 0x2(%rbp), %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 0x1ba09
jmp 0x1ba29
xorl %ebx, %ebx
jmp 0x1bab7
movl %esi, %ecx
movl %ecx, %eax
xorl %edx, %edx
divl %r14d
movl %edx, %edi
leal -0x1(%r14), %esi
movl %ecx, %eax
xorl %edx, %edx
divl %esi
movl %edx, %r15d
incl %r15d
movq 0x10(%rsp), %rax
movq 0x10(%rax), %r12
movl %edi, %ebx
movl %edi, 0x1c(%rsp)
movl %ebx, %eax
shrl $0x4, %eax
movl (%r12,%rax,4), %ebp
leal (%rbx,%rbx), %r13d
andl $0x1e, %r13d
movl %ebp, %eax
movl %r13d, %ecx
shrl %cl, %eax
testb $0x2, %al
jne 0x1baaa
testb $0x1, %al
jne 0x1ba95
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rax
movl %ebx, %ecx
movq (%rax,%rcx,8), %rdi
movq 0x8(%rsp), %rsi
callq 0x34e0
movl 0x1c(%rsp), %edi
xorl %r9d, %r9d
testl %eax, %eax
je 0x1baaa
addl %r15d, %ebx
cmpl %r14d, %ebx
movl %r14d, %eax
cmovbl %r9d, %eax
subl %eax, %ebx
cmpl %edi, %ebx
jne 0x1ba51
jmp 0x1babc
movl %r13d, %ecx
shrl %cl, %ebp
testb $0x3, %bpl
cmovnel %r14d, %ebx
cmpl %r14d, %ebx
jne 0x1bad9
movq 0x28(%rsp), %rax
movl $0x0, (%rax)
movq 0x8(%rsp), %rdi
callq 0x32c0
xorl %r15d, %r15d
jmp 0x1bc1e
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rax
movl %ebx, %ecx
shlq $0x4, %rcx
movq (%rax,%rcx), %r13
movq 0x8(%rax,%rcx), %r12
cmpb $0x0, 0x18(%rsp)
je 0x1bb07
xorl %ebp, %ebp
movl %r13d, %ebx
movl %r13d, %eax
movq 0x20(%rsp), %r14
jmp 0x1bb4f
movq 0x8(%rsp), %rcx
movq 0x30(%rsp), %rbx
leaq (%rcx,%rbx), %rdi
incq %rdi
cmpl 0x4(%rsp), %ebx
movq 0x20(%rsp), %r14
je 0x1bb39
movl 0x4(%rsp), %eax
cmpb $0x2d, (%rcx,%rbx)
je 0x1bc30
incq %rbx
cmpq %rbx, %rax
jne 0x1bb27
callq 0x3410
movl %eax, %ebp
movl %r13d, %ebx
movl %r13d, %eax
xorl %ecx, %ecx
testl %ebp, %ebp
setg %cl
subl %ecx, %ebp
cmpl %ebx, %ebp
cmovael %ebx, %ebp
cmpl %ebx, %eax
cmovbl %eax, %ebx
cmpl %ebx, %ebp
cmovgel %ebx, %ebp
movq 0x8(%rsp), %rdi
callq 0x32c0
subl %ebp, %ebx
leal 0x2(%rbx), %eax
movslq %eax, %rdi
callq 0x31d0
movq %rax, %r15
movq (%r14), %rdi
movq %r13, %rcx
shrq $0x30, %rcx
movl %ebp, %eax
cltd
idivl %ecx
shrq $0x20, %r13
movzwl %r13w, %ecx
imull %ecx, %eax
cltq
movslq %edx, %rcx
addq %rcx, %r12
addq %rax, %r12
xorl %ebp, %ebp
movq %r12, %rsi
xorl %edx, %edx
callq 0x2284b
movq (%r14), %rdi
leaq 0x3(%rsp), %rsi
movl $0x1, %edx
callq 0x22445
cmpl $0x1, %eax
jne 0x1bc0f
testl %ebx, %ebx
jle 0x1bc0f
xorl %ebp, %ebp
leaq 0x3(%rsp), %r12
movq (%r14), %r13
cmpl $0x0, 0x88(%r13)
jne 0x1bc0f
callq 0x3310
movq (%rax), %rcx
movsbq 0x3(%rsp), %rax
cmpw $0x0, (%rcx,%rax,2)
jns 0x1bbf6
movslq %ebp, %rcx
incl %ebp
movb %al, (%r15,%rcx)
movq %r13, %rdi
movq %r12, %rsi
movl $0x1, %edx
callq 0x22445
cmpl $0x1, %eax
jne 0x1bc0f
cmpl %ebx, %ebp
jl 0x1bbcb
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
callq 0x3410
movl %eax, %ebp
cmpl %ebx, 0x4(%rsp)
jle 0x1bb40
movl %ebx, %eax
movq 0x8(%rsp), %rcx
leaq (%rcx,%rax), %rdi
incq %rdi
callq 0x3410
movl %r13d, %ebx
jmp 0x1bb46