// File: 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
movq %rsp, %rbp
subq $0x50, %rsp
movb %sil, %al
movq %rdi, -0x8(%rbp)
movb %al, -0x9(%rbp)
movsbq -0x9(%rbp), %rdi
movl $0x1, %esi
callq 0x5180
movq %rax, -0x18(%rbp)
movsbq -0x9(%rbp), %rdi
movl $0x1, %esi
callq 0x5180
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movsbl -0x9(%rbp), %esi
callq 0x74c0
xorb $-0x1, %al
testb $0x1, %al
jne 0x76a0
jmp 0x78b3
xorps %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
movb $0x0, -0xb(%rbp)
movb $0x0, -0xc(%rbp)
movsd 0x48360(%rip), %xmm0    # 0x4fa18
movsd %xmm0, -0x28(%rbp)
movb $0x0, -0xa(%rbp)
movsbl -0xa(%rbp), %eax
movsbl -0x9(%rbp), %ecx
cmpl %ecx, %eax
jge 0x780a
movq -0x8(%rbp), %rax
movsbq -0xa(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
xorps %xmm0, %xmm0
xorl %eax, %eax
movl $0x1, %ecx
ucomisd %xmm1, %xmm0
cmoval %ecx, %eax
movb %al, %dl
movq -0x20(%rbp), %rax
movsbq -0xa(%rbp), %rcx
movb %dl, (%rax,%rcx)
movq -0x20(%rbp), %rax
movsbq -0xa(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x1, %eax
jne 0x771a
xorps %xmm0, %xmm0
movsd %xmm0, -0x38(%rbp)
jmp 0x772d
movq -0x8(%rbp), %rax
movsbq -0xa(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x38(%rbp)
movsd -0x38(%rbp), %xmm0
movq -0x8(%rbp), %rax
movsbq -0xa(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movsbl -0xc(%rbp), %eax
movq -0x20(%rbp), %rcx
movsbq -0xa(%rbp), %rdx
movsbl (%rcx,%rdx), %ecx
addl %ecx, %eax
movb %al, -0xc(%rbp)
movq -0x8(%rbp), %rax
movsbq -0xa(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd 0x4829c(%rip), %xmm1    # 0x4fa08
xorl %eax, %eax
movl $0x1, %ecx
ucomisd %xmm1, %xmm0
cmoval %ecx, %eax
movb %al, %dl
movq -0x18(%rbp), %rax
movsbq -0xa(%rbp), %rcx
movb %dl, (%rax,%rcx)
movq -0x18(%rbp), %rax
movsbq -0xa(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x1, %eax
jne 0x77a9
movsd 0x48266(%rip), %xmm0    # 0x4fa08
movsd %xmm0, -0x40(%rbp)
jmp 0x77bc
movq -0x8(%rbp), %rax
movsbq -0xa(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x40(%rbp)
movsd -0x40(%rbp), %xmm0
movq -0x8(%rbp), %rax
movsbq -0xa(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movsbl -0xb(%rbp), %eax
movq -0x18(%rbp), %rcx
movsbq -0xa(%rbp), %rdx
movsbl (%rcx,%rdx), %ecx
addl %ecx, %eax
movb %al, -0xb(%rbp)
movq -0x8(%rbp), %rax
movsbq -0xa(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd -0x28(%rbp), %xmm0
movsd %xmm0, -0x28(%rbp)
movb -0xa(%rbp), %al
addb $0x1, %al
movb %al, -0xa(%rbp)
jmp 0x76c1
movsbl -0x9(%rbp), %eax
movsbl -0xb(%rbp), %ecx
movsbl -0xc(%rbp), %edx
addl %edx, %ecx
cmpl %ecx, %eax
jle 0x783e
movsd -0x28(%rbp), %xmm0
movsbl -0x9(%rbp), %eax
movsbl -0xb(%rbp), %ecx
movsbl -0xc(%rbp), %edx
addl %edx, %ecx
subl %ecx, %eax
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x30(%rbp)
movb $0x0, -0xa(%rbp)
movsbl -0xa(%rbp), %eax
movsbl -0x9(%rbp), %ecx
cmpl %ecx, %eax
jge 0x78ae
movq -0x20(%rbp), %rax
movsbq -0xa(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x787e
movq -0x18(%rbp), %rax
movsbq -0xa(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x787e
movsd -0x30(%rbp), %xmm0
movsd %xmm0, -0x48(%rbp)
jmp 0x7888
xorps %xmm0, %xmm0
movsd %xmm0, -0x48(%rbp)
jmp 0x7888
movsd -0x48(%rbp), %xmm1
movq -0x8(%rbp), %rax
movsbq -0xa(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movb -0xa(%rbp), %al
addb $0x1, %al
movb %al, -0xa(%rbp)
jmp 0x7842
jmp 0x7688
movsd 0x4814d(%rip), %xmm0    # 0x4fa08
movsd %xmm0, -0x28(%rbp)
movb $0x0, -0xa(%rbp)
movsbl -0xa(%rbp), %eax
movsbl -0x9(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x78f9
movq -0x8(%rbp), %rax
movsbq -0xa(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
movsd -0x28(%rbp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, -0x28(%rbp)
movb -0xa(%rbp), %al
addb $0x1, %al
movb %al, -0xa(%rbp)
jmp 0x78c4
movsd -0x28(%rbp), %xmm0
movq -0x8(%rbp), %rax
movsbl -0x9(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
movsd 0x480ef(%rip), %xmm0    # 0x4fa08
movsd %xmm0, -0x28(%rbp)
movb $0x1, -0xa(%rbp)
movsbl -0xa(%rbp), %eax
movsbl -0x9(%rbp), %ecx
subl $0x0, %ecx
cmpl %ecx, %eax
jge 0x7957
movq -0x8(%rbp), %rax
movsbq -0xa(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
movsd -0x28(%rbp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, -0x28(%rbp)
movb -0xa(%rbp), %al
addb $0x1, %al
movb %al, -0xa(%rbp)
jmp 0x7922
movsd -0x28(%rbp), %xmm0
movq -0x8(%rbp), %rax
movsd %xmm0, (%rax)
movq -0x18(%rbp), %rdi
callq 0x5380
movq -0x20(%rbp), %rdi
callq 0x5380
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)