// File: /workspace/github_c_makefile_star2_O1/aokad[P]sequeeze/seqtk.c
// Function: stk_subseq
int stk_subseq(int argc, char *argv[])
{
	khash_t(reg) *h = kh_init(reg);
	gzFile fp;
	kseq_t *seq;
	int l, i, j, c, is_tab = 0, line = 0;
	khint_t k;
	while ((c = getopt(argc, argv, "tl:")) >= 0) {
		switch (c) {
		case 't': is_tab = 1; break;
		case 'l': line = atoi(optarg); break;
		}
	}
	if (optind + 2 > argc) {
		fprintf(stderr, "\n");
		fprintf(stderr, "Usage:   seqtk subseq [options] <in.fa> <in.bed>|<name.list>\n\n");
		fprintf(stderr, "Options: -t       TAB delimited output\n");
		fprintf(stderr, "         -l INT   sequence line length [%d]\n\n", line);
		fprintf(stderr, "Note: Use 'samtools faidx' if only a few regions are intended.\n\n");
		return 1;
	}
	h = stk_reg_read(argv[optind+1]);
	if (h == 0) {
		fprintf(stderr, "[E::%s] failed to read the list of regions in file '%s'\n", __func__, argv[optind+1]);
		return 1;
	}
	// subseq
	fp = strcmp(argv[optind], "-")? gzopen(argv[optind], "r") : gzdopen(fileno(stdin), "r");
	if (fp == 0) {
		fprintf(stderr, "[E::%s] failed to open the input file/stream\n", __func__);
		return 1;
	}
	seq = kseq_init(fp);
	while ((l = kseq_read(seq)) >= 0) {
		reglist_t *p;
		k = kh_get(reg, h, seq->name.s);
		if (k == kh_end(h)) continue;
		p = &kh_val(h, k);
		for (i = 0; i < p->n; ++i) {
			int beg = p->a[i]>>32, end = p->a[i];
			if (beg >= seq->seq.l) {
				fprintf(stderr, "[subseq] %s: %d >= %ld\n", seq->name.s, beg, seq->seq.l);
				continue;
			}
			if (end > seq->seq.l) end = seq->seq.l;
			if (is_tab == 0) {
				printf("%c%s", seq->qual.l == seq->seq.l? '@' : '>', seq->name.s);
				if (beg > 0 || (int)p->a[i] != INT_MAX) {
					if (end == INT_MAX) {
						if (beg) printf(":%d", beg+1);
					} else printf(":%d-%d", beg+1, end);
				}
				if (seq->comment.l) printf(" %s", seq->comment.s);
			} else printf("%s\t%d\t", seq->name.s, beg + 1);
			if (end > seq->seq.l) end = seq->seq.l;
			for (j = 0; j < end - beg; ++j) {
				if (is_tab == 0 && (j == 0 || (line > 0 && j % line == 0))) putchar('\n');
				putchar(seq->seq.s[j + beg]);
			}
			putchar('\n');
			if (seq->qual.l != seq->seq.l || is_tab) continue;
			printf("+");
			for (j = 0; j < end - beg; ++j) {
				if (j == 0 || (line > 0 && j % line == 0)) putchar('\n');
				putchar(seq->qual.s[j + beg]);
			}
			putchar('\n');
		}
	}
	// free
	kseq_destroy(seq);
	gzclose(fp);
	stk_reg_destroy(h);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %r12
movl %edi, %ebp
leaq 0x68e8(%rip), %rdx      # 0xb577
callq 0x2240
testl %eax, %eax
js 0x4ce2
movl $0x0, 0xc(%rsp)
movq 0xb331(%rip), %rbx      # 0xffd8
leaq 0x68c9(%rip), %r14      # 0xb577
xorl %r15d, %r15d
cmpl $0x6c, %eax
je 0x4cc3
cmpl $0x74, %eax
jne 0x4ccf
movl $0x1, %r15d
jmp 0x4ccf
movq (%rbx), %rdi
callq 0x2250
movl %eax, 0xc(%rsp)
movl %ebp, %edi
movq %r12, %rsi
movq %r14, %rdx
callq 0x2240
testl %eax, %eax
jns 0x4cb1
jmp 0x4ced
xorl %r15d, %r15d
movl $0x0, 0xc(%rsp)
movq 0xb2dc(%rip), %rbx      # 0xffd0
movslq (%rbx), %rax
leal 0x2(%rax), %ecx
cmpl %ebp, %ecx
jg 0x50dd
movq 0x8(%r12,%rax,8), %rdi
callq 0x314c
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x50e8
movslq (%rbx), %rax
movq (%r12,%rax,8), %r12
leaq 0x7686(%rip), %rsi      # 0xc3ae
movq %r12, %rdi
callq 0x2170
testl %eax, %eax
je 0x4d45
leaq 0x645d(%rip), %rsi      # 0xb198
movq %r12, %rdi
callq 0x2220
jmp 0x4d62
movq 0xb27c(%rip), %rax      # 0xffc8
movq (%rax), %rdi
callq 0x21b0
leaq 0x643d(%rip), %rsi      # 0xb198
movl %eax, %edi
callq 0x20b0
testq %rax, %rax
je 0x50f2
movq %rax, 0x38(%rsp)
movq %rax, %rdi
callq 0x3e57
movq %rax, %r12
movq %rax, %rdi
callq 0x3ea1
testl %eax, %eax
js 0x50b0
movq 0x10(%rsp), %rax
movl (%rax), %edi
movl $0x0, %ebx
testl %edi, %edi
je 0x4e7f
movq 0x10(%r12), %rsi
movsbl (%rsi), %r14d
testl %r14d, %r14d
je 0x4dd3
movb 0x1(%rsi), %al
testb %al, %al
je 0x4dd6
leaq 0x2(%rsi), %rcx
movl %r14d, %edx
shll $0x5, %edx
subl %r14d, %edx
movsbl %al, %r14d
addl %edx, %r14d
movb (%rcx), %al
incq %rcx
testb %al, %al
jne 0x4db8
jmp 0x4dd6
xorl %r14d, %r14d
movq %rdi, 0x30(%rsp)
leal -0x1(%rdi), %edx
movl %r14d, %edi
andl %edx, %edi
movl %r14d, %eax
shrl $0x3, %eax
shll $0x3, %r14d
xorl %eax, %r14d
orl $0x1, %r14d
andl %edx, %r14d
movq 0x10(%rsp), %rax
movq 0x10(%rax), %r8
movl %edi, %ebx
movq %rsi, 0x18(%rsp)
movl %edx, 0x20(%rsp)
movl %edi, 0x2c(%rsp)
movq %r8, 0x40(%rsp)
movl %ebx, %eax
shrl $0x4, %eax
movl (%r8,%rax,4), %r13d
leal (%rbx,%rbx), %ebp
andl $0x1e, %ebp
movl %r13d, %eax
movl %ebp, %ecx
shrl %cl, %eax
testb $0x2, %al
jne 0x4e6e
testb $0x1, %al
jne 0x4e5c
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rax
movl %ebx, %ecx
movq (%rax,%rcx,8), %rdi
callq 0x2170
movq 0x40(%rsp), %r8
movl 0x2c(%rsp), %edi
movl 0x20(%rsp), %edx
movq 0x18(%rsp), %rsi
testl %eax, %eax
je 0x4e6e
addl %r14d, %ebx
andl %edx, %ebx
cmpl %edi, %ebx
jne 0x4e14
movq 0x30(%rsp), %rdi
movl %edi, %ebx
jmp 0x4e7f
movl %ebp, %ecx
shrl %cl, %r13d
testb $0x3, %r13b
movq 0x30(%rsp), %rdi
cmovnel %edi, %ebx
cmpl %edi, %ebx
je 0x50a8
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rcx
movl %ebx, %eax
shlq $0x4, %rax
cmpl $0x0, (%rcx,%rax)
jle 0x50a8
movq %rcx, %rbx
addq %rax, %rbx
xorl %ecx, %ecx
movq %rbx, 0x20(%rsp)
movq 0x8(%rbx), %rax
movq (%rax,%rcx,8), %r13
movq %r13, %r14
shrq $0x20, %r14
movq %r13, %rbp
sarq $0x20, %rbp
movq 0x30(%r12), %r8
cmpq %r8, %rbp
movq %rcx, 0x18(%rsp)
jae 0x5086
movslq %r13d, %rax
cmpq %r8, %rax
cmovaq %r8, %r13
testl %r15d, %r15d
je 0x4f01
movq 0x10(%r12), %rsi
leal 0x1(%r14), %edx
leaq 0x67f3(%rip), %rdi      # 0xb6e8
xorl %eax, %eax
callq 0x20f0
jmp 0x4f8e
movq 0x10(%r12), %rdx
xorl %eax, %eax
cmpq %r8, 0x48(%r12)
sete %al
leal 0x3e(,%rax,2), %esi
leaq 0x67bd(%rip), %rdi      # 0xb6db
xorl %eax, %eax
callq 0x20f0
testl %r14d, %r14d
jg 0x4f3c
movq 0x8(%rbx), %rax
movq 0x18(%rsp), %rcx
cmpl $0x7fffffff, (%rax,%rcx,8) # imm = 0x7FFFFFFF
je 0x4f73
cmpl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
jne 0x4f5e
testq %r14, %r14
je 0x4f73
leal 0x1(%r14), %esi
leaq 0x678b(%rip), %rdi      # 0xb6e0
xorl %eax, %eax
callq 0x20f0
jmp 0x4f73
leal 0x1(%r14), %esi
leaq 0x75fb(%rip), %rdi      # 0xc564
movl %r13d, %edx
xorl %eax, %eax
callq 0x20f0
cmpq $0x0, 0x18(%r12)
je 0x4f8e
movq 0x28(%r12), %rsi
leaq 0x675d(%rip), %rdi      # 0xb6e4
xorl %eax, %eax
callq 0x20f0
movslq %r13d, %rax
movq 0x30(%r12), %rbx
cmpq %rbx, %rax
cmovbel %r13d, %ebx
subl %r14d, %ebx
jle 0x4feb
movl %ebx, %r14d
xorl %r13d, %r13d
testl %r15d, %r15d
jne 0x4fd0
testq %r13, %r13
je 0x4fc6
cmpl $0x0, 0xc(%rsp)
jle 0x4fd0
movl %r13d, %eax
xorl %edx, %edx
divl 0xc(%rsp)
testl %edx, %edx
jne 0x4fd0
movl $0xa, %edi
callq 0x2060
movq 0x40(%r12), %rax
addq %rbp, %rax
movsbl (%r13,%rax), %edi
callq 0x2060
incq %r13
cmpq %r13, %r14
jne 0x4fa8
testl %r15d, %r15d
setne %r14b
movl $0xa, %edi
callq 0x2060
movq 0x48(%r12), %rax
cmpq 0x30(%r12), %rax
setne %al
orb %r14b, %al
movl 0xc(%rsp), %r14d
jne 0x506b
movl $0x2b, %edi
callq 0x2060
testl %ebx, %ebx
jle 0x5061
movl %ebx, %ebx
xorl %r13d, %r13d
testq %r13, %r13
je 0x503c
testl %r14d, %r14d
jle 0x5046
movl %r13d, %eax
xorl %edx, %edx
divl %r14d
testl %edx, %edx
jne 0x5046
movl $0xa, %edi
callq 0x2060
movq 0x58(%r12), %rax
addq %rbp, %rax
movsbl (%r13,%rax), %edi
callq 0x2060
incq %r13
cmpq %r13, %rbx
jne 0x5026
movl $0xa, %edi
callq 0x2060
movq 0x18(%rsp), %rcx
incq %rcx
movq 0x20(%rsp), %rbx
movslq (%rbx), %rax
cmpq %rax, %rcx
jl 0x4ead
jmp 0x50a8
movq 0xaf6b(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movq 0x10(%r12), %rdx
leaq 0x6627(%rip), %rsi      # 0xb6c3
movl %r14d, %ecx
xorl %eax, %eax
callq 0x2190
jmp 0x506b
movq %r12, %rdi
jmp 0x4d7e
movq %r12, %rdi
callq 0x4229
movq 0x38(%rsp), %rdi
callq 0x2040
movq 0x10(%rsp), %rdi
callq 0x3739
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0xc(%rsp), %edi
callq 0x2533
jmp 0x50f7
movq %r12, %rdi
callq 0x2502
jmp 0x50f7
callq 0x24e3
movl $0x1, %eax
jmp 0x50ce