// File: /workspace/github_c_makefile_star2_O2/hansvanstaveren[P]BigDeal/binomial.c
// Function: code_to_hand
void
code_to_hand(dl_num *dnp, dl_int *dip)
/*
 * This function converts a given number to its associated bridge-deal.
 * Read the file Godel.pdf for an explanation of the code. The
 * variable-names used in the comments refer to the variable-names
 * that were used in the pseudo-code in Godel.pdf.
 */
{
	int i,j,a,b;
	byte c[L];
	byte g[L];
	byte tmp1[L],tmp2[L],tmp3[L];
	int compass;

	/*
	 * set g_{-1} = g
	 */
	mp96_assign(g, dnp->dn_num);

	for (compass=COMPASS_NORTH; compass<NCOMPASS; compass++) {
		/*
		 * Computation of an appropriate constant for the given
		 * compass direction. The constant is equal to:
		 * 39_over_13 x 26_over_13   for North,
		 * 26_over_13                for East,
		 * 1                         for South.
		 *
		 * Code continues for West who automatically gets 13 zeroes
		 */
		mp96_one(c);
		for (i=2; i<=COMPASS_WEST-compass; i++) {
			n_over_k(i*13, 13, tmp1);
			mp96_mul(c, tmp1, c);
		}

		/*
		 * set a_{-1} = 0
		 */
		a = 0;
		for (j=0; j<NCARDSPERHAND; j++) {
			/* 
			 * Computation of a_j, x_j and g_j.
			 * a_j is computed by setting variable b to a_{j-1} and
			 * incrementing it until it reaches the value a_j.
			 */
			n_over_k(13*(NCOMPASS-compass)-a-j, 13-j, tmp1);

			/*
			 * b is set to a_{j-1} and up
			 */
			for(b=a; ; b++) {
				n_over_k(13*(NCOMPASS-compass)-b-1-j, 13-j, tmp2);
				mp96_sub(tmp2, tmp1, tmp2);
				mp96_mul(tmp2, tmp2, c);
				/*
				 * Check if b < a_j, i.e. if tmp2 > g
				 */
				if (mp96_cmp(tmp2, g) > 0)
					break;
			}
			n_over_k(13*(NCOMPASS-compass)-a-j, 13-j, tmp1);
			n_over_k(13*(NCOMPASS-compass)-b-j, 13-j, tmp2);
			mp96_sub(tmp3, tmp1, tmp2);
			/*
			 * tmp1 is assigned the value x_j
			 */
			mp96_mul(tmp1, tmp3, c);
			/*
			 * g_j = g_{j-1} - x_j
			 */
			mp96_sub(g, g, tmp1);
			dip->di_hand[compass][j] = b;
			/*
			 * a is set to a_j
			 */
			a = b;
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rsi, 0x30(%rsp)
movq %rdi, %rsi
leaq 0x48(%rsp), %rdi
callq 0x36c6
pushq $-0x35
popq %rax
movl %eax, 0xc(%rsp)
pushq $0x33
popq %rax
movl %eax, 0x8(%rsp)
xorl %edx, %edx
leaq 0x24(%rsp), %rdi
leaq 0x6c(%rsp), %r13
cmpq $0x4, %rdx
je 0x3aa4
movq %rdx, %rbp
callq 0x36b9
movq %rbp, %rdx
pushq $0x3
popq %r14
subq %rbp, %r14
pushq $0x1a
popq %rbx
pushq $0x2
popq %r15
cmpq %r15, %r14
jb 0x394f
movl %ebx, %edi
pushq $0xd
popq %rsi
leaq 0x54(%rsp), %r12
movq %r12, %rdx
callq 0x3884
leaq 0x24(%rsp), %rdi
movq %r12, %rsi
movq %rdi, %rdx
callq 0x374d
movq %rbp, %rdx
incq %r15
addl $0xd, %ebx
jmp 0x391d
pushq $0x4
popq %rax
subl %edx, %eax
imull $0xd, %eax, %eax
movl %eax, 0x14(%rsp)
xorl %ecx, %ecx
movl 0x8(%rsp), %r14d
movl 0xc(%rsp), %r12d
xorl %r15d, %r15d
leaq 0x54(%rsp), %rbx
movq %rdx, 0x38(%rsp)
cmpq $0xd, %rcx
je 0x3a8d
movl 0x14(%rsp), %edi
subl %r15d, %edi
subl %ecx, %edi
pushq $0xd
popq %rsi
movq %rcx, 0x40(%rsp)
subl %ecx, %esi
movl %edi, 0x18(%rsp)
movl %esi, 0x10(%rsp)
movq %rbx, %rdx
callq 0x3884
leal -0x1(%r15), %eax
movq %rbx, %rbp
movl %r14d, 0x20(%rsp)
subl %r15d, %r14d
movl %r12d, 0x1c(%rsp)
addl %r12d, %r15d
movl %r15d, %ebx
movl %eax, %r15d
leaq 0x24(%rsp), %r12
movl %r14d, %edi
movl 0x10(%rsp), %esi
movq %r13, %rdx
callq 0x3884
movq %r13, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x3707
movq %r13, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x374d
movq %r13, %rdi
leaq 0x48(%rsp), %rsi
callq 0x3732
incl %r15d
incl %ebx
decl %r14d
testl %eax, %eax
jle 0x39c3
movl 0x18(%rsp), %edi
movl 0x10(%rsp), %r14d
movl %r14d, %esi
movq %rbp, %rdx
callq 0x3884
negl %ebx
movl %ebx, %edi
movl %r14d, %esi
movq %r13, %rdx
callq 0x3884
leaq 0x60(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x3707
movq %rbp, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x374d
leaq 0x48(%rsp), %rdi
movq %rdi, %rsi
movq %rbp, %rdx
callq 0x3707
movq 0x38(%rsp), %rdx
imulq $0xd, %rdx, %rax
addq 0x30(%rsp), %rax
movq 0x40(%rsp), %rcx
movb %r15b, (%rcx,%rax)
incq %rcx
movl 0x1c(%rsp), %r12d
incl %r12d
movl 0x20(%rsp), %r14d
decl %r14d
movq %rbp, %rbx
jmp 0x3974
incq %rdx
addl $0xd, 0xc(%rsp)
addl $-0xd, 0x8(%rsp)
leaq 0x24(%rsp), %rdi
jmp 0x38fa
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop