// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %r8, 0x20(%rsp)
movq %rcx, %r12
movq %rdx, %r15
movq %rsi, %rbx
movq 0xb0(%rsp), %rdx
movq %rdi, 0x8(%rsp)
movq %r9, 0x38(%rsp)
movq %r9, %rsi
callq 0x60c5
xorl %r14d, %r14d
movq %r12, 0x30(%rsp)
movq %rbx, 0x50(%rsp)
movq 0xb8(%rsp), %rax
movl (%rax), %eax
cmpq %rax, %r14
jae 0x641a
movq %rbx, %rdi
movq %r14, %rsi
callq 0x7746
movq 0x8(%rax), %rax
movsd (%rax), %xmm0
divsd (%r12), %xmm0
callq 0x2400
movsd %xmm0, 0x18(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x7746
movq 0x8(%rax), %rax
movsd 0x8(%rax), %xmm0
movq 0x20(%rsp), %rax
movq %rax, %r12
divsd (%rax), %xmm0
callq 0x2400
movsd %xmm0, 0x10(%rsp)
movq %rbx, %rdi
movq %r14, 0x40(%rsp)
movq %r14, %rsi
callq 0x7746
movq %r15, %r14
cvttsd2si 0x18(%rsp), %rbp
movq 0x8(%rax), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x18(%rsp)
movl %ebp, %r13d
cvtsi2sd %r13, %xmm0
movq 0x30(%rsp), %rax
mulsd (%rax), %xmm0
movsd %xmm0, 0x70(%rsp)
movq %rbx, %rdi
movq 0x40(%rsp), %rsi
callq 0x7746
cvttsd2si 0x10(%rsp), %r15
movq 0x8(%rax), %rax
movsd 0x8(%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
movl %r15d, %ebx
cvtsi2sd %rbx, %xmm0
mulsd (%r12), %xmm0
movsd %xmm0, 0x68(%rsp)
incl %ebp
cvtsi2sd %rbp, %xmm0
movsd %xmm0, 0x28(%rsp)
movq 0x38(%rsp), %rax
movl (%rax), %eax
cvtsi2sd %rax, %xmm0
movsd %xmm0, 0x48(%rsp)
leaq 0x28(%rsp), %rbp
movq %rbp, %rdi
leaq 0x48(%rsp), %r12
movq %r12, %rsi
callq 0x6060
movsd %xmm0, 0x58(%rsp)
incl %r15d
cvtsi2sd %r15, %xmm0
movsd %xmm0, 0x28(%rsp)
movq 0xb0(%rsp), %rax
movl (%rax), %eax
cvtsi2sd %rax, %xmm0
movsd %xmm0, 0x48(%rsp)
movq %rbp, %rdi
movq %r12, %rsi
callq 0x6060
movsd %xmm0, 0x60(%rsp)
movq %r14, %r15
movq %r14, %rdi
movq 0x40(%rsp), %r14
movq %r14, %rsi
callq 0x7768
movq 0x30(%rsp), %r12
movsd 0x18(%rsp), %xmm1
subsd 0x70(%rsp), %xmm1
movsd %xmm1, 0x18(%rsp)
movsd 0x10(%rsp), %xmm2
subsd 0x68(%rsp), %xmm2
movsd (%r12), %xmm0
subsd %xmm1, %xmm0
mulsd (%rax), %xmm0
movq 0x20(%rsp), %rax
movsd (%rax), %xmm1
movsd %xmm2, 0x10(%rsp)
subsd %xmm2, %xmm1
mulsd %xmm0, %xmm1
movq 0x8(%rsp), %rax
movq (%rax), %rax
shlq $0x4, %r13
movq 0x8(%rax,%r13), %rax
addsd (%rax,%rbx,8), %xmm1
movsd %xmm1, (%rax,%rbx,8)
movq %r15, %rdi
movq %r14, %rsi
callq 0x7768
movsd 0x18(%rsp), %xmm1
cvttsd2si 0x60(%rsp), %rcx
movsd (%r12), %xmm0
subsd %xmm1, %xmm0
mulsd (%rax), %xmm0
movq 0x8(%rsp), %rax
movq (%rax), %rax
movl %ecx, %ebp
movq 0x8(%rax,%r13), %rax
mulsd 0x10(%rsp), %xmm0
addsd (%rax,%rbp,8), %xmm0
movsd %xmm0, (%rax,%rbp,8)
movq %r15, %rdi
movq %r14, %rsi
callq 0x7768
movsd 0x18(%rsp), %xmm1
cvttsd2si 0x58(%rsp), %rcx
movsd (%rax), %xmm0
mulsd %xmm1, %xmm0
movq 0x20(%rsp), %rax
movsd (%rax), %xmm1
subsd 0x10(%rsp), %xmm1
mulsd %xmm0, %xmm1
movl %ecx, %r13d
movq 0x8(%rsp), %rax
movq (%rax), %rax
shlq $0x4, %r13
movq 0x8(%rax,%r13), %rax
addsd (%rax,%rbx,8), %xmm1
movsd %xmm1, (%rax,%rbx,8)
movq %r15, %rdi
movq %r14, %rsi
callq 0x7768
movsd 0x18(%rsp), %xmm0
mulsd (%rax), %xmm0
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq 0x8(%rax,%r13), %rax
mulsd 0x10(%rsp), %xmm0
addsd (%rax,%rbp,8), %xmm0
movsd %xmm0, (%rax,%rbp,8)
incq %r14
movq 0x50(%rsp), %rbx
jmp 0x6189
xorl %ebx, %ebx
leaq 0x28(%rsp), %r14
xorl %r13d, %r13d
movq 0x38(%rsp), %rax
movl (%rax), %eax
cmpq %rax, %r13
jae 0x646d
movsd (%r12), %xmm0
mulsd %xmm0, %xmm0
movq 0x20(%rsp), %rax
movsd (%rax), %xmm1
mulsd %xmm1, %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x28(%rsp)
movq 0x8(%rsp), %rax
movq (%rax), %rdi
addq %rbx, %rdi
movq %r14, %rsi
callq 0x778a
incq %r13
addq $0x10, %rbx
jmp 0x6424
movq 0x8(%rsp), %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x648b
jmp 0x648b
jmp 0x648b
jmp 0x648b
jmp 0x648b
movq %rax, %r14
movq 0x8(%rsp), %rdi
callq 0x4b46
movq %r14, %rdi
callq 0x2430