// File: BenLangmead[P]bowtie/sam.cpp
// Function: SAMHitSink::reportMaxed(EList<Hit, 128>&, unsigned long, PatternSourcePerThread&)
void SAMHitSink::reportMaxed(
	EList<Hit>& hs,
	size_t threadId,
	PatternSourcePerThread& p)
{
	HitSink::reportMaxed(hs, threadId, p);
	if(sampleMax_) {
		RandomSource rand;
		rand.init(p.bufa().seed);
		assert_gt(hs.size(), 0);
		bool paired = hs.front().mate > 0;
		size_t num = 1;
		if(paired) {
			num = 0;
			int bestStratum = 999;
			for(size_t i = 0; i < hs.size()-1; i += 2) {
				int strat = min(hs[i].stratum, hs[i+1].stratum);
				if(strat < bestStratum) {
					bestStratum = strat;
					num = 1;
				} else if(strat == bestStratum) {
					num++;
				}
			}
			assert_leq(num, hs.size());
			uint32_t r = rand.nextU32() % num;
			num = 0;
			for(size_t i = 0; i < hs.size()-1; i += 2) {
				int strat = min(hs[i].stratum, hs[i+1].stratum);
				if(strat == bestStratum) {
					if(num == r) {
						reportHits(NULL, &hs, i, i+2, threadId, 0, (int)(hs.size()/2)+1, true, p);
						break;
					}
					num++;
				}
			}
			assert_eq(num, r);
		} else {
			for(size_t i = 1; i < hs.size(); i++) {
				assert_geq(hs[i].stratum, hs[i-1].stratum);
				if(hs[i].stratum == hs[i-1].stratum) num++;
				else break;
			}
			assert_leq(num, hs.size());
			uint32_t r = rand.nextU32() % num;
			reportHits(&hs[r], NULL, 0, 1, threadId, 0, (int)hs.size()+1, true, p);
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x88(%rbp)
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x28(%rbp), %rcx
callq 0x2d780
movq -0x88(%rbp), %rax
testb $0x1, 0x169(%rax)
je 0x860e9
leaq -0x3c(%rbp), %rdi
callq 0x3e790
movq -0x28(%rbp), %rdi
callq 0x37f00
movl 0x18c(%rax), %esi
leaq -0x3c(%rbp), %rdi
callq 0x403d0
movq -0x18(%rbp), %rdi
callq 0x86470
movzbl 0x188(%rax), %eax
cmpl $0x0, %eax
setg %al
andb $0x1, %al
movb %al, -0x3d(%rbp)
movq $0x1, -0x48(%rbp)
testb $0x1, -0x3d(%rbp)
je 0x85fcd
movq $0x0, -0x48(%rbp)
movl $0x3e7, -0x4c(%rbp)     # imm = 0x3E7
movq $0x0, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x18(%rbp), %rdi
callq 0x37e60
movq %rax, %rcx
movq -0x90(%rbp), %rax
subq $0x1, %rcx
cmpq %rcx, %rax
jae 0x85e64
movq -0x18(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x2ecd0
addq $0x180, %rax            # imm = 0x180
movq %rax, -0x98(%rbp)
movq -0x18(%rbp), %rdi
movq -0x58(%rbp), %rsi
addq $0x1, %rsi
callq 0x2ecd0
movq -0x98(%rbp), %rdi
movq %rax, %rsi
addq $0x180, %rsi            # imm = 0x180
callq 0x86490
movsbl (%rax), %eax
movl %eax, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jge 0x85e3b
movl -0x5c(%rbp), %eax
movl %eax, -0x4c(%rbp)
movq $0x1, -0x48(%rbp)
jmp 0x85e51
movl -0x5c(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jne 0x85e4f
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x85e51
jmp 0x85e53
movq -0x58(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x58(%rbp)
jmp 0x85db1
leaq -0x3c(%rbp), %rdi
callq 0x47b20
movl %eax, %eax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x48(%rbp)
movl %edx, %eax
movl %eax, -0x60(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x68(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x18(%rbp), %rdi
callq 0x37e60
movq %rax, %rcx
movq -0xa0(%rbp), %rax
subq $0x1, %rcx
cmpq %rcx, %rax
jae 0x85fc8
movq -0x18(%rbp), %rdi
movq -0x68(%rbp), %rsi
callq 0x2ecd0
addq $0x180, %rax            # imm = 0x180
movq %rax, -0xa8(%rbp)
movq -0x18(%rbp), %rdi
movq -0x68(%rbp), %rsi
addq $0x1, %rsi
callq 0x2ecd0
movq -0xa8(%rbp), %rdi
movq %rax, %rsi
addq $0x180, %rsi            # imm = 0x180
callq 0x86490
movsbl (%rax), %eax
movl %eax, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jne 0x85fb5
movq -0x48(%rbp), %rax
movl -0x60(%rbp), %ecx
cmpq %rcx, %rax
jne 0x85fa9
movq -0x18(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0x68(%rbp), %rax
addq $0x2, %rax
movq %rax, -0xb8(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x18(%rbp), %rdi
callq 0x37e60
movq -0x88(%rbp), %rdi
movq -0xc8(%rbp), %rdx
movq -0xc0(%rbp), %rcx
movq -0xb8(%rbp), %r8
movq -0xb0(%rbp), %r9
shrq %rax
movl %eax, %r11d
addl $0x1, %r11d
movq -0x28(%rbp), %r10
movq (%rdi), %rax
xorl %esi, %esi
xorl %ebx, %ebx
movl $0x0, (%rsp)
movl %r11d, 0x8(%rsp)
movl $0x1, 0x10(%rsp)
movq %r10, 0x18(%rsp)
callq *0x18(%rax)
jmp 0x85fc8
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x85fb7
movq -0x68(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x68(%rbp)
jmp 0x85e8c
jmp 0x860e7
movq $0x1, -0x78(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0x18(%rbp), %rdi
callq 0x37e60
movq %rax, %rcx
movq -0xd0(%rbp), %rax
cmpq %rcx, %rax
jae 0x8605a
movq -0x18(%rbp), %rdi
movq -0x78(%rbp), %rsi
callq 0x2ecd0
movsbl 0x180(%rax), %eax
movl %eax, -0xd4(%rbp)
movq -0x18(%rbp), %rdi
movq -0x78(%rbp), %rsi
subq $0x1, %rsi
callq 0x2ecd0
movq %rax, %rcx
movl -0xd4(%rbp), %eax
movsbl 0x180(%rcx), %ecx
cmpl %ecx, %eax
jne 0x86045
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x86047
jmp 0x8605a
jmp 0x86049
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x85fd5
leaq -0x3c(%rbp), %rdi
callq 0x47b20
movl %eax, %eax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x48(%rbp)
movl %edx, %eax
movl %eax, -0x7c(%rbp)
movq -0x18(%rbp), %rdi
movl -0x7c(%rbp), %eax
movl %eax, %esi
callq 0x2ecd0
movq %rax, -0xe8(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0x18(%rbp), %rdi
callq 0x37e60
movq -0x88(%rbp), %rdi
movq -0xe8(%rbp), %rsi
movq -0xe0(%rbp), %r9
movl %eax, %r11d
addl $0x1, %r11d
movq -0x28(%rbp), %r10
movq (%rdi), %rax
xorl %ecx, %ecx
movl $0x1, %r8d
xorl %edx, %edx
movq %rcx, %rdx
movl $0x0, (%rsp)
movl %r11d, 0x8(%rsp)
movl $0x1, 0x10(%rsp)
movq %r10, 0x18(%rsp)
callq *0x18(%rax)
jmp 0x860e9
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %rbp
retq
nopw %cs:(%rax,%rax)