// File: /workspace/github_cpp_makefile_star2_O1/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 0x4deb1(%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 0x4de81(%rip), %xmm1    # 0xa9308
movsd %xmm0, 0x10(%r15,%r13,8)
incq %r13
cmpq $0x3, %r13
jne 0x5b465
incq %r14
addq $0x28, %r15
cmpq $0x3, %r14
jne 0x5b457
movslq 0xc0(%rbx), %r14
movl 0xc4(%rbx), %r15d
leaq 0x90(%rsp), %r12
movq %r12, %rdi
movl %r14d, %esi
movl %r15d, %edx
callq 0x59bf4
movq 0xb8(%r12), %rax
movq %rax, 0x10(%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)
movq %r14, 0x8(%rsp)
testq %r14, %r14
jle 0x5b783
movslq %r15d, %rcx
movq 0x8(%rsp), %rax
leaq -0x1(%rax), %rdx
movq %rdx, 0x20(%rsp)
decq %rcx
movq %rcx, 0x88(%rsp)
leaq (,%rax,8), %r12
movq %r12, %rax
negq %rax
movq %rax, 0x70(%rsp)
xorl %r13d, %r13d
movq 0x10(%rsp), %rax
movq %rax, 0x28(%rsp)
testl %r15d, %r15d
jle 0x5b76f
leaq -0x1(%r13), %rax
movq %rax, 0x80(%rsp)
leaq 0x1(%r13), %rax
shlq $0x3, %rax
movq %rax, 0x78(%rsp)
leaq (,%r13,8), %rax
movq %rax, 0x18(%rsp)
movq 0x28(%rsp), %rbp
xorl %r14d, %r14d
movq 0xb8(%rbx), %rax
movslq 0xc8(%rbx), %rdx
movslq %r14d, %rcx
imulq %rdx, %rcx
leaq (%rax,%r13,8), %rdx
movsd (%rdx,%rcx,8), %xmm0
ucomisd 0x47a17(%rip), %xmm0    # 0xa2fb8
jne 0x5b5a9
jnp 0x5b760
testq %r13, %r13
je 0x5b608
movslq %ecx, %rcx
movq 0x80(%rsp), %rdx
leaq (%rax,%rdx,8), %rax
movsd (%rax,%rcx,8), %xmm0
ucomisd 0x479ee(%rip), %xmm0    # 0xa2fb8
jne 0x5b608
jp 0x5b608
leaq -0x8(%rbp), %rsi
movsd -0x8(%rbp), %xmm0
addsd 0x46a59(%rip), %xmm0    # 0xa2038
movsd %xmm0, -0x8(%rbp)
movsd 0x4dd24(%rip), %xmm1    # 0xa9310
ucomisd %xmm0, %xmm1
jbe 0x5b5fe
leaq 0x30(%rsp), %rdi
callq 0x2412e
jmp 0x5b608
leaq 0x30(%rsp), %rdi
callq 0x242c4
testq %r14, %r14
je 0x5b67d
movslq 0xc8(%rbx), %rax
leal -0x1(%r14), %ecx
movslq %ecx, %rcx
imulq %rax, %rcx
movq 0xb8(%rbx), %rax
addq 0x18(%rsp), %rax
movsd (%rax,%rcx,8), %xmm0
ucomisd 0x47980(%rip), %xmm0    # 0xa2fb8
jne 0x5b67d
jp 0x5b67d
movq 0x70(%rsp), %rax
leaq (%rax,%rbp), %rsi
movsd (%rbp,%rax), %xmm0
addsd 0x469e5(%rip), %xmm0    # 0xa2038
movsd %xmm0, (%rbp,%rax)
movsd 0x4dcaf(%rip), %xmm1    # 0xa9310
ucomisd %xmm0, %xmm1
jbe 0x5b673
leaq 0x30(%rsp), %rdi
callq 0x2412e
jmp 0x5b67d
leaq 0x30(%rsp), %rdi
callq 0x242c4
cmpq 0x20(%rsp), %r13
jge 0x5b6e9
movslq 0xc8(%rbx), %rax
movslq %r14d, %rcx
imulq %rax, %rcx
movq 0xb8(%rbx), %rax
addq 0x78(%rsp), %rax
movsd (%rax,%rcx,8), %xmm0
ucomisd 0x4790d(%rip), %xmm0    # 0xa2fb8
jne 0x5b6e9
jp 0x5b6e9
leaq 0x8(%rbp), %rsi
movsd 0x8(%rbp), %xmm0
addsd 0x46978(%rip), %xmm0    # 0xa2038
movsd %xmm0, 0x8(%rbp)
movsd 0x4dc43(%rip), %xmm1    # 0xa9310
ucomisd %xmm0, %xmm1
jbe 0x5b6df
leaq 0x30(%rsp), %rdi
callq 0x2412e
jmp 0x5b6e9
leaq 0x30(%rsp), %rdi
callq 0x242c4
cmpq 0x88(%rsp), %r14
jge 0x5b760
movslq 0xc8(%rbx), %rax
leal 0x1(%r14), %ecx
movslq %ecx, %rcx
imulq %rax, %rcx
movq 0xb8(%rbx), %rax
addq 0x18(%rsp), %rax
movsd (%rax,%rcx,8), %xmm0
ucomisd 0x4789a(%rip), %xmm0    # 0xa2fb8
jne 0x5b760
jp 0x5b760
leaq (%r12,%rbp), %rsi
movsd (%rbp,%r12), %xmm0
addsd 0x46903(%rip), %xmm0    # 0xa2038
movsd %xmm0, (%rbp,%r12)
movsd 0x4dbcc(%rip), %xmm1    # 0xa9310
ucomisd %xmm0, %xmm1
jbe 0x5b756
leaq 0x30(%rsp), %rdi
callq 0x2412e
jmp 0x5b760
leaq 0x30(%rsp), %rdi
callq 0x242c4
incq %r14
addq %r12, %rbp
cmpq %r14, %r15
jne 0x5b57b
incq %r13
addq $0x8, 0x28(%rsp)
cmpq 0x8(%rsp), %r13
jne 0x5b544
cmpl $0x0, 0x38(%rsp)
je 0x5ba9b
movq 0x8(%rsp), %rax
decl %eax
movl %eax, 0x18(%rsp)
movslq %r15d, %r12
leal -0x1(%r15), %eax
movl %eax, 0x20(%rsp)
leaq 0x30(%rsp), %r15
movq %r15, %rdi
callq 0x2422a
subq 0x10(%rsp), %rax
shrq $0x3, %rax
cltd
idivl 0x158(%rsp)
movl %eax, %r13d
movl %edx, %ebp
movq 0xb8(%rbx), %rsi
movslq 0xc8(%rbx), %rax
movslq %r13d, %rcx
movslq %edx, %rdx
addq $-0x2, %rcx
movq %rcx, %rdi
imulq %rax, %rdi
leaq (,%rdx,8), %r8
leaq (%r8,%rdi,8), %rdi
addq %rdi, %rsi
addq $-0x10, %rsi
shlq $0x3, %rax
xorpd %xmm0, %xmm0
movq $-0x2, %rdi
leaq 0x160(%rsp), %r8
xorpd %xmm1, %xmm1
movq %rdi, %r9
addq %rdx, %r9
setns %r10b
cmpq 0x8(%rsp), %r9
setl %r9b
andb %r10b, %r9b
movq %rsi, %r10
xorl %r11d, %r11d
testb %r9b, %r9b
je 0x5b865
movq %rcx, %r14
addq %r11, %r14
js 0x5b865
cmpq %r12, %r14
jge 0x5b865
movsd (%r10), %xmm2
ucomisd 0x47769(%rip), %xmm2    # 0xa2fb8
jne 0x5b853
jnp 0x5b865
movsd (%r8,%r11,8), %xmm3
mulsd %xmm3, %xmm2
addsd %xmm2, %xmm0
addsd %xmm3, %xmm1
incq %r11
addq %rax, %r10
cmpq $0x5, %r11
jne 0x5b830
incq %rdi
addq $0x28, %r8
addq $0x8, %rsi
cmpq $0x3, %rdi
jne 0x5b814
ucomisd 0x4695e(%rip), %xmm1    # 0xa21e8
jbe 0x5b8ad
divsd %xmm1, %xmm0
movq 0xb8(%rbx), %rax
movl 0xc8(%rbx), %ecx
imull %r13d, %ecx
addl %ebp, %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
jmp 0x5b8bb
leaq 0x4daf0(%rip), %rdi     # 0xa93a4
xorl %eax, %eax
callq 0x72a1e
testl %ebp, %ebp
jle 0x5b931
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 0x476d4(%rip), %xmm0    # 0xa2fb8
jne 0x5b931
jp 0x5b931
leal -0x1(%rbp), %eax
movl %r13d, %ecx
imull 0x8(%rsp), %ecx
addl %eax, %ecx
movslq %ecx, %rax
movq 0x10(%rsp), %rcx
leaq (%rcx,%rax,8), %rsi
movsd (%rsi), %xmm0
addsd 0x4672b(%rip), %xmm0    # 0xa2038
movsd %xmm0, (%rsi)
movsd 0x4d9f7(%rip), %xmm1    # 0xa9310
ucomisd %xmm0, %xmm1
jbe 0x5b929
movq %r15, %rdi
callq 0x2412e
jmp 0x5b931
movq %r15, %rdi
callq 0x242c4
testl %r13d, %r13d
jle 0x5b9a2
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 0x4765c(%rip), %xmm0    # 0xa2fb8
jne 0x5b9a2
jp 0x5b9a2
imull 0x8(%rsp), %eax
addl %ebp, %eax
cltq
movq 0x10(%rsp), %rcx
leaq (%rcx,%rax,8), %rsi
movsd (%rsi), %xmm0
addsd 0x466ba(%rip), %xmm0    # 0xa2038
movsd %xmm0, (%rsi)
movsd 0x4d986(%rip), %xmm1    # 0xa9310
ucomisd %xmm0, %xmm1
jbe 0x5b99a
movq %r15, %rdi
callq 0x2412e
jmp 0x5b9a2
movq %r15, %rdi
callq 0x242c4
cmpl 0x18(%rsp), %ebp
jge 0x5ba1a
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 0x475eb(%rip), %xmm0    # 0xa2fb8
jne 0x5ba1a
jp 0x5ba1a
leal 0x1(%rbp), %eax
movl %r13d, %ecx
imull 0x8(%rsp), %ecx
addl %eax, %ecx
movslq %ecx, %rax
movq 0x10(%rsp), %rcx
leaq (%rcx,%rax,8), %rsi
movsd (%rsi), %xmm0
addsd 0x46642(%rip), %xmm0    # 0xa2038
movsd %xmm0, (%rsi)
movsd 0x4d90e(%rip), %xmm1    # 0xa9310
ucomisd %xmm0, %xmm1
jbe 0x5ba12
movq %r15, %rdi
callq 0x2412e
jmp 0x5ba1a
movq %r15, %rdi
callq 0x242c4
cmpl 0x20(%rsp), %r13d
jge 0x5ba90
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 0x47571(%rip), %xmm0    # 0xa2fb8
jne 0x5ba90
jp 0x5ba90
imull 0x8(%rsp), %r13d
addl %ebp, %r13d
movslq %r13d, %rax
movq 0x10(%rsp), %rcx
leaq (%rcx,%rax,8), %rsi
movsd (%rsi), %xmm0
addsd 0x465cc(%rip), %xmm0    # 0xa2038
movsd %xmm0, (%rsi)
movsd 0x4d898(%rip), %xmm1    # 0xa9310
ucomisd %xmm0, %xmm1
jbe 0x5ba88
movq %r15, %rdi
callq 0x2412e
jmp 0x5ba90
movq %r15, %rdi
callq 0x242c4
cmpl $0x0, 0x38(%rsp)
jne 0x5b7a9
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x5baaa
callq 0xe5b0
leaq 0x90(%rsp), %rdi
callq 0x5a814
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5badb
jmp 0x5badb
jmp 0x5badb
jmp 0x5badb
jmp 0x5badb
jmp 0x5badb
jmp 0x5badb
jmp 0x5badb
jmp 0x5badb
movq %rax, %rbx
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x5baed
callq 0xe5b0
leaq 0x90(%rsp), %rdi
callq 0x5a814
movq %rbx, %rdi
callq 0xe7e0