// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %rax
movq 0x8(%rdi), %rax
movq %rdi, -0x30(%rbp)
subq (%rdi), %rax
movq %rax, %rbx
sarq $0x3, %rbx
addq $0xf, %rax
andq $-0x10, %rax
movq %rsp, %r15
subq %rax, %r15
movq %r15, %rsp
movq %rsp, %r13
subq %rax, %r13
movq %r13, %rsp
movq %rsp, %r14
subq %rax, %r14
movq %r14, %rsp
movq 0x14230(%rip), %r12     # 0x1cfc8
leaq 0xe96d(%rip), %rsi      # 0x1770c
movl $0x27, %edx
movq %r12, %rdi
callq 0x3300
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x32b0
movsbl %al, %esi
movq %r12, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31e0
leaq 0xe959(%rip), %rsi      # 0x17734
movl $0x1c, %edx
movq %r12, %rdi
callq 0x3300
movq %r12, %rdi
movq %rbx, %rsi
callq 0x32a0
movq %rax, %r12
leaq 0xe954(%rip), %rsi      # 0x17751
movl $0x5, %edx
movq %rax, %rdi
callq 0x3300
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x32b0
movsbl %al, %esi
movq %r12, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31e0
testq %rbx, %rbx
jle 0x8e67
movq -0x30(%rbp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
xorl %esi, %esi
cmpq %rsi, %rdx
je 0x8fc9
movsd (%rax,%rsi,8), %xmm0
movsd %xmm0, (%r15,%rsi,8)
incq %rsi
cmpq %rsi, %rbx
jne 0x8e4b
movsd (%r15), %xmm0
movsd %xmm0, (%r13)
movq $0x0, (%r14)
cmpq $0x2, %rbx
jl 0x8ebe
movsd (%r14), %xmm0
movsd (%r13), %xmm1
movl $0x1, %eax
movsd (%r15,%rax,8), %xmm2
addsd %xmm2, %xmm1
movsd %xmm1, (%r13,%rax,8)
xorps %xmm3, %xmm3
cvtsi2sd %rax, %xmm3
mulsd %xmm2, %xmm3
addsd %xmm3, %xmm0
movsd %xmm0, (%r14,%rax,8)
incq %rax
cmpq %rax, %rbx
jne 0x8e8f
xorl %r15d, %r15d
cmpq $0x2, %rbx
jl 0x8f38
leaq -0x1(%rbx), %rax
xorpd %xmm0, %xmm0
xorl %r15d, %r15d
movsd 0xe176(%rip), %xmm1     # 0x17050
xorl %ecx, %ecx
xorpd %xmm2, %xmm2
movsd (%r13,%rcx,8), %xmm4
ucomisd %xmm0, %xmm4
xorpd %xmm3, %xmm3
jne 0x8ef3
jnp 0x8f20
ucomisd %xmm1, %xmm4
jne 0x8efb
jnp 0x8f20
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 0x8ee0
movq 0x14089(%rip), %rbx     # 0x1cfc8
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x32b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31e0
movq %rax, %rbx
leaq 0xe7e7(%rip), %rsi      # 0x17757
movl $0x11, %edx
movq %rax, %rdi
callq 0x3300
movq %rbx, %rdi
movl %r15d, %esi
callq 0x3430
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x32b0
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 0xe799(%rip), %rdi      # 0x17769
xorl %eax, %eax
callq 0x33a0
nop