// File: /workspace/github_cpp_makefile_star2_O2/DoubleString[P]RtConverter/src/Rnxobs/Com.cpp
// Function: bamboo::ambslv_(int, double*, double*, double*, double*)
int ambslv_(int ncad, double* q22, double* bias,double* bias_sec,double* disall) {
	/**********************************************
	 * parameter:
	 * 	input:  ncad          -- # of candidate ambiguities
	 * 		    q22           -- cofactor matrix
	 * 	output: bias          -- float / fixed ambiguity estimates
	 *          disall        -- norm of optimum & suboptimum solutions
	 */
	double* F = (double*) calloc(2 * ncad, sizeof(double));
	double* s = (double*) calloc(2, sizeof(double));
	double dump,f;
	int info  = 0;
	if (ncad > 1) {
		if(!(info = lambda(ncad, 2, bias, q22, F, s))){
			if (s[0] < s[1]) {
				memcpy(bias, F, sizeof(double) * ncad);
				if(bias_sec!=NULL)
					memcpy(bias_sec,F + ncad,sizeof(double) * ncad);
				disall[0] = s[0];
				disall[1] = s[1];
			} else {
				memcpy(bias, F + ncad, sizeof(double) * ncad);
				if(bias_sec!=NULL)
					memcpy(bias_sec,F,sizeof(double) * ncad);
				disall[0] = s[1];
				disall[1] = s[0];
			}
		}
	} else {
		f = dump = bias[0];
		bias[0] = NINT(bias[0]) * 1.0;
		dump = bias[0] - dump;
		disall[0] = dump * dump / q22[0];

		dump = 1.0 - fabs(dump);
		disall[1] = dump * dump / q22[0];
		if(bias_sec !=NULL)
			bias_sec[0] = fabs(bias[0] + 1.0 - f) < fabs(bias[0] - 1.0 -f) ? bias[0] + 1.0 : bias[0] - 1.0;
	}
	free(F);
	free(s);
	return info;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r8, %r15
movq %rcx, -0x38(%rbp)
movq %rdx, %r13
movq %rsi, -0x30(%rbp)
movl %edi, %ebx
leal (%rbx,%rbx), %eax
movslq %eax, %rdi
pushq $0x8
popq %r14
movq %r14, %rsi
callq 0x8440
movq %rax, %r12
pushq $0x2
popq %rdi
movq %r14, %rsi
callq 0x8440
movq %rax, %r14
cmpl $0x2, %ebx
movq %r12, -0x40(%rbp)
jl 0x1c220
movq %r15, -0x50(%rbp)
pushq $0x2
popq %rsi
movl %ebx, %edi
movq %r13, %rdx
movq -0x30(%rbp), %rcx
movq %r12, %r8
movq %r14, %r9
callq 0x2cd04
movl %eax, %r15d
testl %eax, %eax
jne 0x1c336
movsd (%r14), %xmm0
movsd 0x8(%r14), %xmm1
movsd %xmm0, -0x30(%rbp)
movsd %xmm1, -0x48(%rbp)
ucomisd %xmm0, %xmm1
movl %ebx, %ebx
jbe 0x1c2ed
leaq (,%rbx,8), %r15
movq %r13, %rdi
movq -0x40(%rbp), %r12
movq %r12, %rsi
movq %r15, %rdx
callq 0x84f0
movq -0x38(%rbp), %rdi
testq %rdi, %rdi
je 0x1c204
leaq (%r12,%rbx,8), %rsi
movq %r15, %rdx
callq 0x84f0
movq -0x50(%rbp), %rax
movsd -0x30(%rbp), %xmm0
movsd %xmm0, (%rax)
movsd -0x48(%rbp), %xmm0
movsd %xmm0, 0x8(%rax)
jmp 0x1c333
movsd (%r13), %xmm0
xorpd %xmm1, %xmm1
xorl %eax, %eax
ucomisd %xmm1, %xmm0
seta %al
leaq 0x44896(%rip), %rcx     # 0x60ad0
movsd (%rcx,%rax,8), %xmm1
addsd %xmm0, %xmm1
cvttpd2dq %xmm1, %xmm1
cvtdq2pd %xmm1, %xmm1
movlpd %xmm1, (%r13)
subsd %xmm0, %xmm1
movapd %xmm1, %xmm2
mulsd %xmm1, %xmm2
movq -0x30(%rbp), %rax
divsd (%rax), %xmm2
movsd %xmm2, (%r15)
andpd 0x436be(%rip), %xmm1    # 0x5f930
movsd 0x436a6(%rip), %xmm2    # 0x5f920
subsd %xmm1, %xmm2
mulsd %xmm2, %xmm2
divsd (%rax), %xmm2
movsd %xmm2, 0x8(%r15)
xorl %r15d, %r15d
movq -0x38(%rbp), %rax
testq %rax, %rax
je 0x1c336
movsd (%r13), %xmm1
movsd 0x43676(%rip), %xmm2    # 0x5f920
addsd %xmm1, %xmm2
movapd %xmm2, %xmm3
subsd %xmm0, %xmm3
movapd 0x43672(%rip), %xmm4    # 0x5f930
andpd %xmm4, %xmm3
addsd 0x446d6(%rip), %xmm1    # 0x609a0
movapd %xmm1, %xmm5
subsd %xmm0, %xmm5
andpd %xmm4, %xmm5
cmpltsd %xmm5, %xmm3
andpd %xmm3, %xmm2
andnpd %xmm1, %xmm3
orpd %xmm2, %xmm3
movlpd %xmm3, (%rax)
jmp 0x1c336
movq -0x40(%rbp), %r15
leaq (%r15,%rbx,8), %rsi
shlq $0x3, %rbx
movq %r13, %rdi
movq %rbx, %rdx
callq 0x84f0
movq -0x38(%rbp), %rdi
testq %rdi, %rdi
movq -0x50(%rbp), %r12
je 0x1c31c
movq %r15, %rsi
movq %rbx, %rdx
callq 0x84f0
movsd -0x48(%rbp), %xmm0
movsd %xmm0, (%r12)
movsd -0x30(%rbp), %xmm0
movsd %xmm0, 0x8(%r12)
xorl %r15d, %r15d
movq -0x40(%rbp), %rdi
callq 0x8780
movq %r14, %rdi
callq 0x8780
movl %r15d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq