// File: /workspace/github_cpp_makefile_star2_O1/myudelson[P]hmm-scalable/utils.cpp
// Function: projectsimplexOld(double*, signed char)
void projectsimplexOld(NUMBER* ar, NPAR size) {
	NPAR i, num_at_hi, num_at_lo; // number of elements at lower,upper boundary
	NPAR *at_hi = Calloc(NPAR, (size_t)size);
	NPAR *at_lo = Calloc(NPAR, (size_t)size);
	NUMBER err, lambda;
	while( !issimplex(ar, size)) {
        lambda = 0;
		num_at_hi = 0;
		num_at_lo = 0;
		err = -1; // so that of the sum is over 1, the error is 1-sum
		// threshold
		for(i=0; i<size; i++) {
			at_lo[i] = (ar[i]<0)?1:0;
			ar[i] = (at_lo[i]==1)?0:ar[i];
			num_at_lo = (NPAR)(num_at_lo + at_lo[i]);
            
			at_hi[i] = (ar[i]>1)?1:0;
			ar[i] = (at_hi[i]==1)?1:ar[i];
			num_at_hi = (NPAR)(num_at_hi + at_hi[i]);
			
			err += ar[i];
		}
		if (size > (num_at_hi + num_at_lo) )
			lambda = err / (size - (num_at_hi + num_at_lo));
		for(i=0; i<size; i++)
			ar[i] -= (at_lo[i]==0 && at_hi[i]==0)?lambda:0;
		
	} // until satisfied
    // force last to be 1 - sum of all but last
    err = 1;
    for(i=0; i<(size-1); i++) err -= ar[i];
    ar[size-1] = err;
    err = 1;
    for(i=1; i<(size-0); i++) err -= ar[i];
    ar[0] = err;

	free(at_hi);
	free(at_lo);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movl %esi, %ebp
movq %rdi, %rbx
movslq %esi, %r15
movl $0x1, %esi
movq %r15, %rdi
callq 0x4130
movq %rax, %r14
movl $0x1, %esi
movq %r15, %rdi
callq 0x4130
movq %rax, %r15
testb %bpl, %bpl
setle %cl
movzbl %bpl, %eax
movsd 0x2e17b(%rip), %xmm0    # 0x348f0
movapd 0x2e1b3(%rip), %xmm1    # 0x34930
movsd 0x2e173(%rip), %xmm2    # 0x348f8
movsd 0x2e15b(%rip), %xmm3    # 0x348e8
xorpd %xmm4, %xmm4
movl %ecx, %edx
movapd %xmm0, %xmm5
testb %bpl, %bpl
jle 0x67e2
movsd (%rbx), %xmm6
movapd %xmm0, %xmm5
subsd %xmm6, %xmm5
xorl %edx, %edx
ucomisd %xmm6, %xmm4
ja 0x67e2
ucomisd %xmm0, %xmm6
ja 0x67e2
movl $0x1, %esi
movq %rsi, %rdx
cmpq %rsi, %rax
je 0x67dc
movsd (%rbx,%rdx,8), %xmm6
subsd %xmm6, %xmm5
ucomisd %xmm6, %xmm4
ja 0x67dc
leaq 0x1(%rdx), %rsi
ucomisd %xmm0, %xmm6
jbe 0x67bb
cmpq %rax, %rdx
setae %dl
testb %dl, %dl
je 0x67f4
andpd %xmm1, %xmm5
ucomisd %xmm5, %xmm2
ja 0x68b3
testb %bpl, %bpl
jle 0x685a
xorl %edx, %edx
movapd %xmm3, %xmm5
xorl %esi, %esi
xorl %edi, %edi
movsd (%rbx,%rdx,8), %xmm6
ucomisd %xmm6, %xmm4
seta %r8b
seta (%r15,%rdx)
xorpd %xmm7, %xmm7
maxsd %xmm6, %xmm7
movsd %xmm7, (%rbx,%rdx,8)
addb %r8b, %dil
ucomisd %xmm0, %xmm7
seta %r8b
seta (%r14,%rdx)
movapd %xmm0, %xmm6
minsd %xmm7, %xmm6
movsd %xmm6, (%rbx,%rdx,8)
addb %r8b, %sil
addsd %xmm6, %xmm5
incq %rdx
cmpq %rdx, %rax
jne 0x6803
movsbl %sil, %esi
movsbl %dil, %edx
addl %esi, %edx
jmp 0x6860
xorl %edx, %edx
movapd %xmm3, %xmm5
movl %ebp, %esi
subl %edx, %esi
jg 0x686c
xorpd %xmm5, %xmm5
jmp 0x6877
xorps %xmm6, %xmm6
cvtsi2sd %esi, %xmm6
divsd %xmm6, %xmm5
testb %bpl, %bpl
jle 0x6791
xorl %edx, %edx
xorpd %xmm6, %xmm6
cmpb $0x0, (%r15,%rdx)
jne 0x6898
cmpb $0x0, (%r14,%rdx)
jne 0x6898
movapd %xmm5, %xmm6
movsd (%rbx,%rdx,8), %xmm7
subsd %xmm6, %xmm7
movsd %xmm7, (%rbx,%rdx,8)
incq %rdx
cmpq %rdx, %rax
jne 0x6882
jmp 0x6791
movsbl %bpl, %ecx
decl %ecx
movsd 0x2e02f(%rip), %xmm0    # 0x348f0
movapd %xmm0, %xmm1
cmpb $0x2, %bpl
jl 0x68e7
movsd 0x2e01d(%rip), %xmm1    # 0x348f0
xorl %edx, %edx
movsbq %dl, %rdx
subsd (%rbx,%rdx,8), %xmm1
incb %dl
movsbl %dl, %esi
cmpl %esi, %ecx
jg 0x68d5
movslq %ecx, %rcx
movsd %xmm1, (%rbx,%rcx,8)
cmpb $0x2, %bpl
jl 0x690f
movsd 0x2dff3(%rip), %xmm0    # 0x348f0
movl $0x1, %ecx
subsd (%rbx,%rcx,8), %xmm0
incq %rcx
cmpq %rcx, %rax
jne 0x6902
movsd %xmm0, (%rbx)
movq %r14, %rdi
callq 0x42a0
movq %r15, %rdi
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x42a0