// File: /workspace/github_c_makefile_star2_O2/olcaytaner[P]Isell/ordering.c
// Function: min_table_ordering
void min_table_ordering(bayesiangraphptr mygraph, double **edges, int edgecount)
{
	/*!Last Changed 02.02.2004 added safemalloc*/
 /*!Last Changed 13.03.2001*/
 int i, j, k, orderno, minindex;
 double *table_counts, min;
	table_counts = safemalloc(mygraph->nodecount * sizeof(double), "min_table_ordering", 3);
 if (mygraph->ordering)
   safe_free(mygraph->ordering);
 mygraph->ordering = safemalloc(mygraph->nodecount * sizeof(int), "min_table_ordering", 6);
 /* Step 1 */
 for (i = 0; i < mygraph->nodecount; i++)
   table_counts[i] = mygraph->nodes[i].value_count;
 for (i = 0; i < edgecount; i++)
  {
   j = (int) edges[i][0];
   k = (int) edges[i][1];
   table_counts[j] *= mygraph->nodes[k].value_count;
   table_counts[k] *= mygraph->nodes[j].value_count;
  }
 /* Step 2 */
 orderno = mygraph->nodecount - 1;
 while (orderno >= 0)
  {
   /* Step 3 */
   min = LONG_MAX;
   for (i = 0; i < mygraph->nodecount; i++)
     if (table_counts[i] < min)
      {
       min = table_counts[i];
       minindex = i;
      }
   /* Step 4 */
   mygraph->ordering[minindex] = orderno;
   table_counts[minindex] = LONG_MAX;
   /* Step 5 */
   for (i = 0; i < edgecount; i++)
    {
     j = (int) edges[i][0];
     k = (int) edges[i][1];
     if (j == minindex)
       table_counts[k] /= mygraph->nodes[j].value_count;
     else
       if (k == minindex)
         table_counts[j] /= mygraph->nodes[k].value_count;
    }
   /* Step 6 */
   orderno--;
  }
	safe_free(table_counts);
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
pushq %rax
movl %edx, %ebx
movq %rsi, %r14
movq %rdi, %r15
movslq 0x10(%rdi), %rdi
shlq $0x3, %rdi
leaq 0x4dfff(%rip), %rsi     # 0x9aa20
pushq $0x3
popq %rdx
callq 0x34f59
movq %rax, %r12
movq 0x40(%r15), %rdi
testq %rdi, %rdi
je 0x4ca3a
callq 0x34ff7
movslq 0x10(%r15), %rdi
shlq $0x2, %rdi
leaq 0x4dfd7(%rip), %rsi     # 0x9aa20
pushq $0x6
popq %rdx
callq 0x34f59
movq %rax, 0x40(%r15)
movl 0x10(%r15), %ecx
xorl %edx, %edx
testl %ecx, %ecx
movl $0x0, %esi
cmovgl %ecx, %esi
leaq (%rsi,%rsi,8), %rsi
movq %r12, %rdi
cmpq %rdx, %rsi
je 0x4ca8a
movq 0x8(%r15), %r8
cvtsi2sdl 0x10(%r8,%rdx,8), %xmm0
movsd %xmm0, (%rdi)
addq $0x9, %rdx
addq $0x8, %rdi
jmp 0x4ca6c
xorl %edx, %edx
testl %ebx, %ebx
cmovlel %edx, %ebx
cmpq %rdx, %rbx
je 0x4caed
movq (%r14,%rdx,8), %rsi
movq 0x8(%r15), %rdi
movupd (%rsi), %xmm0
cvttpd2dq %xmm0, %xmm0
pshufd $0x55, %xmm0, %xmm1     # xmm1 = xmm0[1,1,1,1]
movd %xmm1, %esi
movslq %esi, %rsi
imulq $0x48, %rsi, %r8
cvtsi2sdl 0x10(%rdi,%r8), %xmm1
movd %xmm0, %r8d
movslq %r8d, %r8
mulsd (%r12,%r8,8), %xmm1
movsd %xmm1, (%r12,%r8,8)
imulq $0x48, %r8, %r8
cvtsi2sdl 0x10(%rdi,%r8), %xmm0
mulsd (%r12,%rsi,8), %xmm0
movsd %xmm0, (%r12,%rsi,8)
incq %rdx
jmp 0x4ca91
xorl %edx, %edx
movsd 0x49839(%rip), %xmm0    # 0x96330
movabsq $0x43e0000000000000, %rsi # imm = 0x43E0000000000000
testl %ecx, %ecx
jle 0x4cb95
decl %ecx
movl 0x10(%r15), %r8d
testl %r8d, %r8d
cmovlel %edx, %r8d
xorl %r9d, %r9d
movapd %xmm0, %xmm1
cmpq %r9, %r8
je 0x4cb3d
movsd (%r12,%r9,8), %xmm2
ucomisd %xmm2, %xmm1
cmoval %r9d, %edi
minsd %xmm1, %xmm2
incq %r9
movapd %xmm2, %xmm1
jmp 0x4cb1d
movslq %edi, %r8
movl %ecx, (%rax,%r8,4)
movq %rsi, (%r12,%r8,8)
imulq $0x48, %r8, %r8
xorl %r9d, %r9d
cmpq %r9, %rbx
je 0x4cb01
movq (%r14,%r9,8), %r11
cvttsd2si (%r11), %r10d
cvttsd2si 0x8(%r11), %r11d
cmpl %r10d, %edi
jne 0x4cb6d
movl %r11d, %r10d
jmp 0x4cb72
cmpl %r11d, %edi
jne 0x4cb90
movq 0x8(%r15), %r11
cvtsi2sdl 0x10(%r11,%r8), %xmm1
movslq %r10d, %r10
movsd (%r12,%r10,8), %xmm2
divsd %xmm1, %xmm2
movsd %xmm2, (%r12,%r10,8)
incq %r9
jmp 0x4cb4f
movq %r12, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
jmp 0x34ff7