// File: /workspace/github_c_makefile_star2_O2/sheneman[P]clearcut/clearcut.c
// Function: NJ_find_hmin
static inline
float
NJ_find_hmin(DMAT *dmat,
	     long int a,
	     long int *min,
	     long int *hmincount) {

  long int i;     /* index variable for looping                    */
  int size;       /* current size of distance matrix               */
  int mindex = 0; /* holds the current index to the chosen minimum */
  float curval;   /* used to hold current transformed values       */
  float hmin;     /* the value of the transformed minimum          */

  float *ptr, *r2, *val;  /* pointers used to reduce dereferencing in inner loop */

  /* values used for stochastic selection among multiple minima */
  float p, x;  
  long int smallcnt;

  /* initialize the min to something large */
  hmin = (float)HUGE_VAL;

  /* setup some pointers to limit dereferencing later */
  r2       = dmat->r2;
  val      = dmat->val;
  size     = dmat->size;

  /* initialize values associated with minima tie breaking */
  p        = 1.0;
  smallcnt = 0;
  
  
  ptr = &(val[NJ_MAP(a, a+1, size)]);   /* at the start of the horiz. part */
  for(i=a+1;i<size;i++) {

    curval = *(ptr++) - (r2[a] + r2[i]);  /* compute transformed distance */
    
    if(NJ_FLT_EQ(curval, hmin)) {  /* approx. equal */
      
      smallcnt++;

      p = 1.0/(float)smallcnt;
      x = genrand_real2();
      
      /* select this minimum in a way which is proportional to 
	 the number of minima found along the row so far */
      if( x < p ) {
	mindex = i;
      }

    } else if (curval < hmin) {

      smallcnt = 1;
      hmin = curval;
      mindex = i;
    }
  }
  
  /* save off the the minimum index to be returned via reference */
  *min = mindex;
  
  /* save off the number of minima */
  *hmincount = smallcnt;
  
  /* return the value of the smallest tranformed distance */
  return(hmin);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rcx, 0x20(%rsp)
movq %rdx, 0x18(%rsp)
movq %rsi, %r15
movq 0x30(%rdi), %r13
movslq 0x8(%rdi), %rbp
movq %rsi, %rax
notq %rax
leaq (%rax,%rbp,2), %rax
imulq %rsi, %rax
pushq $0x2
popq %rcx
cqto
idivq %rcx
movq %rax, %r12
shlq $0x2, %r12
addq 0x18(%rdi), %r12
leaq 0x1(%rsi), %rbx
movq $0x0, 0x8(%rsp)
movss 0x3a23(%rip), %xmm0     # 0x7004
movaps 0x3a38(%rip), %xmm3     # 0x7020
movss 0x3a18(%rip), %xmm4     # 0x7008
xorl %r14d, %r14d
cmpq %rbp, %rbx
jge 0x3690
movss (%r12,%rbx,4), %xmm2
movss (%r13,%r15,4), %xmm1
addss (%r13,%rbx,4), %xmm1
subss %xmm1, %xmm2
movaps %xmm2, %xmm1
subss %xmm0, %xmm1
andps %xmm3, %xmm1
ucomiss %xmm1, %xmm4
ja 0x3638
ucomiss %xmm2, %xmm0
jbe 0x3688
movaps %xmm2, %xmm0
movl %ebx, %eax
movq %rax, 0x8(%rsp)
pushq $0x1
popq %r14
jmp 0x3688
movss %xmm0, 0x4(%rsp)
incq %r14
cvtsi2ss %r14, %xmm0
movss 0x39be(%rip), %xmm1     # 0x700c
divss %xmm0, %xmm1
movss %xmm1, 0x14(%rsp)
callq 0x65db
cvtsd2ss %xmm0, %xmm0
movss 0x14(%rsp), %xmm1
ucomiss %xmm0, %xmm1
jbe 0x3673
movl %ebx, %eax
movq %rax, 0x8(%rsp)
movss 0x4(%rsp), %xmm0
movaps 0x39a0(%rip), %xmm3     # 0x7020
movss 0x3980(%rip), %xmm4     # 0x7008
incq %rbx
jmp 0x35f3
movslq 0x8(%rsp), %rax
movq 0x18(%rsp), %rcx
movq %rax, (%rcx)
movq 0x20(%rsp), %rax
movq %r14, (%rax)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq