// File: /workspace/github_c_makefile_star2_O2/skoren[P]bacValidation/bamcat.c
// Function: bam_parse_region
int bam_parse_region(bam_header_t *header, const char *str, int *ref_id, int *beg, int *end)
{
	char *s;
	int i, l, k, name_end;
	khiter_t iter;
	khash_t(s) *h;

	bam_init_header_hash(header);
	h = (khash_t(s)*)header->hash;

	*ref_id = *beg = *end = -1;
	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)) {
				if (bam_verbose >= 2) fprintf(stderr, "[%s] fail to determine the sequence name.\n", __func__);
				free(s); return -1;
			} else s[name_end] = ':', name_end = l;
		}
	} else iter = kh_get(s, h, str);
        if (iter == kh_end(h)) {
          free(s); 
          return -1;
        }
	*ref_id = kh_val(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) : 1<<29;
		if (*beg > 0) --*beg;
	} else *beg = 0, *end = 1<<29;
	free(s);
	return *beg <= *end? 0 : -1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r8, %r14
movq %rcx, %r15
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %rbx
callq 0x6403
movq 0x20(%rbx), %rax
movq %rax, -0x50(%rbp)
movq %r14, -0x58(%rbp)
orl $-0x1, (%r14)
movq %r15, -0x48(%rbp)
orl $-0x1, (%r15)
movq %r12, -0x60(%rbp)
orl $-0x1, (%r12)
movq %r13, -0x30(%rbp)
movq %r13, %rdi
callq 0x3120
movq %rax, %r12
incl %eax
movslq %eax, %rdi
callq 0x3470
movq %rax, %r14
xorl %ebx, %ebx
testl %r12d, %r12d
movl $0x0, %r15d
movq %r12, -0x40(%rbp)
cmovgl %r12d, %r15d
xorl %r13d, %r13d
movslq %r13d, %r12
cmpq %rbx, %r15
je 0x6a0d
callq 0x3380
movq (%rax), %rcx
movq -0x30(%rbp), %rax
movsbq (%rax,%rbx), %rax
testb $0x20, 0x1(%rcx,%rax,2)
jne 0x6a08
leal 0x1(%r12), %r13d
movb %al, (%r14,%r12)
incq %rbx
jmp 0x69df
movb $0x0, (%r14,%r12)
movl %r12d, %eax
decq %rax
movl %r13d, %r15d
testl %r15d, %r15d
jle 0x6a31
movl %eax, %ecx
decl %r15d
decq %rax
cmpb $0x3a, (%r14,%rcx)
jne 0x6a1b
jmp 0x6a35
movq -0x40(%rbp), %r15
cmpl %r13d, %r15d
jge 0x6a8a
movq %r14, -0x40(%rbp)
movslq %r15d, %rbx
movl $0x0, -0x38(%rbp)
incq %rbx
movq %rbx, %rcx
movq %rcx, %rbx
cmpq %r12, %rcx
jge 0x6aa3
movq -0x40(%rbp), %rax
movsbq (%rax,%rbx), %r14
cmpq $0x2d, %r14
je 0x6a85
callq 0x3380
leaq 0x1(%rbx), %rcx
cmpb $0x2c, %r14b
je 0x6a4e
movq (%rax), %rax
movzwl (%rax,%r14,2), %eax
andl $0x800, %eax            # imm = 0x800
jne 0x6a4e
jmp 0x6aa3
incl -0x38(%rbp)
jmp 0x6a48
movq -0x50(%rbp), %rbx
movq %rbx, %rdi
movq -0x30(%rbp), %rsi
callq 0x6890
movl %eax, %edx
movl (%rbx), %ecx
jmp 0x6b25
cmpl $0x2, -0x38(%rbp)
cmovael %r13d, %r15d
cmpq %r12, %rbx
cmovll %r13d, %r15d
movslq %r15d, %rax
movq -0x40(%rbp), %r14
movq %rax, -0x38(%rbp)
movb $0x0, (%r14,%rax)
movq -0x50(%rbp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6890
movl %eax, %edx
movl (%rbx), %ecx
cmpl %ecx, %eax
jne 0x6b25
movq %rbx, %rdi
movq -0x30(%rbp), %rsi
movl %edx, %ebx
callq 0x6890
cmpl %ebx, %eax
jne 0x6b15
cmpl $0x2, 0x885e(%rip)      # 0xf350
jl 0x6b29
movq 0x84d5(%rip), %rax      # 0xefd0
movq (%rax), %rdi
leaq 0x4781(%rip), %rsi      # 0xb286
leaq 0x47a5(%rip), %rdx      # 0xb2b1
xorl %eax, %eax
callq 0x34c0
jmp 0x6b29
movl %ebx, %ecx
movq -0x38(%rbp), %rdx
movb $0x3a, (%r14,%rdx)
movl %r13d, %r15d
movl %eax, %edx
cmpl %ecx, %edx
jne 0x6b39
movq %r14, %rdi
callq 0x33d0
pushq $-0x1
popq %rax
jmp 0x6c36
movq -0x50(%rbp), %rax
movq 0x20(%rax), %rax
movl %edx, %ecx
movl (%rax,%rcx,4), %eax
movq -0x60(%rbp), %rcx
movl %eax, (%rcx)
cmpl %r13d, %r15d
jge 0x6b78
leal 0x1(%r15), %esi
movslq %r15d, %rdx
leaq 0x1(%rdx), %rax
movslq %esi, %r13
cmpq %r12, %rax
jge 0x6b99
movb (%r14,%rax), %cl
cmpb $0x2c, %cl
je 0x6b73
incl %esi
movb %cl, (%r14,%r13)
incq %rax
jmp 0x6b5c
movq %r14, %r12
movq -0x48(%rbp), %rcx
andl $0x0, (%rcx)
movq -0x58(%rbp), %rax
movl $0x20000000, (%rax)     # imm = 0x20000000
movl (%rcx), %ebx
movl $0x20000000, %r14d      # imm = 0x20000000
jmp 0x6c24
movl %esi, -0x30(%rbp)
movb $0x0, (%r14,%r13)
leaq (%r14,%rdx), %rbx
movq %r14, %r12
leaq (%r14,%rdx), %rdi
incq %rdi
movq %rdx, %r14
callq 0x3540
movq %r14, %rsi
movq -0x48(%rbp), %rcx
movl %eax, (%rcx)
subq %r14, %r13
shlq $0x20, %rsi
movabsq $0x100000000, %rdx      # imm = 0x100000000
addq %rdx, %rsi
pushq $0x1
popq %rax
movl $0x20000000, %r14d      # imm = 0x20000000
cmpq %rax, %r13
je 0x6c10
cmpb $0x2d, (%rbx,%rax)
je 0x6bf0
addq %rdx, %rsi
incq %rax
jmp 0x6bdd
addl %eax, %r15d
cmpl %r15d, -0x30(%rbp)
jle 0x6c10
sarq $0x20, %rsi
leaq (%r12,%rsi), %rdi
incq %rdi
callq 0x3540
movq -0x48(%rbp), %rcx
movl %eax, %r14d
movq -0x58(%rbp), %rax
movl %r14d, (%rax)
movl (%rcx), %ebx
testl %ebx, %ebx
jle 0x6c24
decl %ebx
movl %ebx, (%rcx)
movl (%rax), %r14d
movq %r12, %rdi
callq 0x33d0
xorl %eax, %eax
cmpl %r14d, %ebx
setg %al
negl %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq