// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x38(%rbp), %rdi
movq -0x8(%rbp), %rsi
callq 0x55a0
movl $0x0, -0x60(%rbp)
cmpl $0x4, -0x60(%rbp)
jge 0x5b69
leaq -0x2c(%rbp), %rdi
callq 0x5570
movl $0x2, -0x14(%rbp)
movl -0x14(%rbp), %eax
movl $0x3, %ecx
subl -0x60(%rbp), %ecx
cmpl %ecx, %eax
jg 0x5a0b
imull $0xd, -0x14(%rbp), %edi
leaq -0x44(%rbp), %rdx
movl $0xd, %esi
callq 0x5930
leaq -0x2c(%rbp), %rdi
leaq -0x44(%rbp), %rsi
leaq -0x2c(%rbp), %rdx
callq 0x5730
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x59ce
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x18(%rbp)
cmpl $0xd, -0x18(%rbp)
jge 0x5b59
movl $0x4, %eax
subl -0x60(%rbp), %eax
imull $0xd, %eax, %edi
subl -0x1c(%rbp), %edi
subl -0x18(%rbp), %edi
movl $0xd, %esi
subl -0x18(%rbp), %esi
leaq -0x44(%rbp), %rdx
callq 0x5930
movl -0x1c(%rbp), %eax
movl %eax, -0x20(%rbp)
movl $0x4, %eax
subl -0x60(%rbp), %eax
imull $0xd, %eax, %edi
subl -0x20(%rbp), %edi
subl $0x1, %edi
subl -0x18(%rbp), %edi
movl $0xd, %esi
subl -0x18(%rbp), %esi
leaq -0x50(%rbp), %rdx
callq 0x5930
leaq -0x50(%rbp), %rdi
leaq -0x44(%rbp), %rsi
leaq -0x50(%rbp), %rdx
callq 0x5650
leaq -0x50(%rbp), %rdi
leaq -0x50(%rbp), %rsi
leaq -0x2c(%rbp), %rdx
callq 0x5730
leaq -0x50(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x56d0
cmpl $0x0, %eax
jle 0x5aa6
jmp 0x5ab3
jmp 0x5aa8
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x5a4b
movl $0x4, %eax
subl -0x60(%rbp), %eax
imull $0xd, %eax, %edi
subl -0x1c(%rbp), %edi
subl -0x18(%rbp), %edi
movl $0xd, %esi
subl -0x18(%rbp), %esi
leaq -0x44(%rbp), %rdx
callq 0x5930
movl $0x4, %eax
subl -0x60(%rbp), %eax
imull $0xd, %eax, %edi
subl -0x20(%rbp), %edi
subl -0x18(%rbp), %edi
movl $0xd, %esi
subl -0x18(%rbp), %esi
leaq -0x50(%rbp), %rdx
callq 0x5930
leaq -0x5c(%rbp), %rdi
leaq -0x44(%rbp), %rsi
leaq -0x50(%rbp), %rdx
callq 0x5650
leaq -0x44(%rbp), %rdi
leaq -0x5c(%rbp), %rsi
leaq -0x2c(%rbp), %rdx
callq 0x5730
leaq -0x38(%rbp), %rdi
leaq -0x38(%rbp), %rsi
leaq -0x44(%rbp), %rdx
callq 0x5650
movl -0x20(%rbp), %eax
movb %al, %dl
movq -0x10(%rbp), %rax
movslq -0x60(%rbp), %rcx
imulq $0xd, %rcx, %rcx
addq %rcx, %rax
movslq -0x18(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x20(%rbp), %eax
movl %eax, -0x1c(%rbp)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x5a19
jmp 0x5b5b
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x59b4
addq $0x60, %rsp
popq %rbp
retq
nop