// File: tomfunkhouser[P]gaps/apps/mshparam/mshparam.cpp
// Function: NormalizeTextureCoordinates(gaps::R3Mesh*)
static int
NormalizeTextureCoordinates(R3Mesh *mesh)
{
  // Check flags
  if (!fit_in_zero_to_one) return 1;
  
  // Get bounding box of texcoords
  R2Box bbox = R2null_box;
  for (int i = 0; i < mesh->NVertices(); i++) {
    R3MeshVertex *vertex = mesh->Vertex(i);
    R2Point texcoords = mesh->VertexTextureCoords(vertex);
    bbox.Union(texcoords);
  }

  // Get scale factor
  RNLength max_length = bbox.LongestAxisLength();
  if (RNIsZero(max_length)) return 0;
  RNScalar scale = 1.0 / max_length;
  
  // Scale texture coords to fit into 0-1
  for (int i = 0; i < mesh->NVertices(); i++) {
    R3MeshVertex *vertex = mesh->Vertex(i);
    R2Point texcoords = mesh->VertexTextureCoords(vertex);
    texcoords[0] = scale * (texcoords[0] - bbox.XMin());
    texcoords[1] = scale * (texcoords[1] - bbox.YMin());
    mesh->SetVertexTextureCoords(vertex, texcoords);
  }

  // Return success
  return 1;
}

// Assembly:
subq $0x128, %rsp            # imm = 0x128
movq %rdi, 0x118(%rsp)
cmpl $0x0, 0x14b21e(%rip)    # 0x15dd24
jne 0x12b18
movl $0x1, 0x124(%rsp)
jmp 0x12e52
leaq 0xf0(%rsp), %rdi
leaq 0x14d251(%rip), %rsi    # 0x15fd78
callq 0xac540
movl $0x0, 0xec(%rsp)
movl 0xec(%rsp), %eax
movl %eax, 0x84(%rsp)
movq 0x118(%rsp), %rdi
callq 0x162a0
movl %eax, %ecx
movl 0x84(%rsp), %eax
cmpl %ecx, %eax
jge 0x12c10
movq 0x118(%rsp), %rdi
movl 0xec(%rsp), %esi
callq 0x16300
movq %rax, 0x78(%rsp)
jmp 0x12b7e
movq 0x78(%rsp), %rax
movq %rax, 0xe0(%rsp)
movq 0x118(%rsp), %rdi
movq 0xe0(%rsp), %rsi
callq 0x19b40
movq %rax, 0x70(%rsp)
jmp 0x12ba7
movq 0x70(%rsp), %rsi
leaq 0xc0(%rsp), %rdi
callq 0xb2290
jmp 0x12bbb
leaq 0xf0(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0xad5d0
jmp 0x12bd2
jmp 0x12bd4
movl 0xec(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xec(%rsp)
jmp 0x12b37
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd8(%rsp)
movl %eax, 0xd4(%rsp)
leaq 0xf0(%rsp), %rdi
callq 0x19b30
jmp 0x12e61
leaq 0xf0(%rsp), %rdi
callq 0xad1d0
movsd %xmm0, 0x68(%rsp)
jmp 0x12c25
movsd 0x68(%rsp), %xmm0
movsd %xmm0, 0xb8(%rsp)
movsd 0xb8(%rsp), %xmm0
leaq 0x14b03c(%rip), %rax    # 0x15dc80
movsd (%rax), %xmm1
callq 0x19b60
movl %eax, 0x64(%rsp)
jmp 0x12c53
movl 0x64(%rsp), %eax
cmpl $0x0, %eax
je 0x12c77
movl $0x0, 0x124(%rsp)
movl $0x1, 0xb4(%rsp)
jmp 0x12e45
movsd 0x10f389(%rip), %xmm0   # 0x122008
divsd 0xb8(%rsp), %xmm0
movsd %xmm0, 0xa8(%rsp)
movl $0x0, 0xa4(%rsp)
movl 0xa4(%rsp), %eax
movl %eax, 0x60(%rsp)
movq 0x118(%rsp), %rdi
callq 0x162a0
movl %eax, %ecx
movl 0x60(%rsp), %eax
cmpl %ecx, %eax
jge 0x12e2f
movq 0x118(%rsp), %rdi
movl 0xa4(%rsp), %esi
callq 0x16300
movq %rax, 0x58(%rsp)
jmp 0x12cdd
movq 0x58(%rsp), %rax
movq %rax, 0x98(%rsp)
movq 0x118(%rsp), %rdi
movq 0x98(%rsp), %rsi
callq 0x19b40
movq %rax, 0x50(%rsp)
jmp 0x12d06
movq 0x50(%rsp), %rsi
leaq 0x88(%rsp), %rdi
callq 0xb2290
jmp 0x12d1a
movsd 0xa8(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
leaq 0x88(%rsp), %rdi
movq %rdi, 0x48(%rsp)
xorl %esi, %esi
movl %esi, 0xc(%rsp)
callq 0x19ac0
movsd (%rax), %xmm0
movsd %xmm0, (%rsp)
leaq 0xf0(%rsp), %rdi
movq %rdi, 0x20(%rsp)
callq 0x19a00
movsd (%rsp), %xmm1
movl 0xc(%rsp), %esi
movq 0x48(%rsp), %rdi
movaps %xmm0, %xmm2
movsd 0x10(%rsp), %xmm0
subsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0x18(%rsp)
callq 0x19ac0
movsd 0x18(%rsp), %xmm0
movq 0x48(%rsp), %rdi
movsd %xmm0, (%rax)
movsd 0xa8(%rsp), %xmm0
movsd %xmm0, 0x38(%rsp)
movl $0x1, %esi
movl %esi, 0x34(%rsp)
callq 0x19ac0
movq 0x20(%rsp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, 0x28(%rsp)
callq 0x19a50
movsd 0x28(%rsp), %xmm1
movl 0x34(%rsp), %esi
movq 0x48(%rsp), %rdi
movaps %xmm0, %xmm2
movsd 0x38(%rsp), %xmm0
subsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0x40(%rsp)
callq 0x19ac0
movsd 0x40(%rsp), %xmm0
movq 0x48(%rsp), %rdx
movsd %xmm0, (%rax)
movq 0x118(%rsp), %rdi
movq 0x98(%rsp), %rsi
callq 0x19a70
jmp 0x12e17
jmp 0x12e19
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rsp)
jmp 0x12c9c
movl $0x1, 0x124(%rsp)
movl $0x1, 0xb4(%rsp)
leaq 0xf0(%rsp), %rdi
callq 0x19b30
movl 0x124(%rsp), %eax
addq $0x128, %rsp            # imm = 0x128
retq
movq 0xd8(%rsp), %rdi
callq 0xf830
nop