// File: tomfunkhouser[P]gaps/apps/grd2pts/grd2pts.cpp
// Function: CreateMask(gaps::R3Grid*, int)
static R3Grid *
CreateMask(R3Grid *grid, RNBoolean local_maxima_only)
{
  // Start statistics
  RNTime start_time;
  start_time.Read();

  // Create mask
  R3Grid *mask = new R3Grid(*grid);
  if (!mask) {
    RNFail("Unable to allocate mask\n");
    return NULL;
  }

  // Keep only local maxima, make others zero
  if (local_maxima_only) {
    for (int k = 0; k < grid->ZResolution(); k++) {
      for (int j = 0; j < grid->YResolution(); j++) {
        for (int i = 0; i < grid->XResolution(); i++) {
          RNScalar grid_value = grid->GridValue(i, j, k);
          for (int t = k-1; t <= k+1; t++) {
            if ((t < 0) || (t >= grid->ZResolution())) continue;
            for (int s = j-1; s <= j+1; s++) {
              if ((s < 0) || (s >= grid->YResolution())) continue;
              for (int r = i-1; r <= i+1; r++) {
                if ((r < 0) || (r >= grid->XResolution())) continue;
                if ((r == i) && (s == j) && (t == k)) continue;
                RNScalar neighbor_value = grid->GridValue(r, s, t);
                if (neighbor_value > grid_value) {
                  mask->SetGridValue(i, j, k, 0);
                  goto exitinteriorloops;
                }
              }
            }
          }
        exitinteriorloops:;
        }
      }
    }
  }

  // Print statistics
  if (print_verbose) {
    printf("Created mask ...\n");
    printf("  Time = %.2f seconds\n", start_time.Elapsed());
    printf("  Resolution = %d %d %d\n", mask->XResolution(), mask->YResolution(), mask->ZResolution());
    const R3Box bbox = mask->WorldBox();
    printf("  World Box = ( %g %g %g ) ( %g %g %g )\n", bbox[0][0], bbox[0][1], bbox[0][2], bbox[1][0], bbox[1][1], bbox[1][2]);
    printf("  Spacing = %g\n", mask->GridToWorldScaleFactor());
    printf("  Cardinality = %d\n", mask->Cardinality());
    printf("  Volume = %g\n", mask->Volume());
    RNInterval grid_range = mask->Range();
    printf("  Minimum = %g\n", grid_range.Min());
    printf("  Maximum = %g\n", grid_range.Max());
    printf("  L1Norm = %g\n", mask->L1Norm());
    printf("  Local Maxima Only = %d\n", local_maxima_only);
    fflush(stdout);
  }

  // Return mask
  return mask;
}

