// File: /workspace/github_c_makefile_star2_O1/waynebhayes[P]SAHap/src/Genome.cpp
// Function: SAHap::Genome::move()
void Genome::move() {
	// Perform a random move, saving enough information so we can revert later

	auto ploidy = this->haplotypes.size();
	size_t moveFrom = rand() % ploidy;
	size_t moveTo;
	// FIXME1: why are we picking the source haplotype FIRST when it's possible there's no
	// reads in it? This while() loop checks for this condition and keeps looping until it finds a haplotype
	// with reads in it... but it's probably better to pick a READ from the entire universe first, the FIND
	// it's haploptype, then choose ANOTHER haplotype to move it to.
	// In any case the current code *should* work... but it's ugly.
	// It's also bad in a WINDOWING system because if the read we pick is one we shouldn't touch, we have to
	// come back all the way to here to start again... which means TWO nested while loops. Very bad.
	// FIXME1: so, pick the read FIRST.
	unsigned numTries = 0;
	while (!this->haplotypes[moveFrom].numReads()) { // chose moveFrom (keep choosing until we find a non-empty haplotype)
		if (ploidy == 2) {
			moveFrom = !moveFrom;
		} else {
			moveFrom = rand() % ploidy;
		}
		assert(numTries++ < 10*ploidy); // this should be MORE than enough to NEVER iterate forever
	}
	// now choose where to move *to* (someplace other than from)
	if (ploidy == 2) {
		moveTo = !moveFrom;
	} else {
		size_t moveOffset = rand() % (ploidy - 1);
		moveTo = (moveFrom + moveOffset + 1) % ploidy;
	}
	assert(moveFrom != moveTo);
	// Move this "pick the read" up above FIXME1, but choose among the entire universe of reads, not just those on
	// "moveFrom". Instead, pick the read, then set moveFrom to it's current haplotype, then choose moveTo as above.
	Read * r = this->haplotypes[moveFrom].randomRead(this->randomEngine);
	// HERE is where you put your code to check if this read shouldn't be touched because it has substantial
	// overlap with the previous window. (And you can make that decision even before looking at it's haplotype.

#if SAHAP_GENOME_DEBUG
	printf("move read %p from %d to %d\n", r, moveFrom, moveTo);
	if (!r) {
		cerr << "DEBUG: Haplotype " << moveFrom << " has no reads remaining" << endl;
		std::raise(SIGINT);
		r = this->haplotypes[moveFrom].randomRead(this->randomEngine);
	}
#endif

	// std::cout << "From: " << moveFrom << ", To: " << moveTo << std::endl;

	this->haplotypes[moveTo].add(r);
	this->haplotypes[moveFrom].remove(r);

	this->lastMove.from = moveFrom;
	this->lastMove.to = moveTo;
	this->lastMove.read = r;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movq 0x2370(%rdi), %r13
subq 0x2368(%rdi), %r13
movq %r13, %r15
sarq $0x3, %r15
movabsq $-0x1084210842108421, %r14 # imm = 0xEF7BDEF7BDEF7BDF
imulq %r15, %r14
callq 0x2070
cltq
xorl %ebp, %ebp
xorl %edx, %edx
movq %r14, (%rsp)
divq %r14
movq %rdx, %r14
movabsq $0x5ad6b5ad6b5ad6b6, %r12 # imm = 0x5AD6B5AD6B5AD6B6
imulq %r15, %r12
imulq $0xf8, %r14, %r15
movq 0x2368(%rbx), %rdi
addq %r15, %rdi
callq 0x6e46
testq %rax, %rax
jne 0x356b
cmpq $0x1f0, %r13            # imm = 0x1F0
jne 0x3533
xorl %eax, %eax
testq %r14, %r14
sete %al
movq %rax, %r14
jmp 0x3543
callq 0x2070
cltq
xorl %edx, %edx
divq (%rsp)
movq %rdx, %r14
movl %ebp, %eax
incl %ebp
cmpq %rax, %r12
ja 0x3502
leaq 0x7c57(%rip), %rdi      # 0xb1aa
leaq 0x7c09(%rip), %rsi      # 0xb163
leaq 0x7c60(%rip), %rcx      # 0xb1c1
movl $0xe4, %edx
callq 0x2180
cmpq $0x1f0, %r13            # imm = 0x1F0
jne 0x3580
xorl %r12d, %r12d
testq %r14, %r14
sete %r12b
jmp 0x35a3
callq 0x2070
cltq
movq (%rsp), %rsi
leaq -0x1(%rsi), %rcx
xorl %edx, %edx
divq %rcx
leaq (%r14,%rdx), %rax
incq %rax
xorl %edx, %edx
divq %rsi
movq %rdx, %r12
cmpq %r12, %r14
je 0x360d
movq 0x2368(%rbx), %rdi
addq %r15, %rdi
leaq 0x2438(%rbx), %rsi
callq 0x74f8
movq %rax, %r13
imulq $0xf8, %r12, %rdi
addq 0x2368(%rbx), %rdi
movq %rax, %rsi
callq 0x722e
addq 0x2368(%rbx), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x7354
movq %r14, 0x3c18(%rbx)
movq %r12, 0x3c20(%rbx)
movq %r13, 0x3c28(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x7bc8(%rip), %rdi      # 0xb1dc
leaq 0x7b48(%rip), %rsi      # 0xb163
leaq 0x7b9f(%rip), %rcx      # 0xb1c1
movl $0xed, %edx
callq 0x2180