// File: schuyler[P]quickfix/include/quickfix/fix.h
// Function: Beacon<float, 2> Beacon<float, 2>::Fix<Beacon<float, 2>::DifferenceSolver>(float, float, int) const
Beacon<F, D> Beacon<F, D>::Fix(F time, F rmsError, int maxTries) const {
    F mseTarget = rmsError * rmsError;
    Beacon best = *this;
    Heap heap;
    int tries;

    best.estimatePosition<Solver>(time);
    heap.push(best);

    for (tries = 0; !heap.empty() && (maxTries <= 0 || tries < maxTries);) {
        Beacon b = heap.top();
        //qfdebug("Fix: " << b.A.rows() << ": " << b.X << " (" << b.Err << ")");
        heap.pop();
        if (b.Err <= mseTarget) break;
        if (b.A.rows() <= D + 2) continue;

        for (int drop = 0; drop < b.A.rows() && (maxTries <= 0 || tries < maxTries); drop++, tries++) {
            Beacon next = b;
            dropRow<Anchors>(next.A, drop);
            dropRow<Ranges>(next.R, drop);
            next.estimatePosition<Solver>(time);
            heap.push(next);
            if (next.Err <= mseTarget) {
                b = next;
                break;
            }
        }
        if (b < best) best = b;
    }
    qfdebug("Fix took " << tries << " of " <<  maxTries << " tries using " << best.A.rows() << " of " <<  A.rows() << " anchors");
    best.clipToBound();
    return best;
}

