// File: /workspace/github_cpp_makefile_star2_O1/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 $0x48, %rsp
movq %rdx, 0x30(%rsp)
movq %rsi, %r12
movq %rdi, 0x28(%rsp)
movq 0x18(%rdi), %rax
movq %rax, 0x8(%rsp)
movq %rsi, %rdi
callq 0x32d0
movq %rax, %r13
incl %eax
movslq %eax, %rdi
callq 0x31d0
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl $0x0, %ebp
andq %r13, %rbx
movq %rax, %r13
je 0x1b0d0
xorl %r14d, %r14d
xorl %ebp, %ebp
movsbq (%r12,%r14), %r15
cmpq $0x2c, %r15
je 0x1b0c8
callq 0x3310
movq (%rax), %rax
testb $0x20, 0x1(%rax,%r15,2)
jne 0x1b0c8
movslq %ebp, %rax
incl %ebp
movb %r15b, (%r13,%rax)
incq %r14
cmpq %r14, %rbx
jne 0x1b0a3
movl %ebp, %edi
movslq %ebp, %rax
movb $0x0, (%r13,%rax)
testl %eax, %eax
sete %dl
je 0x1b10e
cmpb $0x3a, (%r13)
movl $0x0, %r12d
je 0x1b116
leaq -0x1(%rdi), %rax
xorl %ecx, %ecx
cmpq %rcx, %rax
sete %dl
je 0x1b113
leaq 0x1(%rcx), %r12
cmpb $0x3a, 0x1(%r13,%rcx)
movq %r12, %rcx
jne 0x1b0f5
jmp 0x1b116
xorl %r12d, %r12d
jmp 0x1b116
movl %edi, %r12d
movl %r12d, %eax
movq %rax, 0x38(%rsp)
movb $0x0, (%r13,%rax)
movq 0x8(%rsp), %rax
movl (%rax), %ebx
testl %ebx, %ebx
movq %rdi, 0x40(%rsp)
je 0x1b16e
movb %dl, 0x6(%rsp)
movsbl (%r13), %esi
xorl %r8d, %r8d
movl $0x0, %ecx
testl %esi, %esi
je 0x1b178
movb 0x1(%r13), %al
testb %al, %al
je 0x1b176
leaq 0x2(%r13), %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 0x1b157
jmp 0x1b178
xorl %r15d, %r15d
jmp 0x1b223
movl %esi, %ecx
movq %r12, 0x20(%rsp)
movq %r13, 0x10(%rsp)
movl %ecx, %eax
xorl %edx, %edx
divl %ebx
movl %edx, %edi
leal -0x1(%rbx), %esi
movl %ecx, %eax
xorl %edx, %edx
divl %esi
movl %edx, %r14d
incl %r14d
movq 0x8(%rsp), %rax
movq 0x10(%rax), %r12
movl %edi, %r15d
movl %edi, 0x1c(%rsp)
movl %r15d, %eax
shrl $0x4, %eax
movl (%r12,%rax,4), %ebp
leal (%r15,%r15), %r13d
andl $0x1e, %r13d
movl %ebp, %eax
movl %r13d, %ecx
shrl %cl, %eax
testb $0x2, %al
jne 0x1b208
testb $0x1, %al
jne 0x1b1ef
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rax
movl %r15d, %ecx
movq (%rax,%rcx,8), %rdi
movq 0x10(%rsp), %rsi
callq 0x34e0
movl 0x1c(%rsp), %edi
xorl %r8d, %r8d
testl %eax, %eax
je 0x1b208
addl %r14d, %r15d
cmpl %ebx, %r15d
movl %ebx, %eax
cmovbl %r8d, %eax
subl %eax, %r15d
cmpl %edi, %r15d
jne 0x1b1a9
movl %ebx, %r15d
jmp 0x1b215
movl %r13d, %ecx
shrl %cl, %ebp
testb $0x3, %bpl
cmovnel %ebx, %r15d
movq 0x10(%rsp), %r13
movq 0x20(%rsp), %r12
movb 0x6(%rsp), %dl
cmpl %ebx, %r15d
jne 0x1b243
movq 0x30(%rsp), %rax
movl $0x0, (%rax)
movq %r13, %rdi
callq 0x32c0
xorl %r15d, %r15d
jmp 0x1b3aa
movq %r13, %rbx
movq 0x8(%rsp), %rax
movq 0x20(%rax), %rax
movl %r15d, %ecx
shlq $0x4, %rcx
movq (%rax,%rcx), %r13
movq 0x8(%rax,%rcx), %rcx
testb %dl, %dl
movq %rcx, 0x8(%rsp)
je 0x1b274
xorl %ebp, %ebp
movl %r13d, %r12d
movq 0x28(%rsp), %r14
jmp 0x1b2c8
movq 0x38(%rsp), %rax
leaq (%rbx,%rax), %rdi
incq %rdi
movq 0x40(%rsp), %r15
cmpl %r15d, %r12d
movq 0x28(%rsp), %r14
je 0x1b2a5
cmpb $0x2d, (%rbx,%rax)
je 0x1b2a2
incq %rax
cmpq %rax, %r15
jne 0x1b28f
movl %r15d, %r12d
jmp 0x1b2a5
movl %eax, %r12d
callq 0x3410
movl %eax, %ebp
cmpl %r15d, %r12d
jge 0x1b2c5
movl %r12d, %eax
leaq (%rbx,%rax), %rdi
incq %rdi
callq 0x3410
movl %eax, %r12d
jmp 0x1b2c8
movl %r13d, %r12d
xorl %eax, %eax
testl %ebp, %ebp
setg %al
subl %eax, %ebp
cmpl %r13d, %ebp
cmovael %r13d, %ebp
cmpl %r13d, %r12d
cmovael %r13d, %r12d
cmpl %r12d, %ebp
cmovgel %r12d, %ebp
movq %rbx, %rdi
callq 0x32c0
subl %ebp, %r12d
leal 0x2(%r12), %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
movq 0x8(%rsp), %rsi
addq %rcx, %rsi
addq %rax, %rsi
xorl %ebx, %ebx
xorl %edx, %edx
callq 0x22d4c
movq (%r14), %rdi
leaq 0x7(%rsp), %rsi
movl $0x1, %edx
callq 0x2292b
cmpl $0x1, %eax
jne 0x1b39b
testl %r12d, %r12d
jle 0x1b39b
xorl %ebx, %ebx
leaq 0x7(%rsp), %r13
movq (%r14), %rax
cmpl $0x0, 0x88(%rax)
jne 0x1b39b
callq 0x3310
movq (%rax), %rcx
movsbq 0x7(%rsp), %rax
cmpw $0x0, (%rcx,%rax,2)
jns 0x1b381
movslq %ebx, %rcx
incl %ebx
movb %al, (%r15,%rcx)
movq (%r14), %rdi
movq %r13, %rsi
movl $0x1, %edx
callq 0x2292b
cmpl $0x1, %eax
jne 0x1b39b
cmpl %r12d, %ebx
jl 0x1b357
movslq %ebx, %rax
movb $0x0, (%r15,%rax)
movq 0x30(%rsp), %rcx
movl %eax, (%rcx)
movq %r15, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq