// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xb0, %rsp
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x18(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x38(%rbp)
movq $0x0, -0x50(%rbp)
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq 0x6f6d(%rip), %rdx      # 0xdbf4
callq 0x2240
movl %eax, -0x14(%rbp)
cmpl $0x0, %eax
jl 0x6cc8
cmpl $0x73, -0x14(%rbp)
jne 0x6cb7
movq 0xa337(%rip), %rax      # 0x10fd8
movq (%rax), %rdi
callq 0x2230
movq %rax, %rdi
callq 0x2e00
movq %rax, -0x50(%rbp)
jmp 0x6cc6
cmpl $0x32, -0x14(%rbp)
jne 0x6cc4
movl $0x1, -0x18(%rbp)
jmp 0x6cc6
jmp 0x6c79
movq 0xa301(%rip), %rax      # 0x10fd0
movl (%rax), %eax
addl $0x2, %eax
cmpl -0x8(%rbp), %eax
jle 0x6d45
movq 0xa318(%rip), %rax      # 0x10ff8
movq (%rax), %rdi
leaq 0x6a4d(%rip), %rsi      # 0xd737
movb $0x0, %al
callq 0x2190
movq 0xa300(%rip), %rax      # 0x10ff8
movq (%rax), %rdi
leaq 0x6ef6(%rip), %rsi      # 0xdbf8
movb $0x0, %al
callq 0x2190
movq 0xa2e8(%rip), %rax      # 0x10ff8
movq (%rax), %rdi
leaq 0x6f20(%rip), %rsi      # 0xdc3a
movb $0x0, %al
callq 0x2190
movq 0xa2d0(%rip), %rax      # 0x10ff8
movq (%rax), %rdi
leaq 0x6f2d(%rip), %rsi      # 0xdc5f
movb $0x0, %al
callq 0x2190
movl $0x1, -0x4(%rbp)
jmp 0x73c4
movq -0x10(%rbp), %rax
movq 0xa280(%rip), %rcx      # 0x10fd0
movl (%rcx), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
callq 0x20a0
movsd %xmm0, -0x38(%rbp)
movsd -0x38(%rbp), %xmm0
movsd 0x62ad(%rip), %xmm1     # 0xd020
ucomisd %xmm1, %xmm0
jb 0x6dbe
movsd -0x38(%rbp), %xmm0
movsd 0x62a2(%rip), %xmm1     # 0xd028
addsd %xmm1, %xmm0
movaps %xmm0, %xmm1
cvttsd2si %xmm1, %rax
movq %rax, %rdx
sarq $0x3f, %rdx
movsd 0x628f(%rip), %xmm1     # 0xd030
subsd %xmm1, %xmm0
cvttsd2si %xmm0, %rcx
andq %rdx, %rcx
orq %rcx, %rax
movq %rax, -0x28(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x38(%rbp)
jmp 0x6dec
cmpl $0x0, -0x18(%rbp)
je 0x6dea
movq 0xa22d(%rip), %rax      # 0x10ff8
movq (%rax), %rdi
leaq 0x6eda(%rip), %rsi      # 0xdcaf
leaq 0x6f0b(%rip), %rdx      # 0xdce7
movb $0x0, %al
callq 0x2190
movl $0x0, -0x18(%rbp)
jmp 0x6dec
cmpq $0x0, -0x50(%rbp)
jne 0x6e01
movl $0xb, %edi
callq 0x2e00
movq %rax, -0x50(%rbp)
cmpl $0x0, -0x18(%rbp)
jne 0x70d0
movq $0x0, -0x58(%rbp)
cmpq $0x0, -0x28(%rbp)
jbe 0x6e2c
movq -0x28(%rbp), %rdi
movl $0x70, %esi
callq 0x2160
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x28(%rbp)
jbe 0x6e72
cmpq $0x0, -0x58(%rbp)
jne 0x6e72
movq 0xa1b7(%rip), %rax      # 0x10ff8
movq (%rax), %rdi
movq -0x28(%rbp), %rcx
leaq 0x6ea3(%rip), %rsi      # 0xdcf2
leaq 0x6e91(%rip), %rdx      # 0xdce7
movb $0x0, %al
callq 0x2190
movq -0x50(%rbp), %rdi
callq 0x2040
movl $0x1, -0x4(%rbp)
jmp 0x73c4
movq -0x10(%rbp), %rax
movq 0xa153(%rip), %rcx      # 0x10fd0
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x760b(%rip), %rsi      # 0xe496
callq 0x2170
cmpl $0x0, %eax
je 0x6ebc
movq -0x10(%rbp), %rax
movq 0xa130(%rip), %rcx      # 0x10fd0
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x63d2(%rip), %rsi      # 0xd280
callq 0x2220
movq %rax, -0xa0(%rbp)
jmp 0x6ee0
movq 0xa105(%rip), %rax      # 0x10fc8
movq (%rax), %rdi
callq 0x21b0
movl %eax, %edi
leaq 0x63ac(%rip), %rsi      # 0xd280
callq 0x20b0
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x6f1d
movq 0xa0ff(%rip), %rax      # 0x10ff8
movq (%rax), %rdi
leaq 0x6576(%rip), %rsi      # 0xd479
leaq 0x6ddd(%rip), %rdx      # 0xdce7
movb $0x0, %al
callq 0x2190
movl $0x1, -0x4(%rbp)
jmp 0x73c4
movq -0x40(%rbp), %rdi
callq 0x38f0
movq %rax, -0x48(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x48(%rbp), %rdi
callq 0x3930
cmpl $0x0, %eax
jl 0x704b
movq -0x50(%rbp), %rdi
callq 0x2ee0
shrq $0xb, %rax
cvtsi2sd %rax, %xmm0
movsd 0x60da(%rip), %xmm1     # 0xd038
mulsd %xmm1, %xmm0
movsd %xmm0, -0x60(%rbp)
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x7026
movq -0x30(%rbp), %rax
subq $0x1, %rax
cmpq -0x28(%rbp), %rax
jae 0x6f9d
movq -0x30(%rbp), %rax
subq $0x1, %rax
movq %rax, -0xa8(%rbp)
jmp 0x6ffa
movsd -0x60(%rbp), %xmm0
movaps 0x62b7(%rip), %xmm2     # 0xd260
movq -0x30(%rbp), %xmm1
punpckldq %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
movapd 0x62b6(%rip), %xmm2     # 0xd270
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
movaps %xmm0, %xmm1
cvttsd2si %xmm1, %rax
movq %rax, %rdx
sarq $0x3f, %rdx
movsd 0x604c(%rip), %xmm1     # 0xd030
subsd %xmm1, %xmm0
cvttsd2si %xmm0, %rcx
andq %rdx, %rcx
orq %rcx, %rax
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x28(%rbp), %rax
jae 0x7024
movq -0x58(%rbp), %rdi
imulq $0x70, -0x68(%rbp), %rax
addq %rax, %rdi
movq -0x48(%rbp), %rsi
callq 0x73d0
jmp 0x7046
movsd -0x60(%rbp), %xmm1
movsd -0x38(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x7044
movq -0x48(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x7440
jmp 0x7046
jmp 0x6f32
movq $0x0, -0x20(%rbp)
movq -0x20(%rbp), %rax
cmpq -0x28(%rbp), %rax
jae 0x70bb
movq -0x58(%rbp), %rax
imulq $0x70, -0x20(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
cmpq $0x0, 0x30(%rax)
je 0x7086
movq -0x70(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x7440
movq -0x70(%rbp), %rax
movq 0x40(%rax), %rdi
callq 0x2040
movq -0x70(%rbp), %rax
movq 0x58(%rax), %rdi
callq 0x2040
movq -0x70(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x2040
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x7053
cmpq $0x0, -0x58(%rbp)
je 0x70cb
movq -0x58(%rbp), %rdi
callq 0x2040
jmp 0x73a2
movq -0x10(%rbp), %rax
movq 0x9ef5(%rip), %rcx      # 0x10fd0
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x73ad(%rip), %rsi      # 0xe496
callq 0x2170
cmpl $0x0, %eax
jne 0x7127
movq 0x9efe(%rip), %rax      # 0x10ff8
movq (%rax), %rdi
leaq 0x6c36(%rip), %rsi      # 0xdd3a
leaq 0x6bdc(%rip), %rdx      # 0xdce7
movb $0x0, %al
callq 0x2190
movq -0x50(%rbp), %rdi
callq 0x2040
movl $0x1, -0x4(%rbp)
jmp 0x73c4
movq -0x28(%rbp), %rdi
shlq $0x3, %rdi
callq 0x21d0
movq %rax, -0x78(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x20(%rbp), %rax
cmpq -0x28(%rbp), %rax
jae 0x7168
movq -0x78(%rbp), %rax
movq -0x20(%rbp), %rcx
movq $-0x1, (%rax,%rcx,8)
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x7140
movq -0x10(%rbp), %rax
movq 0x9e5d(%rip), %rcx      # 0x10fd0
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x60ff(%rip), %rsi      # 0xd280
callq 0x2220
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x71bc
movq 0x9e60(%rip), %rax      # 0x10ff8
movq (%rax), %rdi
leaq 0x62d7(%rip), %rsi      # 0xd479
leaq 0x6b3e(%rip), %rdx      # 0xdce7
movb $0x0, %al
callq 0x2190
movl $0x1, -0x4(%rbp)
jmp 0x73c4
movq -0x40(%rbp), %rdi
callq 0x38f0
movq %rax, -0x48(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x48(%rbp), %rdi
callq 0x3930
cmpl $0x0, %eax
jl 0x72c7
movq -0x50(%rbp), %rdi
callq 0x2ee0
shrq $0xb, %rax
cvtsi2sd %rax, %xmm0
movsd 0x5e3b(%rip), %xmm1     # 0xd038
mulsd %xmm1, %xmm0
movsd %xmm0, -0x90(%rbp)
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
subq $0x1, %rax
cmpq -0x28(%rbp), %rax
jae 0x7234
movq -0x30(%rbp), %rax
subq $0x1, %rax
movq %rax, -0xb0(%rbp)
jmp 0x7294
movsd -0x90(%rbp), %xmm0
movaps 0x601d(%rip), %xmm2     # 0xd260
movq -0x30(%rbp), %xmm1
punpckldq %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
movapd 0x601c(%rip), %xmm2     # 0xd270
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
movaps %xmm0, %xmm1
cvttsd2si %xmm1, %rax
movq %rax, %rdx
sarq $0x3f, %rdx
movsd 0x5db2(%rip), %xmm1     # 0xd030
subsd %xmm1, %xmm0
cvttsd2si %xmm0, %rcx
andq %rdx, %rcx
orq %rcx, %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
cmpq -0x28(%rbp), %rax
jae 0x72c2
movq -0x30(%rbp), %rdx
movq -0x78(%rbp), %rax
movq -0x98(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
jmp 0x71d1
movq -0x48(%rbp), %rdi
callq 0x3d40
movq -0x40(%rbp), %rdi
callq 0x2030
callq 0xbcf0
movq %rax, -0x80(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x20(%rbp), %rax
cmpq -0x28(%rbp), %rax
jae 0x731e
movq -0x80(%rbp), %rdi
movq -0x78(%rbp), %rax
movq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
leaq -0x84(%rbp), %rdx
callq 0x7470
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x72ea
movq -0x78(%rbp), %rdi
callq 0x2040
movq -0x10(%rbp), %rax
movq 0x9c9e(%rip), %rcx      # 0x10fd0
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x5f40(%rip), %rsi      # 0xd280
callq 0x2220
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
callq 0x38f0
movq %rax, -0x48(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x48(%rbp), %rdi
callq 0x3930
cmpl $0x0, %eax
jl 0x7399
movq -0x80(%rbp), %rdi
movq -0x30(%rbp), %rsi
addq $0x1, %rsi
movq %rsi, -0x30(%rbp)
callq 0x77a0
movq -0x80(%rbp), %rcx
cmpl (%rcx), %eax
je 0x7397
movq -0x48(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x7440
jmp 0x735e
movq -0x80(%rbp), %rdi
callq 0x7900
movq -0x48(%rbp), %rdi
callq 0x3d40
movq -0x40(%rbp), %rdi
callq 0x2030
movq -0x50(%rbp), %rdi
callq 0x2040
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xb0, %rsp
popq %rbp
retq