// Assembly:
subq $0x168, %rsp            # imm = 0x168
movq %rdi, 0x30(%rsp)
movq %rdi, %rax
movq %rax, 0x38(%rsp)
movq %rdi, 0x160(%rsp)
movq %rsi, 0x158(%rsp)
movss %xmm0, 0x154(%rsp)
movss %xmm1, 0x150(%rsp)
movl %edx, 0x14c(%rsp)
movq 0x158(%rsp), %rsi
movss 0x150(%rsp), %xmm0
mulss %xmm0, %xmm0
movss %xmm0, 0x148(%rsp)
movb $0x0, 0x147(%rsp)
callq 0x2a4e30
leaq 0x120(%rsp), %rdi
callq 0x2a3c90
jmp 0x2bfa77
movq 0x30(%rsp), %rdi
movss 0x154(%rsp), %xmm0
callq 0x2b1d10
jmp 0x2bfa8c
movq 0x30(%rsp), %rsi
leaq 0x120(%rsp), %rdi
callq 0x292400
jmp 0x2bfaa0
movl $0x0, 0x110(%rsp)
leaq 0x120(%rsp), %rdi
callq 0x2a8bc0
movb %al, 0x2f(%rsp)
jmp 0x2bfabe
movb 0x2f(%rsp), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, 0x2e(%rsp)
jne 0x2bfafc
jmp 0x2bfacf
movb $0x1, %al
cmpl $0x0, 0x14c(%rsp)
movb %al, 0x2d(%rsp)
jle 0x2bfaf4
movl 0x110(%rsp), %eax
cmpl 0x14c(%rsp), %eax
setl %al
movb %al, 0x2d(%rsp)
movb 0x2d(%rsp), %al
movb %al, 0x2e(%rsp)
movb 0x2e(%rsp), %al
testb $0x1, %al
jne 0x2bfb09
jmp 0x2bfdcd
leaq 0x120(%rsp), %rdi
callq 0x29b160
movq %rax, 0x20(%rsp)
jmp 0x2bfb1d
movq 0x20(%rsp), %rsi
leaq 0xb0(%rsp), %rdi
callq 0x2a4e30
jmp 0x2bfb31
leaq 0x120(%rsp), %rdi
callq 0x29aa20
jmp 0x2bfb40
movss 0xf0(%rsp), %xmm1
movss 0x148(%rsp), %xmm0
ucomiss %xmm1, %xmm0
jb 0x2bfbb2
movl $0x2, 0xac(%rsp)
jmp 0x2bfd8b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x118(%rsp)
movl %eax, 0x114(%rsp)
jmp 0x2bfe2b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x118(%rsp)
movl %eax, 0x114(%rsp)
jmp 0x2bfe05
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x118(%rsp)
movl %eax, 0x114(%rsp)
jmp 0x2bfdbe
leaq 0xb0(%rsp), %rdi
callq 0x2b9410
cmpq $0x4, %rax
jg 0x2bfbd5
movl $0x3, 0xac(%rsp)
jmp 0x2bfd8b
movl $0x0, 0xa8(%rsp)
movslq 0xa8(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0xb0(%rsp), %rdi
callq 0x2b9410
movq 0x10(%rsp), %rcx
movq %rax, %rdx
xorl %eax, %eax
cmpq %rdx, %rcx
movb %al, 0x1f(%rsp)
jge 0x2bfc3a
movb $0x1, %al
cmpl $0x0, 0x14c(%rsp)
movb %al, 0xf(%rsp)
jle 0x2bfc32
movl 0x110(%rsp), %eax
cmpl 0x14c(%rsp), %eax
setl %al
movb %al, 0xf(%rsp)
movb 0xf(%rsp), %al
movb %al, 0x1f(%rsp)
movb 0x1f(%rsp), %al
testb $0x1, %al
jne 0x2bfc47
jmp 0x2bfd52
leaq 0x40(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x2a4e30
jmp 0x2bfc5b
movl 0xa8(%rsp), %esi
leaq 0x40(%rsp), %rdi
callq 0x29d5d0
jmp 0x2bfc6e
leaq 0x50(%rsp), %rdi
movl 0xa8(%rsp), %esi
callq 0x2a7b20
jmp 0x2bfc81
movss 0x154(%rsp), %xmm0
leaq 0x40(%rsp), %rdi
callq 0x2b1d10
jmp 0x2bfc96
leaq 0x120(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x292400
jmp 0x2bfcaa
movss 0x80(%rsp), %xmm1
movss 0x148(%rsp), %xmm0
ucomiss %xmm1, %xmm0
jb 0x2bfd05
leaq 0xb0(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x2b8670
jmp 0x2bfcd5
movl $0x4, 0xac(%rsp)
jmp 0x2bfd10
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x118(%rsp)
movl %eax, 0x114(%rsp)
leaq 0x40(%rsp), %rdi
callq 0x2b39e0
jmp 0x2bfdbe
movl $0x0, 0xac(%rsp)
leaq 0x40(%rsp), %rdi
callq 0x2b39e0
movl 0xac(%rsp), %eax
testl %eax, %eax
je 0x2bfd29
jmp 0x2bfd27
jmp 0x2bfd52
jmp 0x2bfd2b
movl 0xa8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa8(%rsp)
movl 0x110(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x110(%rsp)
jmp 0x2bfbe0
movq 0x30(%rsp), %rsi
leaq 0xb0(%rsp), %rdi
callq 0x291770
testb $0x1, %al
jne 0x2bfd6a
jmp 0x2bfd80
movq 0x30(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x2b8670
jmp 0x2bfd7e
jmp 0x2bfd80
movl $0x0, 0xac(%rsp)
leaq 0xb0(%rsp), %rdi
callq 0x2b39e0
movl 0xac(%rsp), %eax
movl %eax, 0x8(%rsp)
testl %eax, %eax
je 0x2bfdb9
jmp 0x2bfda9
movl 0x8(%rsp), %eax
subl $0x2, %eax
je 0x2bfdcd
jmp 0x2bfdb4
jmp 0x2bfaab
jmp 0x2bfaab
leaq 0xb0(%rsp), %rdi
callq 0x2b39e0
jmp 0x2bfe05
movq 0x30(%rsp), %rdi
callq 0x2b8a00
jmp 0x2bfdd9
movb $0x1, 0x147(%rsp)
movl $0x1, 0xac(%rsp)
leaq 0x120(%rsp), %rdi
callq 0x2b5570
testb $0x1, 0x147(%rsp)
jne 0x2bfe1e
jmp 0x2bfe14
leaq 0x120(%rsp), %rdi
callq 0x2b5570
jmp 0x2bfe2b
movq 0x30(%rsp), %rdi
callq 0x2b39e0
movq 0x38(%rsp), %rax
addq $0x168, %rsp            # imm = 0x168
retq
movq 0x30(%rsp), %rdi
callq 0x2b39e0
movq 0x118(%rsp), %rdi
callq 0x2b6590
nopw %cs:(%rax,%rax)
nopl (%rax)