// File: /workspace/github_c_makefile_star2_O2/aokad[P]sequeeze/seqtk.c
// Function: stk_mutfa
int stk_mutfa(int argc, char *argv[])
{
	khash_t(reg) *h = kh_init(reg);
	gzFile fp;
	kseq_t *seq;
	kstream_t *ks;
	int l, i, dret;
	kstring_t *str;
	khint_t k;
	if (argc < 3) {
		fprintf(stderr, "Usage: seqtk mutfa <in.fa> <in.snp>\n\n");
		fprintf(stderr, "Note: <in.snp> contains at least four columns per line which are:\n");
		fprintf(stderr, "      'chr  1-based-pos  any  base-changed-to'.\n");
		return 1;
	}
	// read the list
	str = calloc(1, sizeof(kstring_t));
	fp = strcmp(argv[2], "-")? gzopen(argv[2], "r") : gzdopen(fileno(stdin), "r");
	if (fp == 0) {
		fprintf(stderr, "[E::%s] failed to open the input file/stream.\n", __func__);
		return 1;
	}
	ks = ks_init(fp);
	while (ks_getuntil(ks, 0, str, &dret) >= 0) {
		char *s = strdup(str->s);
		int beg = 0, ret;
		reglist_t *p;
		k = kh_get(reg, h, s);
		if (k == kh_end(h)) {
			k = kh_put(reg, h, s, &ret);
			memset(&kh_val(h, k), 0, sizeof(reglist_t));
		}
		p = &kh_val(h, k);
		if (ks_getuntil(ks, 0, str, &dret) > 0) beg = atol(str->s) - 1; // 2nd col
		ks_getuntil(ks, 0, str, &dret); // 3rd col
		ks_getuntil(ks, 0, str, &dret); // 4th col
		// skip the rest of the line
		if (dret != '\n') while ((dret = ks_getc(ks)) > 0 && dret != '\n');
		if (isalpha(str->s[0]) && str->l == 1) {
			if (p->n == p->m) {
				p->m = p->m? p->m<<1 : 4;
				p->a = realloc(p->a, p->m * 8);
			}
			p->a[p->n++] = (uint64_t)beg<<32 | str->s[0];
		}
	}
	ks_destroy(ks);
	gzclose(fp);
	free(str->s); free(str);
	// mutfa
	fp = strcmp(argv[1], "-")? gzopen(argv[1], "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)) {
			p = &kh_val(h, k);
			for (i = 0; i < p->n; ++i) {
				int beg = p->a[i]>>32;
				if (beg < seq->seq.l)
					seq->seq.s[beg] = (int)p->a[i];
			}
		}
		printf(">%s", seq->name.s);
		for (i = 0; i < l; ++i) {
			if (i%60 == 0) putchar('\n');
			putchar(seq->seq.s[i]);
		}
		putchar('\n');
	}
	// free
	kseq_destroy(seq);
	gzclose(fp);
	for (k = 0; k < kh_end(h); ++k) {
		if (kh_exist(h, k)) {
			free(kh_val(h, k).a);
			free((char*)kh_key(h, k));
		}
	}
	kh_destroy(reg, h);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r14
