// File: /workspace/github_cpp_makefile_star2_O1/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 0x11e61
movq (%r14), %r12
movq $0x0, 0x18(%r12)
leaq 0x28(%r12), %rdi
callq 0x12b88
movb $0x0, 0x21(%r12)
addq $0x8, %r14
jmp 0x11e3a
cmpl $0x0, 0x80(%rbx)
jle 0x11f5f
xorl %r12d, %r12d
leaq 0x8(%rsp), %r14
leaq 0x10(%rsp), %r15
cmpl $0x0, 0x7c(%rbx)
jle 0x11ed3
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
xorl %r13d, %r13d
movsd 0x2cf7(%rip), %xmm0     # 0x14b88
movsd %xmm0, (%rsp)
movq (%rbx), %rax
movq 0x18(%rbx), %rcx
movq (%rcx,%r12,8), %rdi
movq (%rax,%r13,8), %rsi
callq 0x129d4
movq 0x30(%rbx), %rax
subsd (%rax,%r13,8), %xmm0
movsd (%rsp), %xmm1
ucomisd %xmm0, %xmm1
cmoval %r13d, %ebp
minsd %xmm1, %xmm0
incq %r13
movslq 0x7c(%rbx), %rax
cmpq %rax, %r13
jl 0x11e91
jmp 0x11ed8
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %ebp, %ebp
js 0x12132
movq 0x18(%rbx), %rax
movq (%rax,%r12,8), %rcx
movslq 0xc(%rcx), %rax
movl %ebp, %r13d
cmpl %ebp, %eax
je 0x11f0e
movl %ebp, 0xc(%rcx)
movq (%rbx), %rcx
movq (%rcx,%r13,8), %rdx
movb $0x1, 0x21(%rdx)
cmpl $-0x1, %eax
je 0x11f0e
movq (%rcx,%rax,8), %rax
movb $0x1, 0x21(%rax)
movq (%rbx), %rax
movq (%rax,%r13,8), %rdi
movl %r12d, 0x8(%rsp)
addq $0x28, %rdi
movq %rdi, 0x10(%rsp)
movq %r14, %rsi
movq %r15, %rdx
callq 0x12c50
movq (%rbx), %rax
movq 0x18(%rbx), %rcx
movq (%rax,%r13,8), %rax
movq (%rcx,%r12,8), %rcx
movsd 0x10(%rcx), %xmm0
addsd 0x18(%rax), %xmm0
movsd %xmm0, 0x18(%rax)
incq %r12
movslq 0x80(%rbx), %rax
cmpq %rax, %r12
jl 0x11e7b
cmpb $0x0, 0x71(%rbx)
je 0x1207c
movq 0x18(%rbx), %r14
movq 0x20(%rbx), %r15
cmpq %r15, %r14
je 0x11f84
movq (%r14), %rdi
callq 0x12a72
addq $0x8, %r14
jmp 0x11f71
movq (%rbx), %r13
movq 0x8(%rbx), %rax
movq %rax, (%rsp)
cmpq %rax, %r13
je 0x1207c
leaq 0x10(%rsp), %r14
movq 0x9004(%rip), %r12      # 0x1afa8
leaq 0x2df1(%rip), %rbp      # 0x14d9c
movq (%r13), %r15
movq %r15, %rdi
callq 0x1273c
leaq 0x28(%r15), %rsi
xorl %ecx, %ecx
movq %rcx, 0x10(%rsp)
movq 0x30(%r15), %rax
movq %rax, 0x18(%rsp)
movq %rcx, 0x20(%rsp)
movq 0x40(%r15), %rax
movq %rax, 0x28(%rsp)
movupd 0x48(%r15), %xmm0
leaq 0x30(%rsp), %rax
movupd %xmm0, (%rax)
movq %rcx, 0x40(%rsp)
movq %r14, 0x8(%rsp)
movq %r14, %rdi
leaq 0x8(%rsp), %rdx
callq 0x12f30
movl $0x12, %edx
movq %r12, %rdi
leaq 0x2d7b(%rip), %rsi      # 0x14d89
callq 0x73a0
movq 0x20(%rsp), %r15
testq %r15, %r15
je 0x1203e
movl 0x8(%r15), %esi
movq %r12, %rdi
callq 0x75f0
movl $0x2, %edx
movq %rax, %rdi
movq %rbp, %rsi
callq 0x73a0
movq (%r15), %r15
jmp 0x12018
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x7320
movsbl %al, %esi
movq %r12, %rdi
callq 0x7040
movq %rax, %rdi
callq 0x7240
movq %r14, %rdi
callq 0x1306e
addq $0x8, %r13
cmpq (%rsp), %r13
jne 0x11fab
movslq 0x7c(%rbx), %rax
testq %rax, %rax
jle 0x120b7
movq (%rbx), %rcx
movq 0x48(%rbx), %rdx
xorpd %xmm1, %xmm1
xorl %esi, %esi
movq (%rcx,%rsi,8), %rdi
movsd 0x18(%rdi), %xmm0
subsd 0x10(%rdi), %xmm0
movsd %xmm0, (%rdx,%rsi,8)
maxsd %xmm1, %xmm0
incq %rsi
movapd %xmm0, %xmm1
cmpq %rsi, %rax
jne 0x12092
jmp 0x120bb
xorpd %xmm0, %xmm0
cmpb $0x0, 0x70(%rbx)
jne 0x120c7
cmpb $0x1, 0x71(%rbx)
jne 0x12123
movq 0x8eda(%rip), %rbx      # 0x1afa8
leaq 0x2cca(%rip), %rsi      # 0x14d9f
movl $0x14, %edx
movq %rbx, %rdi
movsd %xmm0, (%rsp)
callq 0x73a0
movq %rbx, %rdi
movsd (%rsp), %xmm0
callq 0x7560
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x7320
movsbl %al, %esi
movq %rbx, %rdi
callq 0x7040
movq %rax, %rdi
callq 0x7240
movsd (%rsp), %xmm0
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8ea7(%rip), %rdi      # 0x1afe0
leaq 0x2bf0(%rip), %rsi      # 0x14d30
callq 0x7330
leaq 0x2a8b(%rip), %rsi      # 0x14bd7
movq %rax, %rdi
callq 0x7330
leaq 0x2a83(%rip), %rsi      # 0x14bde
movq %rax, %rdi
callq 0x7330
movq %rax, %rdi
movl $0xb7, %esi
callq 0x75f0
leaq 0x22dc(%rip), %rsi      # 0x14453
movq %rax, %rdi
callq 0x7330
leaq 0x2bd1(%rip), %rsi      # 0x14d57
movq %rax, %rdi
callq 0x7330
movq %rax, %rdi
callq 0x7190
callq 0x7100
jmp 0x1219d
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x1306e
movq %rbx, %rdi
callq 0x7620