// File: tomfunkhouser[P]gaps/apps/mshsegment/mshsegment.cpp
// Function: WriteMesh(gaps::R3Mesh*, char*)
static int
WriteMesh(R3Mesh *mesh, char *filename)
{
  // Start statistics
  RNTime start_time;
  start_time.Read();

  // Swap face segments and materials
  if (set_face_materials) {
    for (int i = 0; i < mesh->NFaces(); i++) {
      R3MeshFace *face = mesh->Face(i);
      int material = mesh->FaceMaterial(face);
      int segment = mesh->FaceSegment(face);
      mesh->SetFaceMaterial(face, segment);
      mesh->SetFaceSegment(face, material);
    }
  }

  // Write mesh
  if (!mesh->WriteFile(filename)) {
    RNFail("Unable to write mesh to %s\n", filename);
    return 0;
  }

  // Swap back face segments and materials
  if (set_face_materials) {
    for (int i = 0; i < mesh->NFaces(); i++) {
      R3MeshFace *face = mesh->Face(i);
      int material = mesh->FaceMaterial(face);
      int segment = mesh->FaceSegment(face);
      mesh->SetFaceMaterial(face, segment);
      mesh->SetFaceSegment(face, material);
    }
  }

  // Print statistics
  if (print_verbose) {
    printf("Wrote mesh to %s ...\n", filename);
    printf("  Time = %.2f seconds\n", start_time.Elapsed());
    printf("  # Faces = %d\n", mesh->NFaces());
    printf("  # Edges = %d\n", mesh->NEdges());
    printf("  # Vertices = %d\n", mesh->NVertices());
    fflush(stdout);
  }

  // Return success
  return 1;
}

// Assembly:
subq $0x68, %rsp
movq %rdi, 0x58(%rsp)
movq %rsi, 0x50(%rsp)
leaq 0x40(%rsp), %rdi
callq 0xa5c50
leaq 0x40(%rsp), %rdi
callq 0x12040
cmpl $0x0, 0xc0b33(%rip)     # 0xd1ccc
je 0x11234
movl $0x0, 0x3c(%rsp)
movl 0x3c(%rsp), %eax
movl %eax, 0xc(%rsp)
movq 0x58(%rsp), %rdi
callq 0x12060
movl %eax, %ecx
movl 0xc(%rsp), %eax
cmpl %ecx, %eax
jge 0x11232
movq 0x58(%rsp), %rdi
movl 0x3c(%rsp), %esi
callq 0x12080
movq %rax, 0x30(%rsp)
movq 0x58(%rsp), %rdi
movq 0x30(%rsp), %rsi
callq 0x13370
movl %eax, 0x2c(%rsp)
movq 0x58(%rsp), %rdi
movq 0x30(%rsp), %rsi
callq 0x120b0
movl %eax, 0x28(%rsp)
movq 0x58(%rsp), %rdi
movq 0x30(%rsp), %rsi
movl 0x28(%rsp), %edx
callq 0x120d0
movq 0x58(%rsp), %rdi
movq 0x30(%rsp), %rsi
movl 0x2c(%rsp), %edx
callq 0x120f0
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0x111a7
jmp 0x11234
movq 0x58(%rsp), %rdi
movq 0x50(%rsp), %rsi
movq (%rdi), %rax
callq *0x1d0(%rax)
cmpl $0x0, %eax
jne 0x1126c
movq 0x50(%rsp), %rsi
leaq 0x9a0c8(%rip), %rdi     # 0xab320
movb $0x0, %al
callq 0xa9e70
movl $0x0, 0x64(%rsp)
jmp 0x113ab
cmpl $0x0, 0xc0a59(%rip)     # 0xd1ccc
je 0x1130e
movl $0x0, 0x24(%rsp)
movl 0x24(%rsp), %eax
movl %eax, 0x8(%rsp)
movq 0x58(%rsp), %rdi
callq 0x12060
movl %eax, %ecx
movl 0x8(%rsp), %eax
cmpl %ecx, %eax
jge 0x1130c
movq 0x58(%rsp), %rdi
movl 0x24(%rsp), %esi
callq 0x12080
movq %rax, 0x18(%rsp)
movq 0x58(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x13370
movl %eax, 0x14(%rsp)
movq 0x58(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x120b0
movl %eax, 0x10(%rsp)
movq 0x58(%rsp), %rdi
movq 0x18(%rsp), %rsi
movl 0x10(%rsp), %edx
callq 0x120d0
movq 0x58(%rsp), %rdi
movq 0x18(%rsp), %rsi
movl 0x14(%rsp), %edx
callq 0x120f0
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
jmp 0x11281
jmp 0x1130e
cmpl $0x0, 0xc09b3(%rip)     # 0xd1cc8
je 0x113a3
movq 0x50(%rsp), %rsi
leaq 0x9a015(%rip), %rdi     # 0xab33c
movb $0x0, %al
callq 0xd070
leaq 0x40(%rsp), %rdi
callq 0xa5d10
leaq 0x99ee0(%rip), %rdi     # 0xab21f
movb $0x1, %al
callq 0xd070
movq 0x58(%rsp), %rdi
callq 0x12060
movl %eax, %esi
leaq 0x99edd(%rip), %rdi     # 0xab236
movb $0x0, %al
callq 0xd070
movq 0x58(%rsp), %rdi
callq 0x12110
movl %eax, %esi
leaq 0x99ed3(%rip), %rdi     # 0xab246
movb $0x0, %al
callq 0xd070
movq 0x58(%rsp), %rdi
callq 0x12130
movl %eax, %esi
leaq 0x99ec9(%rip), %rdi     # 0xab256
movb $0x0, %al
callq 0xd070
movq 0xbfc05(%rip), %rax     # 0xd0fa0
movq (%rax), %rdi
callq 0xd4b0
movl $0x1, 0x64(%rsp)
movl 0x64(%rsp), %eax
addq $0x68, %rsp
retq
nopw %cs:(%rax,%rax)