movl %edi, %ebp
callq 0x86d9
cmpl $0x2, %ebp
jle 0x4d01
movq %rax, %rbx
pushq $0x1
popq %rdi
pushq $0x18
popq %rsi
callq 0x2160
movq %rax, %r15
movq 0x10(%r14), %rdi
cmpb $0x2d, (%rdi)
jne 0x498d
cmpb $0x0, 0x1(%rdi)
je 0x4c9c
leaq 0x4814(%rip), %rsi      # 0x91a8
callq 0x2220
testq %rax, %rax
je 0x4ce0
movq %r14, 0x8(%rsp)
movq %rax, 0x10(%rsp)
movq %rax, %rdi
callq 0x2636
movq %rax, %r13
movq %rbx, 0x18(%rsp)
movq %r13, %rdi
xorl %esi, %esi
movq %r15, %rdx
leaq 0x4(%rsp), %rcx
callq 0x2667
testl %eax, %eax
js 0x4b0d
movq 0x10(%r15), %rdi
callq 0x2270
movq %rax, %r12
movq %rbx, %rdi
movq %rax, %rsi
callq 0x266f
cmpl (%rbx), %eax
jne 0x4a18
movq %rbx, %rdi
movq %r12, %rsi
leaq 0x24(%rsp), %rdx
callq 0x273f
movq 0x20(%rbx), %r14
movl %eax, %ebp
movq %rbp, %rax
shlq $0x4, %rax
xorps %xmm0, %xmm0
movups %xmm0, (%r14,%rax)
jmp 0x4a1e
movq 0x20(%rbx), %r14
movl %eax, %ebp
movq %r13, %rdi
xorl %esi, %esi
movq %r15, %rdx
leaq 0x4(%rsp), %rcx
callq 0x2667
testl %eax, %eax
jle 0x4a53
movq 0x10(%r15), %rdi
callq 0x2230
movq %rax, %r12
shlq $0x20, %r12
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
addq %rax, %r12
jmp 0x4a56
xorl %r12d, %r12d
movq %r13, %rdi
xorl %esi, %esi
movq %r15, %rdx
leaq 0x4(%rsp), %rbx
movq %rbx, %rcx
callq 0x2667
movq %r13, %rdi
xorl %esi, %esi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x2667
cmpl $0xa, 0x4(%rsp)
je 0x4a97
movq %r13, %rdi
callq 0x2914
movl %eax, 0x4(%rsp)
testl %eax, %eax
jle 0x4a97
cmpl $0xa, %eax
jne 0x4a82
callq 0x2290
movq (%rax), %rax
movq 0x10(%r15), %rbx
movsbq (%rbx), %rcx
testb $0x4, 0x1(%rax,%rcx,2)
je 0x4b03
cmpq $0x1, (%r15)
jne 0x4b03
shlq $0x4, %rbp
addq %rbp, %r14
movl (%r14), %edx
cmpl 0x4(%r14), %edx
jne 0x4aef
leal (%rdx,%rdx), %eax
testl %edx, %edx
pushq $0x4
popq %rcx
cmovel %ecx, %eax
movl %eax, 0x4(%r14)
movq 0x8(%r14), %rdi
shll $0x3, %eax
movslq %eax, %rsi
callq 0x2200
movq %rax, 0x8(%r14)
movsbq (%rbx), %rcx
movl (%r14), %edx
jmp 0x4af3
movq 0x8(%r14), %rax
orq %rcx, %r12
leal 0x1(%rdx), %ecx
movl %ecx, (%r14)
movslq %edx, %rcx
movq %r12, (%rax,%rcx,8)
movq 0x18(%rsp), %rbx
jmp 0x49bc
movq %r13, %rdi
callq 0x2992
movq 0x10(%rsp), %rdi
callq 0x2040
movq 0x10(%r15), %rdi
callq 0x2050
movq %r15, %rdi
callq 0x2050
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rdi
cmpb $0x2d, (%rdi)
jne 0x4b48
cmpb $0x0, 0x1(%rdi)
je 0x4cbe
leaq 0x4659(%rip), %rsi      # 0x91a8
callq 0x2220
testq %rax, %rax
je 0x4ce0
movq %rax, 0x8(%rsp)
movq %rax, %rdi
callq 0x3102
movq %rax, %r15
pushq $0x3c
popq %r12
pushq $0xa
popq %rbp
movq %r15, %rdi
callq 0x312e
testl %eax, %eax
js 0x4c1e
movl %eax, %r13d
movq 0x10(%r15), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x266f
cmpl (%rbx), %eax
jne 0x4be9
leaq 0x4903(%rip), %rdi      # 0x94a4
movq %r14, %rsi
xorl %eax, %eax
callq 0x20f0
movl %r13d, %r13d
xorl %r14d, %r14d
movl %r14d, %eax
xorl %edx, %edx
divl %r12d
cmpq %r14, %r13
je 0x4be0
imull $-0x3c, %eax, %eax
addl %r14d, %eax
jne 0x4bcd
movl %ebp, %edi
callq 0x2060
movq 0x40(%r15), %rax
movsbl (%rax,%r14), %edi
callq 0x2060
incq %r14
jmp 0x4bb1
movl %ebp, %edi
callq 0x2060
jmp 0x4b74
movl %eax, %eax
shlq $0x4, %rax
addq 0x20(%rbx), %rax
xorl %ecx, %ecx
movslq (%rax), %rdx
cmpq %rdx, %rcx
jge 0x4b9a
movq 0x8(%rax), %rdx
movq (%rdx,%rcx,8), %rdx
movq %rdx, %rsi
sarq $0x20, %rsi
cmpq 0x30(%r15), %rsi
jae 0x4c19
movq 0x40(%r15), %rdi
movb %dl, (%rdi,%rsi)
incq %rcx
jmp 0x4bf5
movq %r15, %rdi
callq 0x3329
movq 0x8(%rsp), %rdi
callq 0x2040
movl (%rbx), %r14d
xorl %r15d, %r15d
pushq $0x8
popq %r12
xorl %r13d, %r13d
cmpq %r14, %r13
jae 0x4c83
movq 0x10(%rbx), %rax
movq %r13, %rcx
shrq $0x4, %rcx
movl (%rax,%rcx,4), %eax
movl %r15d, %ecx
andb $0x1e, %cl
shrl %cl, %eax
testb $0x3, %al
jne 0x4c76
movq 0x20(%rbx), %rax
movq (%rax,%r12), %rdi
callq 0x2050
movq 0x18(%rbx), %rax
movq (%rax,%r13,8), %rdi
callq 0x2050
incq %r13
addl $0x2, %r15d
addq $0x10, %r12
jmp 0x4c3d
movq %rbx, %rdi
callq 0x2a20
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x9325(%rip), %rax      # 0xdfc8
movq (%rax), %rdi
callq 0x21b0
leaq 0x44f6(%rip), %rsi      # 0x91a8
movl %eax, %edi
callq 0x20b0
jmp 0x4999
movq 0x9303(%rip), %rax      # 0xdfc8
movq (%rax), %rdi
callq 0x21b0
leaq 0x44d4(%rip), %rsi      # 0x91a8
movl %eax, %edi
callq 0x20b0
jmp 0x4b54
movq 0x9311(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x46b0(%rip), %rsi      # 0x93a1
leaq 0x4cdc(%rip), %rdx      # 0x99d4
xorl %eax, %eax
callq 0x2190
jmp 0x4d4a
movq 0x92f0(%rip), %r14      # 0xdff8
movq (%r14), %rcx
leaq 0x4c28(%rip), %rdi      # 0x993a
pushq $0x25
popq %rsi
pushq $0x1
popq %rbx
movq %rbx, %rdx
callq 0x2260
movq (%r14), %rcx
leaq 0x4c36(%rip), %rdi      # 0x9960
pushq $0x42
popq %rsi
movq %rbx, %rdx
callq 0x2260
movq (%r14), %rcx
leaq 0x4c64(%rip), %rdi      # 0x99a3
pushq $0x30
popq %rsi
movq %rbx, %rdx
callq 0x2260
pushq $0x1
popq %rax
jmp 0x4c8d