// File: 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
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rdi
callq 0x10520
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq %rsp, %rcx
movq %rcx, -0x18(%rbp)
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x70(%rbp)
movq %rcx, %rsp
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x68(%rbp)
movq %rcx, %rsp
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x60(%rbp)
movq %rcx, %rsp
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x58(%rbp)
movq %rcx, %rsp
movq %rax, -0x40(%rbp)
movq 0x2a0c5(%rip), %rdi     # 0x33fc0
leaq 0x1e787(%rip), %rsi     # 0x28689
callq 0x3290
movq %rax, %rdi
movq 0x2a08f(%rip), %rsi     # 0x33fa0
callq 0x32c0
movq 0x2a0a3(%rip), %rdi     # 0x33fc0
leaq 0x1e78d(%rip), %rsi     # 0x286b1
callq 0x3290
movq %rax, %rdi
movq -0x10(%rbp), %rsi
callq 0x34d0
movq %rax, %rdi
leaq 0x1e78f(%rip), %rsi     # 0x286ce
callq 0x3290
movq %rax, %rdi
movq 0x2a052(%rip), %rsi     # 0x33fa0
callq 0x32c0
movq $0x0, -0x48(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x10(%rbp), %rax
jge 0x9f94
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x12490
movq %rax, %rcx
movq -0x70(%rbp), %rax
movsd (%rcx), %xmm0
movq -0x48(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x9f5b
movq -0x60(%rbp), %rax
movq -0x68(%rbp), %rcx
movq -0x70(%rbp), %rdx
movsd (%rdx), %xmm0
movsd %xmm0, (%rcx)
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
movq $0x1, -0x48(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x10(%rbp), %rax
jge 0xa027
movq -0x60(%rbp), %rax
movq -0x70(%rbp), %rcx
movq -0x68(%rbp), %rdx
movq -0x48(%rbp), %rsi
subq $0x1, %rsi
movsd (%rdx,%rsi,8), %xmm0
movq -0x48(%rbp), %rsi
addsd (%rcx,%rsi,8), %xmm0
movq -0x48(%rbp), %rsi
movsd %xmm0, (%rdx,%rsi,8)
movq -0x48(%rbp), %rdx
subq $0x1, %rdx
movsd (%rax,%rdx,8), %xmm1
cvtsi2sdq -0x48(%rbp), %xmm0
movq -0x48(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq -0x48(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x9fb7
movl $0x0, -0x4c(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x38(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq -0x10(%rbp), %rcx
subq $0x1, %rcx
cmpq %rcx, %rax
jge 0xa16a
movq -0x68(%rbp), %rax
movq -0x48(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0xa070
jp 0xa070
jmp 0xa11a
movq -0x68(%rbp), %rax
movq -0x48(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd 0x1dfbb(%rip), %xmm1    # 0x28040
ucomisd %xmm1, %xmm0
jne 0xa092
jp 0xa092
jmp 0xa11a
movq -0x68(%rbp), %rcx
movq -0x60(%rbp), %rax
movq -0x10(%rbp), %rdx
subq $0x1, %rdx
movsd (%rax,%rdx,8), %xmm0
movq -0x48(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm2
movq -0x48(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd 0x1df78(%rip), %xmm1    # 0x28058
callq 0x3490
movq -0x68(%rbp), %rcx
movq -0x58(%rbp), %rax
movq -0x48(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm1
movq -0x48(%rbp), %rdx
movsd 0x1df3e(%rip), %xmm2    # 0x28040
subsd (%rcx,%rdx,8), %xmm2
mulsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movq -0x48(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
jmp 0xa12a
movq -0x58(%rbp), %rax
movq -0x48(%rbp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movq -0x58(%rbp), %rax
movq -0x48(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
ucomisd -0x38(%rbp), %xmm0
jbe 0xa157
movq -0x58(%rbp), %rax
movq -0x48(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x38(%rbp)
movq -0x48(%rbp), %rax
movl %eax, -0x4c(%rbp)
jmp 0xa159
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0xa03e
movq 0x29e4f(%rip), %rdi     # 0x33fc0
movq 0x29e28(%rip), %rsi     # 0x33fa0
callq 0x32c0
movq %rax, %rdi
leaq 0x1e54d(%rip), %rsi     # 0x286d4
callq 0x3290
movq %rax, %rdi
movl -0x4c(%rbp), %esi
callq 0x3470
movq %rax, %rdi
movq 0x29dff(%rip), %rsi     # 0x33fa0
callq 0x32c0
cvtsi2sdl -0x4c(%rbp), %xmm0
movq -0x18(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq
nopw (%rax,%rax)