// File: 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
movq %rsp, %rbp
subq $0x140, %rsp            # imm = 0x140
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x110(%rbp)
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rdi
callq 0x205c0
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rdi
callq 0x205f0
movq %rax, -0x20(%rbp)
leaq -0x18(%rbp), %rdi
leaq -0x20(%rbp), %rsi
callq 0x20620
testb $0x1, %al
jne 0x1f4eb
jmp 0x1f524
leaq -0x18(%rbp), %rdi
callq 0x20660
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
xorps %xmm0, %xmm0
callq 0x20ce0
movq -0x28(%rbp), %rdi
callq 0x20d00
movq -0x28(%rbp), %rdi
callq 0x20d20
leaq -0x18(%rbp), %rdi
callq 0x206a0
jmp 0x1f4d8
movl $0x0, -0x2c(%rbp)
movq -0x110(%rbp), %rcx
movl -0x2c(%rbp), %eax
cmpl 0x80(%rcx), %eax
jge 0x1f76c
movsd 0x686f(%rip), %xmm0     # 0x25db8
movsd %xmm0, -0x38(%rbp)
movl $0xffffffff, -0x3c(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x40(%rbp)
movq -0x110(%rbp), %rcx
movl -0x40(%rbp), %eax
cmpl 0x7c(%rcx), %eax
jge 0x1f609
movq -0x110(%rbp), %rdi
addq $0x18, %rdi
movslq -0x2c(%rbp), %rsi
callq 0x20a80
movq -0x110(%rbp), %rdi
movq (%rax), %rax
movq %rax, -0x120(%rbp)
movslq -0x40(%rbp), %rsi
callq 0x20e10
movq -0x120(%rbp), %rdi
movq (%rax), %rsi
callq 0x20d40
movq -0x110(%rbp), %rdi
movsd %xmm0, -0x118(%rbp)
addq $0x30, %rdi
movslq -0x40(%rbp), %rsi
callq 0x20e30
movsd -0x118(%rbp), %xmm0
subsd (%rax), %xmm0
movsd %xmm0, -0x48(%rbp)
movsd -0x48(%rbp), %xmm1
movsd -0x38(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x1f5f9
movsd -0x48(%rbp), %xmm0
movsd %xmm0, -0x38(%rbp)
movl -0x40(%rbp), %eax
movl %eax, -0x3c(%rbp)
jmp 0x1f5fb
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x1f55c
jmp 0x1f60b
cmpl $0x0, -0x3c(%rbp)
jge 0x1f681
movq 0x179c8(%rip), %rdi     # 0x36fe0
leaq 0x693d(%rip), %rsi      # 0x25f5c
callq 0x9480
movq %rax, %rdi
leaq 0x67d5(%rip), %rsi      # 0x25e03
callq 0x9480
movq %rax, %rdi
leaq 0x67cd(%rip), %rsi      # 0x25e0a
callq 0x9480
movq %rax, %rdi
movl $0xb7, %esi
callq 0x9890
movq %rax, %rdi
leaq 0x5dd2(%rip), %rsi      # 0x2542b
callq 0x9480
movq %rax, %rdi
leaq 0x691b(%rip), %rsi      # 0x25f83
callq 0x9480
movq %rax, %rdi
movq 0x178d1(%rip), %rsi     # 0x36f48
callq 0x94d0
callq 0x9160
jmp 0x1f683
movq -0x110(%rbp), %rdi
addq $0x18, %rdi
movslq -0x2c(%rbp), %rsi
callq 0x20a80
movq (%rax), %rdi
callq 0x20e50
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x3c(%rbp), %eax
je 0x1f701
movq -0x110(%rbp), %rdi
addq $0x18, %rdi
movslq -0x2c(%rbp), %rsi
callq 0x20a80
movq (%rax), %rdi
movl -0x3c(%rbp), %esi
callq 0x20e70
movq -0x110(%rbp), %rdi
movslq -0x3c(%rbp), %rsi
callq 0x20e10
movq (%rax), %rdi
callq 0x20e90
cmpl $-0x1, -0x4c(%rbp)
je 0x1f6ff
movq -0x110(%rbp), %rdi
movslq -0x4c(%rbp), %rsi
callq 0x20e10
movq (%rax), %rdi
callq 0x20e90
jmp 0x1f701
movq -0x110(%rbp), %rdi
movslq -0x3c(%rbp), %rsi
callq 0x20e10
movq (%rax), %rdi
movl -0x2c(%rbp), %esi
callq 0x20eb0
movq -0x110(%rbp), %rdi
movslq -0x3c(%rbp), %rsi
callq 0x20e10
movq -0x110(%rbp), %rdi
movq (%rax), %rax
movq %rax, -0x128(%rbp)
addq $0x18, %rdi
movslq -0x2c(%rbp), %rsi
callq 0x20a80
movq (%rax), %rdi
callq 0x20780
movq -0x128(%rbp), %rdi
callq 0x20ee0
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x1f52b
movq -0x110(%rbp), %rax
testb $0x1, 0x71(%rax)
je 0x1f962
movq -0x110(%rbp), %rax
addq $0x18, %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rdi
callq 0x206c0
movq %rax, -0x60(%rbp)
movq -0x58(%rbp), %rdi
callq 0x206f0
movq %rax, -0x68(%rbp)
leaq -0x60(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x20720
testb $0x1, %al
jne 0x1f7b9
jmp 0x1f7dd
leaq -0x60(%rbp), %rdi
callq 0x20760
movq (%rax), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rdi
callq 0x20f10
leaq -0x60(%rbp), %rdi
callq 0x207a0
jmp 0x1f7a6
movq -0x110(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rdi
callq 0x205c0
movq %rax, -0x80(%rbp)
movq -0x78(%rbp), %rdi
callq 0x205f0
movq %rax, -0x88(%rbp)
leaq -0x80(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x20620
testb $0x1, %al
jne 0x1f81e
jmp 0x1f960
leaq -0x80(%rbp), %rdi
callq 0x20660
movq (%rax), %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rdi
callq 0x20800
movq -0x90(%rbp), %rdi
callq 0x209a0
movq %rax, %rsi
leaq -0xc8(%rbp), %rdi
callq 0x21010
movq 0x17749(%rip), %rdi     # 0x36fa8
leaq 0x674f(%rip), %rsi      # 0x25fb5
callq 0x9480
jmp 0x1f86d
leaq -0xc8(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0xe0(%rbp), %rdi
callq 0x209c0
movq %rax, -0xe8(%rbp)
movq -0xe0(%rbp), %rdi
callq 0x209f0
movq %rax, -0xf0(%rbp)
leaq -0xe8(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x20a20
testb $0x1, %al
jne 0x1f8ba
jmp 0x1f931
leaq -0xe8(%rbp), %rdi
callq 0x20a50
movl (%rax), %eax
movl %eax, -0xf4(%rbp)
movl -0xf4(%rbp), %esi
movq 0x176cd(%rip), %rdi     # 0x36fa8
callq 0x9890
movq %rax, -0x130(%rbp)
jmp 0x1f8e9
movq -0x130(%rbp), %rdi
leaq 0x66d1(%rip), %rsi      # 0x25fc8
callq 0x9480
jmp 0x1f8fe
jmp 0x1f900
leaq -0xe8(%rbp), %rdi
callq 0x20b60
jmp 0x1f8a1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x21040
jmp 0x1fac0
movq 0x17670(%rip), %rdi     # 0x36fa8
movq 0x17609(%rip), %rsi     # 0x36f48
callq 0x94d0
jmp 0x1f946
leaq -0xc8(%rbp), %rdi
callq 0x21040
leaq -0x80(%rbp), %rdi
callq 0x206a0
jmp 0x1f805
jmp 0x1f962
xorps %xmm0, %xmm0
movsd %xmm0, -0x100(%rbp)
movl $0x0, -0x104(%rbp)
movq -0x110(%rbp), %rcx
movl -0x104(%rbp), %eax
cmpl 0x7c(%rcx), %eax
jge 0x1fa63
movq -0x110(%rbp), %rdi
movslq -0x104(%rbp), %rsi
callq 0x20e10
movq (%rax), %rdi
callq 0x20bd0
movq -0x110(%rbp), %rdi
movsd %xmm0, -0x140(%rbp)
movslq -0x104(%rbp), %rsi
callq 0x20e10
movq (%rax), %rdi
callq 0x20680
movq -0x110(%rbp), %rdi
movaps %xmm0, %xmm1
movsd -0x140(%rbp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, -0x138(%rbp)
addq $0x48, %rdi
movslq -0x104(%rbp), %rsi
callq 0x20e30
movsd -0x138(%rbp), %xmm0
movq -0x110(%rbp), %rdi
movsd %xmm0, (%rax)
addq $0x48, %rdi
movslq -0x104(%rbp), %rsi
callq 0x20e30
movsd (%rax), %xmm0
ucomisd -0x100(%rbp), %xmm0
jbe 0x1fa4d
movq -0x110(%rbp), %rdi
addq $0x48, %rdi
movslq -0x104(%rbp), %rsi
callq 0x20e30
movsd (%rax), %xmm0
movsd %xmm0, -0x100(%rbp)
jmp 0x1fa4f
movl -0x104(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x104(%rbp)
jmp 0x1f977
movq -0x110(%rbp), %rax
testb $0x1, 0x70(%rax)
jne 0x1fa7d
movq -0x110(%rbp), %rax
testb $0x1, 0x71(%rax)
je 0x1faaf
movq 0x17524(%rip), %rdi     # 0x36fa8
leaq 0x6540(%rip), %rsi      # 0x25fcb
callq 0x9480
movq %rax, %rdi
movsd -0x100(%rbp), %xmm0
callq 0x9960
movq %rax, %rdi
movq 0x1749e(%rip), %rsi     # 0x36f48
callq 0x94d0
movsd -0x100(%rbp), %xmm0
addq $0x140, %rsp            # imm = 0x140
popq %rbp
retq
movq -0xd0(%rbp), %rdi
callq 0x98c0
nopl (%rax)