// File: /workspace/github_c_makefile_star2_O1/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 $0x48, %rsp
movq %rsi, %r12
movsd %xmm0, 0x18(%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 0x4d8b7
movq %rax, 0x28(%rsp)
movslq 0x10(%r14), %rdi
shlq $0x3, %rdi
leaq 0x59a67(%rip), %rbx     # 0xa76ce
movq %rbx, %rsi
movl $0x4, %edx
callq 0x390f5
movq %rax, %r13
movslq 0x10(%r14), %rdi
shlq $0x2, %rdi
movq %rbx, %rsi
movl $0x5, %edx
callq 0x390f5
movq %rax, %rbp
movq %r12, 0x38(%rsp)
movl $0x0, (%r12)
cmpl $0x0, 0x10(%r14)
jle 0x4dcdc
xorl %r12d, %r12d
leaq 0x59a21(%rip), %rbx     # 0xa76ce
movl $0x1, (%rbp,%r12,4)
movl $0x4, %edi
movq %rbx, %rsi
movl $0xa, %edx
callq 0x390f5
movq %rax, (%r13,%r12,8)
movl %r12d, (%rax)
incq %r12
movslq 0x10(%r14), %rax
cmpq %rax, %r12
jl 0x4dcad
movl 0x4(%rsp), %eax
movq %rax, 0x40(%rsp)
cmpl $0x2, %r15d
movq %r13, 0x30(%rsp)
jl 0x4dedf
movsd 0x54824(%rip), %xmm0    # 0xa2520
subsd 0x18(%rsp), %xmm0
xorl %r15d, %r15d
movq $0x0, 0x8(%rsp)
movsd %xmm0, 0x10(%rsp)
movq 0x28(%rsp), %rax
movq (%rax,%r15,8), %rax
cvttsd2si (%rax), %r12d
cvttsd2si 0x8(%rax), %ebx
movslq 0x10(%r14), %rax
testq %rax, %rax
jle 0x4dd70
movslq %r12d, %rcx
movslq %ebx, %rdx
movl %edx, %esi
movl %ecx, %edi
xorl %r8d, %r8d
cmpq %r8, %rdi
sete %r9b
cmpq %r8, %rsi
sete %r10b
orb %r9b, %r10b
jne 0x4dd68
movq 0x38(%r14), %r9
movq (%r9,%rcx,8), %r9
movq (%r9,%rdx,8), %r9
ucomisd (%r9,%r8,8), %xmm0
ja 0x4decf
incq %r8
cmpq %r8, %rax
jne 0x4dd3d
movq %r15, 0x20(%rsp)
movq 0x38(%rsp), %r15
movslq (%r15), %rax
leaq 0x1(%rax), %rdx
movl %edx, (%r15)
leaq 0x8(,%rax,8), %rsi
movq 0x8(%rsp), %rdi
callq 0x390c8
movq %rax, %r13
movl $0x10, %edi
leaq 0x59929(%rip), %rsi     # 0xa76ce
movl $0x1c, %edx
callq 0x390f5
movslq (%r15), %rcx
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
movq %r13, 0x8(%rsp)
movq %rax, -0x8(%r13,%rcx,8)
movsd %xmm0, (%rax)
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
movsd %xmm0, 0x8(%rax)
movslq 0x10(%r14), %rsi
xorl %ecx, %ecx
testq %rsi, %rsi
jle 0x4de42
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
xorl %edi, %edi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq 0x30(%rsp), %r13
movq 0x20(%rsp), %r15
movsd 0x10(%rsp), %xmm0
movslq (%rbp,%rdi,4), %r8
testq %r8, %r8
jle 0x4de25
movq (%r13,%rdi,8), %r9
xorl %r10d, %r10d
movl (%r9,%r10,4), %r11d
cmpl %r12d, %r11d
cmovel %edi, %edx
cmpl %ebx, %r11d
cmovel %edi, %eax
incq %r10
cmpq %r10, %r8
jne 0x4de0d
xorl %r9d, %r9d
testl %r8d, %r8d
setg %r9b
addl %r9d, %ecx
incq %rdi
cmpq %rsi, %rdi
jne 0x4ddfb
cmpl $0x1, %ecx
sete %cl
jmp 0x4de5c
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq 0x30(%rsp), %r13
movq 0x20(%rsp), %r15
movsd 0x10(%rsp), %xmm0
testb %cl, %cl
jne 0x4deed
cmpl %eax, %edx
je 0x4decf
movslq %edx, %rbx
movq (%r13,%rbx,8), %rdi
movslq (%rbp,%rbx,4), %rcx
movslq %eax, %r12
movslq (%rbp,%r12,4), %rdx
addq %rcx, %rdx
movq %rdx, %rsi
shlq $0x2, %rsi
callq 0x390c8
movsd 0x10(%rsp), %xmm0
movq %rax, (%r13,%rbx,8)
movl (%rbp,%r12,4), %edx
testl %edx, %edx
jle 0x4dec2
movq (%r13,%r12,8), %rcx
xorl %esi, %esi
movl (%rcx,%rsi,4), %edx
movslq (%rbp,%rbx,4), %rdi
addq %rsi, %rdi
movl %edx, (%rax,%rdi,4)
incq %rsi
movslq (%rbp,%r12,4), %rdx
cmpq %rdx, %rsi
jl 0x4dea7
addl %edx, (%rbp,%rbx,4)
movl $0x0, (%rbp,%r12,4)
incq %r15
cmpq 0x40(%rsp), %r15
jne 0x4dd14
jmp 0x4df01
xorl %r15d, %r15d
movq $0x0, 0x8(%rsp)
jmp 0x4df01
movq 0x28(%rsp), %rax
movq (%rax,%r15,8), %rax
movsd 0x10(%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
leal 0x1(%r15), %eax
cmpl 0x4(%rsp), %eax
jge 0x4e018
movsd 0x54609(%rip), %xmm1    # 0xa2520
subsd 0x18(%rsp), %xmm1
movl %r15d, %ebx
incq %rbx
movsd %xmm1, 0x18(%rsp)
movq 0x28(%rsp), %rax
movq (%rax,%rbx,8), %rax
movsd 0x10(%rax), %xmm0
ucomisd 0x10(%rsp), %xmm0
jne 0x4e018
jp 0x4e018
cvttsd2si (%rax), %r15d
cvttsd2si 0x8(%rax), %r12d
movslq 0x10(%r14), %rax
testq %rax, %rax
jle 0x4df99
movslq %r15d, %rcx
movslq %r12d, %rdx
movl %edx, %esi
movl %ecx, %edi
xorl %r8d, %r8d
cmpq %r8, %rdi
sete %r9b
cmpq %r8, %rsi
sete %r10b
orb %r9b, %r10b
jne 0x4df91
movq 0x38(%r14), %r9
movq (%r9,%rcx,8), %r9
movq (%r9,%rdx,8), %r9
ucomisd (%r9,%r8,8), %xmm1
ja 0x4e00a
incq %r8
cmpq %r8, %rax
jne 0x4df6a
movl %r15d, 0x20(%rsp)
movq 0x38(%rsp), %r15
movslq (%r15), %rax
leaq 0x1(%rax), %rdx
movl %edx, (%r15)
leaq 0x8(,%rax,8), %rsi
movq 0x8(%rsp), %rdi
callq 0x390c8
movq %rax, %r13
movl $0x10, %edi
leaq 0x59700(%rip), %rsi     # 0xa76ce
movl $0x50, %edx
callq 0x390f5
movsd 0x18(%rsp), %xmm1
movslq (%r15), %rcx
movq %r13, 0x8(%rsp)
movq %rax, -0x8(%r13,%rcx,8)
movq 0x30(%rsp), %r13
xorps %xmm0, %xmm0
cvtsi2sdl 0x20(%rsp), %xmm0
movsd %xmm0, (%rax)
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
movsd %xmm0, 0x8(%rax)
incq %rbx
cmpq 0x40(%rsp), %rbx
jne 0x4df29
movl 0x10(%r14), %esi
movq %r13, %rdi
callq 0x3915f
movq %rbp, %rdi
callq 0x39194
movq 0x28(%rsp), %rdi
movl 0x4(%rsp), %esi
callq 0x3915f
movq 0x8(%rsp), %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq