// File: /workspace/github_c_makefile_star2_O1/aokad[P]sequeeze/seqtk.c
// Function: stk_sample
int stk_sample(int argc, char *argv[])
{
	int c, twopass = 0;
	uint64_t i, num = 0, n_seqs = 0;
	double frac = 0.;
	gzFile fp;
	kseq_t *seq;
	krand_t *kr = 0;

	while ((c = getopt(argc, argv, "2s:")) >= 0)
		if (c == 's') kr = kr_srand(atol(optarg));
		else if (c == '2') twopass = 1;

	if (optind + 2 > argc) {
		fprintf(stderr, "\n");
		fprintf(stderr, "Usage:   seqtk sample [-2] [-s seed=11] <in.fa> <frac>|<number>\n\n");
		fprintf(stderr, "Options: -s INT       RNG seed [11]\n");
		fprintf(stderr, "         -2           2-pass mode: twice as slow but with much reduced memory\n\n");
		return 1;
	}
	frac = atof(argv[optind+1]);
	if (frac >= 1.0) num = (uint64_t)(frac + .499), frac = 0.;
	else if (twopass) {
		fprintf(stderr, "[W::%s] when sampling a fraction, option -2 is ignored.", __func__);
		twopass = 0;
	}
	if (kr == 0) kr = kr_srand(11);

	if (!twopass) { // the streaming version
		kseq_t *buf = 0;
		if (num > 0) buf = calloc(num, sizeof(kseq_t));
		if (num > 0 && buf == NULL) {
			fprintf(stderr, "[E::%s] Could not allocate enough memory for %" PRIu64 " sequences. Exiting...\n", __func__, num);
			free(kr);
			return 1;
		}

		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);
		n_seqs = 0;
		while (kseq_read(seq) >= 0) {
			double r = kr_drand(kr);
			++n_seqs;
			if (num) {
				uint64_t y = n_seqs - 1 < num? n_seqs - 1 : (uint64_t)(r * n_seqs);
				if (y < num) cpy_kseq(&buf[y], seq);
			} else if (r < frac) stk_printseq(seq, UINT_MAX);
		}
		for (i = 0; i < num; ++i) {
			kseq_t *p = &buf[i];
			if (p->seq.l) stk_printseq(p, UINT_MAX);
			free(p->seq.s); free(p->qual.s); free(p->name.s);
		}
		if (buf != NULL) free(buf);
	} else {
		uint64_t *buf;
		khash_t(64) *hash;
		int absent;

		if (strcmp(argv[optind], "-") == 0) {
			fprintf(stderr, "[E::%s] in the 2-pass mode, the input cannot be STDIN.\n", __func__);
			free(kr);
			return 1;
		}

		// 1st pass
		buf = malloc(num * 8);
		for (i = 0; i < num; ++i) buf[i] = UINT64_MAX;
		fp = gzopen(argv[optind], "r");
		if (fp == 0) {
			fprintf(stderr, "[E::%s] failed to open the input file/stream.\n", __func__);
			return 1;
		}
		seq = kseq_init(fp);
		n_seqs = 0;
		while (kseq_read(seq) >= 0) {
			double r = kr_drand(kr);
			uint64_t y;
			++n_seqs;
			y = n_seqs - 1 < num? n_seqs - 1 : (uint64_t)(r * n_seqs);
			if (y < num) buf[y] = n_seqs;
		}
		kseq_destroy(seq);
		gzclose(fp);
		hash = kh_init(64);
		for (i = 0; i < num; ++i) kh_put(64, hash, buf[i], &absent);
		free(buf);
		// 2nd pass
		fp = gzopen(argv[optind], "r");
		seq = kseq_init(fp);
		n_seqs = 0;
		while (kseq_read(seq) >= 0)
			if (kh_get(64, hash, ++n_seqs) != kh_end(hash))
				stk_printseq(seq, UINT_MAX);
		kh_destroy(64, hash);
	}

	kseq_destroy(seq);
	gzclose(fp);
	free(kr);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r15
movl %edi, %ebp
leaq 0x5450(%rip), %rdx      # 0xbb0c
callq 0x2240
xorl %ebx, %ebx
testl %eax, %eax
js 0x671d
leaq 0x543e(%rip), %r14      # 0xbb0c
movq 0x9903(%rip), %r12      # 0xffd8
movq $0x0, 0x8(%rsp)
cmpl $0x32, %eax
je 0x6700
cmpl $0x73, %eax
jne 0x6705
movq (%r12), %rdi
callq 0x2230
movq %rax, %rdi
callq 0x37bc
movq %rax, 0x8(%rsp)
jmp 0x6705
movl $0x1, %ebx
movl %ebp, %edi
movq %r15, %rsi
movq %r14, %rdx
callq 0x2240
testl %eax, %eax
jns 0x66de
testl %ebx, %ebx
sete %bl
jmp 0x6728
movb $0x1, %bl
movq $0x0, 0x8(%rsp)
movq 0x98a1(%rip), %r12      # 0xffd0
movslq (%r12), %rax
leal 0x2(%rax), %ecx
cmpl %ebp, %ecx
jg 0x6e6f
movq 0x8(%r15,%rax,8), %rdi
callq 0x20a0
ucomisd 0x4918(%rip), %xmm0     # 0xb068
jae 0x6761
testb %bl, %bl
je 0x6e7b
xorl %r14d, %r14d
movb $0x1, %bl
jmp 0x678c
addsd 0x4907(%rip), %xmm0     # 0xb070
cvttsd2si %xmm0, %rax
movq %rax, %rcx
subsd 0x48ff(%rip), %xmm0     # 0xb078
cvttsd2si %xmm0, %r14
sarq $0x3f, %rcx
andq %rcx, %r14
orq %rax, %r14
xorpd %xmm0, %xmm0
cmpq $0x0, 0x8(%rsp)
jne 0x67af
movl $0xb, %edi
movapd %xmm0, 0x10(%rsp)
callq 0x37bc
movapd 0x10(%rsp), %xmm0
movq %rax, 0x8(%rsp)
testb %bl, %bl
je 0x67d7
movapd %xmm0, 0x10(%rsp)
testq %r14, %r14
je 0x6bfa
movl $0x70, %esi
movq %r14, %rdi
callq 0x2160
movq %rax, %r13
jmp 0x6bfd
movslq (%r12), %rax
movq %r15, 0x20(%rsp)
movq (%r15,%rax,8), %r13
leaq 0x5bc3(%rip), %rsi      # 0xc3ae
movq %r13, %rdi
callq 0x2170
testl %eax, %eax
je 0x6e91
leaq (,%r14,8), %rbp
movq %rbp, %rdi
callq 0x21d0
movq %rax, %r15
testq %r14, %r14
je 0x6823
movq %r15, %rdi
movl $0xff, %esi
movq %rbp, %rdx
callq 0x2120
leaq 0x496e(%rip), %rsi      # 0xb198
movq %r13, %rdi
callq 0x2220
testq %rax, %rax
je 0x6ed2
movq %rax, %r12
movq %rax, %rdi
callq 0x3e57
movq %rax, %rbp
movq %rax, %rdi
callq 0x3ea1
testl %eax, %eax
js 0x68e5
movl $0x1, %ebx
leaq -0x1(%rbx), %r13
movq 0x8(%rsp), %rdi
callq 0x3818
cmpq %r14, %r13
jb 0x68c9
shrq $0xb, %rax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
mulsd 0x47fb(%rip), %xmm0     # 0xb080
movq %rbx, %xmm1
punpckldq 0x478e(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x4796(%rip), %xmm1     # 0xb030
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
mulsd %xmm0, %xmm2
cvttsd2si %xmm2, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
subsd 0x47ba(%rip), %xmm2     # 0xb078
cvttsd2si %xmm2, %r13
andq %rcx, %r13
orq %rax, %r13
cmpq %r14, %r13
jae 0x68d2
movq %rbx, (%r15,%r13,8)
movq %rbp, %rdi
callq 0x3ea1
incq %rbx
testl %eax, %eax
jns 0x685e
movq %rbp, %rdi
callq 0x4229
movq %r12, %rdi
callq 0x2040
movl $0x1, %edi
movl $0x28, %esi
callq 0x2160
movq %rax, %r13
testq %r14, %r14
je 0x6a81
xorl %ebp, %ebp
movq %r15, 0x10(%rsp)
movq (%r15,%rbp,8), %rbx
movl 0x8(%r13), %eax
cmpl 0xc(%r13), %eax
jb 0x6945
movl (%r13), %eax
movl 0x4(%r13), %ecx
addl %ecx, %ecx
cmpl %eax, %ecx
movl $0x0, %esi
sbbl %esi, %esi
orl $0x1, %esi
addl %eax, %esi
movq %r13, %rdi
callq 0xa24c
movl (%r13), %esi
leal -0x1(%rsi), %edi
movq %rbx, %r9
shrq $0x21, %r9
movl %ebx, %eax
shll $0xb, %eax
xorl %eax, %r9d
xorl %ebx, %r9d
movl %edi, %edx
andl %r9d, %edx
movq 0x10(%r13), %rax
movl %edx, %ecx
shrl $0x4, %ecx
movl (%rax,%rcx,4), %r8d
leal (%rdx,%rdx), %ecx
shrl %cl, %r8d
testb $0x2, %r8b
jne 0x6a0f
movl %r9d, %ecx
shrl $0x3, %ecx
shll $0x3, %r9d
xorl %ecx, %r9d
orl $0x1, %r9d
andl %edi, %r9d
movl %esi, %r8d
movl %edx, %r10d
movl %r10d, %ecx
shrl $0x4, %ecx
movl (%rax,%rcx,4), %r12d
leal (%r10,%r10), %ecx
andl $0x1e, %ecx
movl %r12d, %r11d
shrl %cl, %r11d
testb $0x2, %r11b
jne 0x69e2
testb $0x1, %r11b
jne 0x69ca
movq 0x18(%r13), %r11
movl %r10d, %r15d
cmpq %rbx, (%r11,%r15,8)
je 0x69e2
btl %ecx, %r12d
cmovbl %r10d, %r8d
addl %r9d, %r10d
andl %edi, %r10d
movl %r8d, %ecx
cmpl %edx, %r10d
jne 0x699a
jmp 0x69e4
movl %esi, %ecx
movl %ecx, %edx
cmpl %esi, %ecx
movq 0x10(%rsp), %r15
jne 0x6a0f
movl %r10d, %ecx
shrl $0x4, %ecx
movl (%rax,%rcx,4), %edx
leal (%r10,%r10), %ecx
shrl %cl, %edx
cmpl %esi, %r8d
cmovel %r10d, %r8d
testb $0x2, %dl
cmovel %r10d, %r8d
movl %r8d, %edx
movl %edx, %esi
shrl $0x4, %esi
movl (%rax,%rsi,4), %edi
leal (%rdx,%rdx), %ecx
andl $0x1e, %ecx
movl %edi, %r8d
shrl %cl, %r8d
testb $0x2, %r8b
jne 0x6a51
testb $0x1, %r8b
je 0x6a75
movq 0x18(%r13), %r8
movl $0x3, %r9d
shll %cl, %r9d
movl %edx, %ecx
movq %rbx, (%r8,%rcx,8)
notl %r9d
andl %r9d, %edi
movl %edi, (%rax,%rsi,4)
incl 0x4(%r13)
jmp 0x6a75
movq 0x18(%r13), %r8
movl $0x3, %r9d
shll %cl, %r9d
movl %edx, %ecx
movq %rbx, (%r8,%rcx,8)
notl %r9d
andl %r9d, %edi
movl %edi, (%rax,%rsi,4)
incl 0x4(%r13)
incl 0x8(%r13)
incq %rbp
cmpq %r14, %rbp
jne 0x6917
movq %r15, %rdi
callq 0x2050
movq 0x9540(%rip), %rax      # 0xffd0
movslq (%rax), %rax
movq 0x20(%rsp), %rcx
movq (%rcx,%rax,8), %rdi
leaq 0x46f5(%rip), %rsi      # 0xb198
callq 0x2220
movq %rax, 0x10(%rsp)
movq %rax, %rdi
callq 0x3e57
movq %rax, %r12
movq %rax, %rdi
callq 0x3ea1
testl %eax, %eax
js 0x6bc6
movl (%r13), %ebx
leal -0x1(%rbx), %ebp
xorl %r14d, %r14d
movq 0x94e7(%rip), %r15      # 0xffc0
incq %r14
movl $0x0, %edx
testl %ebx, %ebx
je 0x6b54
movq %r14, %rax
shrq $0x21, %rax
movl %r14d, %ecx
shll $0xb, %ecx
xorl %ecx, %eax
xorl %r14d, %eax
movl %ebp, %esi
andl %eax, %esi
movl %eax, %ecx
shrl $0x3, %ecx
shll $0x3, %eax
xorl %ecx, %eax
orl $0x1, %eax
andl %ebp, %eax
movq 0x10(%r13), %rdi
movl %esi, %edx
movl %edx, %ecx
shrl $0x4, %ecx
movl (%rdi,%rcx,4), %r8d
leal (%rdx,%rdx), %ecx
andl $0x1e, %ecx
movl %r8d, %r9d
shrl %cl, %r9d
testb $0x2, %r9b
jne 0x6b4a
testb $0x1, %r9b
jne 0x6b3e
movq 0x18(%r13), %r9
movl %edx, %r10d
cmpq %r14, (%r9,%r10,8)
je 0x6b4a
addl %eax, %edx
andl %ebp, %edx
cmpl %esi, %edx
jne 0x6b10
movl %ebx, %edx
jmp 0x6b54
shrl %cl, %r8d
testb $0x3, %r8b
cmovnel %ebx, %edx
cmpl %ebx, %edx
je 0x6bb6
xorl %eax, %eax
cmpq $0x0, 0x48(%r12)
setne %al
leal 0x3e(,%rax,2), %edi
callq 0x2060
movq 0x10(%r12), %rdi
movq (%r15), %rsi
callq 0x2110
movl $0xa, %edi
callq 0x2060
movq 0x40(%r12), %rdi
callq 0x2080
cmpq $0x0, 0x48(%r12)
je 0x6bb6
movl $0x2b, %edi
callq 0x2060
movl $0xa, %edi
callq 0x2060
movq 0x58(%r12), %rdi
callq 0x2080
movq %r12, %rdi
callq 0x3ea1
testl %eax, %eax
jns 0x6ad9
movb $0x1, %bl
testq %r13, %r13
je 0x6bf0
movq 0x18(%r13), %rdi
callq 0x2050
movq 0x10(%r13), %rdi
callq 0x2050
movq 0x20(%r13), %rdi
callq 0x2050
movq %r13, %rdi
callq 0x2050
movq 0x10(%rsp), %rbp
jmp 0x6e3b
xorl %r13d, %r13d
testq %r14, %r14
sete %al
testq %r13, %r13
setne %cl
orb %al, %cl
je 0x6e9f
movslq (%r12), %rax
movq (%r15,%rax,8), %r15
leaq 0x578e(%rip), %rsi      # 0xc3ae
movq %r15, %rdi
callq 0x2170
testl %eax, %eax
je 0x6c3d
leaq 0x4565(%rip), %rsi      # 0xb198
movq %r15, %rdi
callq 0x2220
jmp 0x6c5a
movq 0x9384(%rip), %rax      # 0xffc8
movq (%rax), %rdi
callq 0x21b0
leaq 0x4545(%rip), %rsi      # 0xb198
movl %eax, %edi
callq 0x20b0
movq %rax, %rbp
testq %rax, %rax
je 0x6ed9
movq %rbp, %rdi
callq 0x3e57
movq %rax, %r12
movq %rax, %rdi
callq 0x3ea1
testl %eax, %eax
js 0x6d92
movl $0x1, %ebx
movq 0x9333(%rip), %r15      # 0xffc0
movq 0x8(%rsp), %rdi
callq 0x3818
shrq $0xb, %rax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
mulsd 0x43d5(%rip), %xmm0     # 0xb080
testq %r14, %r14
je 0x6d15
movq %rbx, %xmm1
punpckldq 0x4363(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x436b(%rip), %xmm1     # 0xb030
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
mulsd %xmm2, %xmm0
cvttsd2si %xmm0, %rcx
leaq -0x1(%rbx), %rdx
movq %rcx, %rsi
sarq $0x3f, %rsi
subsd 0x438b(%rip), %xmm0     # 0xb078
cvttsd2si %xmm0, %rax
andq %rsi, %rax
orq %rcx, %rax
cmpq %r14, %rdx
cmovbq %rdx, %rax
cmpq %r14, %rax
jae 0x6d7f
imulq $0x70, %rax, %rdi
addq %r13, %rdi
movq %r12, %rsi
callq 0x6ee7
jmp 0x6d7f
movapd 0x10(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x6d7f
xorl %eax, %eax
cmpq $0x0, 0x48(%r12)
setne %al
leal 0x3e(,%rax,2), %edi
callq 0x2060
movq 0x10(%r12), %rdi
movq (%r15), %rsi
callq 0x2110
movl $0xa, %edi
callq 0x2060
movq 0x40(%r12), %rdi
callq 0x2080
cmpq $0x0, 0x48(%r12)
je 0x6d7f
movl $0x2b, %edi
callq 0x2060
movl $0xa, %edi
callq 0x2060
movq 0x58(%r12), %rdi
callq 0x2080
movq %r12, %rdi
callq 0x3ea1
incq %rbx
testl %eax, %eax
jns 0x6c8d
testq %r14, %r14
je 0x6e2c
leaq 0x58(%r13), %rbx
movq 0x921a(%rip), %r15      # 0xffc0
cmpq $0x0, -0x28(%rbx)
je 0x6e05
xorl %eax, %eax
cmpq $0x0, -0x10(%rbx)
setne %al
leal 0x3e(,%rax,2), %edi
callq 0x2060
movq -0x48(%rbx), %rdi
movq (%r15), %rsi
callq 0x2110
movl $0xa, %edi
callq 0x2060
movq -0x18(%rbx), %rdi
callq 0x2080
cmpq $0x0, -0x10(%rbx)
je 0x6e05
movl $0x2b, %edi
callq 0x2060
movl $0xa, %edi
callq 0x2060
movq (%rbx), %rdi
callq 0x2080
movq -0x18(%rbx), %rdi
callq 0x2050
movq (%rbx), %rdi
callq 0x2050
movq -0x48(%rbx), %rdi
callq 0x2050
addq $0x70, %rbx
decq %r14
jne 0x6da6
movb $0x1, %bl
testq %r13, %r13
je 0x6e3b
movq %r13, %rdi
callq 0x2050
movl $0x1, %eax
testb %bl, %bl
je 0x6e60
movq %r12, %rdi
callq 0x4229
movq %rbp, %rdi
callq 0x2040
movq 0x8(%rsp), %rdi
callq 0x2050
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x28e0
movl $0x1, %eax
jmp 0x6e60
movapd %xmm0, 0x10(%rsp)
callq 0x2857
movapd 0x10(%rsp), %xmm0
jmp 0x675a
movq 0x8(%rsp), %rdi
callq 0x2895
xorl %ebx, %ebx
jmp 0x6ecd
movq 0x9152(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x4d5a(%rip), %rsi      # 0xbc0a
leaq 0x4d48(%rip), %rdx      # 0xbbff
xorl %ebx, %ebx
movq %r14, %rcx
xorl %eax, %eax
callq 0x2190
movq 0x8(%rsp), %rdi
callq 0x2050
jmp 0x6e3b
callq 0x2876
jmp 0x6ede
callq 0x28c1
xorl %ebx, %ebx
xorl %ebp, %ebp
jmp 0x6e3b