// File: /workspace/github_c_makefile_star2_O1/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, -0x2c(%rbp)
movq %rdi, -0x48(%rbp)
movl %edx, -0x30(%rbp)
movl %edx, %eax
movq %rax, -0x40(%rbp)
movl $0x0, -0x34(%rbp)
xorl %r15d, %r15d
movq %rsp, -0x50(%rbp)
movq -0x48(%rbp), %rdx
movq (%rdx), %rax
movq %rsp, %rbx
leaq (%rax,%rax,4), %rax
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %rbx
movq %rbx, %rsp
movq (%rdx), %rcx
movq 0x8(%rdx), %rax
cmpq $0x2, %rcx
jb 0x63bf
movslq -0x2c(%rbp), %rdx
movl $0x1, %esi
movq %rax, %rdi
movl $0x1, %r11d
xorl %r10d, %r10d
xorl %r13d, %r13d
xorl %r9d, %r9d
movq (%rdi), %r14
movq 0x10(%rdi), %r8
leaq (%r14,%rdx), %r12
cmpq %r12, %r8
ja 0x635f
testq %r14, %r14
setne %r14b
testq %r8, %r8
sete %r12b
orb %r14b, %r12b
je 0x635f
movq %r9, %r8
jmp 0x6399
leaq (,%r13,4), %r14
addq %r13, %r14
movl %r13d, (%rbx,%r14,8)
movq %r11, 0x8(%rbx,%r14,8)
movq %r9, 0x10(%rbx,%r14,8)
movq (%rdi), %r9
movq %r9, 0x18(%rbx,%r14,8)
shlq $0x4, %r10
movq (%rax,%r10), %r9
incq %r13
movq %r9, 0x20(%rbx,%r14,8)
movq %rsi, %r10
xorl %r11d, %r11d
movq %r8, %r9
leaq 0x1(%r11), %r14
andl $0x1, %r11d
movq %r11, %r12
addq %r10, %r12
incq %rsi
addq $0x10, %rdi
movq %r14, %r11
movq %r12, %r10
cmpq %rsi, %rcx
jne 0x6337
jmp 0x63ce
movl $0x1, %r14d
xorl %r8d, %r8d
xorl %r13d, %r13d
xorl %r12d, %r12d
leaq (,%r13,4), %rdx
addq %r13, %rdx
movl %r13d, (%rbx,%rdx,8)
movq %r14, 0x8(%rbx,%rdx,8)
movq %r8, 0x10(%rbx,%rdx,8)
shlq $0x4, %rcx
movq -0x10(%rax,%rcx), %rcx
movq %rcx, 0x18(%rbx,%rdx,8)
shlq $0x4, %r12
movq (%rax,%r12), %rax
movq %rax, 0x20(%rbx,%rdx,8)
movl $0x10, %edi
callq 0x21d0
testq %rax, %rax
je 0x6539
movq %rax, %r12
incq %r13
leaq (,%r13,8), %rax
leaq (%rax,%rax,4), %rdi
callq 0x21d0
testq %rax, %rax
je 0x6539
movq %rax, 0x8(%r12)
movq %r13, (%r12)
testq %r13, %r13
je 0x6482
movl $0x20, %ecx
movq %r13, %rdx
movl -0x20(%rbx,%rcx), %esi
movl %esi, -0x20(%rax,%rcx)
movq -0x18(%rbx,%rcx), %rsi
movq %rsi, -0x18(%rax,%rcx)
movq -0x10(%rbx,%rcx), %rsi
movq %rsi, -0x10(%rax,%rcx)
movq -0x8(%rbx,%rcx), %rsi
movq %rsi, -0x8(%rax,%rcx)
movq (%rbx,%rcx), %rsi
movq %rsi, (%rax,%rcx)
addq $0x28, %rcx
decq %rdx
jne 0x644b
cmpl $0x0, -0x30(%rbp)
setne %cl
cmpq -0x40(%rbp), %r13
setne %bl
andb %cl, %bl
jne 0x64b8
movl %r15d, %r14d
movq -0x50(%rbp), %rsp
testb %bl, %bl
je 0x6527
cmpl $0x0, -0x2c(%rbp)
jle 0x6527
movl %r14d, %r15d
cmpl $0x0, -0x34(%rbp)
je 0x62e7
jmp 0x6527
movq %rax, %rdi
callq 0x2030
movq %r12, %rdi
callq 0x2030
movl -0x30(%rbp), %r14d
subl %r13d, %r14d
testl %r15d, %r15d
sets %al
testl %r14d, %r14d
setg %cl
testb %cl, %al
jne 0x64ef
testl %r15d, %r15d
setle %al
testl %r14d, %r14d
setns %cl
orb %al, %cl
jne 0x64f6
movl $0x1, -0x34(%rbp)
movl -0x2c(%rbp), %r8d
subl %r14d, %r8d
movq 0xaaec(%rip), %rax      # 0x10ff0
movq (%rax), %rdi
leaq 0x6451(%rip), %rsi      # 0xc95f
movq %r13, %rdx
movl -0x30(%rbp), %ecx
movl %r8d, -0x2c(%rbp)
xorl %eax, %eax
callq 0x2170
xorl %r12d, %r12d
jmp 0x6497
movq %r12, %rax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6e08(%rip), %rdi      # 0xd348
leaq 0x55b8(%rip), %rsi      # 0xbaff
leaq 0x55ca(%rip), %rcx      # 0xbb18
movl $0x59, %edx
callq 0x2110