// File: /workspace/github_cpp_makefile_star2_O2/ShootMe[P]Klondike-Solver/Solitaire.cpp
// Function: Solitaire::GetTalonCards(Card*, int*)
int Solitaire::GetTalonCards(Card talon[], int talonMoves[]) {
	int index = 0;

	//Check waste
	Pile & waste = piles[WASTE];
	int wasteSize = waste.Size();
	if (wasteSize > 0) {
		talon[index] = waste.Low();
		talonMoves[index++] = 0;
	}

	//Check cards waiting to be turned over from stock
	Pile & stock = piles[STOCK];
	int stockSize = stock.Size();
	for (int j = (stockSize > 0 && stockSize - drawCount <= 0) ? 0 : stockSize - drawCount; j >= 0; j -= drawCount) {
		talon[index] = stock.Up(j);
		talonMoves[index++] = stockSize - j;

		if (j > 0 && j < drawCount) { j = drawCount; }
	}

	//Check cards already turned over in the waste, meaning we have to "redeal" the deck to get to it
	int amountToDraw = stockSize;
	amountToDraw += stockSize;
	amountToDraw += wasteSize;
	amountToDraw++;
	wasteSize--;

	int lastIndex = drawCount - 1;
	while (lastIndex < wasteSize) {
		talon[index] = waste.Up(lastIndex);
		talonMoves[index++] = amountToDraw + lastIndex;
		lastIndex += drawCount;
	}

	//Check cards in stock after a "redeal". Only happens when draw count > 1 and you have access to more cards in the talon
	if (lastIndex > wasteSize && wasteSize > -1) {
		amountToDraw += wasteSize;
		amountToDraw += stockSize;
		for (int j = (stockSize > 0 && stockSize - lastIndex + wasteSize <= 0) ? 0 : stockSize - lastIndex + wasteSize; j > 0; j -= drawCount) {
			talon[index] = stock.Up(j);
			talonMoves[index++] = amountToDraw - j;
		}
	}

	return index;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdx, %rbp
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x800(%rdi), %r15
movq %r15, %rdi
callq 0x3874
xorl %r13d, %r13d
movq %rax, 0x18(%rsp)
testl %eax, %eax
movq %r15, 0x30(%rsp)
jle 0x647f
movq %r15, %rdi
callq 0x38e0
movl %eax, (%rbx)
shrq $0x20, %rax
movw %ax, 0x4(%rbx)
andl $0x0, (%rbp)
pushq $0x1
popq %r12
jmp 0x6482
xorl %r12d, %r12d
movq %rbp, %r15
leaq 0x1160(%r14), %rdi
movq %rdi, 0x28(%rsp)
callq 0x3874
movl 0x1904(%r14), %ebp
movl %eax, %ecx
subl %ebp, %ecx
cmovgl %ecx, %r13d
movq %rax, 0x8(%rsp)
testl %eax, %eax
cmovlel %ecx, %r13d
leaq (%r15,%r12,4), %r15
imulq $0x6, %r12, %rax
addq %rax, %rbx
testl %r13d, %r13d
js 0x650b
movq 0x28(%rsp), %rdi
movl %r13d, %esi
callq 0x38c2
movl %eax, (%rbx)
shrq $0x20, %rax
movw %ax, 0x4(%rbx)
movq 0x8(%rsp), %rax
subl %r13d, %eax
incq %r12
movl %eax, (%r15)
movl 0x1904(%r14), %ebp
cmpl %ebp, %r13d
movl %ebp, %eax
cmovgl %r13d, %eax
testl %r13d, %r13d
cmovel %r13d, %eax
subl %ebp, %eax
addq $0x4, %r15
addq $0x6, %rbx
movl %eax, %r13d
jmp 0x64bb
movq %r14, 0x20(%rsp)
movq 0x18(%rsp), %rax
movq 0x8(%rsp), %rcx
leal (%rax,%rcx,2), %edx
movl %edx, 0x14(%rsp)
leal (%rax,%rcx,2), %r14d
incl %r14d
leal -0x1(%rax), %r13d
decl %ebp
cmpl %r13d, %ebp
jge 0x6568
movq 0x30(%rsp), %rdi
movl %ebp, %esi
callq 0x38c2
movl %eax, (%rbx)
shrq $0x20, %rax
movw %ax, 0x4(%rbx)
leal (%r14,%rbp), %eax
incq %r12
movl %eax, (%r15)
movq 0x20(%rsp), %rax
addl 0x1904(%rax), %ebp
addq $0x4, %r15
addq $0x6, %rbx
jmp 0x652e
movq 0x18(%rsp), %rax
testl %eax, %eax
jle 0x65d3
cmpl %eax, %ebp
jl 0x65d3
movq 0x8(%rsp), %rcx
addl %ecx, %r13d
subl %ebp, %r13d
xorl %ebp, %ebp
testl %r13d, %r13d
cmovgl %r13d, %ebp
testl %ecx, %ecx
cmovlel %r13d, %ebp
addl %ecx, %eax
addl 0x14(%rsp), %eax
movq 0x20(%rsp), %r14
movq %rax, %r13
testl %ebp, %ebp
jle 0x65d3
movq 0x28(%rsp), %rdi
movl %ebp, %esi
callq 0x38c2
movl %eax, (%rbx)
shrq $0x20, %rax
movw %ax, 0x4(%rbx)
movl %r13d, %eax
subl %ebp, %eax
incq %r12
movl %eax, (%r15)
subl 0x1904(%r14), %ebp
addq $0x4, %r15
addq $0x6, %rbx
jmp 0x659d
movl %r12d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop