// File: /workspace/github_c_makefile_star2_O3/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 $0x38, %rsp
movl %esi, -0x2c(%rbp)
movl %edx, %eax
movq %rax, -0x58(%rbp)
movq $0x0, -0x40(%rbp)
xorl %ebx, %ebx
movl %edx, -0x30(%rbp)
movq %rdi, -0x50(%rbp)
movl %ebx, -0x34(%rbp)
movq %rsp, -0x48(%rbp)
movq (%rdi), %rax
movq %rsp, %r12
leaq (%rax,%rax,4), %rax
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %r12
movq %r12, %rsp
movq (%rdi), %rcx
movq 0x8(%rdi), %rax
cmpq $0x2, %rcx
jb 0x6355
movslq -0x2c(%rbp), %rdx
movq (%rax), %r9
leaq 0x10(%rax), %rsi
movl $0x1, %r14d
movl $0x1, %r11d
xorl %edi, %edi
xorl %r13d, %r13d
xorl %r15d, %r15d
movq (%rsi), %r8
leaq (%r9,%rdx), %r10
cmpq %r10, %r8
ja 0x62f5
testq %r9, %r9
setne %r9b
testq %r8, %r8
sete %r10b
orb %r9b, %r10b
jne 0x6330
leaq (,%r13,4), %r9
addq %r13, %r9
movl %r13d, (%r12,%r9,8)
movq %r11, 0x8(%r12,%r9,8)
movq %r15, 0x10(%r12,%r9,8)
movq -0x10(%rsi), %r10
movq %r10, 0x18(%r12,%r9,8)
shlq $0x4, %rdi
movq (%rax,%rdi), %rdi
incq %r13
movq %rdi, 0x20(%r12,%r9,8)
movq %r8, %r15
movq %r14, %rdi
xorl %r11d, %r11d
leaq 0x1(%r11), %rbx
andl $0x1, %r11d
movq %r11, %r10
addq %rdi, %r10
incq %r14
addq $0x10, %rsi
movq %r8, %r9
movq %rbx, %r11
movq %r10, %rdi
cmpq %r14, %rcx
jne 0x62d6
jmp 0x6363
movl $0x1, %ebx
xorl %r15d, %r15d
xorl %r13d, %r13d
xorl %r10d, %r10d
leaq (,%r13,4), %rdx
addq %r13, %rdx
movl %r13d, (%r12,%rdx,8)
movq %rbx, 0x8(%r12,%rdx,8)
movq %r15, 0x10(%r12,%rdx,8)
shlq $0x4, %rcx
movq -0x10(%rax,%rcx), %rcx
movq %rcx, 0x18(%r12,%rdx,8)
shlq $0x4, %r10
movq (%rax,%r10), %rax
movq %rax, 0x20(%r12,%rdx,8)
movl $0x10, %edi
callq 0x21d0
testq %rax, %rax
je 0x64ae
movq %rax, %rbx
incq %r13
leaq (,%r13,8), %rax
leaq (%rax,%rax,4), %rdi
callq 0x21d0
testq %rax, %rax
je 0x64ae
movq %rax, 0x8(%rbx)
movq %r13, (%rbx)
testq %r13, %r13
je 0x6401
xorl %ecx, %ecx
movq %r13, %rdx
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
decq %rdx
jne 0x63db
movl -0x30(%rbp), %r15d
testl %r15d, %r15d
je 0x6493
cmpq -0x58(%rbp), %r13
je 0x6493
movq %rax, %rdi
callq 0x2030
movq %rbx, %rdi
callq 0x2030
movl %r15d, %ebx
subl %r13d, %ebx
movl -0x34(%rbp), %edx
testl %edx, %edx
sets %al
testl %ebx, %ebx
setg %cl
testb %cl, %al
jne 0x644c
testl %edx, %edx
setle %al
testl %ebx, %ebx
setns %cl
movb $0x1, %r14b
orb %al, %cl
jne 0x644f
xorl %r14d, %r14d
movq 0x9b9a(%rip), %rax      # 0xfff0
movq (%rax), %rdi
movl -0x2c(%rbp), %r12d
movl %r12d, %r8d
subl %ebx, %r8d
leaq 0x54f5(%rip), %rsi      # 0xb95f
movq %r13, %rdx
movl %r15d, %ecx
xorl %eax, %eax
callq 0x2170
subl %ebx, %r12d
movl %r12d, -0x2c(%rbp)
movq -0x48(%rbp), %rsp
jle 0x649b
testb %r14b, %r14b
movq -0x50(%rbp), %rdi
jne 0x6283
jmp 0x649b
movq -0x48(%rbp), %rsp
movq %rbx, -0x40(%rbp)
movq -0x40(%rbp), %rax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5e93(%rip), %rdi      # 0xc348
leaq 0x4643(%rip), %rsi      # 0xaaff
leaq 0x4655(%rip), %rcx      # 0xab18
movl $0x59, %edx
callq 0x2110