// File: /workspace/github_c_makefile_star2_O3/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 0x56519(%rip), %rsi     # 0xaaaf0
movl $0x3, %edx
callq 0x3a0e1
movq %rax, %r12
movq 0x40(%r15), %rdi
testq %rdi, %rdi
je 0x545f2
callq 0x3a180
movslq 0x10(%r15), %rdi
shlq $0x2, %rdi
leaq 0x564ef(%rip), %rsi     # 0xaaaf0
movl $0x6, %edx
callq 0x3a0e1
movq %rax, 0x40(%r15)
movslq 0x10(%r15), %rdx
testq %rdx, %rdx
jle 0x5463b
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 0x54622
movl %ebx, %ecx
testl %ebx, %ebx
jle 0x546a5
movq 0x8(%r15), %rsi
xorl %edi, %edi
movq (%r14,%rdi,8), %r8
movupd (%r8), %xmm0
cvttpd2dq %xmm0, %xmm0
pshufd $0x55, %xmm0, %xmm1     # xmm1 = xmm0[1,1,1,1]
movd %xmm1, %r8d
movslq %r8d, %r8
leaq (%r8,%r8,8), %r9
xorps %xmm1, %xmm1
cvtsi2sdl 0x10(%rsi,%r9,8), %xmm1
movd %xmm0, %r9d
movslq %r9d, %r9
mulsd (%r12,%r9,8), %xmm1
leaq (%r9,%r9,8), %r10
movsd %xmm1, (%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 0x54647
testl %edx, %edx
jle 0x5475b
movsd 0x51c7b(%rip), %xmm0    # 0xa6330
movabsq $0x43e0000000000000, %rsi # imm = 0x43E0000000000000
movl %edx, %r8d
movslq 0x10(%r15), %rdx
testq %rdx, %rdx
jle 0x546f0
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 0x546d2
leal -0x1(%r8), %edx
movslq %edi, %r9
movl %edx, (%rax,%r9,4)
movq %rsi, (%r12,%r9,8)
testl %ebx, %ebx
jle 0x54751
leaq (%r9,%r9,8), %r9
xorl %r10d, %r10d
movq (%r14,%r10,8), %r13
cvttsd2si (%r13), %r11d
cvttsd2si 0x8(%r13), %ebp
cmpl %r11d, %edi
jne 0x54724
movl %ebp, %r11d
jmp 0x54728
cmpl %ebp, %edi
jne 0x54749
movq 0x8(%r15), %r13
xorps %xmm1, %xmm1
cvtsi2sdl 0x10(%r13,%r9,8), %xmm1
movslq %r11d, %r11
movsd (%r12,%r11,8), %xmm2
divsd %xmm1, %xmm2
movsd %xmm2, (%r12,%r11,8)
incq %r10
cmpq %r10, %rcx
jne 0x5470a
cmpl $0x2, %r8d
jge 0x546bf
movq %r12, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3a180