// File: /workspace/github_cpp_makefile_star2_O3/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 $0x68, %rsp
movq %r8, %r14
movq %rcx, %r12
movq %rdx, 0x30(%rsp)
movq %rsi, 0x38(%rsp)
movq 0xa8(%rsp), %rbx
movq 0xa0(%rsp), %rdx
movq %rdi, 0x8(%rsp)
movq %r9, 0x18(%rsp)
movq %r9, %rsi
callq 0x858b
cmpl $0x0, (%rbx)
je 0x8891
movl $0x8, %esi
xorl %ebx, %ebx
movq 0x38(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rdx
subq %rax, %rdx
sarq $0x4, %rdx
cmpq %rbx, %rdx
jbe 0x8919
movq (%rax,%rsi), %rax
movsd (%rax), %xmm0
movsd 0x8(%rax), %xmm1
movsd %xmm1, 0x20(%rsp)
movsd (%r12), %xmm1
movsd %xmm0, 0x58(%rsp)
movsd %xmm1, 0x50(%rsp)
divsd %xmm1, %xmm0
movq %rsi, 0x60(%rsp)
callq 0x3430
cvttsd2si %xmm0, %r13
movsd (%r14), %xmm1
movsd 0x20(%rsp), %xmm0
movsd %xmm1, 0x48(%rsp)
divsd %xmm1, %xmm0
callq 0x3430
cvttsd2si %xmm0, %rbp
leal 0x1(%r13), %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0x18(%rsp), %rax
movl (%rax), %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movsd %xmm0, 0x28(%rsp)
movq %r14, %r15
leaq 0x10(%rsp), %rdi
leaq 0x28(%rsp), %r14
movq %r14, %rsi
callq 0x8526
movsd %xmm0, 0x40(%rsp)
leal 0x1(%rbp), %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0xa0(%rsp), %rax
movl (%rax), %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movsd %xmm0, 0x28(%rsp)
leaq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x8526
movq 0x30(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %rbx, %rdx
jbe 0x8908
cvttsd2si %xmm0, %rsi
cvttsd2si 0x40(%rsp), %rdx
movl %ebp, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
mulsd 0x48(%rsp), %xmm0
movl %r13d, %r8d
xorps %xmm1, %xmm1
cvtsi2sd %r8, %xmm1
movsd 0x20(%rsp), %xmm3
subsd %xmm0, %xmm3
mulsd 0x50(%rsp), %xmm1
movsd 0x58(%rsp), %xmm2
subsd %xmm1, %xmm2
movsd (%r12), %xmm0
subsd %xmm2, %xmm0
mulsd (%rax,%rbx,8), %xmm0
movsd (%r15), %xmm1
subsd %xmm3, %xmm1
mulsd %xmm0, %xmm1
movq 0x8(%rsp), %rdi
movq (%rdi), %rdi
shlq $0x4, %r8
movq 0x8(%rdi,%r8), %r8
addsd (%r8,%rcx,8), %xmm1
movsd %xmm1, (%r8,%rcx,8)
movsd (%r12), %xmm0
subsd %xmm2, %xmm0
mulsd (%rax,%rbx,8), %xmm0
movl %esi, %esi
mulsd %xmm3, %xmm0
addsd (%r8,%rsi,8), %xmm0
movsd %xmm0, (%r8,%rsi,8)
movsd (%rax,%rbx,8), %xmm0
mulsd %xmm2, %xmm0
movsd (%r15), %xmm1
subsd %xmm3, %xmm1
mulsd %xmm0, %xmm1
movl %edx, %edx
shlq $0x4, %rdx
movq 0x8(%rdi,%rdx), %rdx
addsd (%rdx,%rcx,8), %xmm1
movsd %xmm1, (%rdx,%rcx,8)
mulsd (%rax,%rbx,8), %xmm2
mulsd %xmm3, %xmm2
addsd (%rdx,%rsi,8), %xmm2
movsd %xmm2, (%rdx,%rsi,8)
incq %rbx
movq 0xa8(%rsp), %rax
movl (%rax), %eax
movq 0x60(%rsp), %rsi
addq $0x10, %rsi
cmpq %rax, %rbx
movq %r15, %r14
jb 0x868f
movq 0x18(%rsp), %rax
movl (%rax), %eax
testq %rax, %rax
je 0x88f4
movq 0x8(%rsp), %rcx
movq (%rcx), %rcx
xorl %edx, %edx
movq %rdx, %rsi
shlq $0x4, %rsi
movq (%rcx,%rsi), %rdi
testq %rdi, %rdi
jle 0x88ec
movsd (%r12), %xmm0
mulsd %xmm0, %xmm0
movsd (%r14), %xmm1
mulsd %xmm1, %xmm0
mulsd %xmm1, %xmm0
movq 0x8(%rcx,%rsi), %rsi
leaq (%rsi,%rdi,8), %rdi
movsd (%rsi), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rsi)
addq $0x8, %rsi
cmpq %rdi, %rsi
jb 0x88d7
incq %rdx
cmpq %rax, %rdx
jne 0x88a7
movq 0x8(%rsp), %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2a56(%rip), %rdi      # 0xb365
movq %rbx, %rsi
xorl %eax, %eax
callq 0x3380
leaq 0x2a45(%rip), %rdi      # 0xb365
movq %rbx, %rsi
xorl %eax, %eax
callq 0x3380
jmp 0x892c
movq %rax, %rbx
movq 0x8(%rsp), %rdi
callq 0x7026
movq %rbx, %rdi
callq 0x3460