// File: icemiliang[P]vot/src/vot/diagram.cc
// Function: vot::Diagram::compute_gradient()
double Diagram::compute_gradient() {
        // Reset cell mass
        for (int i = 0; i < mNumDiracs; i++) {
            mDiracs[i].reset_mass();
        }
        
        // Assign cellIndex to every measure and add mass to that cell
        int cellIndex = -1;
        for (int i = 0; i < mNumEmpiricals; i++) {
            cellIndex = mDiagram->cell_index(mEmpiricals[i].x(),
                                             mEmpiricals[i].y(), 
                                             mEmpiricals[i].z());
            mEmpiricals[i].set_cellIndex(cellIndex);
            mDiracs[cellIndex].add_mass(mEmpiricals[i].mass());
        }

        // Multiple-point check
        if (mFlagDebug) {
            double sumMass = 0;
            double sumH = 0;
            for (int i = 0; i < mNumDiracs; i++) {
                sumMass += mDiracs[i].mass();
                sumH += mHs[i];
            }
            std::cout << "[Debug] Total mass of Diracs: " << sumMass << std::endl;
            std::cout << "[Debug] Total h of Diracs: " << sumH << std::endl;
            getchar();
        }


        // Compute gradient and their norm
        double gradNorm = 0;
        mGradient = Eigen::VectorXd::Zero(mNumDiracs);
        for (int i = 0; i < mNumDiracs; i++) {
            mGradient(i) = mDiracs[i].mass() - mDiracs[i].dirac();
        }
        gradNorm = mGradient.norm();
        if (mFlagVerbose) {
            std::cout << "Gradient norm: " << gradNorm << std::endl;
        }

        return gradNorm;
    }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x60(%rbp)
movl $0x0, -0xc(%rbp)
movq -0x60(%rbp), %rcx
movl -0xc(%rbp), %eax
cmpl 0x38(%rcx), %eax
jge 0x203fe
movq -0x60(%rbp), %rdi
addq $0x20, %rdi
movslq -0xc(%rbp), %rsi
callq 0x218b0
movq %rax, %rdi
callq 0x22a10
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0x203ce
movl $0xffffffff, -0x10(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x14(%rbp)
movq -0x60(%rbp), %rcx
movl -0x14(%rbp), %eax
cmpl 0x3c(%rcx), %eax
jge 0x204f2
movq -0x60(%rbp), %rdi
movq (%rdi), %rax
movq %rax, -0x80(%rbp)
addq $0x8, %rdi
movslq -0x14(%rbp), %rsi
callq 0x21ab0
movq %rax, %rdi
callq 0x218d0
movq -0x60(%rbp), %rdi
movsd %xmm0, -0x70(%rbp)
addq $0x8, %rdi
movslq -0x14(%rbp), %rsi
callq 0x21ab0
movq %rax, %rdi
callq 0x218f0
movq -0x60(%rbp), %rdi
movsd %xmm0, -0x78(%rbp)
addq $0x8, %rdi
movslq -0x14(%rbp), %rsi
callq 0x21ab0
movq %rax, %rdi
callq 0x21910
movq -0x80(%rbp), %rdi
movsd -0x78(%rbp), %xmm1
movaps %xmm0, %xmm2
movsd -0x70(%rbp), %xmm0
callq 0x57310
movq -0x60(%rbp), %rdi
movl %eax, -0x10(%rbp)
addq $0x8, %rdi
movslq -0x14(%rbp), %rsi
callq 0x21ab0
movq %rax, %rdi
movl -0x10(%rbp), %esi
callq 0x22a30
movq -0x60(%rbp), %rdi
addq $0x20, %rdi
movslq -0x10(%rbp), %rsi
callq 0x218b0
movq -0x60(%rbp), %rdi
movq %rax, -0x68(%rbp)
addq $0x8, %rdi
movslq -0x14(%rbp), %rsi
callq 0x21ab0
movq %rax, %rdi
callq 0x21ad0
movq -0x68(%rbp), %rdi
callq 0x22a50
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x2040c
movq -0x60(%rbp), %rax
testb $0x1, 0x69(%rax)
je 0x205d3
xorps %xmm0, %xmm0
movsd %xmm0, -0x20(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movq -0x60(%rbp), %rcx
movl -0x2c(%rbp), %eax
cmpl 0x38(%rcx), %eax
jge 0x20570
movq -0x60(%rbp), %rdi
addq $0x20, %rdi
movslq -0x2c(%rbp), %rsi
callq 0x218b0
movq %rax, %rdi
callq 0x21930
movq -0x60(%rbp), %rdi
addsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x20(%rbp)
addq $0x50, %rdi
movslq -0x2c(%rbp), %rsi
callq 0x21950
movsd (%rax), %xmm0
addsd -0x28(%rbp), %xmm0
movsd %xmm0, -0x28(%rbp)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x20517
movq 0x9ea21(%rip), %rdi     # 0xbef98
leaq 0x6d909(%rip), %rsi     # 0x8de87
callq 0xa4a0
movq %rax, %rdi
movsd -0x20(%rbp), %xmm0
callq 0xa9f0
movq %rax, %rdi
movq 0x9e98e(%rip), %rsi     # 0xbef28
callq 0xa500
movq 0x9e9f2(%rip), %rdi     # 0xbef98
leaq 0x6d8f9(%rip), %rsi     # 0x8dea6
callq 0xa4a0
movq %rax, %rdi
movsd -0x28(%rbp), %xmm0
callq 0xa9f0
movq %rax, %rdi
movq 0x9e95f(%rip), %rsi     # 0xbef28
callq 0xa500
callq 0xa710
movq -0x60(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, -0x38(%rbp)
movslq 0x38(%rax), %rsi
leaq -0x50(%rbp), %rdi
callq 0x22a80
movq -0x60(%rbp), %rdi
addq $0x40, %rdi
leaq -0x50(%rbp), %rsi
callq 0x22ac0
movl $0x0, -0x54(%rbp)
movq -0x60(%rbp), %rcx
movl -0x54(%rbp), %eax
cmpl 0x38(%rcx), %eax
jge 0x2068c
movq -0x60(%rbp), %rdi
addq $0x20, %rdi
movslq -0x54(%rbp), %rsi
callq 0x218b0
movq %rax, %rdi
callq 0x21930
movq -0x60(%rbp), %rdi
movsd %xmm0, -0x90(%rbp)
addq $0x20, %rdi
movslq -0x54(%rbp), %rsi
callq 0x218b0
movq %rax, %rdi
callq 0x21970
movq -0x60(%rbp), %rdi
movaps %xmm0, %xmm1
movsd -0x90(%rbp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, -0x88(%rbp)
addq $0x40, %rdi
movslq -0x54(%rbp), %rsi
callq 0x22af0
movsd -0x88(%rbp), %xmm0
movsd %xmm0, (%rax)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x20604
movq -0x60(%rbp), %rdi
addq $0x40, %rdi
callq 0x22b80
movq -0x60(%rbp), %rax
movsd %xmm0, -0x38(%rbp)
testb $0x1, 0x68(%rax)
je 0x206d7
movq 0x9e8e9(%rip), %rdi     # 0xbef98
leaq 0x6d80c(%rip), %rsi     # 0x8dec2
callq 0xa4a0
movq %rax, %rdi
movsd -0x38(%rbp), %xmm0
callq 0xa9f0
movq %rax, %rdi
movq 0x9e856(%rip), %rsi     # 0xbef28
callq 0xa500
movsd -0x38(%rbp), %xmm0
addq $0x90, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)