// File: /workspace/github_cpp_makefile_star2_O2/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 0x8(%rdi), %rax
movq %rdi, -0x38(%rbp)
subq (%rdi), %rax
movq %rax, %r14
sarq $0x3, %r14
addq $0xf, %rax
andq $-0x10, %rax
movq %rsp, %rbx
subq %rax, %rbx
movq %rbx, %rsp
movq %rsp, %r12
subq %rax, %r12
movq %r12, %rsp
movq %rsp, %r13
subq %rax, %r13
movq %r13, %rsp
movq 0x1118f(%rip), %r15     # 0x17fc8
leaq 0xa881(%rip), %rsi      # 0x116c1
movq %r15, %rdi
callq 0x3260
movq %rax, %rdi
callq 0x3110
leaq 0xa892(%rip), %rsi      # 0x116e9
movq %r15, %rdi
callq 0x3260
movq %rax, %rdi
movq %r14, %rsi
callq 0x3250
leaq 0xa895(%rip), %rsi      # 0x11706
movq %rax, %rdi
callq 0x3260
movq %rax, %rdi
callq 0x3110
xorl %r15d, %r15d
movq %r14, %rax
testq %r14, %r14
movl $0x0, %r14d
movq %rax, -0x30(%rbp)
cmovgq %rax, %r14
cmpq %r15, %r14
je 0x6eb8
movq -0x38(%rbp), %rdi
movq %r15, %rsi
callq 0xa6b2
movsd (%rax), %xmm0
movsd %xmm0, (%rbx,%r15,8)
incq %r15
jmp 0x6e98
andq $0x0, (%r13)
movsd (%rbx), %xmm0
movsd %xmm0, (%r12)
pushq $0x1
popq %rax
xorpd %xmm1, %xmm1
movq -0x30(%rbp), %rdx
cmpq %rdx, %rax
jge 0x6eff
movsd (%rbx,%rax,8), %xmm2
addsd %xmm2, %xmm0
movsd %xmm0, (%r12,%rax,8)
cvtsi2sd %rax, %xmm3
mulsd %xmm2, %xmm3
addsd %xmm3, %xmm1
movsd %xmm1, (%r13,%rax,8)
incq %rax
jmp 0x6ed2
leaq -0x1(%rdx), %rax
xorl %ecx, %ecx
testq %rax, %rax
cmovleq %rcx, %rax
xorpd %xmm1, %xmm1
movsd 0xa130(%rip), %xmm0     # 0x11048
xorl %r14d, %r14d
cmpq %rcx, %rax
je 0x6f75
movsd (%r12,%rcx,8), %xmm3
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm3
jne 0x6f32
jnp 0x6f60
ucomisd %xmm0, %xmm3
jne 0x6f3a
jnp 0x6f60
movsd -0x8(%r13,%rdx,8), %xmm2
mulsd %xmm3, %xmm2
subsd (%r13,%rcx,8), %xmm2
mulsd %xmm2, %xmm2
movapd %xmm0, %xmm4
subsd %xmm3, %xmm4
mulsd %xmm3, %xmm4
divsd %xmm4, %xmm2
ucomisd %xmm1, %xmm2
cmoval %ecx, %r14d
maxsd %xmm1, %xmm2
incq %rcx
movapd %xmm2, %xmm1
jmp 0x6f1b
movq 0x1104c(%rip), %rdi     # 0x17fc8
callq 0x3110
leaq 0xa784(%rip), %rsi      # 0x1170c
movq %rax, %rdi
callq 0x3260
movq %rax, %rdi
movl %r14d, %esi
callq 0x3410
movq %rax, %rdi
callq 0x3110
cvtsi2sd %r14d, %xmm0
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop