// File: /workspace/github_cpp_makefile_star2_O2/icemiliang[P]vot/src/votx/otx.cc
// Function: votx::OTX::compute_gradient()
double OTX::compute_gradient() {
        // Reset mass
        for (auto d : vDiracXs) { d->set_mass(0.0); d->clear_idx(); d->reset_upate_flag(); }

        // Loop over empiricals, find nearest dirac by brute-force add mass
        for (int i = 0; i < vNumEmpiricalXs; i++) {
            double dist = OTX_MAX_DOUBLE;
            int newIdx = -1;

            // Brute-force search for nearest dirac
            for (int j = 0; j < vNumDiracXs; j++) {
                double tmpDist = vEmpiricalXs[i]->distL22(*vDiracXs[j]) - vHs[j];
                if (tmpDist < dist) {
                    dist = tmpDist;
                    newIdx = j;
                }
            }
            ASSERT_VOT(newIdx >= 0, "index < 0, no nearby dirac found for a empirical.");
            // Assign index 

            int oldIdx = vEmpiricalXs[i]->di();
            if (oldIdx != newIdx) {
                vEmpiricalXs[i]->set_dirac_idx(newIdx);
                vDiracXs[newIdx]->set_to_update();
                if (oldIdx != -1) vDiracXs[oldIdx]->set_to_update();
            }
            vDiracXs[newIdx]->add_empirical_idx(i);
            vDiracXs[newIdx]->add_mass(vEmpiricalXs[i]->mass());
        }

        if (vFlagDebug) {
            for (auto e : vEmpiricalXs) e->print();
            for (auto d : vDiracXs) {
                d->print();
                std::unordered_set<int> tmpSet = d->set();
                std::cout << "enclosed indices: ";
                for (auto j : tmpSet) std::cout << j << ", ";
                std::cout << std::endl;
            }
        }

        // update gradient 
        double maxDer = 0.0;
        for (int i = 0; i < vNumDiracXs; i++) {
            vGradient[i] = vDiracXs[i]->mass() - vDiracXs[i]->dirac();
            if (vGradient[i] > maxDer) maxDer = vGradient[i];
        }

        if (vFlagVerb || vFlagDebug) {
            std::cout << "Largest derivative: " << maxDer << std::endl;
        }
        
        return maxDer;
    }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %rbx
