// File: /workspace/github_c_makefile_star2_O3/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 $0x68, %rsp
movq %rsi, %r12
movq %xmm0, 0x28(%rsp)
movq %rdi, %r14
movl 0x10(%rdi), %eax
leal -0x1(%rax), %r15d
imull %eax, %r15d
movl %r15d, %eax
shrl $0x1f, %eax
addl %r15d, %eax
sarl %eax
movl %eax, 0x4(%rsp)
callq 0x4ed12
movq %rax, 0x8(%rsp)
movslq 0x10(%r14), %rdi
shlq $0x3, %rdi
leaq 0x5b694(%rip), %rbx     # 0xaa76e
movq %rbx, %rsi
movl $0x4, %edx
callq 0x3a0e1
movq %rax, %r13
movslq 0x10(%r14), %rdi
shlq $0x2, %rdi
movq %rbx, %rsi
movl $0x5, %edx
callq 0x3a0e1
movq %rax, %rbp
movq %r12, 0x30(%rsp)
movl $0x0, (%r12)
cmpl $0x0, 0x10(%r14)
jle 0x4f14f
xorl %r12d, %r12d
leaq 0x5b64e(%rip), %rbx     # 0xaa76e
movl $0x1, (%rbp,%r12,4)
movl $0x4, %edi
movq %rbx, %rsi
movl $0xa, %edx
callq 0x3a0e1
movq %rax, (%r13,%r12,8)
movl %r12d, (%rax)
incq %r12
movslq 0x10(%r14), %rax
cmpq %rax, %r12
jl 0x4f120
movl 0x4(%rsp), %eax
movq %rax, 0x38(%rsp)
cmpl $0x2, %r15d
jl 0x4f365
movsd 0x563b6(%rip), %xmm1    # 0xa5520
subsd 0x28(%rsp), %xmm1
xorl %r15d, %r15d
xorl %r11d, %r11d
movq 0x8(%rsp), %rbx
movsd %xmm1, 0x20(%rsp)
movq (%rbx,%r15,8), %rax
movupd (%rax), %xmm0
cvttpd2dq %xmm0, %xmm2
movslq 0x10(%r14), %rax
movd %xmm2, %r12d
pshufd $0x55, %xmm2, %xmm0     # xmm0 = xmm2[1,1,1,1]
testq %rax, %rax
jle 0x4f1df
movslq %r12d, %rcx
movd %xmm0, %edx
movslq %edx, %rsi
xorl %edi, %edi
cmpq %rdi, %r12
sete %r8b
cmpq %rdi, %rdx
sete %r9b
orb %r8b, %r9b
jne 0x4f1d7
movq 0x38(%r14), %r8
movq (%r8,%rcx,8), %r8
movq (%r8,%rsi,8), %r8
ucomisd (%r8,%rdi,8), %xmm1
ja 0x4f34e
incq %rdi
cmpq %rdi, %rax
jne 0x4f1ac
movdqa %xmm0, 0x50(%rsp)
movq %r15, 0x40(%rsp)
movq 0x30(%rsp), %rbx
movslq (%rbx), %rax
leaq 0x1(%rax), %rdx
movl %edx, (%rbx)
leaq 0x8(,%rax,8), %rsi
movq %r11, %rdi
movaps %xmm2, 0x10(%rsp)
callq 0x3a0b4
movq %rax, %r15
movl $0x10, %edi
leaq 0x5b552(%rip), %rsi     # 0xaa76e
movl $0x1c, %edx
callq 0x3a0e1
movq %r15, %r11
movslq (%rbx), %rcx
cvtdq2pd 0x10(%rsp), %xmm0
movq %rax, -0x8(%r15,%rcx,8)
movdqu %xmm0, (%rax)
movslq 0x10(%r14), %rcx
testq %rcx, %rcx
jle 0x4f2cc
movq %r11, 0x10(%rsp)
movdqa 0x50(%rsp), %xmm0
movd %xmm0, %esi
xorl %edi, %edi
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
movq 0x40(%rsp), %r15
movsd 0x20(%rsp), %xmm1
movslq (%rbp,%rdi,4), %r9
testq %r9, %r9
jle 0x4f29a
movq (%r13,%rdi,8), %r10
xorl %r11d, %r11d
movl (%r10,%r11,4), %ebx
cmpl %r12d, %ebx
cmovel %edi, %edx
cmpl %esi, %ebx
cmovel %edi, %eax
incq %r11
cmpq %r11, %r9
jne 0x4f283
xorl %r10d, %r10d
testl %r9d, %r9d
setg %r10b
addl %r10d, %r8d
incq %rdi
cmpq %rcx, %rdi
jne 0x4f271
cmpl $0x1, %r8d
je 0x4f372
cmpl %eax, %edx
jne 0x4f2de
movq 0x8(%rsp), %rbx
movq 0x10(%rsp), %r11
jmp 0x4f34e
movq 0x40(%rsp), %r15
movq 0x8(%rsp), %rbx
movsd 0x20(%rsp), %xmm1
jmp 0x4f34e
movslq %edx, %r12
movq (%r13,%r12,8), %rdi
movslq (%rbp,%r12,4), %rcx
movslq %eax, %rbx
movslq (%rbp,%rbx,4), %rdx
addq %rcx, %rdx
movq %rdx, %rsi
shlq $0x2, %rsi
callq 0x3a0b4
movsd 0x20(%rsp), %xmm1
movq %rax, (%r13,%r12,8)
movl (%rbp,%rbx,4), %edx
testl %edx, %edx
movq 0x10(%rsp), %r11
jle 0x4f33c
movq (%r13,%rbx,8), %rcx
xorl %esi, %esi
movl (%rcx,%rsi,4), %edx
movslq (%rbp,%r12,4), %rdi
addq %rsi, %rdi
movl %edx, (%rax,%rdi,4)
incq %rsi
movslq (%rbp,%rbx,4), %rdx
cmpq %rdx, %rsi
jl 0x4f321
addl %edx, (%rbp,%r12,4)
movl $0x0, (%rbp,%rbx,4)
movq 0x8(%rsp), %rbx
incq %r15
cmpq 0x38(%rsp), %r15
jne 0x4f181
movl 0x4(%rsp), %eax
movl %eax, %r15d
jmp 0x4f38b
xorl %r15d, %r15d
xorl %r11d, %r11d
movq 0x8(%rsp), %rbx
jmp 0x4f38b
movq 0x8(%rsp), %rbx
movq (%rbx,%r15,8), %rax
movq 0x10(%rax), %xmm0
movq %xmm0, 0x50(%rsp)
movq 0x10(%rsp), %r11
leal 0x1(%r15), %eax
cmpl 0x4(%rsp), %eax
jge 0x4f499
movsd 0x5617f(%rip), %xmm1    # 0xa5520
subsd 0x28(%rsp), %xmm1
movl %r15d, %r15d
incq %r15
movq %r13, 0x60(%rsp)
movsd %xmm1, 0x10(%rsp)
movq (%rbx,%r15,8), %rax
movsd 0x10(%rax), %xmm0
ucomisd 0x50(%rsp), %xmm0
jne 0x4f499
jp 0x4f499
movupd (%rax), %xmm0
cvttpd2dq %xmm0, %xmm2
movslq 0x10(%r14), %rax
testq %rax, %rax
jle 0x4f429
movd %xmm2, %ecx
movslq %ecx, %rdx
pshufd $0x55, %xmm2, %xmm0     # xmm0 = xmm2[1,1,1,1]
movd %xmm0, %esi
movslq %esi, %rdi
xorl %r8d, %r8d
cmpq %r8, %rcx
sete %r9b
cmpq %r8, %rsi
sete %r10b
orb %r9b, %r10b
jne 0x4f421
movq 0x38(%r14), %r9
movq (%r9,%rdx,8), %r9
movq (%r9,%rdi,8), %r9
ucomisd (%r9,%r8,8), %xmm1
ja 0x4f48b
incq %r8
cmpq %r8, %rax
jne 0x4f3fa
movq %rbx, %r13
movq 0x30(%rsp), %rbx
movslq (%rbx), %rax
leaq 0x1(%rax), %rdx
movl %edx, (%rbx)
leaq 0x8(,%rax,8), %rsi
movq %r11, %rdi
movaps %xmm2, 0x40(%rsp)
callq 0x3a0b4
movq %rax, %r12
movl $0x10, %edi
leaq 0x5b310(%rip), %rsi     # 0xaa76e
movl $0x50, %edx
callq 0x3a0e1
movsd 0x10(%rsp), %xmm1
movq %r12, %r11
movslq (%rbx), %rcx
movq %r13, %rbx
movq 0x60(%rsp), %r13
movq %rax, -0x8(%r12,%rcx,8)
cvtdq2pd 0x40(%rsp), %xmm0
movdqu %xmm0, (%rax)
incq %r15
cmpq 0x38(%rsp), %r15
jne 0x4f3b8
movl 0x10(%r14), %esi
movq %r13, %rdi
movq %rbx, %r14
movq %r11, %rbx
callq 0x3a14b
movq %rbp, %rdi
callq 0x3a180
movq %r14, %rdi
movl 0x4(%rsp), %esi
callq 0x3a14b
movq %rbx, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq