// File: /workspace/github_cpp_makefile_star2_O3/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 0x11dcf
movq (%r14), %r12
movq $0x0, 0x18(%r12)
leaq 0x28(%r12), %rdi
callq 0x12b98
movb $0x0, 0x21(%r12)
addq $0x8, %r14
jmp 0x11da8
cmpl $0x0, 0x80(%rbx)
jle 0x11ec9
xorl %r12d, %r12d
leaq 0x8(%rsp), %r14
leaq 0x10(%rsp), %r15
cmpl $0x0, 0x7c(%rbx)
jle 0x1209c
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
xorl %r13d, %r13d
movsd 0x2da5(%rip), %xmm0     # 0x14ba8
movsd %xmm0, (%rsp)
movq (%rbx), %rax
movq 0x18(%rbx), %rcx
movq (%rcx,%r12,8), %rdi
movq (%rax,%r13,8), %rsi
callq 0x129e4
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 0x11e03
testl %ebp, %ebp
js 0x1209c
movq (%rbx), %rax
movq 0x18(%rbx), %rcx
movq (%rcx,%r12,8), %rdx
movslq 0xc(%rdx), %rcx
movl %ebp, %r13d
cmpl %ebp, %ecx
jne 0x11e67
movq (%rax,%r13,8), %rdi
jmp 0x11e7f
movl %ebp, 0xc(%rdx)
movq (%rax,%r13,8), %rdi
movb $0x1, 0x21(%rdi)
cmpl $-0x1, %ecx
je 0x11e7f
movq (%rax,%rcx,8), %rax
movb $0x1, 0x21(%rax)
movl %r12d, 0x8(%rsp)
addq $0x28, %rdi
movq %rdi, 0x10(%rsp)
movq %r14, %rsi
movq %r15, %rdx
callq 0x12c60
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 0x11de9
cmpb $0x0, 0x71(%rbx)
je 0x11fe6
movq 0x18(%rbx), %r14
movq 0x20(%rbx), %r15
cmpq %r15, %r14
je 0x11eee
movq (%r14), %rdi
callq 0x12a82
addq $0x8, %r14
jmp 0x11edb
movq (%rbx), %r13
movq 0x8(%rbx), %rax
movq %rax, (%rsp)
cmpq %rax, %r13
je 0x11fe6
leaq 0x10(%rsp), %r14
movq 0x909a(%rip), %r12      # 0x1afa8
leaq 0x2ea7(%rip), %rbp      # 0x14dbc
movq (%r13), %r15
movq %r15, %rdi
callq 0x1274a
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 0x12f08
movl $0x12, %edx
movq %r12, %rdi
leaq 0x2e31(%rip), %rsi      # 0x14da9
callq 0x73a0
movq 0x20(%rsp), %r15
testq %r15, %r15
je 0x11fa8
movl 0x8(%r15), %esi
movq %r12, %rdi
callq 0x75f0
movl $0x2, %edx
movq %rax, %rdi
movq %rbp, %rsi
callq 0x73a0
movq (%r15), %r15
jmp 0x11f82
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 0x13046
addq $0x8, %r13
cmpq (%rsp), %r13
jne 0x11f15
movslq 0x7c(%rbx), %rax
testq %rax, %rax
jle 0x12021
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 0x11ffc
jmp 0x12025
xorpd %xmm0, %xmm0
cmpb $0x0, 0x70(%rbx)
jne 0x12031
cmpb $0x1, 0x71(%rbx)
jne 0x1208d
movq 0x8f70(%rip), %rbx      # 0x1afa8
leaq 0x2d80(%rip), %rsi      # 0x14dbf
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 0x8f3d(%rip), %rdi      # 0x1afe0
leaq 0x2ca6(%rip), %rsi      # 0x14d50
callq 0x7330
leaq 0x2b41(%rip), %rsi      # 0x14bf7
movq %rax, %rdi
callq 0x7330
leaq 0x2b39(%rip), %rsi      # 0x14bfe
movq %rax, %rdi
callq 0x7330
movq %rax, %rdi
movl $0xb7, %esi
callq 0x75f0
leaq 0x2372(%rip), %rsi      # 0x14453
movq %rax, %rdi
callq 0x7330
leaq 0x2c87(%rip), %rsi      # 0x14d77
movq %rax, %rdi
callq 0x7330
movq %rax, %rdi
callq 0x7190
callq 0x7100
jmp 0x12107
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x13046
movq %rbx, %rdi
callq 0x7620