// File: /workspace/github_c_makefile_star2_O2/LPD-EPFL[P]mctop/src/cdf.c
// Function: cdf_calc
cdf_t*
cdf_calc(uint64_t* vals, size_t n_vals)
{
  cdf_t* cdf = calloc(1, sizeof(cdf_t));
  assert(cdf != NULL);

  cdf_point_t* points = malloc(n_vals * sizeof(cdf_point_t));
  assert(points != NULL);

  uint64_t* vals_sorted = malloc(n_vals * sizeof(uint64_t));
  assert(vals_sorted != NULL);
  memcpy(vals_sorted, vals, n_vals * sizeof(uint64_t));
  qsort(vals_sorted, n_vals, sizeof(uint64_t), cdf_comp);

  size_t n_points = 0;
  uint64_t curr = vals_sorted[0];
  for (size_t i = 1; i < n_vals; i++)
    {
      if (vals_sorted[i] != curr)
	{
	  points[n_points].val = curr;
	  points[n_points].percentile = (100.0 * i) / n_vals;
	  n_points++;
	  curr = vals_sorted[i];
	}
    }
  points[n_points].val = curr;
  points[n_points].percentile = 100.0;
  n_points++;

  cdf->n_points = n_points;
  cdf->points = malloc(n_points * sizeof(cdf_point_t));
  assert(cdf->points != NULL);
  memcpy(cdf->points, points, n_points * sizeof(cdf_point_t));

  free(points);
  free(vals_sorted);
  return cdf;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r12
movq %rdi, %rbp
pushq $0x1
popq %r13
pushq $0x10
popq %rsi
movq %r13, %rdi
callq 0x2150
testq %rax, %rax
je 0x59f7
movq %rax, (%rsp)
movq %r12, %rdi
shlq $0x4, %rdi
callq 0x21d0
testq %rax, %rax
je 0x5a10
movq %rax, %r14
leaq (,%r12,8), %rbx
movq %rbx, %rdi
callq 0x21d0
testq %rax, %rax
je 0x5a29
movq %rax, %r15
movq %rax, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x2190
leaq 0x169(%rip), %rcx       # 0x5a5f
pushq $0x8
popq %rdx
movq %r15, %rdi
movq %r12, %rsi
callq 0x20a0
movq %r12, %xmm1
punpckldq 0x473f(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x4747(%rip), %xmm1     # 0xa060
movq (%r15), %rax
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
xorl %ebp, %ebp
movq 0x471e(%rip), %xmm1     # 0xa050
movapd 0x4726(%rip), %xmm2     # 0xa060
movsd 0x46c6(%rip), %xmm3     # 0xa008
movq (%rsp), %rbx
cmpq %r12, %r13
jae 0x5992
movq (%r15,%r13,8), %rcx
cmpq %rax, %rcx
je 0x598d
movq %rbp, %rdx
shlq $0x4, %rdx
movq %rax, (%r14,%rdx)
movq %r13, %xmm4
punpckldq %xmm1, %xmm4    # xmm4 = xmm4[0],xmm1[0],xmm4[1],xmm1[1]
subpd %xmm2, %xmm4
movapd %xmm4, %xmm5
unpckhpd %xmm4, %xmm5            # xmm5 = xmm5[1],xmm4[1]
addsd %xmm4, %xmm5
mulsd %xmm3, %xmm5
divsd %xmm0, %xmm5
movsd %xmm5, 0x8(%r14,%rdx)
incq %rbp
movq %rcx, %rax
incq %r13
jmp 0x5946
movq %rbp, %rcx
shlq $0x4, %rcx
movq %rax, (%r14,%rcx)
movabsq $0x4059000000000000, %rax # imm = 0x4059000000000000
movq %rax, 0x8(%r14,%rcx)
incq %rbp
movq %rbp, (%rbx)
shlq $0x4, %rbp
movq %rbp, %rdi
callq 0x21d0
movq %rax, 0x8(%rbx)
testq %rax, %rax
je 0x5a42
movq %rax, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x2190
movq %r14, %rdi
callq 0x2030
movq %r15, %rdi
callq 0x2030
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5ef1(%rip), %rdi      # 0xb8ef
leaq 0x5ef6(%rip), %rsi      # 0xb8fb
leaq 0x5ef9(%rip), %rcx      # 0xb905
pushq $0x18
jmp 0x5a59
leaq 0x5f2b(%rip), %rdi      # 0xb942
leaq 0x5edd(%rip), %rsi      # 0xb8fb
leaq 0x5ee0(%rip), %rcx      # 0xb905
pushq $0x1b
jmp 0x5a59
leaq 0x5ef9(%rip), %rdi      # 0xb929
leaq 0x5ec4(%rip), %rsi      # 0xb8fb
leaq 0x5ec7(%rip), %rcx      # 0xb905
pushq $0x1e
jmp 0x5a59
leaq 0x5ef4(%rip), %rdi      # 0xb93d
leaq 0x5eab(%rip), %rsi      # 0xb8fb
leaq 0x5eae(%rip), %rcx      # 0xb905
pushq $0x34
popq %rdx
callq 0x2110