// File: /workspace/github_cpp_makefile_star2_O3/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 %r12
pushq %rbx
movl %esi, %ebp
movq %rdi, %rbx
movl %ebp, %r12d
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
movzbl %bpl, %eax
movsd 0x2d279(%rip), %xmm0    # 0x338f0
movapd 0x2d2b1(%rip), %xmm1    # 0x33930
movsd 0x2d271(%rip), %xmm2    # 0x338f8
movsd 0x2d259(%rip), %xmm3    # 0x338e8
xorpd %xmm4, %xmm4
testb %r12b, %r12b
jle 0x66de
movsd (%rbx), %xmm6
ucomisd %xmm6, %xmm4
ja 0x66fb
ucomisd %xmm0, %xmm6
ja 0x66fb
movapd %xmm0, %xmm5
subsd %xmm6, %xmm5
movl $0x1, %edx
movq %rdx, %rcx
cmpq %rdx, %rax
je 0x66d6
movsd (%rbx,%rcx,8), %xmm6
subsd %xmm6, %xmm5
ucomisd %xmm6, %xmm4
ja 0x66d6
leaq 0x1(%rcx), %rdx
ucomisd %xmm0, %xmm6
jbe 0x66b5
cmpq %rax, %rcx
setae %cl
jmp 0x66e4
movb $0x1, %cl
movapd %xmm0, %xmm5
testb %cl, %cl
je 0x66f6
andpd %xmm1, %xmm5
ucomisd %xmm5, %xmm2
ja 0x67b3
testb %r12b, %r12b
jle 0x6693
xorl %ecx, %ecx
movapd %xmm3, %xmm5
xorl %edx, %edx
xorl %esi, %esi
movsd (%rbx,%rcx,8), %xmm7
ucomisd %xmm7, %xmm4
seta %dil
seta (%r15,%rcx)
jbe 0x6727
movb $0x0, (%r14,%rcx)
xorpd %xmm6, %xmm6
xorl %r8d, %r8d
jmp 0x6740
ucomisd %xmm0, %xmm7
seta (%r14,%rcx)
movb $0x1, %r8b
movapd %xmm0, %xmm6
ja 0x6740
xorl %r8d, %r8d
movapd %xmm7, %xmm6
addb %r8b, %dl
addb %dil, %sil
movsd %xmm6, (%rbx,%rcx,8)
addsd %xmm6, %xmm5
incq %rcx
cmpq %rcx, %rax
jne 0x6705
movsbl %dl, %ecx
movsbl %sil, %edx
addl %ecx, %edx
movl %ebp, %ecx
subl %edx, %ecx
jg 0x676c
xorpd %xmm5, %xmm5
jmp 0x6777
xorps %xmm6, %xmm6
cvtsi2sd %ecx, %xmm6
divsd %xmm6, %xmm5
testb %r12b, %r12b
jle 0x6693
xorl %ecx, %ecx
xorpd %xmm6, %xmm6
cmpb $0x0, (%r15,%rcx)
jne 0x6798
cmpb $0x0, (%r14,%rcx)
jne 0x6798
movapd %xmm5, %xmm6
movsd (%rbx,%rcx,8), %xmm7
subsd %xmm6, %xmm7
movsd %xmm7, (%rbx,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x6782
jmp 0x6693
decl %ebp
cmpb $0x2, %r12b
jl 0x6801
movsd 0x2d12d(%rip), %xmm0    # 0x338f0
xorl %ecx, %ecx
movsbq %cl, %rcx
subsd (%rbx,%rcx,8), %xmm0
incb %cl
movsbl %cl, %edx
cmpl %edx, %ebp
jg 0x67c5
movslq %ebp, %rcx
movsd %xmm0, (%rbx,%rcx,8)
cmpb $0x2, %r12b
jl 0x6812
movsd 0x2d103(%rip), %xmm0    # 0x338f0
movl $0x1, %ecx
subsd (%rbx,%rcx,8), %xmm0
incq %rcx
cmpq %rcx, %rax
jne 0x67f2
jmp 0x681a
movslq %ebp, %rax
movabsq $0x3ff0000000000000, %rcx # imm = 0x3FF0000000000000
movq %rcx, (%rbx,%rax,8)
movsd 0x2d0d6(%rip), %xmm0    # 0x338f0
movsd %xmm0, (%rbx)
movq %r14, %rdi
callq 0x42a0
movq %r15, %rdi
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x42a0