// File: /workspace/github_c_makefile_star2_O1/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 %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %edx, %ebx
movq %rsi, %r14
movq %rdi, %r15
movslq 0x10(%rdi), %rdi
shlq $0x3, %rdi
leaq 0x54969(%rip), %rsi     # 0xa7a40
movl $0x3, %edx
callq 0x390f5
movq %rax, %r12
movq 0x40(%r15), %rdi
testq %rdi, %rdi
je 0x530f2
callq 0x39194
movslq 0x10(%r15), %rdi
shlq $0x2, %rdi
leaq 0x5493f(%rip), %rsi     # 0xa7a40
movl $0x6, %edx
callq 0x390f5
movq %rax, 0x40(%r15)
movslq 0x10(%r15), %rdx
testq %rdx, %rdx
jle 0x5313b
movq 0x8(%r15), %rcx
addq $0x10, %rcx
xorl %esi, %esi
xorps %xmm0, %xmm0
cvtsi2sdl (%rcx), %xmm0
movsd %xmm0, (%r12,%rsi,8)
incq %rsi
addq $0x48, %rcx
cmpq %rsi, %rdx
jne 0x53122
movl %ebx, %ecx
testl %ebx, %ebx
jle 0x53198
movq 0x8(%r15), %rsi
xorl %edi, %edi
movq (%r14,%rdi,8), %r8
cvttsd2si (%r8), %r9d
cvttsd2si 0x8(%r8), %r8d
movslq %r8d, %r8
leaq (%r8,%r8,8), %r10
xorps %xmm0, %xmm0
cvtsi2sdl 0x10(%rsi,%r10,8), %xmm0
movslq %r9d, %r9
mulsd (%r12,%r9,8), %xmm0
leaq (%r9,%r9,8), %r10
movsd %xmm0, (%r12,%r9,8)
xorps %xmm0, %xmm0
cvtsi2sdl 0x10(%rsi,%r10,8), %xmm0
mulsd (%r12,%r8,8), %xmm0
movsd %xmm0, (%r12,%r8,8)
incq %rdi
cmpq %rdi, %rcx
jne 0x53147
testl %edx, %edx
jle 0x53256
movsd 0x50188(%rip), %xmm0    # 0xa3330
movabsq $0x43e0000000000000, %rsi # imm = 0x43E0000000000000
movl %edx, %r8d
movslq 0x10(%r15), %rdx
testq %rdx, %rdx
jle 0x531e3
xorl %r9d, %r9d
movapd %xmm0, %xmm1
movsd (%r12,%r9,8), %xmm2
ucomisd %xmm2, %xmm1
cmoval %r9d, %edi
minsd %xmm1, %xmm2
incq %r9
movapd %xmm2, %xmm1
cmpq %r9, %rdx
jne 0x531c5
leal -0x1(%r8), %edx
movslq %edi, %r9
movl %edx, (%rax,%r9,4)
movq %rsi, (%r12,%r9,8)
testl %ebx, %ebx
jle 0x5324c
xorl %r9d, %r9d
movq (%r14,%r9,8), %r11
cvttsd2si (%r11), %r10d
cvttsd2si 0x8(%r11), %ebp
cmpl %r10d, %edi
jne 0x53215
movl %r10d, %r11d
movl %ebp, %r10d
jmp 0x5321c
movl %ebp, %r11d
cmpl %ebp, %edi
jne 0x53244
movq 0x8(%r15), %r13
movslq %r11d, %r11
leaq (%r11,%r11,8), %r11
xorps %xmm1, %xmm1
cvtsi2sdl 0x10(%r13,%r11,8), %xmm1
movslq %r10d, %r10
movsd (%r12,%r10,8), %xmm2
divsd %xmm1, %xmm2
movsd %xmm2, (%r12,%r10,8)
incq %r9
cmpq %r9, %rcx
jne 0x531f9
cmpl $0x2, %r8d
jge 0x531b2
movq %r12, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x39194