// File: /workspace/github_c_makefile_star2_O1/mohuangrui[P]ArtraCFD/immersed_boundary.c
// Function: InverseDistanceWeighting
static Real InverseDistanceWeighting(const int tn, const int n[restrict], const Real p[restrict],
        const int h, const int type, const int did, const Partition *const part,
        const Node *const node, const Model *model, Real Uo[restrict])
{
    int idx = 0; /* linear array index math variable */
    const RealVec sMin = {part->domain[X][MIN], part->domain[Y][MIN], part->domain[Z][MIN]};
    const RealVec d = {part->d[X], part->d[Y], part->d[Z]};
    const IntVec ng = {part->ng[X], part->ng[Y], part->ng[Z]};
    Real Uoh[DIMUo] = {0.0}; /* primitive at neighbouring node */
    RealVec ph = {0.0}; /* neighbouring point */
    IntVec nh = {0}; /* neighbouring node */
    Real weightSum = 0.0;
    memset(Uo, 0, DIMUo * sizeof(*Uo));
    /*
     * Search nodes with required "type" in the domain specified by the center node
     * "n" and initial range "h" as interpolation stencils for the interpolated point "p".
     * To preserve symmetry, the search range in each direction should be symmetric, and
     * the search operator on each direction index should be symmetric. In addition,
     * any temporal priority should be strictly prevented in treating the solution nodes.
     */
    for (int r = h, tally = 0; 0 == tally; ++r) {
        for (int kh = -r; kh <= r; ++kh) {
            for (int jh = -r; jh <= r; ++jh) {
                for (int ih = -r; ih <= r; ++ih) {
                    nh[X] = n[X] + ih;
                    nh[Y] = n[Y] + jh;
                    nh[Z] = n[Z] + kh;
                    if (!InPartBox(nh[Z], nh[Y], nh[X], part->ns[PIN])) {
                        continue;
                    }
                    idx = IndexNode(nh[Z], nh[Y], nh[X], part->n[Y], part->n[X]);
                    /* be aware of the validity of ih = jh = kh = 0 */
                    if (did != node[idx].did) {
                        continue; /* skip node not in target domain */
                    }
                    switch (type) {
                        case TYPED: /* use node in target domain */
                            break;
                        case TYPEF: /* use original node in target domain to avoid priority */
                            if ((did != node[idx].gst) || (0 > node[idx].fid)) {
                                continue; /* skip changed node either reconstructed or not */
                            }
                            break;
                        default: /* use node in target domain layer */
                            if (type != node[idx].gst) {
                                continue;
                            }
                            break;
                    }
                    ++tally;
                    ph[X] = MapPoint(nh[X], sMin[X], d[X], ng[X]);
                    ph[Y] = MapPoint(nh[Y], sMin[Y], d[Y], ng[Y]);
                    ph[Z] = MapPoint(nh[Z], sMin[Z], d[Z], ng[Z]);
                    MapPrimitive(model->gamma, model->gasR, node[idx].U[tn], Uoh);
                    ApplyWeighting(Uoh, part->tinyL, Dist2(p, ph), &weightSum, Uo);
                }
            }
        }
    }
    return weightSum;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movl %r9d, 0x14(%rsp)
movl %r8d, 0x2c(%rsp)
movl %ecx, %r14d
movq %rdx, 0x90(%rsp)
movq %rsi, 0x30(%rsp)
movq 0x158(%rsp), %rbx
movq 0x140(%rsp), %rax
movsd 0x3c8(%rax), %xmm0
movsd %xmm0, 0x88(%rsp)
movsd 0x3d8(%rax), %xmm0
movsd %xmm0, 0x80(%rsp)
movsd 0x3e8(%rax), %xmm0
movsd %xmm0, 0x78(%rsp)
movsd 0x30(%rax), %xmm0
movsd %xmm0, 0x70(%rsp)
movsd 0x38(%rax), %xmm0
movsd %xmm0, 0x68(%rsp)
movsd 0x40(%rax), %xmm0
movsd %xmm0, 0x60(%rsp)
movl 0x18(%rax), %ecx
movl %ecx, 0x28(%rsp)
movl 0x1c(%rax), %ecx
movl %ecx, 0x24(%rsp)
movl 0x20(%rax), %ecx
movl %ecx, 0x20(%rsp)
xorpd %xmm0, %xmm0
leaq 0xd0(%rsp), %rcx
movapd %xmm0, 0x20(%rcx)
movapd %xmm0, 0x10(%rcx)
movapd %xmm0, (%rcx)
leaq 0xb0(%rsp), %rcx
movapd %xmm0, (%rcx)
movq $0x0, 0x10(%rcx)
movupd %xmm0, 0x20(%rbx)
movupd %xmm0, 0x10(%rbx)
movupd %xmm0, (%rbx)
movslq %edi, %rcx
movq %rcx, 0x58(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x18(%rsp)
addq $0x68, %rax
movq %rax, 0xa0(%rsp)
testl %r14d, %r14d
js 0x12b06
movl %r14d, %ecx
negl %ecx
movq 0x30(%rsp), %rax
movl (%rax), %edx
movq %rdx, 0xa8(%rsp)
movl 0x4(%rax), %edx
movq %rdx, 0x48(%rsp)
movl 0x8(%rax), %eax
movq %rax, 0x38(%rsp)
movl $0x0, 0x10(%rsp)
movl %ecx, 0xc(%rsp)
movl %r14d, 0x8(%rsp)
movq 0x38(%rsp), %rax
movq %rcx, 0x40(%rsp)
leal (%rax,%rcx), %r12d
movl 0xc(%rsp), %eax
movl %eax, %ecx
movq 0x48(%rsp), %rax
movq %rcx, 0x50(%rsp)
leal (%rax,%rcx), %r15d
movl 0xc(%rsp), %eax
movl %eax, %r13d
movq 0xa8(%rsp), %rax
leal (%rax,%r13), %ebp
movl %r12d, %edi
movl %r15d, %esi
movl %ebp, %edx
movq 0xa0(%rsp), %rcx
callq 0x7eb0
testl %eax, %eax
je 0x12ac6
movq 0x140(%rsp), %rax
movl 0xc(%rax), %r8d
movl 0x10(%rax), %ecx
movl %r12d, %edi
movl %r15d, %esi
movl %ebp, %edx
callq 0x7ea3
cltq
movq %rax, %rcx
shlq $0x7, %rcx
leaq (%rcx,%rax,8), %r14
movl 0x14(%rsp), %eax
movq 0x148(%rsp), %rcx
cmpl %eax, (%rcx,%r14)
jne 0x12ac1
addq %rcx, %r14
movl 0x2c(%rsp), %eax
cmpl $-0x1, %eax
je 0x129af
cmpl $-0x2, %eax
jne 0x129a5
movl 0x14(%rsp), %eax
cmpl %eax, 0xc(%r14)
jne 0x12ac1
cmpl $0x0, 0x4(%r14)
jns 0x129af
jmp 0x12ac1
cmpl %eax, 0xc(%r14)
jne 0x12ac1
movl %ebp, %edi
movsd 0x88(%rsp), %xmm0
movsd 0x70(%rsp), %xmm1
movl 0x28(%rsp), %esi
callq 0x7f08
movsd %xmm0, 0xb0(%rsp)
movl %r15d, %edi
movsd 0x80(%rsp), %xmm0
movsd 0x68(%rsp), %xmm1
movl 0x24(%rsp), %esi
callq 0x7f08
movsd %xmm0, 0xb8(%rsp)
movl %r12d, %edi
movsd 0x78(%rsp), %xmm0
movsd 0x60(%rsp), %xmm1
movl 0x20(%rsp), %esi
callq 0x7f08
movsd %xmm0, 0xc0(%rsp)
movq 0x150(%rsp), %rax
movsd 0x40(%rax), %xmm0
movsd 0x48(%rax), %xmm1
movq 0x58(%rsp), %rax
leaq (%rax,%rax,4), %rax
leaq (%r14,%rax,8), %rdi
addq $0x10, %rdi
leaq 0xd0(%rsp), %rsi
callq 0x7d63
movq 0x140(%rsp), %rax
movsd 0x60(%rax), %xmm0
movsd %xmm0, 0x98(%rsp)
movq 0x90(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x7fe0
movsd 0x98(%rsp), %xmm1
maxsd %xmm0, %xmm1
movsd 0x7581(%rip), %xmm0     # 0x1a008
divsd %xmm1, %xmm0
xorl %eax, %eax
movsd 0xd0(%rsp,%rax,8), %xmm1
mulsd %xmm0, %xmm1
addsd (%rbx,%rax,8), %xmm1
movsd %xmm1, (%rbx,%rax,8)
incq %rax
cmpq $0x6, %rax
jne 0x12a8d
incl 0x10(%rsp)
movsd 0x18(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x18(%rsp)
movl 0x8(%rsp), %r14d
leal 0x1(%r13), %eax
cmpl %r14d, %r13d
movl %eax, %r13d
jl 0x12912
movq 0x50(%rsp), %rcx
leal 0x1(%rcx), %eax
cmpl %r14d, %ecx
movl %eax, %ecx
jl 0x128fd
movq 0x40(%rsp), %rcx
leal 0x1(%rcx), %eax
cmpl %r14d, %ecx
movl %eax, %ecx
jl 0x128e9
cmpl $0x0, 0x10(%rsp)
sete %al
jmp 0x12b08
movb $0x1, %al
incl %r14d
testb %al, %al
jne 0x128ab
movsd 0x18(%rsp), %xmm0
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq