// File: tomfunkhouser[P]gaps/pkgs/RGBD/RGBDUtil.cpp
// Function: gaps::RGBDCreateTangentChannels(gaps::R2Grid const&, gaps::R2Grid const&, gaps::R2Grid const&, gaps::R2Grid const&, gaps::R2Grid const&, gaps::R2Grid&, gaps::R2Grid&, gaps::R2Grid&, gaps::R2Grid&, gaps::R2Grid&, gaps::R2Grid&, gaps::R2Grid&, gaps::R2Grid&, gaps::R3Point const&, gaps::R3Vector const&, gaps::R3Vector const&, double, int, int)
int RGBDCreateTangentChannels(const R2Grid& input_depth_image, 
  const R2Grid& input_px_image, const R2Grid& input_py_image, const R2Grid& input_pz_image,
  const R2Grid& input_boundary_image,
  R2Grid& output_nx_image, R2Grid& output_ny_image, R2Grid& output_nz_image,
  R2Grid& output_tx_image, R2Grid& output_ty_image, R2Grid& output_tz_image,
  R2Grid& output_r1_image, R2Grid& output_r2_image,
  const R3Point& viewpoint, const R3Vector& towards, const R3Vector& up,
  RNLength neighborhood_world_radius, int neighborhood_pixel_radius, RNBoolean neighborhood_search)
{
  // Initialize normal images
  output_nx_image = input_depth_image; 
  output_nx_image.Clear(R2_GRID_UNKNOWN_VALUE);
  output_ny_image = output_nx_image;
  output_nz_image = output_nx_image;
  output_tx_image = output_nx_image;
  output_ty_image = output_nx_image;
  output_tz_image = output_nx_image;
  output_r1_image = output_nx_image;
  output_r2_image = output_nx_image;

  // Allocate array of neighborhood points
  int pixel_radius = neighborhood_pixel_radius * input_depth_image.XResolution() / 640.0 + 0.5;
  if (pixel_radius == 0) pixel_radius = 1;
  int neighborhood_pixel_radius_squared = pixel_radius * pixel_radius;
  RNScalar neighborhood_world_radius_squared = neighborhood_world_radius * neighborhood_world_radius;
  R3Point *neighborhood_points = new R3Point [ input_depth_image.NEntries() ];

  // Allocate search grid
  R2Grid search_grid(input_depth_image.XResolution(), input_depth_image.YResolution());
  const RNScalar *search_valuesp = search_grid.GridValues();
  search_grid.Clear(-1);

  // Fill normal images
  for (int i = 0; i < input_depth_image.XResolution(); i++) {
    for (int j = 0; j < input_depth_image.YResolution(); j++) {
      int neighborhood_npoints = 0;

      // Check depth
      RNScalar depth = input_depth_image.GridValue(i, j);
      if (RNIsNegativeOrZero(depth)) continue;

      // Get image position
      R2Point image_position(i, j);

      // Get world position
      RNScalar x = input_px_image.GridValue(i, j);
      RNScalar y = input_py_image.GridValue(i, j);
      RNScalar z = input_pz_image.GridValue(i, j);
      R3Point world_position(x, y, z);

      // Create array of neighborhood points
      if (neighborhood_search) {
        // Initialize stack
        int seed_index;
        RNArray<const RNScalar *> stack;
        search_grid.IndicesToIndex(i, j, seed_index);
        const RNScalar *seed_valuep = &search_valuesp[seed_index];
        search_grid.SetGridValue(seed_index, seed_index);
        stack.Insert(seed_valuep);

        // Depth first search, not extending beyond boundaries
        while (!stack.IsEmpty()) {
          // Pop current point off stack
          int ix, iy;
          const RNScalar *current_valuep = stack.Tail(); stack.RemoveTail();
          int current_index = current_valuep - search_valuesp;
          search_grid.IndexToIndices(current_index, ix, iy);

          // Get current info
          RNScalar current_x = input_px_image.GridValue(ix, iy);
          RNScalar current_y = input_py_image.GridValue(ix, iy);
          RNScalar current_z = input_pz_image.GridValue(ix, iy);
          R3Point current_position(current_x, current_y, current_z);
          RNScalar current_boundary_value = input_boundary_image.GridValue(ix, iy);
          int current_boundary = (int) (current_boundary_value + 0.5);
        
          // Add point to array
          neighborhood_points[neighborhood_npoints++] = current_position;

          // Add adjacent points to stack
          for (int inx = ix-1; inx <= ix+1; inx++) {
            if ((inx < 0) || (inx >= search_grid.XResolution())) continue;
            for (int iny = iy-1; iny <= iy+1; iny++) {
              if ((iny < 0) || (iny >= search_grid.YResolution())) continue;

              // Check if neighbor has already been visited
              RNScalar neighbor_value = search_grid.GridValue(inx, iny);
              if (RNIsEqual(neighbor_value, seed_index)) continue;
              search_grid.SetGridValue(inx, iny, seed_index);

              // Check neighbor depth
              RNScalar neighbor_depth = input_depth_image.GridValue(inx, iny);
              if (RNIsNegativeOrZero(neighbor_depth)) continue;

              // Check neighbor image distance
              R2Point neighbor_image_position(inx, iny);
              if (R2SquaredDistance(image_position, neighbor_image_position) > neighborhood_pixel_radius_squared) continue;
            
              // Check neighbor world distance
              RNScalar neighbor_x = input_px_image.GridValue(inx, iny);
              RNScalar neighbor_y = input_py_image.GridValue(inx, iny);
              RNScalar neighbor_z = input_pz_image.GridValue(inx, iny);
              R3Point neighbor_world_position(neighbor_x, neighbor_y, neighbor_z);
              if (neighborhood_world_radius_squared > 0) {
                if (R3SquaredDistance(world_position, neighbor_world_position) > neighborhood_world_radius_squared) continue;
              }

              // Check if neighbor is across boundary
              int neighbor_boundary = (int) (input_boundary_image.GridValue(inx, iny) + 0.5);
              if ((current_boundary == RGBD_SHADOW_BOUNDARY) && (neighbor_boundary == RGBD_SILHOUETTE_BOUNDARY)) continue;
              if ((current_boundary == RGBD_SILHOUETTE_BOUNDARY) && (neighbor_boundary == RGBD_SHADOW_BOUNDARY)) continue;
        
              // Add neighbor to search
              int neighbor_index;
              search_grid.IndicesToIndex(inx, iny, neighbor_index);
              const RNScalar *neighbor_valuep = &search_valuesp[neighbor_index];
              stack.Insert(neighbor_valuep);
            }
          }
        }
      }
      else {
        // Add neighbor points to array
        for (int inx = i-neighborhood_pixel_radius; inx <= i+neighborhood_pixel_radius; inx++) {
          if ((inx < 0) || (inx >= input_depth_image.XResolution())) continue;
          for (int iny = j-neighborhood_pixel_radius; iny <= j+neighborhood_pixel_radius; iny++) {
            if ((iny < 0) || (iny >= input_depth_image.YResolution())) continue;
            
            // Check neighbor depth
            RNScalar neighbor_depth = input_depth_image.GridValue(inx, iny);
            if (RNIsNegativeOrZero(neighbor_depth)) continue;

            // Check neighbor world position
            RNScalar neighbor_x = input_px_image.GridValue(inx, iny);
            RNScalar neighbor_y = input_py_image.GridValue(inx, iny);
            RNScalar neighbor_z = input_pz_image.GridValue(inx, iny);
            R3Point neighbor_world_position(neighbor_x, neighbor_y, neighbor_z);
            if (neighborhood_world_radius_squared > 0) {
              if (R3SquaredDistance(world_position, neighbor_world_position) > neighborhood_world_radius_squared) continue;
            }

            // Add neighbor point to array
            neighborhood_points[neighborhood_npoints++] = neighbor_world_position;
          }
        }
      }

      // Check number of neighbor points
      if (neighborhood_npoints < 3) continue;

      // Solve for normal
      RNScalar variances[3];
      R3Point centroid = R3Centroid(neighborhood_npoints, neighborhood_points);
      R3Triad triad = R3PrincipleAxes(centroid, neighborhood_npoints, neighborhood_points, NULL, variances);
      R3Vector normal = triad[2];
      R3Vector tangent = triad[0];
      
      // Flip normal to point towards camera
      R3Plane plane(world_position, normal);
      if (R3SignedDistance(plane, viewpoint) < 0) normal.Flip(); 

      // Fill normal images
      output_nx_image.SetGridValue(i, j, normal.X());
      output_ny_image.SetGridValue(i, j, normal.Y());
      output_nz_image.SetGridValue(i, j, normal.Z());

      // Fill tangent images
      output_tx_image.SetGridValue(i, j, tangent.X());
      output_ty_image.SetGridValue(i, j, tangent.Y());
      output_tz_image.SetGridValue(i, j, tangent.Z());

      // Fill radius images
      RNScalar r1 = 3 * sqrt(variances[0]);
      RNScalar r2 = 3 * sqrt(variances[1]);
      if (neighborhood_pixel_radius > 1) r1 /= neighborhood_pixel_radius;
      if (neighborhood_pixel_radius > 1) r2 /= neighborhood_pixel_radius;
      output_r1_image.SetGridValue(i, j, r1);
      output_r2_image.SetGridValue(i, j, r2);
    }
  }

  // Delete array of neighborhood points
  delete [] neighborhood_points;

  // Return success 
  return 1;
}