// Assembly:
subq $0x138, %rsp            # imm = 0x138
movq %rdi, 0x128(%rsp)
movl %esi, 0x124(%rsp)
leaq 0x110(%rsp), %rdi
movq %rdi, 0x78(%rsp)
callq 0xd1830
movq 0x78(%rsp), %rdi
callq 0x13fe0
movl $0x250, %edi            # imm = 0x250
callq 0xf4c0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x80(%rsp)
movq 0x128(%rsp), %rsi
callq 0x25010
jmp 0x133f9
movq 0x80(%rsp), %rax
movq %rax, 0x108(%rsp)
cmpq $0x0, 0x108(%rsp)
jne 0x1345e
leaq 0x120ed0(%rip), %rdi    # 0x1342eb
movb $0x0, %al
callq 0xd5a50
movq $0x0, 0x130(%rsp)
jmp 0x13a50
movq 0x80(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x100(%rsp)
movl %eax, 0xfc(%rsp)
movl $0x250, %esi            # imm = 0x250
callq 0xf4d0
jmp 0x13a60
cmpl $0x0, 0x124(%rsp)
je 0x13767
movl $0x0, 0xf8(%rsp)
movl 0xf8(%rsp), %eax
movl %eax, 0x74(%rsp)
movq 0x128(%rsp), %rdi
callq 0x14260
movl %eax, %ecx
movl 0x74(%rsp), %eax
cmpl %ecx, %eax
jge 0x13765
movl $0x0, 0xf4(%rsp)
movl 0xf4(%rsp), %eax
movl %eax, 0x70(%rsp)
movq 0x128(%rsp), %rdi
callq 0x14240
movl %eax, %ecx
movl 0x70(%rsp), %eax
cmpl %ecx, %eax
jge 0x1374d
movl $0x0, 0xf0(%rsp)
movl 0xf0(%rsp), %eax
movl %eax, 0x6c(%rsp)
movq 0x128(%rsp), %rdi
callq 0x14220
movl %eax, %ecx
movl 0x6c(%rsp), %eax
cmpl %ecx, %eax
jge 0x13735
movq 0x128(%rsp), %rdi
movl 0xf0(%rsp), %esi
movl 0xf4(%rsp), %edx
movl 0xf8(%rsp), %ecx
callq 0x14520
movsd %xmm0, 0xe8(%rsp)
movl 0xf8(%rsp), %eax
subl $0x1, %eax
movl %eax, 0xe4(%rsp)
movl 0xe4(%rsp), %eax
movl 0xf8(%rsp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jg 0x1371b
cmpl $0x0, 0xe4(%rsp)
jl 0x13580
movl 0xe4(%rsp), %eax
movl %eax, 0x68(%rsp)
movq 0x128(%rsp), %rdi
callq 0x14260
movl %eax, %ecx
movl 0x68(%rsp), %eax
cmpl %ecx, %eax
jl 0x13585
jmp 0x13705
movl 0xf4(%rsp), %eax
subl $0x1, %eax
movl %eax, 0xe0(%rsp)
movl 0xe0(%rsp), %eax
movl 0xf4(%rsp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jg 0x13703
cmpl $0x0, 0xe0(%rsp)
jl 0x135db
movl 0xe0(%rsp), %eax
movl %eax, 0x64(%rsp)
movq 0x128(%rsp), %rdi
callq 0x14240
movl %eax, %ecx
movl 0x64(%rsp), %eax
cmpl %ecx, %eax
jl 0x135e0
jmp 0x136ed
movl 0xf0(%rsp), %eax
subl $0x1, %eax
movl %eax, 0xdc(%rsp)
movl 0xdc(%rsp), %eax
movl 0xf0(%rsp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jg 0x136eb
cmpl $0x0, 0xdc(%rsp)
jl 0x13636
movl 0xdc(%rsp), %eax
movl %eax, 0x60(%rsp)
movq 0x128(%rsp), %rdi
callq 0x14220
movl %eax, %ecx
movl 0x60(%rsp), %eax
cmpl %ecx, %eax
jl 0x1363b
jmp 0x136d5
movl 0xdc(%rsp), %eax
cmpl 0xf0(%rsp), %eax
jne 0x1366d
movl 0xe0(%rsp), %eax
cmpl 0xf4(%rsp), %eax
jne 0x1366d
movl 0xe4(%rsp), %eax
cmpl 0xf8(%rsp), %eax
jne 0x1366d
jmp 0x136d5
movq 0x128(%rsp), %rdi
movl 0xdc(%rsp), %esi
movl 0xe0(%rsp), %edx
movl 0xe4(%rsp), %ecx
callq 0x14520
movsd %xmm0, 0xd0(%rsp)
movsd 0xd0(%rsp), %xmm0
ucomisd 0xe8(%rsp), %xmm0
jbe 0x136d3
movq 0x108(%rsp), %rdi
movl 0xf0(%rsp), %esi
movl 0xf4(%rsp), %edx
movl 0xf8(%rsp), %ecx
xorps %xmm0, %xmm0
callq 0x14570
jmp 0x1371d
jmp 0x136d5
movl 0xdc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xdc(%rsp)
jmp 0x135f1
jmp 0x136ed
movl 0xe0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xe0(%rsp)
jmp 0x13596
jmp 0x13705
movl 0xe4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xe4(%rsp)
jmp 0x1353b
jmp 0x1371d
jmp 0x1371f
movl 0xf0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xf0(%rsp)
jmp 0x134d9
jmp 0x13737
movl 0xf4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xf4(%rsp)
jmp 0x134a8
jmp 0x1374f
movl 0xf8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xf8(%rsp)
jmp 0x13477
jmp 0x13767
cmpl $0x0, 0x165dee(%rip)    # 0x17955c
je 0x13a40
leaq 0x120b89(%rip), %rdi    # 0x134304
xorl %eax, %eax
movl %eax, 0x24(%rsp)
movb %al, 0x23(%rsp)
callq 0xf070
leaq 0x110(%rsp), %rdi
callq 0xd18f0
leaq 0x1208e6(%rip), %rdi    # 0x134084
movb $0x1, %al
callq 0xf070
movq 0x108(%rsp), %rdi
callq 0x14220
movl %eax, 0x18(%rsp)
movq 0x108(%rsp), %rdi
callq 0x14240
movl %eax, 0x1c(%rsp)
movq 0x108(%rsp), %rdi
callq 0x14260
movl 0x18(%rsp), %esi
movl 0x1c(%rsp), %edx
movl %eax, %ecx
movb 0x23(%rsp), %al
leaq 0x120a50(%rip), %rdi    # 0x134239
callq 0xf070
movq 0x108(%rsp), %rsi
leaq 0x98(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0x142e0
movl 0x24(%rsp), %esi
movq 0x28(%rsp), %rdi
callq 0x143a0
movl 0x24(%rsp), %esi
movq %rax, %rdi
callq 0x143e0
movl 0x24(%rsp), %esi
movq 0x28(%rsp), %rdi
movsd %xmm0, 0x58(%rsp)
callq 0x143a0
movq %rax, %rdi
movl $0x1, %esi
movl %esi, 0x30(%rsp)
callq 0x143e0
movl 0x24(%rsp), %esi
movq 0x28(%rsp), %rdi
movsd %xmm0, 0x38(%rsp)
callq 0x143a0
movq %rax, %rdi
movl $0x2, %esi
movl %esi, 0x34(%rsp)
callq 0x143e0
movq 0x28(%rsp), %rdi
movl 0x30(%rsp), %esi
movsd %xmm0, 0x40(%rsp)
callq 0x143a0
movl 0x24(%rsp), %esi
movq %rax, %rdi
callq 0x143e0
movq 0x28(%rsp), %rdi
movl 0x30(%rsp), %esi
movsd %xmm0, 0x48(%rsp)
callq 0x143a0
movl 0x30(%rsp), %esi
movq %rax, %rdi
callq 0x143e0
movq 0x28(%rsp), %rdi
movl 0x30(%rsp), %esi
movsd %xmm0, 0x50(%rsp)
callq 0x143a0
movl 0x34(%rsp), %esi
movq %rax, %rdi
callq 0x143e0
movsd 0x38(%rsp), %xmm1
movsd 0x40(%rsp), %xmm2
movsd 0x48(%rsp), %xmm3
movsd 0x50(%rsp), %xmm4
movaps %xmm0, %xmm5
movsd 0x58(%rsp), %xmm0
leaq 0x12095e(%rip), %rdi    # 0x134252
movb $0x6, %al
callq 0xf070
jmp 0x138fd
movq 0x108(%rsp), %rdi
callq 0x14400
leaq 0x12096a(%rip), %rdi    # 0x13427b
movb $0x1, %al
callq 0xf070
jmp 0x1391a
movq 0x108(%rsp), %rdi
callq 0x25510
movl %eax, 0x14(%rsp)
jmp 0x1392d
movl 0x14(%rsp), %esi
leaq 0x120953(%rip), %rdi    # 0x13428b
xorl %eax, %eax
callq 0xf070
jmp 0x13941
movq 0x108(%rsp), %rdi
callq 0x14420
movsd %xmm0, 0x8(%rsp)
jmp 0x13956
movsd 0x8(%rsp), %xmm0
leaq 0x12093c(%rip), %rdi    # 0x13429f
movb $0x1, %al
callq 0xf070
jmp 0x1396c
movq 0x108(%rsp), %rsi
leaq 0x88(%rsp), %rdi
callq 0x25310
jmp 0x13983
leaq 0x88(%rsp), %rdi
callq 0x14460
leaq 0x120917(%rip), %rdi    # 0x1342ae
movb $0x1, %al
callq 0xf070
jmp 0x139a0
leaq 0x88(%rsp), %rdi
callq 0x14470
leaq 0x12090a(%rip), %rdi    # 0x1342be
movb $0x1, %al
callq 0xf070
jmp 0x139bd
movq 0x108(%rsp), %rdi
callq 0x25400
movsd %xmm0, (%rsp)
jmp 0x139d1
movsd (%rsp), %xmm0
leaq 0x1208f1(%rip), %rdi    # 0x1342ce
movb $0x1, %al
callq 0xf070
jmp 0x139e6
movl 0x124(%rsp), %esi
leaq 0x120922(%rip), %rdi    # 0x134316
xorl %eax, %eax
callq 0xf070
jmp 0x139fd
movq 0x15c58c(%rip), %rax    # 0x16ff90
movq (%rax), %rdi
callq 0xf5d0
jmp 0x13a0e
leaq 0x98(%rsp), %rdi
callq 0x14480
jmp 0x13a40
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x100(%rsp)
movl %eax, 0xfc(%rsp)
leaq 0x98(%rsp), %rdi
callq 0x14480
jmp 0x13a60
movq 0x108(%rsp), %rax
movq %rax, 0x130(%rsp)
movq 0x130(%rsp), %rax
addq $0x138, %rsp            # imm = 0x138
retq
movq 0x100(%rsp), %rdi
callq 0xf890
nopl (%rax)