// File: /workspace/github_cpp_makefile_star2_O2/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
pushq $-0x2
popq %r14
leaq 0x160(%rsp), %r15
movq %r14, %r12
cmpq $0x3, %r12
je 0x52cf3
movq %r12, %r13
imulq %r12, %r13
movq %r14, %rbp
cmpq $0x3, %rbp
je 0x52cea
movl %ebp, %eax
imull %ebp, %eax
addl %r13d, %eax
cvtsi2sd %eax, %xmm0
divsd 0x475df(%rip), %xmm0    # 0x9a2b8
callq 0xe820
movsd %xmm0, 0x10(%r15,%rbp,8)
incq %rbp
jmp 0x52cbf
incq %r12
addq $0x28, %r15
jmp 0x52caf
movl 0xc0(%rbx), %ecx
movl 0xc4(%rbx), %eax
movl %ecx, 0x4(%rsp)
movslq %ecx, %r14
movslq %eax, %r15
leaq 0x90(%rsp), %r12
movq %r12, %rdi
movl %r14d, %esi
movl %r15d, %edx
callq 0x51628
movq 0xb8(%r12), %rcx
xorpd %xmm0, %xmm0
leaq 0x20(%rsp), %rdx
movapd %xmm0, (%rdx)
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
movq %rax, 0x10(%rdx)
leaq -0x1(%r14), %rax
movq %rax, 0x68(%rsp)
leaq -0x1(%r15), %rax
movq %rax, 0x60(%rsp)
xorl %ebp, %ebp
testl %r15d, %r15d
movl $0x0, %eax
cmovgl %r15d, %eax
movq %rax, 0x18(%rsp)
testl %r14d, %r14d
movl $0x0, %eax
cmovgl %r14d, %eax
movq %rax, 0x70(%rsp)
movupd %xmm0, 0x18(%rdx)
movupd %xmm0, 0x24(%rdx)
movq %r14, 0x88(%rsp)
leaq (,%r14,8), %r12
movq %r12, %rax
negq %rax
movq %rax, 0x80(%rsp)
movq %rcx, 0x8(%rsp)
movq %rcx, %r14
cmpq 0x70(%rsp), %rbp
je 0x53300
leaq -0x1(%rbp), %rax
movq %rax, 0x10(%rsp)
leaq 0x1(%rbp), %rax
movq %rax, 0x58(%rsp)
movq %r14, 0x78(%rsp)
xorl %r13d, %r13d
cmpq %r13, 0x18(%rsp)
je 0x52fd2
movq 0xb8(%rbx), %rax
movslq 0xc8(%rbx), %rdx
movslq %r13d, %rcx
imulq %rdx, %rcx
leaq (%rax,%rbp,8), %rdx
movsd (%rdx,%rcx,8), %xmm0
ucomisd 0x4116a(%rip), %xmm0    # 0x93f68
jne 0x52e06
jnp 0x52fc7
testq %rbp, %rbp
je 0x52e61
movq 0x10(%rsp), %rdx
leaq (%rax,%rdx,8), %rax
movsd (%rax,%rcx,8), %xmm0
ucomisd 0x41147(%rip), %xmm0    # 0x93f68
jne 0x52e61
jp 0x52e61
leaq -0x8(%r14), %rsi
movsd -0x8(%r14), %xmm0
addsd 0x401d1(%rip), %xmm0    # 0x93008
movsd %xmm0, -0x8(%r14)
movsd 0x4747b(%rip), %xmm1    # 0x9a2c0
ucomisd %xmm0, %xmm1
jbe 0x52e57
leaq 0x20(%rsp), %rdi
callq 0x218b6
jmp 0x52e61
leaq 0x20(%rsp), %rdi
callq 0x21a94
testq %r13, %r13
je 0x52edc
movslq 0xc8(%rbx), %rax
leal -0x1(%r13), %ecx
movslq %ecx, %rcx
leaq (,%rbp,8), %rdx
addq 0xb8(%rbx), %rdx
imulq %rax, %rcx
movsd (%rdx,%rcx,8), %xmm0
ucomisd 0x410d4(%rip), %xmm0    # 0x93f68
jne 0x52edc
jp 0x52edc
movq 0x80(%rsp), %rax
leaq (%r14,%rax), %rsi
movsd (%r14,%rax), %xmm0
addsd 0x40156(%rip), %xmm0    # 0x93008
movsd %xmm0, (%r14,%rax)
movsd 0x47400(%rip), %xmm1    # 0x9a2c0
ucomisd %xmm0, %xmm1
jbe 0x52ed2
leaq 0x20(%rsp), %rdi
callq 0x218b6
jmp 0x52edc
leaq 0x20(%rsp), %rdi
callq 0x21a94
cmpq 0x68(%rsp), %rbp
jge 0x52f52
movslq 0xc8(%rbx), %rax
movslq %r13d, %rcx
movq 0x58(%rsp), %rdx
leaq (,%rdx,8), %rdx
addq 0xb8(%rbx), %rdx
imulq %rax, %rcx
movsd (%rdx,%rcx,8), %xmm0
ucomisd 0x41056(%rip), %xmm0    # 0x93f68
jne 0x52f52
jp 0x52f52
leaq 0x8(%r14), %rsi
movsd 0x8(%r14), %xmm0
addsd 0x400e0(%rip), %xmm0    # 0x93008
movsd %xmm0, 0x8(%r14)
movsd 0x4738a(%rip), %xmm1    # 0x9a2c0
ucomisd %xmm0, %xmm1
jbe 0x52f48
leaq 0x20(%rsp), %rdi
callq 0x218b6
jmp 0x52f52
leaq 0x20(%rsp), %rdi
callq 0x21a94
cmpq 0x60(%rsp), %r13
jge 0x52fc7
movslq 0xc8(%rbx), %rax
leal 0x1(%r13), %ecx
movslq %ecx, %rcx
leaq (,%rbp,8), %rdx
addq 0xb8(%rbx), %rdx
imulq %rax, %rcx
movsd (%rdx,%rcx,8), %xmm0
ucomisd 0x40fe1(%rip), %xmm0    # 0x93f68
jne 0x52fc7
jp 0x52fc7
leaq (%r14,%r12), %rsi
movsd (%r14,%r12), %xmm0
addsd 0x4006b(%rip), %xmm0    # 0x93008
movsd %xmm0, (%r14,%r12)
movsd 0x47315(%rip), %xmm1    # 0x9a2c0
ucomisd %xmm0, %xmm1
jbe 0x52fbd
leaq 0x20(%rsp), %rdi
callq 0x218b6
jmp 0x52fc7
leaq 0x20(%rsp), %rdi
callq 0x21a94
incq %r13
addq %r12, %r14
jmp 0x52dcd
movq 0x78(%rsp), %r14
addq $0x8, %r14
movq 0x58(%rsp), %rbp
jmp 0x52da8
leaq 0x20(%rsp), %rdi
callq 0x219be
subq 0x8(%rsp), %rax
shrq $0x3, %rax
cltd
idivl 0x158(%rsp)
movl %eax, %r12d
movl %edx, %ebp
movq 0xb8(%rbx), %rcx
movslq 0xc8(%rbx), %rdx
cltq
movslq %ebp, %rsi
addq $-0x2, %rax
movq %rax, %rdi
imulq %rdx, %rdi
leaq (,%rsi,8), %r8
leaq (%r8,%rdi,8), %rdi
movq %rcx, 0x18(%rsp)
addq %rcx, %rdi
addq $-0x10, %rdi
movq %rdx, 0x10(%rsp)
movq %rdx, %r8
shlq $0x3, %r8
xorpd %xmm1, %xmm1
leaq 0x160(%rsp), %r9
pushq $-0x2
popq %r10
xorpd %xmm0, %xmm0
cmpq $0x3, %r10
je 0x530e5
movq %r12, %rcx
movq %r10, %r11
addq %rsi, %r11
setns %r14b
cmpq 0x88(%rsp), %r11
setl %r11b
andb %r14b, %r11b
movq %rdi, %r14
xorl %r12d, %r12d
cmpq $0x5, %r12
je 0x530d2
testb %r11b, %r11b
je 0x530ca
movq %rax, %rdx
addq %r12, %rdx
sets %r13b
cmpq %r15, %rdx
setge %dl
orb %r13b, %dl
jne 0x530ca
movsd (%r14), %xmm2
ucomisd 0x40eb4(%rip), %xmm2    # 0x93f68
jne 0x530b8
jnp 0x530ca
movsd (%r9,%r12,8), %xmm3
mulsd %xmm3, %xmm2
addsd %xmm2, %xmm0
addsd %xmm3, %xmm1
incq %r12
addq %r8, %r14
jmp 0x53087
incq %r10
addq $0x8, %rdi
addq $0x28, %r9
movq %rcx, %r12
jmp 0x5305b
ucomisd 0x400fb(%rip), %xmm1    # 0x931e8
jbe 0x5310c
divsd %xmm1, %xmm0
movq 0x10(%rsp), %rax
imull %r12d, %eax
addl %ebp, %eax
cltq
movq 0x18(%rsp), %rcx
movsd %xmm0, (%rcx,%rax,8)
jmp 0x5311a
leaq 0x47221(%rip), %rdi     # 0x9a334
xorl %eax, %eax
callq 0x681ca
testl %ebp, %ebp
jle 0x53194
movq 0xb8(%rbx), %rax
movl 0xc8(%rbx), %ecx
imull %r12d, %ecx
addl %ebp, %ecx
decl %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
ucomisd 0x40e25(%rip), %xmm0    # 0x93f68
jne 0x53194
jp 0x53194
leal -0x1(%rbp), %eax
movl %r12d, %ecx
imull 0x4(%rsp), %ecx
addl %eax, %ecx
movslq %ecx, %rax
movq 0x8(%rsp), %rcx
leaq (%rcx,%rax,8), %rsi
movsd (%rsi), %xmm0
addsd 0x3fe9c(%rip), %xmm0    # 0x93008
movsd %xmm0, (%rsi)
movsd 0x47148(%rip), %xmm1    # 0x9a2c0
ucomisd %xmm0, %xmm1
jbe 0x5318a
leaq 0x20(%rsp), %rdi
callq 0x218b6
jmp 0x53194
leaq 0x20(%rsp), %rdi
callq 0x21a94
testl %r12d, %r12d
jle 0x5320a
leal -0x1(%r12), %eax
movq 0xb8(%rbx), %rcx
movl 0xc8(%rbx), %edx
imull %eax, %edx
addl %ebp, %edx
movslq %edx, %rdx
movsd (%rcx,%rdx,8), %xmm0
ucomisd 0x40da8(%rip), %xmm0    # 0x93f68
jne 0x5320a
jp 0x5320a
imull 0x4(%rsp), %eax
addl %ebp, %eax
cltq
movq 0x8(%rsp), %rcx
leaq (%rcx,%rax,8), %rsi
movsd (%rsi), %xmm0
addsd 0x3fe26(%rip), %xmm0    # 0x93008
movsd %xmm0, (%rsi)
movsd 0x470d2(%rip), %xmm1    # 0x9a2c0
ucomisd %xmm0, %xmm1
jbe 0x53200
leaq 0x20(%rsp), %rdi
callq 0x218b6
jmp 0x5320a
leaq 0x20(%rsp), %rdi
callq 0x21a94
cmpl 0x68(%rsp), %ebp
jge 0x53286
movq 0xb8(%rbx), %rax
movl 0xc8(%rbx), %ecx
imull %r12d, %ecx
addl %ebp, %ecx
incl %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
ucomisd 0x40d33(%rip), %xmm0    # 0x93f68
jne 0x53286
jp 0x53286
leal 0x1(%rbp), %eax
movl %r12d, %ecx
imull 0x4(%rsp), %ecx
addl %eax, %ecx
movslq %ecx, %rax
movq 0x8(%rsp), %rcx
leaq (%rcx,%rax,8), %rsi
movsd (%rsi), %xmm0
addsd 0x3fdaa(%rip), %xmm0    # 0x93008
movsd %xmm0, (%rsi)
movsd 0x47056(%rip), %xmm1    # 0x9a2c0
ucomisd %xmm0, %xmm1
jbe 0x5327c
leaq 0x20(%rsp), %rdi
callq 0x218b6
jmp 0x53286
leaq 0x20(%rsp), %rdi
callq 0x21a94
cmpl 0x60(%rsp), %r12d
jge 0x53300
incl %r12d
movq 0xb8(%rbx), %rax
movl 0xc8(%rbx), %ecx
imull %r12d, %ecx
addl %ebp, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
ucomisd 0x40cb5(%rip), %xmm0    # 0x93f68
jne 0x53300
jp 0x53300
imull 0x4(%rsp), %r12d
addl %ebp, %r12d
movslq %r12d, %rax
movq 0x8(%rsp), %rcx
leaq (%rcx,%rax,8), %rsi
movsd (%rsi), %xmm0
addsd 0x3fd30(%rip), %xmm0    # 0x93008
movsd %xmm0, (%rsi)
movsd 0x46fdc(%rip), %xmm1    # 0x9a2c0
ucomisd %xmm0, %xmm1
jbe 0x532f6
leaq 0x20(%rsp), %rdi
callq 0x218b6
jmp 0x53300
leaq 0x20(%rsp), %rdi
callq 0x21a94
cmpl $0x0, 0x28(%rsp)
jne 0x52fe5
leaq 0x20(%rsp), %rdi
callq 0x21c10
leaq 0x90(%rsp), %rdi
callq 0x5216c
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x53346
jmp 0x53346
jmp 0x53346
jmp 0x53346
jmp 0x53346
jmp 0x53346
jmp 0x53346
jmp 0x53346
jmp 0x53346
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x21c10
leaq 0x90(%rsp), %rdi
callq 0x5216c
movq %rbx, %rdi
callq 0xe810