// Assembly:
subq $0x598, %rsp            # imm = 0x598
movl 0x5f8(%rsp), %eax
movl 0x5f0(%rsp), %eax
movq 0x5e8(%rsp), %rax
movq 0x5e0(%rsp), %rax
movq 0x5d8(%rsp), %rax
movq 0x5d0(%rsp), %rax
movq 0x5c8(%rsp), %rax
movq 0x5c0(%rsp), %rax
movq 0x5b8(%rsp), %rax
movq 0x5b0(%rsp), %rax
movq 0x5a8(%rsp), %rax
movq 0x5a0(%rsp), %rax
movq %rdi, 0x590(%rsp)
movq %rsi, 0x588(%rsp)
movq %rdx, 0x580(%rsp)
movq %rcx, 0x578(%rsp)
movq %r8, 0x570(%rsp)
movq %r9, 0x568(%rsp)
movsd %xmm0, 0x560(%rsp)
movq 0x590(%rsp), %rsi
movq 0x568(%rsp), %rdi
callq 0xb2f20
movq 0x568(%rsp), %rdi
movsd 0x13418d(%rip), %xmm0   # 0x166020
callq 0xb5a20
movq 0x568(%rsp), %rsi
movq 0x5a0(%rsp), %rdi
callq 0xb2f20
movq 0x568(%rsp), %rsi
movq 0x5a8(%rsp), %rdi
callq 0xb2f20
movq 0x568(%rsp), %rsi
movq 0x5b0(%rsp), %rdi
callq 0xb2f20
movq 0x568(%rsp), %rsi
movq 0x5b8(%rsp), %rdi
callq 0xb2f20
movq 0x568(%rsp), %rsi
movq 0x5c0(%rsp), %rdi
callq 0xb2f20
movq 0x568(%rsp), %rsi
movq 0x5c8(%rsp), %rdi
callq 0xb2f20
movq 0x568(%rsp), %rsi
movq 0x5d0(%rsp), %rdi
callq 0xb2f20
movl 0x5f0(%rsp), %eax
movl %eax, 0x20c(%rsp)
movq 0x590(%rsp), %rdi
callq 0x27940
movl %eax, %ecx
movl 0x20c(%rsp), %eax
imull %ecx, %eax
cvtsi2sd %eax, %xmm0
movsd 0x135252(%rip), %xmm1   # 0x1671b0
divsd %xmm1, %xmm0
movsd 0x1340be(%rip), %xmm1   # 0x166028
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x55c(%rsp)
cmpl $0x0, 0x55c(%rsp)
jne 0x31f8e
movl $0x1, 0x55c(%rsp)
movl 0x55c(%rsp), %eax
imull 0x55c(%rsp), %eax
movl %eax, 0x558(%rsp)
movsd 0x560(%rsp), %xmm0
mulsd 0x560(%rsp), %xmm0
movsd %xmm0, 0x550(%rsp)
movq 0x590(%rsp), %rdi
callq 0x30740
cltq
movq %rax, 0x1f8(%rsp)
movl $0x18, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
movq %rdi, 0x1f0(%rsp)
callq 0x12040
movq %rax, %rcx
movq 0x1f8(%rsp), %rax
movq %rcx, 0x200(%rsp)
cmpq $0x0, %rax
je 0x3207a
movq 0x200(%rsp), %rax
movq 0x1f8(%rsp), %rcx
imulq $0x18, %rcx, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq %rcx, 0x1e0(%rsp)
movq %rax, 0x1e8(%rsp)
movq 0x1e8(%rsp), %rdi
movq %rdi, 0x1d8(%rsp)
callq 0x8a990
jmp 0x32059
movq 0x1e0(%rsp), %rcx
movq 0x1d8(%rsp), %rax
addq $0x18, %rax
cmpq %rcx, %rax
movq %rax, 0x1e8(%rsp)
jne 0x32042
movq 0x200(%rsp), %rax
movq %rax, 0x548(%rsp)
movq 0x590(%rsp), %rdi
callq 0x27940
movl %eax, 0x1c4(%rsp)
movq 0x590(%rsp), %rdi
callq 0x27960
movl 0x1c4(%rsp), %esi
movl %eax, %edx
leaq 0x468(%rsp), %rdi
movq %rdi, 0x1c8(%rsp)
callq 0xb1ee0
movq 0x1c8(%rsp), %rdi
callq 0x358f0
movq %rax, 0x1d0(%rsp)
jmp 0x320e0
movq 0x1d0(%rsp), %rax
movq %rax, 0x460(%rsp)
leaq 0x468(%rsp), %rdi
movsd 0x133f18(%rip), %xmm0   # 0x166018
callq 0xb5a20
jmp 0x32107
movl $0x0, 0x45c(%rsp)
movl 0x45c(%rsp), %eax
movl %eax, 0x1bc(%rsp)
movq 0x590(%rsp), %rdi
callq 0x27940
movl %eax, 0x1c0(%rsp)
jmp 0x32136
movl 0x1bc(%rsp), %eax
movl 0x1c0(%rsp), %ecx
cmpl %ecx, %eax
jge 0x33193
movl $0x0, 0x458(%rsp)
movl 0x458(%rsp), %eax
movl %eax, 0x1b4(%rsp)
movq 0x590(%rsp), %rdi
callq 0x27960
movl %eax, 0x1b8(%rsp)
jmp 0x3217b
movl 0x1b4(%rsp), %eax
movl 0x1b8(%rsp), %ecx
cmpl %ecx, %eax
jge 0x3317b
movl $0x0, 0x454(%rsp)
movq 0x590(%rsp), %rdi
movl 0x45c(%rsp), %esi
movl 0x458(%rsp), %edx
callq 0x1a060
movsd %xmm0, 0x1a8(%rsp)
jmp 0x321c2
movsd 0x1a8(%rsp), %xmm0
movsd %xmm0, 0x448(%rsp)
movsd 0x448(%rsp), %xmm0
leaq 0x17fcdc(%rip), %rax    # 0x1b1ec0
movsd (%rax), %xmm1
callq 0x1a3d0
movl %eax, 0x1a4(%rsp)
jmp 0x321f6
movl 0x1a4(%rsp), %eax
cmpl $0x0, %eax
je 0x3224e
jmp 0x33165
movq 0x1f0(%rsp), %rsi
movq 0x200(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x540(%rsp)
movl %eax, 0x53c(%rsp)
callq 0x12d80
jmp 0x331d7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x540(%rsp)
movl %eax, 0x53c(%rsp)
jmp 0x331ca
cvtsi2sdl 0x45c(%rsp), %xmm0
cvtsi2sdl 0x458(%rsp), %xmm1
leaq 0x438(%rsp), %rdi
callq 0xcfd80
jmp 0x3226f
movq 0x588(%rsp), %rdi
movl 0x45c(%rsp), %esi
movl 0x458(%rsp), %edx
callq 0x1a060
movsd %xmm0, 0x198(%rsp)
jmp 0x32295
movsd 0x198(%rsp), %xmm0
movsd %xmm0, 0x430(%rsp)
movq 0x580(%rsp), %rdi
movl 0x45c(%rsp), %esi
movl 0x458(%rsp), %edx
callq 0x1a060
movsd %xmm0, 0x190(%rsp)
jmp 0x322cd
movsd 0x190(%rsp), %xmm0
movsd %xmm0, 0x428(%rsp)
movq 0x578(%rsp), %rdi
movl 0x45c(%rsp), %esi
movl 0x458(%rsp), %edx
callq 0x1a060
movsd %xmm0, 0x188(%rsp)
jmp 0x32305
movsd 0x188(%rsp), %xmm0
movsd %xmm0, 0x420(%rsp)
movsd 0x430(%rsp), %xmm0
movsd 0x428(%rsp), %xmm1
movsd 0x420(%rsp), %xmm2
leaq 0x408(%rsp), %rdi
callq 0x8a9a0
jmp 0x32341
cmpl $0x0, 0x5f8(%rsp)
je 0x32a6d
leaq 0x3f0(%rsp), %rdi
callq 0x35910
jmp 0x3235e
movl 0x45c(%rsp), %esi
movl 0x458(%rsp), %edx
leaq 0x468(%rsp), %rdi
leaq 0x404(%rsp), %rcx
callq 0x35920
jmp 0x32383
movq 0x460(%rsp), %rax
movslq 0x404(%rsp), %rcx
leaq (%rax,%rcx,8), %rax
movq %rax, 0x3e8(%rsp)
movl 0x404(%rsp), %esi
cvtsi2sd %esi, %xmm0
leaq 0x468(%rsp), %rdi
callq 0x30780
jmp 0x323b9
movq 0x3e8(%rsp), %rsi
leaq 0x3f0(%rsp), %rdi
callq 0x35950
jmp 0x323d0
jmp 0x323d2
leaq 0x3f0(%rsp), %rdi
callq 0x35980
movl %eax, 0x184(%rsp)
jmp 0x323e8
movl 0x184(%rsp), %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x32400
jmp 0x32a5b
leaq 0x3f0(%rsp), %rdi
callq 0x359a0
movq %rax, 0x178(%rsp)
jmp 0x32417
movq 0x178(%rsp), %rax
movq %rax, 0x3d8(%rsp)
leaq 0x3f0(%rsp), %rdi
callq 0x224a0
jmp 0x32436
movq 0x3d8(%rsp), %rax
movq 0x460(%rsp), %rcx
subq %rcx, %rax
shrq $0x3, %rax
movl %eax, 0x3d4(%rsp)
movl 0x3d4(%rsp), %esi
leaq 0x468(%rsp), %rdi
leaq 0x3e4(%rsp), %rdx
leaq 0x3e0(%rsp), %rcx
callq 0x359b0
jmp 0x3247a
movq 0x588(%rsp), %rdi
movl 0x3e4(%rsp), %esi
movl 0x3e0(%rsp), %edx
callq 0x1a060
movsd %xmm0, 0x170(%rsp)
jmp 0x324a0
movsd 0x170(%rsp), %xmm0
movsd %xmm0, 0x3c8(%rsp)
movq 0x580(%rsp), %rdi
movl 0x3e4(%rsp), %esi
movl 0x3e0(%rsp), %edx
callq 0x1a060
movsd %xmm0, 0x168(%rsp)
jmp 0x324d8
movsd 0x168(%rsp), %xmm0
movsd %xmm0, 0x3c0(%rsp)
movq 0x578(%rsp), %rdi
movl 0x3e4(%rsp), %esi
movl 0x3e0(%rsp), %edx
callq 0x1a060
movsd %xmm0, 0x160(%rsp)
jmp 0x32510
movsd 0x160(%rsp), %xmm0
movsd %xmm0, 0x3b8(%rsp)
movsd 0x3c8(%rsp), %xmm0
movsd 0x3c0(%rsp), %xmm1
movsd 0x3b8(%rsp), %xmm2
leaq 0x3a0(%rsp), %rdi
callq 0x8a9a0
jmp 0x3254c
movq 0x570(%rsp), %rdi
movl 0x3e4(%rsp), %esi
movl 0x3e0(%rsp), %edx
callq 0x1a060
movsd %xmm0, 0x158(%rsp)
jmp 0x32572
movsd 0x158(%rsp), %xmm0
movsd %xmm0, 0x398(%rsp)
movsd 0x398(%rsp), %xmm0
movsd 0x133a93(%rip), %xmm1   # 0x166028
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x394(%rsp)
movq 0x548(%rsp), %rax
movslq 0x454(%rsp), %rcx
movl %ecx, %edx
incl %edx
movl %edx, 0x454(%rsp)
leaq (%rcx,%rcx,2), %rcx
leaq (%rax,%rcx,8), %rdi
leaq 0x3a0(%rsp), %rsi
callq 0x8b030
jmp 0x325d6
movl 0x3e4(%rsp), %eax
subl $0x1, %eax
movl %eax, 0x390(%rsp)
movl 0x390(%rsp), %eax
movl 0x3e4(%rsp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jg 0x32a56
cmpl $0x0, 0x390(%rsp)
jl 0x32640
movl 0x390(%rsp), %eax
movl %eax, 0x150(%rsp)
leaq 0x468(%rsp), %rdi
callq 0x27940
movl %eax, 0x154(%rsp)
jmp 0x3262e
movl 0x150(%rsp), %eax
movl 0x154(%rsp), %ecx
cmpl %ecx, %eax
jl 0x3266b
jmp 0x32a40
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x540(%rsp)
movl %eax, 0x53c(%rsp)
leaq 0x3f0(%rsp), %rdi
callq 0x35a20
jmp 0x331ca
movl 0x3e0(%rsp), %eax
subl $0x1, %eax
movl %eax, 0x38c(%rsp)
movl 0x38c(%rsp), %eax
movl 0x3e0(%rsp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jg 0x32a3e
cmpl $0x0, 0x38c(%rsp)
jl 0x326d5
movl 0x38c(%rsp), %eax
movl %eax, 0x148(%rsp)
leaq 0x468(%rsp), %rdi
callq 0x27960
movl %eax, 0x14c(%rsp)
jmp 0x326c3
movl 0x148(%rsp), %eax
movl 0x14c(%rsp), %ecx
cmpl %ecx, %eax
jl 0x326da
jmp 0x32a28
movl 0x390(%rsp), %esi
movl 0x38c(%rsp), %edx
leaq 0x468(%rsp), %rdi
callq 0x1a060
movsd %xmm0, 0x140(%rsp)
jmp 0x32700
movsd 0x140(%rsp), %xmm0
movsd %xmm0, 0x380(%rsp)
movsd 0x380(%rsp), %xmm0
cvtsi2sdl 0x404(%rsp), %xmm1
leaq 0x17f795(%rip), %rax    # 0x1b1ec0
movsd (%rax), %xmm2
callq 0x359f0
movl %eax, 0x13c(%rsp)
jmp 0x3273d
movl 0x13c(%rsp), %eax
cmpl $0x0, %eax
je 0x3274e
jmp 0x32a28
movl 0x390(%rsp), %esi
movl 0x38c(%rsp), %edx
cvtsi2sdl 0x404(%rsp), %xmm0
leaq 0x468(%rsp), %rdi
callq 0x1a1b0
jmp 0x32774
movq 0x590(%rsp), %rdi
movl 0x390(%rsp), %esi
movl 0x38c(%rsp), %edx
callq 0x1a060
movsd %xmm0, 0x130(%rsp)
jmp 0x3279a
movsd 0x130(%rsp), %xmm0
movsd %xmm0, 0x378(%rsp)
movsd 0x378(%rsp), %xmm0
leaq 0x17f704(%rip), %rax    # 0x1b1ec0
movsd (%rax), %xmm1
callq 0x1a3d0
movl %eax, 0x12c(%rsp)
jmp 0x327ce
movl 0x12c(%rsp), %eax
cmpl $0x0, %eax
je 0x327df
jmp 0x32a28
cvtsi2sdl 0x390(%rsp), %xmm0
cvtsi2sdl 0x38c(%rsp), %xmm1
leaq 0x368(%rsp), %rdi
callq 0xcfd80
jmp 0x32800
leaq 0x438(%rsp), %rdi
leaq 0x368(%rsp), %rsi
callq 0xac460
movsd %xmm0, 0x120(%rsp)
jmp 0x32820
movsd 0x120(%rsp), %xmm0
cvtsi2sdl 0x558(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x3283d
jmp 0x32a28
movq 0x588(%rsp), %rdi
movl 0x390(%rsp), %esi
movl 0x38c(%rsp), %edx
callq 0x1a060
movsd %xmm0, 0x118(%rsp)
jmp 0x32863
movsd 0x118(%rsp), %xmm0
movsd %xmm0, 0x360(%rsp)
movq 0x580(%rsp), %rdi
movl 0x390(%rsp), %esi
movl 0x38c(%rsp), %edx
callq 0x1a060
movsd %xmm0, 0x110(%rsp)
jmp 0x3289b
movsd 0x110(%rsp), %xmm0
movsd %xmm0, 0x358(%rsp)
movq 0x578(%rsp), %rdi
movl 0x390(%rsp), %esi
movl 0x38c(%rsp), %edx
callq 0x1a060
movsd %xmm0, 0x108(%rsp)
jmp 0x328d3
movsd 0x108(%rsp), %xmm0
movsd %xmm0, 0x350(%rsp)
movsd 0x360(%rsp), %xmm0
movsd 0x358(%rsp), %xmm1
movsd 0x350(%rsp), %xmm2
leaq 0x338(%rsp), %rdi
callq 0x8a9a0
jmp 0x3290f
movsd 0x550(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x3295c
leaq 0x408(%rsp), %rdi
leaq 0x338(%rsp), %rsi
callq 0x3b180
movsd %xmm0, 0x100(%rsp)
jmp 0x32941
movsd 0x100(%rsp), %xmm0
ucomisd 0x550(%rsp), %xmm0
jbe 0x3295a
jmp 0x32a28
jmp 0x3295c
movq 0x570(%rsp), %rdi
movl 0x390(%rsp), %esi
movl 0x38c(%rsp), %edx
callq 0x1a060
movsd %xmm0, 0xf8(%rsp)
jmp 0x32982
movsd 0xf8(%rsp), %xmm0
movsd 0x133695(%rip), %xmm1   # 0x166028
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x334(%rsp)
cmpl $0x4, 0x394(%rsp)
jne 0x329b8
cmpl $0x2, 0x334(%rsp)
jne 0x329b8
jmp 0x32a28
cmpl $0x2, 0x394(%rsp)
jne 0x329ce
cmpl $0x4, 0x334(%rsp)
jne 0x329ce
jmp 0x32a28
movl 0x390(%rsp), %esi
movl 0x38c(%rsp), %edx
leaq 0x468(%rsp), %rdi
leaq 0x330(%rsp), %rcx
callq 0x35920
jmp 0x329f3
movq 0x460(%rsp), %rax
movslq 0x330(%rsp), %rcx
leaq (%rax,%rcx,8), %rax
movq %rax, 0x328(%rsp)
movq 0x328(%rsp), %rsi
leaq 0x3f0(%rsp), %rdi
callq 0x35950
jmp 0x32a26
jmp 0x32a28
movl 0x38c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x38c(%rsp)
jmp 0x3267c
jmp 0x32a40
movl 0x390(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x390(%rsp)
jmp 0x325e7
jmp 0x323d2
leaq 0x3f0(%rsp), %rdi
callq 0x35a20
jmp 0x32d46
movl 0x45c(%rsp), %eax
subl 0x5f0(%rsp), %eax
movl %eax, 0x324(%rsp)
movl 0x324(%rsp), %eax
movl 0x45c(%rsp), %ecx
addl 0x5f0(%rsp), %ecx
cmpl %ecx, %eax
jg 0x32d44
cmpl $0x0, 0x324(%rsp)
jl 0x32adf
movl 0x324(%rsp), %eax
movl %eax, 0xf0(%rsp)
movq 0x590(%rsp), %rdi
callq 0x27940
movl %eax, 0xf4(%rsp)
jmp 0x32acd
movl 0xf0(%rsp), %eax
movl 0xf4(%rsp), %ecx
cmpl %ecx, %eax
jl 0x32ae4
jmp 0x32d2e
movl 0x458(%rsp), %eax
subl 0x5f0(%rsp), %eax
movl %eax, 0x320(%rsp)
movl 0x320(%rsp), %eax
movl 0x458(%rsp), %ecx
addl 0x5f0(%rsp), %ecx
cmpl %ecx, %eax
jg 0x32d2c
cmpl $0x0, 0x320(%rsp)
jl 0x32b56
movl 0x320(%rsp), %eax
movl %eax, 0xe8(%rsp)
movq 0x590(%rsp), %rdi
callq 0x27960
movl %eax, 0xec(%rsp)
jmp 0x32b44
movl 0xe8(%rsp), %eax
movl 0xec(%rsp), %ecx
cmpl %ecx, %eax
jl 0x32b5b
jmp 0x32d16
movq 0x590(%rsp), %rdi
movl 0x324(%rsp), %esi
movl 0x320(%rsp), %edx
callq 0x1a060
movsd %xmm0, 0xe0(%rsp)
jmp 0x32b81
movsd 0xe0(%rsp), %xmm0
movsd %xmm0, 0x318(%rsp)
movsd 0x318(%rsp), %xmm0
leaq 0x17f31d(%rip), %rax    # 0x1b1ec0
movsd (%rax), %xmm1
callq 0x1a3d0
movl %eax, 0xdc(%rsp)
jmp 0x32bb5
movl 0xdc(%rsp), %eax
cmpl $0x0, %eax
je 0x32bc6
jmp 0x32d16
movq 0x588(%rsp), %rdi
movl 0x324(%rsp), %esi
movl 0x320(%rsp), %edx
callq 0x1a060
movsd %xmm0, 0xd0(%rsp)
jmp 0x32bec
movsd 0xd0(%rsp), %xmm0
movsd %xmm0, 0x310(%rsp)
movq 0x580(%rsp), %rdi
movl 0x324(%rsp), %esi
movl 0x320(%rsp), %edx
callq 0x1a060
movsd %xmm0, 0xc8(%rsp)
jmp 0x32c24
movsd 0xc8(%rsp), %xmm0
movsd %xmm0, 0x308(%rsp)
movq 0x578(%rsp), %rdi
movl 0x324(%rsp), %esi
movl 0x320(%rsp), %edx
callq 0x1a060
movsd %xmm0, 0xc0(%rsp)
jmp 0x32c5c
movsd 0xc0(%rsp), %xmm0
movsd %xmm0, 0x300(%rsp)
movsd 0x310(%rsp), %xmm0
movsd 0x308(%rsp), %xmm1
movsd 0x300(%rsp), %xmm2
leaq 0x2e8(%rsp), %rdi
callq 0x8a9a0
jmp 0x32c98
movsd 0x550(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x32ce2
leaq 0x408(%rsp), %rdi
leaq 0x2e8(%rsp), %rsi
callq 0x3b180
movsd %xmm0, 0xb8(%rsp)
jmp 0x32cca
movsd 0xb8(%rsp), %xmm0
ucomisd 0x550(%rsp), %xmm0
jbe 0x32ce0
jmp 0x32d16
jmp 0x32ce2
movq 0x548(%rsp), %rax
movslq 0x454(%rsp), %rcx
movl %ecx, %edx
incl %edx
movl %edx, 0x454(%rsp)
leaq (%rcx,%rcx,2), %rcx
leaq (%rax,%rcx,8), %rdi
leaq 0x2e8(%rsp), %rsi
callq 0x8b030
jmp 0x32d14
jmp 0x32d16
movl 0x320(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x320(%rsp)
jmp 0x32af9
jmp 0x32d2e
movl 0x324(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x324(%rsp)
jmp 0x32a82
jmp 0x32d46
cmpl $0x3, 0x454(%rsp)
jge 0x32d55
jmp 0x33165
movl 0x454(%rsp), %esi
movq 0x548(%rsp), %rdx
xorl %eax, %eax
movl %eax, %ecx
leaq 0x2b8(%rsp), %rdi
callq 0x43970
jmp 0x32d77
movl 0x454(%rsp), %edx
movq 0x548(%rsp), %rcx
xorl %eax, %eax
movl %eax, %r8d
leaq 0x270(%rsp), %rdi
leaq 0x2b8(%rsp), %rsi
leaq 0x2d0(%rsp), %r9
callq 0x43a30
jmp 0x32daa
leaq 0x270(%rsp), %rdi
movl $0x2, %esi
callq 0x304e0
movq %rax, 0xb0(%rsp)
jmp 0x32dc6
movq 0xb0(%rsp), %rsi
leaq 0x258(%rsp), %rdi
callq 0x8bb40
jmp 0x32ddd
leaq 0x270(%rsp), %rdi
xorl %esi, %esi
callq 0x304e0
movq %rax, 0xa8(%rsp)
jmp 0x32df6
movq 0xa8(%rsp), %rsi
leaq 0x240(%rsp), %rdi
callq 0x8bb40
jmp 0x32e0d
leaq 0x220(%rsp), %rdi
leaq 0x408(%rsp), %rsi
leaq 0x258(%rsp), %rdx
callq 0x88100
jmp 0x32e2c
movq 0x5d8(%rsp), %rsi
leaq 0x220(%rsp), %rdi
callq 0x3b500
movsd %xmm0, 0xa0(%rsp)
jmp 0x32e4c
movsd 0xa0(%rsp), %xmm1
xorps %xmm0, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x32e6f
leaq 0x258(%rsp), %rdi
callq 0x8c2e0
jmp 0x32e6d
jmp 0x32e6f
movq 0x568(%rsp), %rax
movq %rax, 0x88(%rsp)
movl 0x45c(%rsp), %eax
movl %eax, 0x90(%rsp)
movl 0x458(%rsp), %eax
movl %eax, 0x94(%rsp)
leaq 0x258(%rsp), %rdi
callq 0x22660
movsd %xmm0, 0x98(%rsp)
jmp 0x32eb3
movsd 0x98(%rsp), %xmm0
movl 0x94(%rsp), %edx
movl 0x90(%rsp), %esi
movq 0x88(%rsp), %rdi
callq 0x1a1b0
jmp 0x32ed9
movq 0x5a0(%rsp), %rax
movq %rax, 0x70(%rsp)
movl 0x45c(%rsp), %eax
movl %eax, 0x78(%rsp)
movl 0x458(%rsp), %eax
movl %eax, 0x7c(%rsp)
leaq 0x258(%rsp), %rdi
callq 0x22670
movsd %xmm0, 0x80(%rsp)
jmp 0x32f14
movsd 0x80(%rsp), %xmm0
movl 0x7c(%rsp), %edx
movl 0x78(%rsp), %esi
movq 0x70(%rsp), %rdi
callq 0x1a1b0
jmp 0x32f31
movq 0x5a8(%rsp), %rax
movq %rax, 0x58(%rsp)
movl 0x45c(%rsp), %eax
movl %eax, 0x60(%rsp)
movl 0x458(%rsp), %eax
movl %eax, 0x64(%rsp)
leaq 0x258(%rsp), %rdi
callq 0x22680
movsd %xmm0, 0x68(%rsp)
jmp 0x32f69
movsd 0x68(%rsp), %xmm0
movl 0x64(%rsp), %edx
movl 0x60(%rsp), %esi
movq 0x58(%rsp), %rdi
callq 0x1a1b0
jmp 0x32f83
movq 0x5b0(%rsp), %rax
movq %rax, 0x40(%rsp)
movl 0x45c(%rsp), %eax
movl %eax, 0x48(%rsp)
movl 0x458(%rsp), %eax
movl %eax, 0x4c(%rsp)
leaq 0x240(%rsp), %rdi
callq 0x22660
movsd %xmm0, 0x50(%rsp)
jmp 0x32fbb
movsd 0x50(%rsp), %xmm0
movl 0x4c(%rsp), %edx
movl 0x48(%rsp), %esi
movq 0x40(%rsp), %rdi
callq 0x1a1b0
jmp 0x32fd5
movq 0x5b8(%rsp), %rax
movq %rax, 0x28(%rsp)
movl 0x45c(%rsp), %eax
movl %eax, 0x30(%rsp)
movl 0x458(%rsp), %eax
movl %eax, 0x34(%rsp)
leaq 0x240(%rsp), %rdi
callq 0x22670
movsd %xmm0, 0x38(%rsp)
jmp 0x3300d
movsd 0x38(%rsp), %xmm0
movl 0x34(%rsp), %edx
movl 0x30(%rsp), %esi
movq 0x28(%rsp), %rdi
callq 0x1a1b0
jmp 0x33027
movq 0x5c0(%rsp), %rax
movq %rax, 0x10(%rsp)
movl 0x45c(%rsp), %eax
movl %eax, 0x18(%rsp)
movl 0x458(%rsp), %eax
movl %eax, 0x1c(%rsp)
leaq 0x240(%rsp), %rdi
callq 0x22680
movsd %xmm0, 0x20(%rsp)
jmp 0x3305f
movsd 0x20(%rsp), %xmm0
movl 0x1c(%rsp), %edx
movl 0x18(%rsp), %esi
movq 0x10(%rsp), %rdi
callq 0x1a1b0
jmp 0x33079
movsd 0x2d0(%rsp), %xmm0
callq 0x12540
movaps %xmm0, %xmm1
movsd 0x134126(%rip), %xmm0   # 0x1671b8
mulsd %xmm1, %xmm0
movsd %xmm0, 0x218(%rsp)
movsd 0x2d8(%rsp), %xmm0
callq 0x12540
movaps %xmm0, %xmm1
movsd 0x134100(%rip), %xmm0   # 0x1671b8
mulsd %xmm1, %xmm0
movsd %xmm0, 0x210(%rsp)
cmpl $0x1, 0x5f0(%rsp)
jle 0x330ee
cvtsi2sdl 0x5f0(%rsp), %xmm1
movsd 0x218(%rsp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x218(%rsp)
cmpl $0x1, 0x5f0(%rsp)
jle 0x33117
cvtsi2sdl 0x5f0(%rsp), %xmm1
movsd 0x210(%rsp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x210(%rsp)
movq 0x5c8(%rsp), %rdi
movl 0x45c(%rsp), %esi
movl 0x458(%rsp), %edx
movsd 0x218(%rsp), %xmm0
callq 0x1a1b0
jmp 0x3313d
movq 0x5d0(%rsp), %rdi
movl 0x45c(%rsp), %esi
movl 0x458(%rsp), %edx
movsd 0x210(%rsp), %xmm0
callq 0x1a1b0
jmp 0x33163
jmp 0x33165
movl 0x458(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x458(%rsp)
jmp 0x32157
jmp 0x3317d
movl 0x45c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x45c(%rsp)
jmp 0x32112
movq 0x548(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x331b0
movq 0x8(%rsp), %rdi
callq 0x12920
leaq 0x468(%rsp), %rdi
callq 0xb3390
movl $0x1, %eax
addq $0x598, %rsp            # imm = 0x598
retq
leaq 0x468(%rsp), %rdi
callq 0xb3390
movq 0x540(%rsp), %rdi
callq 0x12c20
nopw %cs:(%rax,%rax)