// File: /workspace/github_cpp_makefile_star2_O3/spatialfruitsalad[P]volume2position/./cSegmentationByOtsu.h
// Function: cSegmentationByOtsu::CalculateSegmentationThreshold(std::vector<double, std::allocator<double>> const&)
static double CalculateSegmentationThreshold (std::vector<double> const & _vals)
    {
        // main code by fabian
        long GRAYLEVEL = _vals.size();
        double prob[GRAYLEVEL], omega[GRAYLEVEL]; /* prob of graylevels */
        double myu[GRAYLEVEL];   /* mean value for separation */
        double max_sigma, sigma[GRAYLEVEL]; /* inter-class variance */
        long i; /* Loop variable */
        int threshold; /* threshold for binarization */

        std::cout << "Otsu's binarization process starts now." << std::endl;
        std::cout << "Working on a histogram with " << GRAYLEVEL << " bins" << std::endl;


        /* calculation of probability density */
        for ( i = 0; i < GRAYLEVEL; i ++ ) {
            prob[i] = _vals.at(i);
        }

        /* omega & myu generation */
        omega[0] = prob[0];
        myu[0] = 0.0;       /* 0.0 times prob[0] equals zero */
        for (i = 1; i < GRAYLEVEL; i++) {
            omega[i] = omega[i-1] + prob[i];
            myu[i] = myu[i-1] + i*prob[i];
        }

        /* sigma maximization
           sigma stands for inter-class variance 
           and determines optimal threshold value */
        threshold = 0;
        max_sigma = 0.0;
        for (i = 0; i < GRAYLEVEL-1; i++) 
        {
            if (omega[i] != 0.0 && omega[i] != 1.0)
            {
                sigma[i] = pow(myu[GRAYLEVEL-1]*omega[i] - myu[i], 2) / (omega[i]*(1.0 - omega[i]));
            }
            else
            {
                sigma[i] = 0.0;
            }
            if (sigma[i] > max_sigma) 
            {
                max_sigma = sigma[i];
                threshold = i;
            }
        }
        std::cout << std::endl << "threshold value ="  << threshold << std::endl;
        return threshold;  
    }

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %r15
movq 0x8(%rdi), %rax
subq (%rdi), %rax
movq %rax, %rbx
sarq $0x3, %rbx
movq %rax, -0x30(%rbp)
addq $0xf, %rax
andq $-0x10, %rax
movq %rsp, %r13
subq %rax, %r13
movq %r13, %rsp
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x38(%rbp)
movq %rcx, %rsp
movq %rsp, %r14
subq %rax, %r14
movq %r14, %rsp
movq 0x14398(%rip), %r12     # 0x1cfc8
leaq 0xeac5(%rip), %rsi      # 0x176fc
movl $0x27, %edx
movq %r12, %rdi
callq 0x32f0
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x32a0
movsbl %al, %esi
movq %r12, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31e0
leaq 0xeab1(%rip), %rsi      # 0x17724
movl $0x1c, %edx
movq %r12, %rdi
callq 0x32f0
movq %r12, %rdi
movq %rbx, %rsi
callq 0x3290
movq %rax, %r12
leaq 0xeaac(%rip), %rsi      # 0x17741
movl $0x5, %edx
movq %rax, %rdi
callq 0x32f0
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x32a0
movsbl %al, %esi
movq %r12, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31e0
testq %rbx, %rbx
jle 0x8ddb
movq (%r15), %rax
movq 0x8(%r15), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
xorl %ecx, %ecx
cmpq %rcx, %rdx
je 0x8e70
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%r13,%rcx,8)
incq %rcx
cmpq %rcx, %rbx
jne 0x8ce3
movsd (%r13), %xmm0
movq -0x38(%rbp), %rdx
movsd %xmm0, (%rdx)
movq $0x0, (%r14)
xorl %r15d, %r15d
movq -0x30(%rbp), %rcx
cmpq $0x8, %rcx
je 0x8dde
movl $0x1, %eax
xorpd %xmm1, %xmm1
movsd (%r13,%rax,8), %xmm2
addsd %xmm2, %xmm0
movsd %xmm0, (%rdx,%rax,8)
xorps %xmm3, %xmm3
cvtsi2sd %rax, %xmm3
mulsd %xmm2, %xmm3
addsd %xmm3, %xmm1
movsd %xmm1, (%r14,%rax,8)
incq %rax
cmpq %rax, %rbx
jne 0x8d2f
cmpq $0x8, %rcx
movq 0x14260(%rip), %r12     # 0x1cfc8
je 0x8de5
leaq -0x1(%rbx), %rax
xorpd %xmm0, %xmm0
xorl %r15d, %r15d
movsd 0xe2d3(%rip), %xmm1     # 0x17050
xorl %ecx, %ecx
xorpd %xmm2, %xmm2
movsd (%rdx,%rcx,8), %xmm4
ucomisd %xmm0, %xmm4
xorpd %xmm3, %xmm3
jne 0x8d94
jnp 0x8dc1
ucomisd %xmm1, %xmm4
jne 0x8d9c
jnp 0x8dc1
movsd -0x8(%r14,%rbx,8), %xmm3
mulsd %xmm4, %xmm3
subsd (%r14,%rcx,8), %xmm3
mulsd %xmm3, %xmm3
movapd %xmm1, %xmm5
subsd %xmm4, %xmm5
mulsd %xmm4, %xmm5
divsd %xmm5, %xmm3
ucomisd %xmm2, %xmm3
cmoval %ecx, %r15d
maxsd %xmm2, %xmm3
incq %rcx
movapd %xmm3, %xmm2
cmpq %rcx, %rax
jne 0x8d83
jmp 0x8de5
xorl %r15d, %r15d
movq 0x141e3(%rip), %r12     # 0x1cfc8
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x32a0
movsbl %al, %esi
movq %r12, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31e0
movq %rax, %rbx
leaq 0xe930(%rip), %rsi      # 0x17747
movl $0x11, %edx
movq %rax, %rdi
callq 0x32f0
movq %rbx, %rdi
movl %r15d, %esi
callq 0x3420
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x32a0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31e0
xorps %xmm0, %xmm0
cvtsi2sd %r15d, %xmm0
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xe8e2(%rip), %rdi      # 0x17759
movq %rdx, %rsi
xorl %eax, %eax
callq 0x3390
nop