// File: /workspace/github_c_makefile_star2_O2/LPD-EPFL[P]mctop/src/cdf.c
// Function: cdf_cluster
cdf_cluster_t*
cdf_cluster(cdf_t* cdf, const uint sens, const uint target_n_clusters)
{
  int direction = 0;
  uint stop = 0;
  int sensitivity = sens;
  cdf_cluster_t* cc = NULL;

  do
    {
      cdf_cluster_point_t clusters[cdf->n_points];

      size_t pprev = cdf->points[0].val, pprev_min = 0, n_clusters = 0, median = 0, cluster_size = 1;
      for (int i = 1; i < cdf->n_points; i++)
	{
	   if (cdf->points[i].val > (pprev + sensitivity) || (pprev == 0 && cdf->points[i].val > 0))
	    {
	      CDF_CLUSTER_DEBUG_PRINT(" -- size %zu\n", cluster_size);
	      clusters[n_clusters].idx = n_clusters;

	      clusters[n_clusters].size = cluster_size;
	      clusters[n_clusters].val_min = pprev_min;
	      clusters[n_clusters].val_max = cdf->points[i - 1].val;
	      clusters[n_clusters++].median = cdf->points[median].val;
	      pprev_min = cdf->points[i].val;
	      median = i;
	      cluster_size = 0;
	    }
	  CDF_CLUSTER_DEBUG_PRINT("%-3zu ", cdf->points[i].val);
	  pprev = cdf->points[i].val;
	  median += (cluster_size++) & 0x1; /* increment once every two */
	}

      CDF_CLUSTER_DEBUG_PRINT(" -- size %zu\n", cluster_size);
      clusters[n_clusters].idx = n_clusters;
      clusters[n_clusters].size = cluster_size;
      clusters[n_clusters].val_min = pprev_min;
      clusters[n_clusters].val_max = cdf->points[cdf->n_points - 1].val;
      clusters[n_clusters++].median = cdf->points[median].val;


      cc = malloc_assert(sizeof(cdf_cluster_t));
      cc->clusters = malloc_assert(n_clusters * sizeof(cdf_cluster_point_t));

      cc->n_clusters = n_clusters;
      for (int i = 0; i < n_clusters; i++)
	{
	  cc->clusters[i].idx = clusters[i].idx;
	  cc->clusters[i].size = clusters[i].size;
	  cc->clusters[i].val_min = clusters[i].val_min;
	  cc->clusters[i].val_max = clusters[i].val_max;
	  cc->clusters[i].median = clusters[i].median;
	}

      if (target_n_clusters && n_clusters != target_n_clusters)
	{
	  //	  cdf_cluster_print(cc);
	  cdf_cluster_free(cc);
	  int dir = (target_n_clusters - n_clusters);
	  if ((direction < 0 && dir > 0) || (direction > 0 && dir < 0))
	    {
	      stop = 1;
	    }
	  direction = dir;
	  sensitivity -= dir;
	  fprintf(stderr, "## Warning: Found %zu clusters, expected %u. Retrying with sensitivity: %-2u!\n",
		  n_clusters, target_n_clusters, sensitivity);
	  cc = NULL;
	}
      else
	{
	  break;
	}
    }
  while (sensitivity > 0 && !stop);

  return cc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %esi, %r15d
movl %edx, %eax
movq %rax, -0x50(%rbp)
movq $0x0, -0x38(%rbp)
pushq $0x1
popq %r11
xorl %ebx, %ebx
movl %edx, -0x2c(%rbp)
movq %rdi, -0x48(%rbp)
movl %ebx, -0x30(%rbp)
movq %rsp, -0x40(%rbp)
imulq $0x28, (%rdi), %rax
movq %rsp, %r12
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %r12
movq %r12, %rsp
movq (%rdi), %rcx
movq 0x8(%rdi), %rax
movslq %r15d, %rdx
movq (%rax), %rbx
leaq 0x10(%rax), %rsi
movq %r11, %r8
xorl %r9d, %r9d
xorl %r13d, %r13d
xorl %edi, %edi
cmpq %r8, %rcx
jbe 0x5bbc
movq (%rsi), %r10
leaq (%rbx,%rdx), %r14
cmpq %r14, %r10
ja 0x5b6e
testq %rbx, %rbx
setne %bl
testq %r10, %r10
sete %r14b
orb %bl, %r14b
jne 0x5ba2
imulq $0x28, %r13, %rbx
movl %r13d, (%r12,%rbx)
movq %r11, 0x8(%r12,%rbx)
movq %r9, 0x10(%r12,%rbx)
movq -0x10(%rsi), %r9
movq %r9, 0x18(%r12,%rbx)
shlq $0x4, %rdi
movq (%rax,%rdi), %rdi
incq %r13
movq %rdi, 0x20(%r12,%rbx)
movq %r10, %r9
movq %r8, %rdi
xorl %r11d, %r11d
leaq 0x1(%r11), %r14
andl $0x1, %r11d
addq %r11, %rdi
incq %r8
addq $0x10, %rsi
movq %r10, %rbx
movq %r14, %r11
jmp 0x5b4b
imulq $0x28, %r13, %r14
movl %r13d, (%r12,%r14)
movq %r11, 0x8(%r12,%r14)
movq %r9, 0x10(%r12,%r14)
shlq $0x4, %rcx
movq -0x10(%rax,%rcx), %rcx
movq %rcx, 0x18(%r12,%r14)
shlq $0x4, %rdi
movq (%rax,%rdi), %rax
incq %r13
movq %rax, 0x20(%r12,%r14)
pushq $0x10
popq %rdi
callq 0x5cde
movq %rax, %rbx
addq $0x28, %r14
movq %r14, %rdi
callq 0x5cde
movq %rax, 0x8(%rbx)
movq %r13, (%rbx)
xorl %ecx, %ecx
movq %r13, %rdx
subq $0x1, %rdx
jb 0x5c38
movl (%r12,%rcx), %esi
movl %esi, (%rax,%rcx)
movups 0x8(%r12,%rcx), %xmm0
movups %xmm0, 0x8(%rax,%rcx)
movups 0x18(%r12,%rcx), %xmm0
movups %xmm0, 0x18(%rax,%rcx)
addq $0x28, %rcx
jmp 0x5c0f
movl -0x2c(%rbp), %r12d
testl %r12d, %r12d
sete %al
cmpq -0x50(%rbp), %r13
sete %cl
orb %al, %cl
jne 0x5cc3
movq %rbx, %rdi
callq 0x5d08
movl %r12d, %ebx
subl %r13d, %ebx
movl -0x30(%rbp), %edx
testl %edx, %edx
sets %al
testl %ebx, %ebx
setg %cl
testb %cl, %al
jne 0x5c80
testl %edx, %edx
setg %al
testl %ebx, %ebx
sets %cl
andb %al, %cl
movb $0x1, %r14b
cmpb $0x1, %cl
jne 0x5c83
xorl %r14d, %r14d
movq 0xa366(%rip), %rax      # 0xfff0
movq (%rax), %rdi
movl %r15d, %r8d
subl %ebx, %r8d
leaq 0x5cc5(%rip), %rsi      # 0xb95f
movq %r13, %rdx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x2170
subl %ebx, %r15d
movq -0x40(%rbp), %rsp
jle 0x5ccb
testb %r14b, %r14b
movq -0x48(%rbp), %rdi
pushq $0x1
popq %r11
jne 0x5b13
jmp 0x5ccb
movq -0x40(%rbp), %rsp
movq %rbx, -0x38(%rbp)
movq -0x38(%rbp), %rax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq