// File: /workspace/github_c_makefile_star2_O2/olcaytaner[P]Isell/network.c
// Function: find_edges
double **find_edges(bayesiangraphptr mygraph,double threshold,int *edgecount)
{
	/*!Last Changed 02.02.2004 added safemalloc*/
 /*!Last Changed 13.03.2001*/
 int i,j,k,l,found,total=(mygraph->nodecount)*(mygraph->nodecount-1)/2;
 double **sorted=sort_independence(mygraph),thrhold,**edges=NULL;
 int ind1,ind2,pos1,pos2,**connected_components,*component_counts;
 connected_components = safemalloc (mygraph->nodecount * sizeof(int *), "find_edges", 4);
 component_counts = safemalloc (mygraph->nodecount * sizeof(int), "find_edges", 5);
 *edgecount=0;
 for (i = 0; i < mygraph->nodecount; i++)
  {
   component_counts[i]=1;
   connected_components[i] = (int *) safemalloc (sizeof(int), "find_edges", 10);
   connected_components[i][0]=i;
  }
 for (i=0;i<total;i++)
  {
   j = (int) sorted[i][0];
   k = (int) sorted[i][1];
   found=0;
   for (l=0;l<mygraph->nodecount;l++)/*One independence lara bakiyorum*/
     if ((l!=j)&&(l!=k)&&(mygraph->one_independence[j][k][l]<1-threshold))
      {
       found=1;
       break;
      }
   if (!found)
    {
     (*edgecount)++;
     edges=alloc(edges,(*edgecount)*sizeof(double *),*edgecount);
     edges[(*edgecount)-1] = safemalloc(2*sizeof(double), "find_edges", 28);
     edges[(*edgecount)-1][0]=j;
     edges[(*edgecount)-1][1]=k;
     ind1=j;
     ind2=k;
     pos1=-1;
     pos2=-1;
     j=0;
     l=0;
     while (j<mygraph->nodecount)
      {
       for (k=0;k<component_counts[j];k++)
        {
         if (connected_components[j][k]==ind1)
           pos1=j;
         if (connected_components[j][k]==ind2)
           pos2=j;
        }
       if (component_counts[j]>0)
         l++;
       j++;
      }
     if (l==1)
      {
       thrhold = sorted[i][2];
       break;
      }
     if (pos1!=pos2)
      {
       connected_components[pos1] = alloc(connected_components[pos1], (component_counts[pos1] + component_counts[pos2]) * sizeof(int), component_counts[pos1] + component_counts[pos2]);
       for (j=0;j<component_counts[pos2];j++)
         connected_components[pos1][component_counts[pos1]+j]=connected_components[pos2][j];
       component_counts[pos1]+=component_counts[pos2];
       component_counts[pos2]=0;
      }
    }
  }
 i++;  
 while (i < total && sorted[i][2] == thrhold)
  {
   j = (int) sorted[i][0];
   k = (int) sorted[i][1];
   found = 0;
   for (l = 0; l < mygraph->nodecount; l++)
     if ((l != j) && (l != k) && (mygraph->one_independence[j][k][l] < 1-threshold)){
       found = 1;
       break;
     }
   if (!found)
    {
     (*edgecount)++;
     edges=alloc(edges,(*edgecount)*sizeof(double *),*edgecount);
     edges[(*edgecount)-1] = safemalloc(2*sizeof(double), "find_edges", 80);
     edges[(*edgecount)-1][0]=j;
     edges[(*edgecount)-1][1]=k;
    }
   i++;
  }
	free_2d((void**)connected_components, mygraph->nodecount);
 safe_free(component_counts);
	free_2d((void**)sorted, total);
 return edges;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %rbx
movq %xmm0, (%rsp)
movq %rdi, %r15
movl 0x10(%rdi), %eax
leal -0x1(%rax), %ecx
imull %eax, %ecx
movl %ecx, 0x8(%rsp)
callq 0x47a16
movq %rax, 0x18(%rsp)
movslq 0x10(%r15), %rdi
shlq $0x3, %rdi
leaq 0x5293d(%rip), %r14     # 0x9a69e
pushq $0x4
popq %rdx
movq %r14, %rsi
callq 0x34f59
movq %rax, %r12
movslq 0x10(%r15), %rdi
shlq $0x2, %rdi
pushq $0x5
popq %rdx
movq %r14, %rsi
callq 0x34f59
movq %rbx, 0x38(%rsp)
andl $0x0, (%rbx)
movq %rax, %r13
xorl %ebp, %ebp
pushq $0x4
popq %rbx
pushq $0xa
popq %r14
movslq 0x10(%r15), %rax
cmpq %rax, %rbp
jge 0x47dc5
movl $0x1, (%r13,%rbp,4)
movq %rbx, %rdi
leaq 0x528ec(%rip), %rsi     # 0x9a69e
movl %r14d, %edx
callq 0x34f59
movq %rax, (%r12,%rbp,8)
movl %ebp, (%rax)
incq %rbp
jmp 0x47d96
pushq $0x2
popq %rcx
movl 0x8(%rsp), %eax
cltd
idivl %ecx
movsd 0x4d749(%rip), %xmm1    # 0x95520
subsd (%rsp), %xmm1
testl %eax, %eax
movl $0x0, %ebx
movl %eax, 0x14(%rsp)
cmovgl %eax, %ebx
xorl %r9d, %r9d
movq $0x0, (%rsp)
movsd %xmm1, 0x8(%rsp)
movq %r15, 0x30(%rsp)
movq %rbx, 0x50(%rsp)
cmpq %rbx, %r9
je 0x47faf
movq 0x18(%rsp), %rax
movq (%rax,%r9,8), %rax
movupd (%rax), %xmm0
cvttpd2dq %xmm0, %xmm2
pshufd $0x55, %xmm2, %xmm0     # xmm0 = xmm2[1,1,1,1]
movd %xmm0, %ebp
movd %xmm2, %r14d
movl 0x10(%r15), %eax
movslq %r14d, %rcx
movslq %ebp, %rdx
testl %eax, %eax
movl $0x0, %esi
cmovlel %esi, %eax
xorl %esi, %esi
cmpq %rsi, %rax
je 0x47e77
cmpq %rsi, %r14
sete %dil
cmpq %rsi, %rbp
sete %r8b
orb %dil, %r8b
jne 0x47e72
movq 0x38(%r15), %rdi
movq (%rdi,%rcx,8), %rdi
movq (%rdi,%rdx,8), %rdi
ucomisd (%rdi,%rsi,8), %xmm1
ja 0x47fa7
incq %rsi
jmp 0x47e43
movq %r9, 0x20(%rsp)
movq 0x38(%rsp), %rbx
movslq (%rbx), %rax
leaq 0x1(%rax), %rdx
movl %edx, (%rbx)
leaq 0x8(,%rax,8), %rsi
movq (%rsp), %rdi
movaps %xmm2, 0x40(%rsp)
callq 0x34f30
movq %rax, %r15
pushq $0x10
popq %rdi
leaq 0x527f1(%rip), %rsi     # 0x9a69e
pushq $0x1c
popq %rdx
callq 0x34f59
movslq (%rbx), %rcx
movq %r15, (%rsp)
movq %rax, -0x8(%r15,%rcx,8)
movq 0x30(%rsp), %r15
cvtdq2pd 0x40(%rsp), %xmm0
movdqu %xmm0, (%rax)
movl 0x10(%r15), %ecx
xorl %esi, %esi
testl %ecx, %ecx
cmovlel %esi, %ecx
xorl %edi, %edi
pushq $-0x1
popq %rax
movl %eax, %edx
cmpq %rcx, %rsi
je 0x47f2c
movl (%r13,%rsi,4), %r8d
testl %r8d, %r8d
movl $0x0, %r9d
cmovgl %r8d, %r9d
xorl %r10d, %r10d
cmpq %r10, %r9
je 0x47f1a
movq (%r12,%rsi,8), %r11
movl (%r11,%r10,4), %r11d
cmpl %r14d, %r11d
cmovel %esi, %edx
cmpl %ebp, %r11d
cmovel %esi, %eax
incq %r10
jmp 0x47efc
xorl %r9d, %r9d
testl %r8d, %r8d
setg %r9b
addl %r9d, %edi
incq %rsi
jmp 0x47ee2
cmpl $0x1, %edi
movsd 0x8(%rsp), %xmm1
movq 0x50(%rsp), %rbx
je 0x47fb1
cmpl %eax, %edx
jne 0x47f47
movq 0x20(%rsp), %r9
jmp 0x47fa7
movslq %edx, %r14
movq (%r12,%r14,8), %rdi
movslq (%r13,%r14,4), %rcx
movslq %eax, %rbp
movslq (%r13,%rbp,4), %rdx
addq %rcx, %rdx
movq %rdx, %rsi
shlq $0x2, %rsi
callq 0x34f30
movsd 0x8(%rsp), %xmm1
movq %rax, (%r12,%r14,8)
xorl %ecx, %ecx
movq 0x20(%rsp), %r9
movslq (%r13,%rbp,4), %rdx
cmpq %rdx, %rcx
jge 0x47f9c
movq (%r12,%rbp,8), %rdx
movl (%rdx,%rcx,4), %edx
movslq (%r13,%r14,4), %rsi
addq %rcx, %rsi
movl %edx, (%rax,%rsi,4)
incq %rcx
jmp 0x47f7b
addl %edx, (%r13,%r14,4)
andl $0x0, (%r13,%rbp,4)
incq %r9
jmp 0x47e05
jmp 0x47fcc
movq 0x18(%rsp), %rax
movq 0x20(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movsd 0x10(%rax), %xmm0
movsd %xmm0, 0x40(%rsp)
movl %ecx, %ebx
movslq 0x14(%rsp), %r14
incq %rbx
cmpq %r14, %rbx
jge 0x480c5
movq 0x18(%rsp), %rax
movq (%rax,%rbx,8), %rax
movsd 0x10(%rax), %xmm0
ucomisd 0x40(%rsp), %xmm0
movl 0x10(%r15), %esi
jne 0x480c9
jp 0x480c9
movupd (%rax), %xmm0
cvttpd2dq %xmm0, %xmm2
pshufd $0x55, %xmm2, %xmm0     # xmm0 = xmm2[1,1,1,1]
movd %xmm0, %eax
movd %xmm2, %ecx
movslq %ecx, %rdx
movslq %eax, %rdi
testl %esi, %esi
movl $0x0, %r8d
cmovlel %r8d, %esi
xorl %r8d, %r8d
cmpq %r8, %rsi
je 0x48060
cmpq %r8, %rcx
sete %r9b
cmpq %r8, %rax
sete %r10b
orb %r9b, %r10b
jne 0x4805b
movq 0x38(%r15), %r9
movq (%r9,%rdx,8), %r9
movq (%r9,%rdi,8), %r9
ucomisd (%r9,%r8,8), %xmm1
ja 0x47fd1
incq %r8
jmp 0x4802b
movq %rbx, %rbp
movq 0x38(%rsp), %rbx
movslq (%rbx), %rax
leaq 0x1(%rax), %rdx
movl %edx, (%rbx)
leaq 0x8(,%rax,8), %rsi
movq (%rsp), %rdi
movaps %xmm2, 0x20(%rsp)
callq 0x34f30
movq %rax, %r15
pushq $0x10
popq %rdi
leaq 0x5260a(%rip), %rsi     # 0x9a69e
pushq $0x50
popq %rdx
callq 0x34f59
movsd 0x8(%rsp), %xmm1
movslq (%rbx), %rcx
movq %rbp, %rbx
movq %r15, (%rsp)
movq %rax, -0x8(%r15,%rcx,8)
movq 0x30(%rsp), %r15
cvtdq2pd 0x20(%rsp), %xmm0
movdqu %xmm0, (%rax)
jmp 0x47fd1
movl 0x10(%r15), %esi
movq %r12, %rdi
callq 0x34fc3
movq %r13, %rdi
callq 0x34ff7
movq 0x18(%rsp), %rdi
movl 0x14(%rsp), %esi
callq 0x34fc3
movq (%rsp), %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq