// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Grid.cpp
// Function: gaps::R2Grid::FillHoles()
void R2Grid::
FillHoles(void)
{
  // Build Gaussian filter
  RNScalar sigma = 1.5;
  double denom = -2.0 * sigma * sigma;
  RNScalar filter[5][5];
  for (int i = -2; i <= 2; i++) {
    for (int j = -2; j <= 2; j++) {
      filter[i+2][j+2] = exp((i*i+j*j)/denom);
    }
  }

  // Initialize image with heap state
  int width = XResolution();
  int height = YResolution();
  R2Grid heap_image(width, height);
  RNScalar *heap_values = (RNScalar *) heap_image.GridValues();

  // Seed heap with border unknown values
  RNHeap<RNScalar *> heap(0, -1, FALSE);
  for (int x = 0; x < width; x++) {
    for (int y = 0; y < height; y++) {
      if (GridValue(x, y) == R2_GRID_UNKNOWN_VALUE) continue;
     
      // Push neighbors with unknown values onto queue
      if ((x > 0) && (GridValue(x-1, y) == R2_GRID_UNKNOWN_VALUE)) {
        RNScalar *heap_value = &heap_values[y*width+(x-1)];
        *heap_value += 1;
        if (*heap_value < 1.5) heap.Push(heap_value);
        else heap.Update(heap_value);
      }
      if ((y > 0) && (GridValue(x, y-1) == R2_GRID_UNKNOWN_VALUE)) {
        RNScalar *heap_value = &heap_values[(y-1)*width+x];
        *heap_value += 1;
        if (*heap_value < 1.5) heap.Push(heap_value);
        else heap.Update(heap_value);
      }
      if ((x < width-1) && (GridValue(x+1, y) == R2_GRID_UNKNOWN_VALUE)) {
        RNScalar *heap_value = &heap_values[y*width+(x+1)];
        *heap_value += 1;
        if (*heap_value < 1.5) heap.Push(heap_value);
        else heap.Update(heap_value);
      }
      if ((y < height-1) && (GridValue(x, y+1) == R2_GRID_UNKNOWN_VALUE)) {
        RNScalar *heap_value = &heap_values[(y+1)*width+x];
        *heap_value += 1;
        if (*heap_value < 1.5) heap.Push(heap_value);
        else heap.Update(heap_value);
      }
    }
  }

  // Iteratively update border unknown values with blur of immediate neighbors
  while (!heap.IsEmpty()) {
    // Pop grid cell from heap
    RNScalar *heap_value = heap.Pop();
    assert(*heap_value > 0);
    int index = heap_value - heap_values;
    assert((index >= 0) && (index < heap_image.NEntries()));
    int x, y; heap_image.IndexToIndices(index, x, y);
    assert(GridValue(index) == R2_GRID_UNKNOWN_VALUE);

    // Update value
    RNScalar sum = 0;
    RNScalar weight = 0;
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int nx = x + i;
        int ny = y + j;
        if ((nx < 0) || (nx >= width)) continue;
        if ((ny < 0) || (ny >= height)) continue;
        RNScalar value = GridValue(nx, ny);
        if (value == R2_GRID_UNKNOWN_VALUE) continue;
        RNScalar w = filter[i+2][j+2];
        sum += w * value;
        weight += w;
      }
    }

    // Divide by total weight
    if (weight > 0) SetGridValue(x, y, sum / weight);
    else RNFail("Zero weight in fill holes\n");

    // Push neighbors with unknown values onto heap
    if ((x > 0) && (GridValue(x-1, y) == R2_GRID_UNKNOWN_VALUE)) {
      RNScalar *heap_value = &heap_values[y*width+(x-1)];
      *heap_value += 1;
      if (*heap_value < 1.5) heap.Push(heap_value);
      else heap.Update(heap_value);
    }
    if ((y > 0) && (GridValue(x, y-1) == R2_GRID_UNKNOWN_VALUE)) {
      RNScalar *heap_value = &heap_values[(y-1)*width+x];
      *heap_value += 1;
      if (*heap_value < 1.5) heap.Push(heap_value);
      else heap.Update(heap_value);
    }
    if ((x < width-1) && (GridValue(x+1, y) == R2_GRID_UNKNOWN_VALUE)) {
      RNScalar *heap_value = &heap_values[y*width+(x+1)];
      *heap_value += 1;
      if (*heap_value < 1.5) heap.Push(heap_value);
      else heap.Update(heap_value);
    }
    if ((y < height-1) && (GridValue(x, y+1) == R2_GRID_UNKNOWN_VALUE)) {
      RNScalar *heap_value = &heap_values[(y+1)*width+x];
      *heap_value += 1;
      if (*heap_value < 1.5) heap.Push(heap_value);
      else heap.Update(heap_value);
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x228, %rsp            # imm = 0x228
movq %rdi, %rbx
movq $-0x2, %r14
leaq 0x160(%rsp), %r15
movsd 0x4eba5(%rip), %xmm1    # 0xa9308
movq %r14, %r12
imulq %r14, %r12
movq $-0x2, %r13
movl %r13d, %eax
imull %r13d, %eax
addl %r12d, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd %xmm1, %xmm0
callq 0xe7f0
movsd 0x4eb75(%rip), %xmm1    # 0xa9308
movsd %xmm0, 0x10(%r15,%r13,8)
incq %r13
cmpq $0x3, %r13
jne 0x5a771
incq %r14
addq $0x28, %r15
cmpq $0x3, %r14
jne 0x5a763
movslq 0xc0(%rbx), %r14
movl 0xc4(%rbx), %r15d
leaq 0x90(%rsp), %r12
movq %r12, %rdi
movl %r14d, %esi
movl %r15d, %edx
callq 0x58ed8
movq 0xb8(%r12), %rax
movq %rax, 0x8(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x30(%rsp)
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
movq %rax, 0x40(%rsp)
movupd %xmm0, 0x48(%rsp)
movupd %xmm0, 0x54(%rsp)
testq %r14, %r14
jle 0x5adaf
movslq %r15d, %rax
leal -0x1(%r14), %ecx
movq %rcx, 0x28(%rsp)
decq %rax
movq %rax, 0x20(%rsp)
leaq (,%r14,8), %r12
movq %r12, %rax
negq %rax
movq %rax, 0x78(%rsp)
xorl %ebp, %ebp
movq %r15, 0x10(%rsp)
testl %r15d, %r15d
jle 0x5aa8c
leaq -0x1(%rbp), %rax
movq %rax, 0x88(%rsp)
leaq 0x1(%rbp), %rcx
movq 0x8(%rsp), %rax
leaq (%rax,%rbp,8), %r13
movq %rcx, 0x70(%rsp)
leaq (,%rcx,8), %rax
movq %rax, 0x80(%rsp)
leaq (,%rbp,8), %rax
movq %rax, 0x18(%rsp)
xorl %r15d, %r15d
movq 0xb8(%rbx), %rax
movslq 0xc8(%rbx), %rdx
movslq %r15d, %rcx
imulq %rdx, %rcx
leaq (%rax,%rbp,8), %rdx
movsd (%rdx,%rcx,8), %xmm0
ucomisd 0x4870e(%rip), %xmm0    # 0xa2fb8
jne 0x5a8b2
jnp 0x5aa6c
testq %rbp, %rbp
je 0x5a910
movq 0x88(%rsp), %rdx
leaq (%rax,%rdx,8), %rax
movsd (%rax,%rcx,8), %xmm0
ucomisd 0x486e8(%rip), %xmm0    # 0xa2fb8
jne 0x5a910
jp 0x5a910
leaq -0x8(%r13), %rsi
movsd -0x8(%r13), %xmm0
addsd 0x47752(%rip), %xmm0    # 0xa2038
movsd %xmm0, -0x8(%r13)
movsd 0x4ea1c(%rip), %xmm1    # 0xa9310
ucomisd %xmm0, %xmm1
jbe 0x5a906
leaq 0x30(%rsp), %rdi
callq 0x23edc
jmp 0x5a910
leaq 0x30(%rsp), %rdi
callq 0x24096
testq %r15, %r15
je 0x5a987
movslq 0xc8(%rbx), %rax
leal -0x1(%r15), %ecx
movslq %ecx, %rcx
imulq %rax, %rcx
movq 0xb8(%rbx), %rax
addq 0x18(%rsp), %rax
movsd (%rax,%rcx,8), %xmm0
ucomisd 0x48678(%rip), %xmm0    # 0xa2fb8
jne 0x5a987
jp 0x5a987
movq 0x78(%rsp), %rax
leaq (%rax,%r13), %rsi
movsd (%r13,%rax), %xmm0
addsd 0x476dc(%rip), %xmm0    # 0xa2038
movsd %xmm0, (%r13,%rax)
movsd 0x4e9a5(%rip), %xmm1    # 0xa9310
ucomisd %xmm0, %xmm1
jbe 0x5a97d
leaq 0x30(%rsp), %rdi
callq 0x23edc
jmp 0x5a987
leaq 0x30(%rsp), %rdi
callq 0x24096
cmpq 0x28(%rsp), %rbp
jge 0x5a9f8
movslq 0xc8(%rbx), %rax
movslq %r15d, %rcx
imulq %rax, %rcx
movq 0xb8(%rbx), %rax
addq 0x80(%rsp), %rax
movsd (%rax,%rcx,8), %xmm0
ucomisd 0x48600(%rip), %xmm0    # 0xa2fb8
jne 0x5a9f8
jp 0x5a9f8
leaq 0x8(%r13), %rsi
movsd 0x8(%r13), %xmm0
addsd 0x4766a(%rip), %xmm0    # 0xa2038
movsd %xmm0, 0x8(%r13)
movsd 0x4e934(%rip), %xmm1    # 0xa9310
ucomisd %xmm0, %xmm1
jbe 0x5a9ee
leaq 0x30(%rsp), %rdi
callq 0x23edc
jmp 0x5a9f8
leaq 0x30(%rsp), %rdi
callq 0x24096
cmpq 0x20(%rsp), %r15
jge 0x5aa6c
movslq 0xc8(%rbx), %rax
leal 0x1(%r15), %ecx
movslq %ecx, %rcx
imulq %rax, %rcx
movq 0xb8(%rbx), %rax
addq 0x18(%rsp), %rax
movsd (%rax,%rcx,8), %xmm0
ucomisd 0x4858e(%rip), %xmm0    # 0xa2fb8
jne 0x5aa6c
jp 0x5aa6c
leaq (%r12,%r13), %rsi
movsd (%r13,%r12), %xmm0
addsd 0x475f7(%rip), %xmm0    # 0xa2038
movsd %xmm0, (%r13,%r12)
movsd 0x4e8c0(%rip), %xmm1    # 0xa9310
ucomisd %xmm0, %xmm1
jbe 0x5aa62
leaq 0x30(%rsp), %rdi
callq 0x23edc
jmp 0x5aa6c
leaq 0x30(%rsp), %rdi
callq 0x24096
incq %r15
addq %r12, %r13
cmpq %r15, 0x10(%rsp)
jne 0x5a884
movq 0x70(%rsp), %rax
movq %rax, %rbp
movq 0x10(%rsp), %r15
jmp 0x5aa92
incq %rbp
movq %rbp, %rax
cmpq %r14, %rax
jne 0x5a83d
cmpl $0x0, 0x38(%rsp)
je 0x5adaf
leal -0x1(%r14), %eax
movl %eax, 0x28(%rsp)
movslq %r15d, %r12
leal -0x1(%r15), %eax
movl %eax, 0x20(%rsp)
leaq 0x30(%rsp), %r15
movq %r15, %rdi
callq 0x23ff0
subq 0x8(%rsp), %rax
shrq $0x3, %rax
cltd
idivl 0x158(%rsp)
movl %eax, %r13d
movq 0xb8(%rbx), %rdi
movslq 0xc8(%rbx), %rcx
movslq %eax, %rbp
movq %rdx, 0x10(%rsp)
movslq %edx, %rsi
addq $-0x2, %rbp
movq %rbp, %rax
imulq %rcx, %rax
leaq (,%rsi,8), %rdx
leaq (%rdx,%rax,8), %rax
movq %rdi, 0x18(%rsp)
leaq (%rdi,%rax), %r15
addq $-0x10, %r15
movq %rcx, %r8
shlq $0x3, %r8
xorpd %xmm0, %xmm0
movq $-0x2, %r9
leaq 0x160(%rsp), %r10
xorpd %xmm1, %xmm1
leaq (%r9,%rsi), %r11
movq %r15, %rdi
xorl %eax, %eax
cmpq %r14, %r11
jae 0x5ab72
movq %rbp, %rdx
addq %rax, %rdx
js 0x5ab72
cmpq %r12, %rdx
jge 0x5ab72
movsd (%rdi), %xmm2
ucomisd 0x4845c(%rip), %xmm2    # 0xa2fb8
jne 0x5ab60
jnp 0x5ab72
movsd (%r10,%rax,8), %xmm3
mulsd %xmm3, %xmm2
addsd %xmm2, %xmm0
addsd %xmm3, %xmm1
incq %rax
addq %r8, %rdi
cmpq $0x5, %rax
jne 0x5ab3e
incq %r9
addq $0x28, %r10
addq $0x8, %r15
cmpq $0x3, %r9
jne 0x5ab35
ucomisd 0x47651(%rip), %xmm1    # 0xa21e8
jbe 0x5abbc
divsd %xmm1, %xmm0
imull %r13d, %ecx
movq 0x10(%rsp), %rbp
addl %ebp, %ecx
movslq %ecx, %rax
movq 0x18(%rsp), %rcx
movsd %xmm0, (%rcx,%rax,8)
leaq 0x30(%rsp), %r15
jmp 0x5abd4
leaq 0x4e7e1(%rip), %rdi     # 0xa93a4
xorl %eax, %eax
callq 0x719d2
leaq 0x30(%rsp), %r15
movq 0x10(%rsp), %rbp
testl %ebp, %ebp
jle 0x5ac49
movq 0xb8(%rbx), %rax
movl 0xc8(%rbx), %ecx
imull %r13d, %ecx
addl %ebp, %ecx
decl %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
ucomisd 0x483bb(%rip), %xmm0    # 0xa2fb8
jne 0x5ac49
jp 0x5ac49
leal -0x1(%rbp), %eax
movl %r13d, %ecx
imull %r14d, %ecx
addl %eax, %ecx
movslq %ecx, %rax
movq 0x8(%rsp), %rcx
leaq (%rcx,%rax,8), %rsi
movsd (%rsi), %xmm0
addsd 0x47413(%rip), %xmm0    # 0xa2038
movsd %xmm0, (%rsi)
movsd 0x4e6df(%rip), %xmm1    # 0xa9310
ucomisd %xmm0, %xmm1
jbe 0x5ac41
movq %r15, %rdi
callq 0x23edc
jmp 0x5ac49
movq %r15, %rdi
callq 0x24096
testl %r13d, %r13d
jle 0x5acb9
leal -0x1(%r13), %eax
movq 0xb8(%rbx), %rcx
movl 0xc8(%rbx), %edx
imull %eax, %edx
addl %ebp, %edx
movslq %edx, %rdx
movsd (%rcx,%rdx,8), %xmm0
ucomisd 0x48344(%rip), %xmm0    # 0xa2fb8
jne 0x5acb9
jp 0x5acb9
imull %r14d, %eax
addl %ebp, %eax
cltq
movq 0x8(%rsp), %rcx
leaq (%rcx,%rax,8), %rsi
movsd (%rsi), %xmm0
addsd 0x473a3(%rip), %xmm0    # 0xa2038
movsd %xmm0, (%rsi)
movsd 0x4e66f(%rip), %xmm1    # 0xa9310
ucomisd %xmm0, %xmm1
jbe 0x5acb1
movq %r15, %rdi
callq 0x23edc
jmp 0x5acb9
movq %r15, %rdi
callq 0x24096
cmpl 0x28(%rsp), %ebp
jge 0x5ad30
movq 0xb8(%rbx), %rax
movl 0xc8(%rbx), %ecx
imull %r13d, %ecx
addl %ebp, %ecx
incl %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
ucomisd 0x482d4(%rip), %xmm0    # 0xa2fb8
jne 0x5ad30
jp 0x5ad30
leal 0x1(%rbp), %eax
movl %r13d, %ecx
imull %r14d, %ecx
addl %eax, %ecx
movslq %ecx, %rax
movq 0x8(%rsp), %rcx
leaq (%rcx,%rax,8), %rsi
movsd (%rsi), %xmm0
addsd 0x4732c(%rip), %xmm0    # 0xa2038
movsd %xmm0, (%rsi)
movsd 0x4e5f8(%rip), %xmm1    # 0xa9310
ucomisd %xmm0, %xmm1
jbe 0x5ad28
movq %r15, %rdi
callq 0x23edc
jmp 0x5ad30
movq %r15, %rdi
callq 0x24096
cmpl 0x20(%rsp), %r13d
jge 0x5ada4
incl %r13d
movq 0xb8(%rbx), %rax
movl 0xc8(%rbx), %ecx
imull %r13d, %ecx
addl %ebp, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
ucomisd 0x4825b(%rip), %xmm0    # 0xa2fb8
jne 0x5ada4
jp 0x5ada4
imull %r14d, %r13d
addl %ebp, %r13d
movslq %r13d, %rax
movq 0x8(%rsp), %rcx
leaq (%rcx,%rax,8), %rsi
movsd (%rsi), %xmm0
addsd 0x472b8(%rip), %xmm0    # 0xa2038
movsd %xmm0, (%rsi)
movsd 0x4e584(%rip), %xmm1    # 0xa9310
ucomisd %xmm0, %xmm1
jbe 0x5ad9c
movq %r15, %rdi
callq 0x23edc
jmp 0x5ada4
movq %r15, %rdi
callq 0x24096
cmpl $0x0, 0x38(%rsp)
jne 0x5aabe
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x5adbe
callq 0xe5b0
leaq 0x90(%rsp), %rdi
callq 0x59b0c
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5adef
jmp 0x5adef
jmp 0x5adef
jmp 0x5adef
jmp 0x5adef
jmp 0x5adef
jmp 0x5adef
jmp 0x5adef
jmp 0x5adef
movq %rax, %rbx
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x5ae01
callq 0xe5b0
leaq 0x90(%rsp), %rdi
callq 0x59b0c
movq %rbx, %rdi
callq 0xe7e0