// File: /workspace/github_c_makefile_star2_O2/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 $0x38, %rsp
movq %rsi, %r14
movl %edi, %ebp
callq 0x86d9
movl $0x0, 0x4(%rsp)
movb $0x1, %r15b
leaq 0x5871(%rip), %r12      # 0x9587
movq 0xa2bb(%rip), %r13      # 0xdfd8
xorl %ebx, %ebx
movl %ebp, %edi
movq %r14, %rsi
movq %r12, %rdx
callq 0x2240
testl %eax, %eax
js 0x3d52
cmpl $0x6c, %eax
je 0x3d45
cmpl $0x74, %eax
jne 0x3d1f
movb $0x1, %al
movl %eax, 0x4(%rsp)
xorl %r15d, %r15d
jmp 0x3d1f
movq (%r13), %rdi
callq 0x2250
movl %eax, %ebx
jmp 0x3d1f
movq 0xa277(%rip), %r12      # 0xdfd0
movslq (%r12), %rax
leal 0x2(%rax), %ecx
cmpl %ebp, %ecx
jg 0x4045
movq 0x8(%r14,%rax,8), %rdi
callq 0x23ac
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x40af
movslq (%r12), %rax
movq (%r14,%rax,8), %rdi
cmpb $0x2d, (%rdi)
jne 0x3d97
cmpb $0x0, 0x1(%rdi)
je 0x4023
leaq 0x540a(%rip), %rsi      # 0x91a8
callq 0x2220
testq %rax, %rax
je 0x40d9
movq %rax, 0x30(%rsp)
movq %rax, %rdi
callq 0x3102
movq %rax, %r12
movq %r12, %rdi
callq 0x312e
testl %eax, %eax
js 0x3ff6
movq 0x10(%r12), %rsi
movq 0x10(%rsp), %r14
movq %r14, %rdi
movq %rsi, 0x18(%rsp)
callq 0x266f
cmpl (%r14), %eax
je 0x3dbc
movl %eax, %ecx
shlq $0x4, %rcx
movq 0x10(%rsp), %rax
addq 0x20(%rax), %rcx
xorl %edx, %edx
movq %rcx, 0x20(%rsp)
movslq (%rcx), %rax
cmpq %rax, %rdx
jge 0x3dbc
movq 0x8(%rcx), %rax
movq %rdx, 0x28(%rsp)
movq (%rax,%rdx,8), %r14
movq %r14, %r13
shrq $0x20, %r13
movq %r14, %rbp
sarq $0x20, %rbp
movq 0x30(%r12), %r8
cmpq %r8, %rbp
jae 0x3fd4
movslq %r14d, %rax
cmpq %r8, %rax
cmovaq %r8, %r14
testb $0x1, %r15b
je 0x3ea6
xorl %eax, %eax
movq %r8, 0x8(%rsp)
cmpq %r8, 0x48(%r12)
sete %al
leal 0x3e(,%rax,2), %esi
leaq 0x588f(%rip), %rdi      # 0x96eb
movq 0x18(%rsp), %rdx
xorl %eax, %eax
callq 0x20f0
testl %r13d, %r13d
jg 0x3e84
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x28(%rsp), %rcx
cmpl $0x7fffffff, (%rax,%rcx,8) # imm = 0x7FFFFFFF
je 0x3ed9
cmpl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
jne 0x3ec4
testq %r13, %r13
je 0x3ed9
leal 0x1(%r13), %esi
leaq 0x5853(%rip), %rdi      # 0x96f0
xorl %eax, %eax
callq 0x20f0
jmp 0x3ed9
leal 0x1(%r13), %edx
leaq 0x5847(%rip), %rdi      # 0x96f8
movq 0x18(%rsp), %rsi
xorl %eax, %eax
callq 0x20f0
movq 0x30(%r12), %rcx
jmp 0x3ef9
leal 0x1(%r13), %esi
leaq 0x66a5(%rip), %rdi      # 0xa574
movl %r14d, %edx
xorl %eax, %eax
callq 0x20f0
cmpq $0x0, 0x18(%r12)
je 0x3ef4
movq 0x28(%r12), %rsi
leaq 0x5807(%rip), %rdi      # 0x96f4
xorl %eax, %eax
callq 0x20f0
movq 0x8(%rsp), %rcx
movslq %r14d, %rax
cmpq %rcx, %rax
movq %rcx, 0x8(%rsp)
cmoval %ecx, %r14d
subl %r13d, %r14d
testl %r14d, %r14d
movl $0x0, %eax
cmovlel %eax, %r14d
xorl %r13d, %r13d
cmpq %r13, %r14
je 0x3f59
testb $0x1, %r15b
je 0x3f41
testq %r13, %r13
je 0x3f39
testl %ebx, %ebx
jle 0x3f41
movl %r13d, %eax
xorl %edx, %edx
divl %ebx
testl %edx, %edx
jne 0x3f41
pushq $0xa
popq %rdi
callq 0x2060
movq 0x40(%r12), %rax
addq %rbp, %rax
movsbl (%r13,%rax), %edi
callq 0x2060
incq %r13
jmp 0x3f1a
pushq $0xa
popq %rdi
callq 0x2060
movq 0x8(%rsp), %rax
cmpq %rax, 0x48(%r12)
setne %al
orb 0x4(%rsp), %al
testb $0x1, %al
jne 0x3fc2
pushq $0x2b
popq %rdi
callq 0x2060
xorl %r13d, %r13d
cmpq %r13, %r14
je 0x3fba
testq %r13, %r13
je 0x3f9a
testl %ebx, %ebx
jle 0x3fa2
movl %r13d, %eax
xorl %edx, %edx
divl %ebx
testl %edx, %edx
jne 0x3fa2
pushq $0xa
popq %rdi
callq 0x2060
movq 0x58(%r12), %rax
addq %rbp, %rax
movsbl (%r13,%rax), %edi
callq 0x2060
incq %r13
jmp 0x3f81
pushq $0xa
popq %rdi
callq 0x2060
movq 0x28(%rsp), %rdx
incq %rdx
movq 0x20(%rsp), %rcx
jmp 0x3dfe
movq 0xa01d(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x56ee(%rip), %rsi      # 0x96d3
movq 0x18(%rsp), %rdx
movl %r13d, %ecx
xorl %eax, %eax
callq 0x2190
jmp 0x3fc2
movq %r12, %rdi
callq 0x3329
movq 0x30(%rsp), %rdi
callq 0x2040
movq 0x10(%rsp), %rdi
callq 0x29ad
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x9f9e(%rip), %rax      # 0xdfc8
movq (%rax), %rdi
callq 0x21b0
leaq 0x516f(%rip), %rsi      # 0x91a8
movl %eax, %edi
callq 0x20b0
jmp 0x3da3
movq 0x9fac(%rip), %r15      # 0xdff8
movq (%r15), %rsi
pushq $0xa
popq %rdi
callq 0x2140
movq (%r15), %rcx
leaq 0x552a(%rip), %rdi      # 0x958b
pushq $0x3e
popq %rsi
pushq $0x1
popq %r14
movq %r14, %rdx
callq 0x2260
movq (%r15), %rcx
leaq 0x5550(%rip), %rdi      # 0x95ca
pushq $0x27
popq %rsi
movq %r14, %rdx
callq 0x2260
movq (%r15), %rdi
leaq 0x5563(%rip), %rsi      # 0x95f2
movl %ebx, %edx
xorl %eax, %eax
callq 0x2190
movq (%r15), %rcx
leaq 0x557e(%rip), %rdi      # 0x9620
pushq $0x40
popq %rsi
movq %r14, %rdx
callq 0x2260
jmp 0x40f8
movq 0x9f42(%rip), %rax      # 0xdff8
movq (%rax), %rdi
movslq (%r12), %rax
movq 0x8(%r14,%rax,8), %rcx
leaq 0x5598(%rip), %rsi      # 0x9661
leaq 0x55ca(%rip), %rdx      # 0x969a
xorl %eax, %eax
callq 0x2190
jmp 0x40f8
movq 0x9f18(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x55bb(%rip), %rsi      # 0x96a5
leaq 0x55a9(%rip), %rdx      # 0x969a
xorl %eax, %eax
callq 0x2190
pushq $0x1
popq %rax
jmp 0x4014