// File: /workspace/github_c_makefile_star2_O0/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
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
movl $0x0, -0x18(%rbp)
movl -0xc(%rbp), %eax
movl %eax, -0x1c(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rsp, %rcx
movq %rcx, -0x30(%rbp)
leaq (%rax,%rax,4), %rcx
leaq 0xf(,%rcx,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x78(%rbp)
movq %rcx, %rsp
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x1, -0x60(%rbp)
movl $0x1, -0x64(%rbp)
movslq -0x64(%rbp), %rax
movq -0x8(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x7b09
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x64(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq -0x40(%rbp), %rcx
movslq -0x1c(%rbp), %rdx
addq %rdx, %rcx
cmpq %rcx, %rax
ja 0x7a05
cmpq $0x0, -0x40(%rbp)
jne 0x7ac6
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x64(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
cmpq $0x0, (%rax)
jbe 0x7ac6
movq -0x78(%rbp), %rax
movq -0x50(%rbp), %rcx
movl %ecx, %edx
imulq $0x28, -0x50(%rbp), %rsi
movq %rax, %rcx
addq %rsi, %rcx
movl %edx, (%rcx)
movq -0x60(%rbp), %rdx
imulq $0x28, -0x50(%rbp), %rsi
movq %rax, %rcx
addq %rsi, %rcx
movq %rdx, 0x8(%rcx)
movq -0x48(%rbp), %rdx
imulq $0x28, -0x50(%rbp), %rsi
movq %rax, %rcx
addq %rsi, %rcx
movq %rdx, 0x10(%rcx)
movq -0x8(%rbp), %rcx
movq 0x8(%rcx), %rcx
movl -0x64(%rbp), %edx
subl $0x1, %edx
movslq %edx, %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movq (%rcx), %rdx
imulq $0x28, -0x50(%rbp), %rsi
movq %rax, %rcx
addq %rsi, %rcx
movq %rdx, 0x18(%rcx)
movq -0x8(%rbp), %rcx
movq 0x8(%rcx), %rcx
movq -0x58(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movq (%rcx), %rcx
movq -0x50(%rbp), %rdx
movq %rdx, %rsi
addq $0x1, %rsi
movq %rsi, -0x50(%rbp)
imulq $0x28, %rdx, %rdx
addq %rdx, %rax
movq %rcx, 0x20(%rax)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x64(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq %rax, -0x48(%rbp)
movslq -0x64(%rbp), %rax
movq %rax, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x64(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x60(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x60(%rbp)
andq $0x1, %rax
addq -0x58(%rbp), %rax
movq %rax, -0x58(%rbp)
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x79a6
movq -0x78(%rbp), %rax
movq -0x50(%rbp), %rcx
movl %ecx, %edx
imulq $0x28, -0x50(%rbp), %rsi
movq %rax, %rcx
addq %rsi, %rcx
movl %edx, (%rcx)
movq -0x60(%rbp), %rdx
imulq $0x28, -0x50(%rbp), %rsi
movq %rax, %rcx
addq %rsi, %rcx
movq %rdx, 0x8(%rcx)
movq -0x48(%rbp), %rdx
imulq $0x28, -0x50(%rbp), %rsi
movq %rax, %rcx
addq %rsi, %rcx
movq %rdx, 0x10(%rcx)
movq -0x8(%rbp), %rcx
movq 0x8(%rcx), %rcx
movq -0x8(%rbp), %rdx
movq (%rdx), %rdx
subq $0x1, %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movq (%rcx), %rdx
imulq $0x28, -0x50(%rbp), %rsi
movq %rax, %rcx
addq %rsi, %rcx
movq %rdx, 0x18(%rcx)
movq -0x8(%rbp), %rcx
movq 0x8(%rcx), %rcx
movq -0x58(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movq (%rcx), %rcx
movq -0x50(%rbp), %rdx
movq %rdx, %rsi
addq $0x1, %rsi
movq %rsi, -0x50(%rbp)
imulq $0x28, %rdx, %rdx
addq %rdx, %rax
movq %rcx, 0x20(%rax)
movl $0x10, %edi
callq 0x7d90
movq %rax, -0x28(%rbp)
imulq $0x28, -0x50(%rbp), %rdi
callq 0x7d90
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x50(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movl $0x0, -0x68(%rbp)
movslq -0x68(%rbp), %rax
cmpq -0x50(%rbp), %rax
jae 0x7cbd
movq -0x78(%rbp), %rax
movslq -0x68(%rbp), %rcx
imulq $0x28, %rcx, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movl (%rcx), %edx
movq -0x28(%rbp), %rcx
movq 0x8(%rcx), %rcx
movslq -0x68(%rbp), %rsi
imulq $0x28, %rsi, %rsi
addq %rsi, %rcx
movl %edx, (%rcx)
movslq -0x68(%rbp), %rcx
imulq $0x28, %rcx, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq 0x8(%rcx), %rdx
movq -0x28(%rbp), %rcx
movq 0x8(%rcx), %rcx
movslq -0x68(%rbp), %rsi
imulq $0x28, %rsi, %rsi
addq %rsi, %rcx
movq %rdx, 0x8(%rcx)
movslq -0x68(%rbp), %rcx
imulq $0x28, %rcx, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq 0x10(%rcx), %rdx
movq -0x28(%rbp), %rcx
movq 0x8(%rcx), %rcx
movslq -0x68(%rbp), %rsi
imulq $0x28, %rsi, %rsi
addq %rsi, %rcx
movq %rdx, 0x10(%rcx)
movslq -0x68(%rbp), %rcx
imulq $0x28, %rcx, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq 0x18(%rcx), %rdx
movq -0x28(%rbp), %rcx
movq 0x8(%rcx), %rcx
movslq -0x68(%rbp), %rsi
imulq $0x28, %rsi, %rsi
addq %rsi, %rcx
movq %rdx, 0x18(%rcx)
movslq -0x68(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x20(%rax), %rcx
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x68(%rbp), %rdx
imulq $0x28, %rdx, %rdx
addq %rdx, %rax
movq %rcx, 0x20(%rax)
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x7bd7
cmpl $0x0, -0x10(%rbp)
je 0x7d3f
movq -0x50(%rbp), %rax
movl -0x10(%rbp), %ecx
cmpq %rcx, %rax
je 0x7d3f
movq -0x28(%rbp), %rdi
callq 0x7de0
movl -0x10(%rbp), %eax
subq -0x50(%rbp), %rax
movl %eax, -0x6c(%rbp)
cmpl $0x0, -0x14(%rbp)
jge 0x7cee
cmpl $0x0, -0x6c(%rbp)
jg 0x7cfa
cmpl $0x0, -0x14(%rbp)
jle 0x7d01
cmpl $0x0, -0x6c(%rbp)
jge 0x7d01
movl $0x1, -0x18(%rbp)
movl -0x6c(%rbp), %eax
movl %eax, -0x14(%rbp)
movl -0x6c(%rbp), %ecx
movl -0x1c(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x1c(%rbp)
movq 0xd2df(%rip), %rax      # 0x14ff8
movq (%rax), %rdi
movq -0x50(%rbp), %rdx
movl -0x10(%rbp), %ecx
movl -0x1c(%rbp), %r8d
leaq 0x8e21(%rip), %rsi      # 0x10b4f
movb $0x0, %al
callq 0x2140
movq $0x0, -0x28(%rbp)
jmp 0x7d48
movl $0x2, -0x70(%rbp)
jmp 0x7d4f
movl $0x0, -0x70(%rbp)
movq -0x30(%rbp), %rax
movq %rax, %rsp
movl -0x70(%rbp), %eax
testl %eax, %eax
je 0x7d61
jmp 0x7d5f
jmp 0x7d85
jmp 0x7d63
xorl %eax, %eax
cmpl $0x0, -0x1c(%rbp)
movb %al, -0x79(%rbp)
jle 0x7d7a
cmpl $0x0, -0x18(%rbp)
setne %al
xorb $-0x1, %al
movb %al, -0x79(%rbp)
movb -0x79(%rbp), %al
testb $0x1, %al
jne 0x7941
movq -0x28(%rbp), %rax
movq %rbp, %rsp
popq %rbp
retq
nop