// File: tomfunkhouser[P]gaps/apps/mshsegment/mshsegment.cpp
// Function: WriteJson(gaps::R3Mesh*, char const*)
static int
WriteJson(R3Mesh *mesh, const char *filename)
{
  // Check filename
  if (!filename) return 1;

  // Start statistics
  RNTime start_time;
  start_time.Read();

  // Open file
  FILE* fp = fopen(filename, "w");
  if (!fp) {
    RNFail("Unable to open json file %s\n", filename);
    return 0;
  }

  // Write segments
  fprintf(fp, "{\n");
  fprintf(fp, "  \"sceneId\": \"xxx\",\n");
  fprintf(fp, "  \"segIndices\": [\n");
  int junk_segment_id = 9999999;
  for (int i = 0; i < mesh->NVertices(); i++) {
    R3MeshVertex *vertex = mesh->Vertex(i);

    // Find best segment for vertex
    int best_segment = -1;
    for (int j = 0; j < mesh->VertexValence(vertex); j++) {
      R3MeshEdge *edge = mesh->EdgeOnVertex(vertex, j);
      R3MeshFace *face = mesh->FaceOnEdge(edge, vertex, RN_CW);
      if (!face) continue;
      int segment = mesh->FaceSegment(face);
      if (segment < 0) continue;
      if ((best_segment > 0) && (segment > best_segment)) continue;
      best_segment = segment;
    }
    if (i > 0) fprintf(fp, ",\n");
    if (best_segment >= 0) fprintf(fp, "%d", best_segment); 
    else fprintf(fp, "%d", junk_segment_id);
  }
  fprintf(fp, "\n]\n");
  fprintf(fp, "}\n");

  // Close file
  fclose(fp);

  // Print statistics
  if (print_verbose) {
    printf("Wrote segments to %s ...\n", filename);
    printf("  Time = %.2f seconds\n", start_time.Elapsed());
    fflush(stdout);
  }

  // Return success
  return 1;
}

// Assembly:
subq $0x68, %rsp
movq %rdi, 0x58(%rsp)
movq %rsi, 0x50(%rsp)
cmpq $0x0, 0x50(%rsp)
jne 0x113e3
movl $0x1, 0x64(%rsp)
jmp 0x1164b
leaq 0x40(%rsp), %rdi
callq 0xa5c50
leaq 0x40(%rsp), %rdi
callq 0x12040
movq 0x50(%rsp), %rdi
leaq 0x9bd1d(%rip), %rsi     # 0xad120
callq 0xd4c0
movq %rax, 0x38(%rsp)
cmpq $0x0, 0x38(%rsp)
jne 0x11435
movq 0x50(%rsp), %rsi
leaq 0x99f31(%rip), %rdi     # 0xab352
movb $0x0, %al
callq 0xa9e70
movl $0x0, 0x64(%rsp)
jmp 0x1164b
movq 0x38(%rsp), %rdi
leaq 0x99f2e(%rip), %rsi     # 0xab36f
movb $0x0, %al
callq 0xd590
movq 0x38(%rsp), %rdi
leaq 0x99f1e(%rip), %rsi     # 0xab372
movb $0x0, %al
callq 0xd590
movq 0x38(%rsp), %rdi
leaq 0x99f20(%rip), %rsi     # 0xab387
movb $0x0, %al
callq 0xd590
movl $0x98967f, 0x34(%rsp)   # imm = 0x98967F
movl $0x0, 0x30(%rsp)
movl 0x30(%rsp), %eax
movl %eax, 0x8(%rsp)
movq 0x58(%rsp), %rdi
callq 0x12130
movl %eax, %ecx
movl 0x8(%rsp), %eax
cmpl %ecx, %eax
jge 0x115d0
movq 0x58(%rsp), %rdi
movl 0x30(%rsp), %esi
callq 0x13390
movq %rax, 0x28(%rsp)
movl $0xffffffff, 0x24(%rsp) # imm = 0xFFFFFFFF
movl $0x0, 0x20(%rsp)
movl 0x20(%rsp), %eax
movl %eax, 0x4(%rsp)
movq 0x58(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0x133c0
movl %eax, %ecx
movl 0x4(%rsp), %eax
cmpl %ecx, %eax
jge 0x1156d
movq 0x58(%rsp), %rdi
movq 0x28(%rsp), %rsi
movl 0x20(%rsp), %edx
callq 0x133f0
movq %rax, 0x18(%rsp)
movq 0x58(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq 0x28(%rsp), %rdx
movl $0x1, %ecx
callq 0x13420
movq %rax, 0x10(%rsp)
cmpq $0x0, 0x10(%rsp)
jne 0x11526
jmp 0x1155d
movq 0x58(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x120b0
movl %eax, 0xc(%rsp)
cmpl $0x0, 0xc(%rsp)
jge 0x11542
jmp 0x1155d
cmpl $0x0, 0x24(%rsp)
jle 0x11555
movl 0xc(%rsp), %eax
cmpl 0x24(%rsp), %eax
jle 0x11555
jmp 0x1155d
movl 0xc(%rsp), %eax
movl %eax, 0x24(%rsp)
movl 0x20(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x20(%rsp)
jmp 0x114c1
cmpl $0x0, 0x30(%rsp)
jle 0x11587
movq 0x38(%rsp), %rdi
leaq 0x99e04(%rip), %rsi     # 0xab384
movb $0x0, %al
callq 0xd590
cmpl $0x0, 0x24(%rsp)
jl 0x115a7
movq 0x38(%rsp), %rdi
movl 0x24(%rsp), %edx
leaq 0x9abd9(%rip), %rsi     # 0xac177
movb $0x0, %al
callq 0xd590
jmp 0x115be
movq 0x38(%rsp), %rdi
movl 0x34(%rsp), %edx
leaq 0x9abc0(%rip), %rsi     # 0xac177
movb $0x0, %al
callq 0xd590
jmp 0x115c0
movl 0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30(%rsp)
jmp 0x1147e
movq 0x38(%rsp), %rdi
leaq 0x99dbe(%rip), %rsi     # 0xab39a
movb $0x0, %al
callq 0xd590
movq 0x38(%rsp), %rdi
leaq 0x99daf(%rip), %rsi     # 0xab39e
movb $0x0, %al
callq 0xd590
movq 0x38(%rsp), %rdi
callq 0xd3c0
cmpl $0x0, 0xc06c1(%rip)     # 0xd1cc8
je 0x11643
movq 0x50(%rsp), %rsi
leaq 0x99d8c(%rip), %rdi     # 0xab3a1
movb $0x0, %al
callq 0xd070
leaq 0x40(%rsp), %rdi
callq 0xa5d10
leaq 0x99bf2(%rip), %rdi     # 0xab21f
movb $0x1, %al
callq 0xd070
movq 0xbf965(%rip), %rax     # 0xd0fa0
movq (%rax), %rdi
callq 0xd4b0
movl $0x1, 0x64(%rsp)
movl 0x64(%rsp), %eax
addq $0x68, %rsp
retq
nopw %cs:(%rax,%rax)