movq (%rdi), %r14
movq 0x8(%rdi), %r15
cmpq %r15, %r14
je 0xe806
movq (%r14), %r12
andq $0x0, 0x18(%r12)
leaq 0x28(%r12), %rdi
callq 0xf46c
movb $0x0, 0x21(%r12)
addq $0x8, %r14
jmp 0xe7e2
xorl %r14d, %r14d
pushq $-0x1
popq %rbp
movslq 0x80(%rbx), %rax
cmpq %rax, %r14
jge 0xe8dc
xorl %r12d, %r12d
movl %ebp, %r15d
movsd 0x233e(%rip), %xmm0     # 0x10b68
movslq 0x7c(%rbx), %rax
cmpq %rax, %r12
jge 0xe86e
movq (%rbx), %rax
movq 0x18(%rbx), %rcx
movq (%rcx,%r14,8), %rdi
movq (%rax,%r12,8), %rsi
movsd %xmm0, 0x8(%rsp)
callq 0xf130
movq 0x30(%rbx), %rax
subsd (%rax,%r12,8), %xmm0
movsd 0x8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
cmoval %r12d, %r15d
minsd %xmm1, %xmm0
incq %r12
jmp 0xe82a
testl %r15d, %r15d
js 0xea0f
movq 0x18(%rbx), %rax
movq (%rax,%r14,8), %rcx
movslq 0xc(%rcx), %rax
movl %r15d, %r12d
cmpl %r15d, %eax
je 0xe8a7
movl %r15d, 0xc(%rcx)
movq (%rbx), %rcx
movq (%rcx,%r12,8), %rdx
movb $0x1, 0x21(%rdx)
cmpl $-0x1, %eax
je 0xe8a7
movq (%rcx,%rax,8), %rax
movb $0x1, 0x21(%rax)
movq (%rbx), %rax
movq (%rax,%r12,8), %rdi
movl %r14d, %esi
callq 0xf1c2
movq (%rbx), %rax
movq 0x18(%rbx), %rcx
movq (%rax,%r12,8), %rax
movq (%rcx,%r14,8), %rcx
movsd 0x10(%rcx), %xmm0
addsd 0x18(%rax), %xmm0
movsd %xmm0, 0x18(%rax)
incq %r14
jmp 0xe80c
cmpb $0x0, 0x71(%rbx)
je 0xe985
movq 0x18(%rbx), %r14
movq 0x20(%rbx), %r15
cmpq %r15, %r14
je 0xe901
movq (%r14), %rdi
callq 0xf1d8
addq $0x8, %r14
jmp 0xe8ee
movq (%rbx), %r12
movq 0x8(%rbx), %r14
movq 0xa699(%rip), %r15      # 0x18fa8
leaq 0x2466(%rip), %r13      # 0x10d7c
cmpq %r14, %r12
je 0xe985
movq (%r12), %rbp
movq %rbp, %rdi
callq 0xeee0
addq $0x28, %rbp
leaq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0xf8e8
movq %r15, %rdi
leaq 0x2427(%rip), %rsi      # 0x10d69
callq 0x7330
leaq 0x20(%rsp), %rbp
movq (%rbp), %rbp
testq %rbp, %rbp
je 0xe96d
movl 0x8(%rbp), %esi
movq %r15, %rdi
callq 0x75f0
movq %rax, %rdi
movq %r13, %rsi
callq 0x7330
jmp 0xe94c
movq %r15, %rdi
callq 0x71b0
leaq 0x10(%rsp), %rdi
callq 0xfa1a
addq $0x8, %r12
jmp 0xe916
movl 0x7c(%rbx), %eax
movq (%rbx), %rcx
movq 0x48(%rbx), %rdx
xorl %esi, %esi
testl %eax, %eax
cmovlel %esi, %eax
xorpd %xmm0, %xmm0
cmpq %rsi, %rax
je 0xe9bf
movq (%rcx,%rsi,8), %rdi
movsd 0x18(%rdi), %xmm1
subsd 0x10(%rdi), %xmm1
movsd %xmm1, (%rdx,%rsi,8)
maxsd %xmm0, %xmm1
incq %rsi
movapd %xmm1, %xmm0
jmp 0xe99a
cmpb $0x0, 0x70(%rbx)
jne 0xe9cb
cmpb $0x1, 0x71(%rbx)
jne 0xea00
movq 0xa5d6(%rip), %rdi      # 0x18fa8
leaq 0x23a6(%rip), %rsi      # 0x10d7f
movsd %xmm0, 0x8(%rsp)
callq 0x7330
movq %rax, %rdi
movsd 0x8(%rsp), %xmm0
callq 0x7560
movq %rax, %rdi
callq 0x71b0
movsd 0x8(%rsp), %xmm0
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xa5ca(%rip), %rdi      # 0x18fe0
leaq 0x22f3(%rip), %rsi      # 0x10d10
callq 0x7330
leaq 0x218e(%rip), %rsi      # 0x10bb7
movq %rax, %rdi
callq 0x7330
leaq 0x2186(%rip), %rsi      # 0x10bbe
movq %rax, %rdi
callq 0x7330
movq %rax, %rdi
movl $0xb7, %esi
callq 0x75f0
leaq 0x19e7(%rip), %rsi      # 0x1043b
movq %rax, %rdi
callq 0x7330
leaq 0x22d4(%rip), %rsi      # 0x10d37
movq %rax, %rdi
callq 0x7330
movq %rax, %rdi
callq 0x71b0
callq 0x7120
jmp 0xea7a
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0xfa1a
movq %rbx, %rdi
callq 0x7620
nop