// File: /workspace/github_c_makefile_star2_O3/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 $0x58, %rsp
movq %rsi, %r14
movl %edi, %r12d
leaq 0x6883(%rip), %rdx      # 0xb587
callq 0x2240
testl %eax, %eax
js 0x4d56
movl $0x0, 0xc(%rsp)
movq 0xb2bc(%rip), %rbx      # 0xffd8
leaq 0x6864(%rip), %r15      # 0xb587
xorl %ebp, %ebp
cmpl $0x6c, %eax
je 0x4d36
cmpl $0x74, %eax
jne 0x4d42
movl $0x1, %ebp
jmp 0x4d42
movq (%rbx), %rdi
callq 0x2250
movl %eax, 0xc(%rsp)
movl %r12d, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x2240
testl %eax, %eax
jns 0x4d25
jmp 0x4d60
xorl %ebp, %ebp
movl $0x0, 0xc(%rsp)
movq 0xb269(%rip), %rbx      # 0xffd0
movslq (%rbx), %rax
leal 0x2(%rax), %ecx
cmpl %r12d, %ecx
jg 0x516d
movq 0x8(%r14,%rax,8), %rdi
callq 0x314c
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x5178
movslq (%rbx), %rax
movq (%r14,%rax,8), %rdi
cmpb $0x2d, (%rdi)
jne 0x4da4
cmpb $0x0, 0x1(%rdi)
je 0x514b
leaq 0x63fd(%rip), %rsi      # 0xb1a8
callq 0x2220
testq %rax, %rax
je 0x5182
movq %rax, 0x50(%rsp)
movq %rax, %rdi
callq 0x3edd
movq %rax, %r12
movq %rax, %rdi
callq 0x3f27
testl %eax, %eax
js 0x511e
movq 0x10(%rsp), %rax
movl (%rax), %eax
leal -0x1(%rax), %ecx
movl %ecx, 0x1c(%rsp)
movq %rax, 0x30(%rsp)
movl %ebp, 0x2c(%rsp)
movq 0x10(%r12), %r9
movl $0x0, %r15d
testl %eax, %eax
movq %r9, 0x40(%rsp)
je 0x4ed2
movsbl (%r9), %ebx
testl %ebx, %ebx
je 0x4e33
movb 0x1(%r9), %al
testb %al, %al
je 0x4e35
leaq 0x2(%r9), %rcx
movl %ebx, %edx
shll $0x5, %edx
subl %ebx, %edx
movsbl %al, %ebx
addl %edx, %ebx
movb (%rcx), %al
incq %rcx
testb %al, %al
jne 0x4e1c
jmp 0x4e35
xorl %ebx, %ebx
movl %ebx, %edx
movl 0x1c(%rsp), %ecx
andl %ecx, %edx
movl %ebx, %eax
shrl $0x3, %eax
shll $0x3, %ebx
xorl %eax, %ebx
orl $0x1, %ebx
andl %ecx, %ebx
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rsi
movl %edx, %r15d
movq %rsi, 0x20(%rsp)
movl %r15d, %eax
shrl $0x4, %eax
movl (%rsi,%rax,4), %r13d
leal (%r15,%r15), %r14d
andl $0x1e, %r14d
movl %r13d, %eax
movl %r14d, %ecx
shrl %cl, %eax
testb $0x2, %al
jne 0x4ebf
testb $0x1, %al
jne 0x4ead
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rax
movl %r15d, %ecx
movq (%rax,%rcx,8), %rdi
movq %r9, %rsi
movl %edx, %ebp
callq 0x2170
movq 0x20(%rsp), %rsi
movl %ebp, %edx
movl 0x2c(%rsp), %ebp
movq 0x40(%rsp), %r9
testl %eax, %eax
je 0x4ebf
addl %ebx, %r15d
andl 0x1c(%rsp), %r15d
cmpl %edx, %r15d
jne 0x4e5d
jmp 0x5109
movl %r14d, %ecx
shrl %cl, %r13d
testb $0x3, %r13b
movq 0x30(%rsp), %rax
cmovnel %eax, %r15d
cmpl %eax, %r15d
je 0x5109
movq 0x10(%rsp), %rax
movq 0x20(%rax), %r13
movl %r15d, %eax
shlq $0x4, %rax
cmpl $0x0, (%r13,%rax)
jle 0x5109
addq %rax, %r13
movq 0x30(%r12), %r8
xorl %ecx, %ecx
movq %r13, 0x38(%rsp)
movq 0x8(%r13), %rax
movq (%rax,%rcx,8), %rbx
movq %rbx, %r14
shrq $0x20, %r14
movq %rbx, %r15
sarq $0x20, %r15
cmpq %r8, %r15
movq %rcx, 0x48(%rsp)
jae 0x50e3
movslq %ebx, %rax
cmpq %r8, %rax
cmovaq %r8, %rbx
testl %ebp, %ebp
je 0x4f52
leal 0x1(%r14), %edx
leaq 0x67b5(%rip), %rdi      # 0xb6f8
movq %r9, %rsi
xorl %eax, %eax
callq 0x20f0
jmp 0x4fdb
xorl %eax, %eax
cmpq %r8, 0x48(%r12)
sete %al
leal 0x3e(,%rax,2), %esi
leaq 0x6781(%rip), %rdi      # 0xb6eb
movq %r9, %rdx
xorl %eax, %eax
callq 0x20f0
testl %r14d, %r14d
jg 0x4f8b
movq 0x8(%r13), %rax
movq 0x48(%rsp), %rcx
cmpl $0x7fffffff, (%rax,%rcx,8) # imm = 0x7FFFFFFF
je 0x4fc0
cmpl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
jne 0x4fac
testq %r14, %r14
je 0x4fc0
leal 0x1(%r14), %esi
leaq 0x674d(%rip), %rdi      # 0xb6f0
xorl %eax, %eax
callq 0x20f0
jmp 0x4fc0
leal 0x1(%r14), %esi
leaq 0x75bd(%rip), %rdi      # 0xc574
movl %ebx, %edx
xorl %eax, %eax
callq 0x20f0
cmpq $0x0, 0x18(%r12)
je 0x4fdb
movq 0x28(%r12), %rsi
leaq 0x6720(%rip), %rdi      # 0xb6f4
xorl %eax, %eax
callq 0x20f0
movslq %ebx, %rax
movq 0x30(%r12), %rcx
cmpq %rcx, %rax
movq %rcx, 0x20(%rsp)
cmoval %ecx, %ebx
subl %r14d, %ebx
jle 0x503a
movl %ebx, %r13d
xorl %r14d, %r14d
testl %ebp, %ebp
jne 0x5020
testq %r14, %r14
je 0x5016
cmpl $0x0, 0xc(%rsp)
jle 0x5020
movl %r14d, %eax
xorl %edx, %edx
divl 0xc(%rsp)
testl %edx, %edx
jne 0x5020
movl $0xa, %edi
callq 0x2060
movq 0x40(%r12), %rax
addq %r15, %rax
movsbl (%r14,%rax), %edi
callq 0x2060
incq %r14
cmpq %r14, %r13
jne 0x4ff9
testl %ebp, %ebp
setne %r14b
movl $0xa, %edi
callq 0x2060
movq 0x20(%rsp), %r8
cmpq %r8, 0x48(%r12)
setne %al
orb %r14b, %al
je 0x5063
movq 0x38(%rsp), %r13
jmp 0x50c7
movl $0x2b, %edi
callq 0x2060
testl %ebx, %ebx
movq 0x38(%rsp), %r13
jle 0x50b8
movl %ebx, %ebx
xorl %r14d, %r14d
testq %r14, %r14
je 0x5094
cmpl $0x0, 0xc(%rsp)
jle 0x509e
movl %r14d, %eax
xorl %edx, %edx
divl 0xc(%rsp)
testl %edx, %edx
jne 0x509e
movl $0xa, %edi
callq 0x2060
movq 0x58(%r12), %rax
addq %r15, %rax
movsbl (%r14,%rax), %edi
callq 0x2060
incq %r14
cmpq %r14, %rbx
jne 0x507b
movl $0xa, %edi
callq 0x2060
movq 0x20(%rsp), %r8
movq 0x48(%rsp), %rcx
incq %rcx
movslq (%r13), %rax
cmpq %rax, %rcx
movq 0x40(%rsp), %r9
jl 0x4f06
jmp 0x5109
movq 0xaf0e(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x65df(%rip), %rsi      # 0xb6d3
movq %r9, %rdx
movl %r14d, %ecx
xorl %eax, %eax
movq %r8, %rbx
callq 0x2190
movq %rbx, %r8
jmp 0x50c7
movq %r12, %rdi
callq 0x3f27
testl %eax, %eax
movq 0x30(%rsp), %rax
jns 0x4df0
movq %r12, %rdi
callq 0x428d
movq 0x50(%rsp), %rdi
callq 0x2040
movq 0x10(%rsp), %rdi
callq 0x37b8
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xae76(%rip), %rax      # 0xffc8
movq (%rax), %rdi
callq 0x21b0
leaq 0x6047(%rip), %rsi      # 0xb1a8
movl %eax, %edi
callq 0x20b0
jmp 0x4db0
movl 0xc(%rsp), %edi
callq 0x2533
jmp 0x5187
movq %r14, %rdi
callq 0x2502
jmp 0x5187
callq 0x24e3
movl $0x1, %eax
jmp 0x513c