// File: dfrodriguezp[P]PiCM-cpp/functions.cc
// Function: density(std::vector<std::valarray<double>, std::allocator<std::valarray<double>>> const&, std::vector<double, std::allocator<double>> const&, double const&, double const&, unsigned int const&, unsigned int const&, unsigned int const&)
VecArr density(const VecArr &positions,
               const std::vector<Real> &charges, const Real &dx,
               const Real &dy, const Int &Nx, const Int &Ny, const Int &N)
{
    VecArr rho = valarraysVector(Nx, Ny);

    for (Int p = 0; p < N; ++p)
    {
        Int i = std::floor(positions.at(p)[0] / dx);
        Int j = std::floor(positions.at(p)[1] / dy);
        Real hx = positions.at(p)[0] - (i * dx);
        Real hy = positions.at(p)[1] - (j * dy);
        Int nxt_i = Int(mod(Real(i + 1), Real(Nx)));
        Int nxt_j = Int(mod(Real(j + 1), Real(Ny)));

        rho[i][j] += charges.at(p) * (dx - hx) * (dy - hy);
        rho[i][nxt_j] += charges.at(p) * (dx - hx) * hy;
        rho[nxt_i][j] += charges.at(p) * hx * (dy - hy);
        rho[nxt_i][nxt_j] += charges.at(p) * hx * hy;
    }

    for (Int i = 0; i < Nx; ++i)
    {
        rho[i] /= (dx * dx * dy * dy);
    }

    return rho;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x170, %rsp            # imm = 0x170
movq %rdi, -0xb0(%rbp)
movq %rdi, %rax
movq %rax, -0xa8(%rbp)
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movb $0x0, -0x31(%rbp)
movq -0x30(%rbp), %rsi
movq 0x10(%rbp), %rdx
callq 0xdae0
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
movq 0x18(%rbp), %rcx
cmpl (%rcx), %eax
jae 0xe084
movq -0x10(%rbp), %rdi
movl -0x38(%rbp), %eax
movl %eax, %esi
callq 0x10bd0
movq %rax, -0xb8(%rbp)
jmp 0xdc3a
movq -0xb8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x10c10
movq %rax, -0xc0(%rbp)
jmp 0xdc53
movq -0xc0(%rbp), %rax
movsd (%rax), %xmm0
movq -0x20(%rbp), %rax
movsd (%rax), %xmm1
divsd %xmm1, %xmm0
callq 0x34f0
cvttsd2si %xmm0, %rax
movl %eax, -0x3c(%rbp)
movq -0x10(%rbp), %rdi
movl -0x38(%rbp), %eax
movl %eax, %esi
callq 0x10bd0
movq %rax, -0xc8(%rbp)
jmp 0xdc8e
movq -0xc8(%rbp), %rdi
movl $0x1, %esi
callq 0x10c10
movq %rax, -0xd0(%rbp)
jmp 0xdca8
movq -0xd0(%rbp), %rax
movsd (%rax), %xmm0
movq -0x28(%rbp), %rax
movsd (%rax), %xmm1
divsd %xmm1, %xmm0
callq 0x34f0
cvttsd2si %xmm0, %rax
movl %eax, -0x50(%rbp)
movq -0x10(%rbp), %rdi
movl -0x38(%rbp), %eax
movl %eax, %esi
callq 0x10bd0
movq %rax, -0xd8(%rbp)
jmp 0xdce3
movq -0xd8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x10c10
movq %rax, -0xe0(%rbp)
jmp 0xdcfc
movq -0xe0(%rbp), %rax
movsd (%rax), %xmm0
movl -0x3c(%rbp), %eax
cvtsi2sd %rax, %xmm1
movq -0x20(%rbp), %rax
movsd (%rax), %xmm2
mulsd %xmm2, %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, -0x58(%rbp)
movq -0x10(%rbp), %rdi
movl -0x38(%rbp), %eax
movl %eax, %esi
callq 0x10bd0
movq %rax, -0xe8(%rbp)
jmp 0xdd3b
movq -0xe8(%rbp), %rdi
movl $0x1, %esi
callq 0x10c10
movq %rax, -0xf0(%rbp)
jmp 0xdd55
movq -0xf0(%rbp), %rax
movsd (%rax), %xmm0
movl -0x50(%rbp), %eax
cvtsi2sd %rax, %xmm1
movq -0x28(%rbp), %rax
movsd (%rax), %xmm2
mulsd %xmm2, %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, -0x60(%rbp)
movl -0x3c(%rbp), %eax
incl %eax
cvtsi2sd %rax, %xmm0
movsd %xmm0, -0x70(%rbp)
movq -0x30(%rbp), %rax
movl (%rax), %eax
cvtsi2sd %rax, %xmm0
movsd %xmm0, -0x78(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0xda30
cvttsd2si %xmm0, %rax
movl %eax, -0x64(%rbp)
movl -0x50(%rbp), %eax
incl %eax
cvtsi2sd %rax, %xmm0
movsd %xmm0, -0x88(%rbp)
movq 0x10(%rbp), %rax
movl (%rax), %eax
cvtsi2sd %rax, %xmm0
movsd %xmm0, -0x90(%rbp)
leaq -0x88(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0xda30
cvttsd2si %xmm0, %rax
movl %eax, -0x7c(%rbp)
movq -0x18(%rbp), %rdi
movl -0x38(%rbp), %eax
movl %eax, %esi
callq 0x10c40
movq %rax, -0xf8(%rbp)
jmp 0xde08
movq -0xb0(%rbp), %rdi
movq -0xf8(%rbp), %rax
movsd (%rax), %xmm0
movq -0x20(%rbp), %rax
movsd (%rax), %xmm1
movsd -0x58(%rbp), %xmm2
subsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0x110(%rbp)
movq -0x28(%rbp), %rax
movsd (%rax), %xmm0
movsd -0x60(%rbp), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, -0x108(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, %esi
callq 0x7d10
movq %rax, %rdi
movl -0x50(%rbp), %eax
movl %eax, %esi
callq 0x6fb0
movq %rax, -0x100(%rbp)
jmp 0xde70
movq -0x100(%rbp), %rax
movsd -0x108(%rbp), %xmm2
movsd -0x110(%rbp), %xmm0
movsd (%rax), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax)
movq -0x18(%rbp), %rdi
movl -0x38(%rbp), %eax
movl %eax, %esi
callq 0x10c40
movq %rax, -0x118(%rbp)
jmp 0xdeae
movq -0xb0(%rbp), %rdi
movq -0x118(%rbp), %rax
movsd (%rax), %xmm0
movq -0x20(%rbp), %rax
movsd (%rax), %xmm1
movsd -0x58(%rbp), %xmm2
subsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0x130(%rbp)
movsd -0x60(%rbp), %xmm0
movsd %xmm0, -0x128(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, %esi
callq 0x7d10
movq %rax, %rdi
movl -0x7c(%rbp), %eax
movl %eax, %esi
callq 0x6fb0
movq %rax, -0x120(%rbp)
jmp 0xdf0a
movq -0x120(%rbp), %rax
movsd -0x128(%rbp), %xmm2
movsd -0x130(%rbp), %xmm0
movsd (%rax), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax)
movq -0x18(%rbp), %rdi
movl -0x38(%rbp), %eax
movl %eax, %esi
callq 0x10c40
movq %rax, -0x138(%rbp)
jmp 0xdf48
movq -0xb0(%rbp), %rdi
movq -0x138(%rbp), %rax
movsd (%rax), %xmm0
movsd -0x58(%rbp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0x150(%rbp)
movq -0x28(%rbp), %rax
movsd (%rax), %xmm0
movsd -0x60(%rbp), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, -0x148(%rbp)
movl -0x64(%rbp), %eax
movl %eax, %esi
callq 0x7d10
movq %rax, %rdi
movl -0x50(%rbp), %eax
movl %eax, %esi
callq 0x6fb0
movq %rax, -0x140(%rbp)
jmp 0xdfa4
movq -0x140(%rbp), %rax
movsd -0x148(%rbp), %xmm2
movsd -0x150(%rbp), %xmm0
movsd (%rax), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax)
movq -0x18(%rbp), %rdi
movl -0x38(%rbp), %eax
movl %eax, %esi
callq 0x10c40
movq %rax, -0x158(%rbp)
jmp 0xdfe2
movq -0xb0(%rbp), %rdi
movq -0x158(%rbp), %rax
movsd (%rax), %xmm0
movsd -0x58(%rbp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0x170(%rbp)
movsd -0x60(%rbp), %xmm0
movsd %xmm0, -0x168(%rbp)
movl -0x64(%rbp), %eax
movl %eax, %esi
callq 0x7d10
movq %rax, %rdi
movl -0x7c(%rbp), %eax
movl %eax, %esi
callq 0x6fb0
movq %rax, -0x160(%rbp)
jmp 0xe032
movq -0x160(%rbp), %rax
movsd -0x168(%rbp), %xmm2
movsd -0x170(%rbp), %xmm0
movsd (%rax), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0xdc14
movq -0xb0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
callq 0x76f0
jmp 0xe11e
movl $0x0, -0x94(%rbp)
movl -0x94(%rbp), %eax
movq -0x30(%rbp), %rcx
cmpl (%rcx), %eax
jae 0xe0f8
movq -0xb0(%rbp), %rdi
movq -0x20(%rbp), %rax
movsd (%rax), %xmm0
mulsd %xmm0, %xmm0
movq -0x28(%rbp), %rax
movsd (%rax), %xmm1
mulsd %xmm1, %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, -0xa0(%rbp)
movl -0x94(%rbp), %eax
movl %eax, %esi
callq 0x7d10
movq %rax, %rdi
leaq -0xa0(%rbp), %rsi
callq 0x10c80
jmp 0xe0e5
jmp 0xe0e7
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
jmp 0xe08e
movb $0x1, -0x31(%rbp)
testb $0x1, -0x31(%rbp)
jne 0xe10e
movq -0xb0(%rbp), %rdi
callq 0x76f0
movq -0xa8(%rbp), %rax
addq $0x170, %rsp            # imm = 0x170
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x3530
nopw (%rax,%rax)