// File: tomfunkhouser[P]gaps/pkgs/R3Graphics/R3Scene.cpp
// Function: gaps::WriteSUNCGNode(gaps::R3Scene const*, gaps::R3SceneNode const*, _IO_FILE*)
static int
WriteSUNCGNode(const R3Scene *scene, const R3SceneNode *node, FILE *fp)
{
  // Check node
  if (!node->Name()) return 0;

  // NOTE: THE SCENE GRAPH MUST FOLLOW THE SUNCG HIERARCHY CONVENTIONS
  // ROOT -> LEVEL* -> OBJECT* | BOX* | GROUND* | (ROOM -> OBJECT*)*
  
  // Check node name
  static int counter = 0;
  if (!strncmp(node->Name(), "Level#", 6)) {
    // Write header
    counter = 0;
    const char *id = &(node->Name()[6]);
    const R3Box& b = node->WorldBBox();
    fprintf(fp, "  {\n");
    fprintf(fp, "    \"id\":\"%s\",\n", id);
    fprintf(fp, "    \"valid\":1,\n");
    fprintf(fp, "    \"bbox\":{\"min\":[%g,%g,%g],\"max\":[%g,%g,%g]},\n", b[0][0], b[0][1], b[0][2], b[1][0], b[1][1], b[1][2]);

    // Write children
    fprintf(fp, "    \"nodes\":[\n");
    for (int i = 0; i < node->NChildren(); i++) {
      R3SceneNode *child = node->Child(i);
      if (!WriteSUNCGNode(scene, child, fp)) return 0;
    }
    fprintf(fp, "    ]\n");
    
    // Write trailer
    fprintf(fp, "  }");
    if (node->ParentIndex() < node->Parent()->NChildren()-1) fprintf(fp, ",");
    fprintf(fp, "\n");
  }
  else if (!strncmp(node->Name(), "Ground#", 7) || !strncmp(node->Name(), "Room#", 5)) {
    // Get id and type
    char nodetype[4096];
    strncpy(nodetype, node->Name(), 4095);
    char *id = strchr(nodetype, '#');
    if (!id) return 0;
    else *(id++) = '\0';

    // Get modelId
    char modelId[4096], buffer[4096];
    strncpy(buffer, id, 4095);
    char *level = strtok(buffer, "_\n");
    if (!level) return 0;
    char *idx = strtok(NULL, "\n");
    if (!idx) return 0;
    sprintf(modelId, "fr_%srm_%s", level, idx);

    // Write json
    const R3Box& b = node->WorldBBox();
    if (counter++ > 0) fprintf(fp, ",\n");
    fprintf(fp, "      {\n");
    fprintf(fp, "        \"id\":\"%s\",\n", id);
    fprintf(fp, "        \"type\":\"%s\",\n", nodetype);
    fprintf(fp, "        \"modelId\":\"%s\",\n", modelId);
    fprintf(fp, "        \"valid\":1,\n");
    fprintf(fp, "        \"bbox\":{\"min\":[%g,%g,%g],\"max\":[%g,%g,%g]}\n",
      b[0][0], b[0][1], b[0][2], b[1][0], b[1][1], b[1][2]);

#if 0
    // Write node indices
    int first = 1;
    fprintf(fp, "        \"nodeIndices\":[");
    for (int i = 0; i < node->NChildren(); i++) {
      R3SceneNode *child = node->Child(i);
      if (!child->Name()) continue;
      if (strncmp(child->Name(), "Object#", 7)) continue;
      const char *s = strrchr(child->Name(), '_');
      if (!s) continue;
      if (!first) fprintf(fp, ","); 
      fprintf(fp, "%s", xxx <- needs to be index of the json entry in the level);
      first = 0;       
    }
    fprintf(fp, "]\n");
#endif
    
    fprintf(fp, "      }");

    // Write children
    for (int i = 0; i < node->NChildren(); i++) {
      R3SceneNode *child = node->Child(i);
      if (!WriteSUNCGNode(scene, child, fp)) return 0;
    }
  }
  else if (!strncmp(node->Name(), "Object#", 7)) {
    // Get id and type
    char nodetype[4096];
    strncpy(nodetype, node->Name(), 4095);
    char *id = strchr(nodetype, '#');
    if (!id) return 0;
    else *(id++) = '\0';

    // Get modelId
    char modelId[4096];
    if (node->NReferences() == 0) return 0; 
    R3SceneReference *reference = node->Reference(0);
    R3Scene *referenced_scene = reference->ReferencedScene();
    if (!referenced_scene->Name()) return 0;
    if (strncmp(referenced_scene->Root()->Name(), "Model#", 6)) return 0;
    strncpy(modelId, &(referenced_scene->Root()->Name()[6]), 4095);

    // Write json
    const R3Box& b = node->WorldBBox();
    R4Matrix matrix = node->Transformation().Matrix();
    RNBoolean is_mirrored = node->Transformation().IsMirrored();
    if (counter++ > 0) fprintf(fp, ",\n");
    fprintf(fp, "      {\n");
    fprintf(fp, "        \"id\":\"%s\",\n", id);
    fprintf(fp, "        \"type\":\"%s\",\n", nodetype);
    fprintf(fp, "        \"modelId\":\"%s\",\n", modelId);
    fprintf(fp, "        \"isMirrored\":%d,\n", is_mirrored);
    fprintf(fp, "        \"bbox\":{\"min\":[%g,%g,%g],\"max\":[%g,%g,%g]},\n",
      b[0][0], b[0][1], b[0][2], b[1][0], b[1][1], b[1][2]);
    if (reference->NMaterials() > 0) {
      fprintf(fp, "        \"materials\":[");
      for (int i = 0; i < reference->NMaterials(); i++) {
        R3Material *material = reference->Material(i);
        const char *material_name = (material) ? material->Name() : "none";
        const R3Brdf *brdf = (material) ? material->Brdf() : NULL;
        const R2Texture *texture = (material) ? material->Texture() : NULL;
        if (i > 0) fprintf(fp, ", ");
        fprintf(fp, "{ \"name\":\"%s\"", material_name);
        if (brdf) {
          const RNRgb& diffuse = brdf->Diffuse();
          int r = 255.0 * diffuse.R() + 0.5;
          int g = 255.0 * diffuse.G() + 0.5;
          int b = 255.0 * diffuse.B() + 0.5;
          r = (r < 255) ? ((r > 0) ? r : 0) : 255;
          g = (g < 255) ? ((g > 0) ? g : 0) : 255;
          b = (b < 255) ? ((b > 0) ? b : 0) : 255;
          fprintf(fp, ", \"diffuse\":\"#%02x%02x%02x\"", r, g, b);
        }
        if (texture && texture->Name()) {
          fprintf(fp, ", \"texture\":\"%s\"", texture->Name());
        }
        fprintf(fp, " }");
      }
      fprintf(fp, "],\n");
    }
    if (!matrix.IsIdentity()) {
      fprintf(fp, "        \"transform\":[");
      for (int j = 0; j < 4; j++) {
        for (int i = 0; i < 4; i++) {
          if ((i > 0) || (j > 0)) fprintf(fp, ", ");
          fprintf(fp, "%g", matrix[i][j]);
        }
      }
      fprintf(fp, "],\n");
    }
    fprintf(fp, "        \"valid\":1\n");
    fprintf(fp, "      }");
  }
  else if (!strncmp(node->Name(), "Box#", 4)) {
    RNFail("Warning: skipping %s\n", node->Name());
  }
  
  // Return success
  return 1;
}

// Assembly:
subq $0x5428, %rsp           # imm = 0x5428
movq %rdi, 0x5418(%rsp)
movq %rsi, 0x5410(%rsp)
movq %rdx, 0x5408(%rsp)
movq 0x5410(%rsp), %rdi
callq 0x2b800
cmpq $0x0, %rax
jne 0x3fef2
movl $0x0, 0x5424(%rsp)
jmp 0x4128d
movq 0x5410(%rsp), %rdi
callq 0x2b800
movq %rax, %rdi
leaq 0x14fb2f(%rip), %rsi    # 0x18fa38
movl $0x6, %edx
callq 0x22450
cmpl $0x0, %eax
jne 0x4034a
movl $0x0, 0x1bbe3a(%rip)    # 0x1fbd60
movq 0x5410(%rsp), %rdi
callq 0x2b800
addq $0x6, %rax
movq %rax, 0x5400(%rsp)
movq 0x5410(%rsp), %rsi
leaq 0x53c0(%rsp), %rdi
movq %rdi, 0x1e8(%rsp)
callq 0x56da0
movq 0x1e8(%rsp), %rax
movq %rax, 0x53f8(%rsp)
movq 0x5408(%rsp), %rdi
leaq 0x14faf7(%rip), %rsi    # 0x18fa72
xorl %eax, %eax
movl %eax, 0x1f4(%rsp)
movb %al, 0x1f3(%rsp)
callq 0x22f30
movb 0x1f3(%rsp), %al
movq 0x5408(%rsp), %rdi
movq 0x5400(%rsp), %rdx
leaq 0x14facd(%rip), %rsi    # 0x18fa7b
callq 0x22f30
movb 0x1f3(%rsp), %al
movq 0x5408(%rsp), %rdi
leaq 0x14faf5(%rip), %rsi    # 0x18fabe
callq 0x22f30
movl 0x1f4(%rsp), %esi
movq 0x5408(%rsp), %rax
movq %rax, 0x1f8(%rsp)
movq 0x53f8(%rsp), %rdi
callq 0x4fd70
movq %rax, 0x200(%rsp)
jmp 0x3fffc
movq 0x200(%rsp), %rdi
xorl %esi, %esi
callq 0x4fdb0
movsd %xmm0, 0x1e0(%rsp)
jmp 0x40016
movq 0x53f8(%rsp), %rdi
xorl %esi, %esi
callq 0x4fd70
movq %rax, 0x1d8(%rsp)
jmp 0x4002f
movq 0x1d8(%rsp), %rdi
movl $0x1, %esi
callq 0x4fdb0
movsd %xmm0, 0x1d0(%rsp)
jmp 0x4004c
movq 0x53f8(%rsp), %rdi
xorl %esi, %esi
callq 0x4fd70
movq %rax, 0x1c8(%rsp)
jmp 0x40065
movq 0x1c8(%rsp), %rdi
movl $0x2, %esi
callq 0x4fdb0
movsd %xmm0, 0x1c0(%rsp)
jmp 0x40082
movq 0x53f8(%rsp), %rdi
movl $0x1, %esi
callq 0x4fd70
movq %rax, 0x1b8(%rsp)
jmp 0x4009e
movq 0x1b8(%rsp), %rdi
xorl %esi, %esi
callq 0x4fdb0
movsd %xmm0, 0x1b0(%rsp)
jmp 0x400b8
movq 0x53f8(%rsp), %rdi
movl $0x1, %esi
callq 0x4fd70
movq %rax, 0x1a8(%rsp)
jmp 0x400d4
movq 0x1a8(%rsp), %rdi
movl $0x1, %esi
callq 0x4fdb0
movsd %xmm0, 0x1a0(%rsp)
jmp 0x400f1
movq 0x53f8(%rsp), %rdi
movl $0x1, %esi
callq 0x4fd70
movq %rax, 0x198(%rsp)
jmp 0x4010d
movq 0x198(%rsp), %rdi
movl $0x2, %esi
callq 0x4fdb0
movsd %xmm0, 0x190(%rsp)
jmp 0x4012a
movsd 0x190(%rsp), %xmm5
movsd 0x1a0(%rsp), %xmm4
movsd 0x1b0(%rsp), %xmm3
movsd 0x1c0(%rsp), %xmm2
movsd 0x1d0(%rsp), %xmm1
movsd 0x1e0(%rsp), %xmm0
movq 0x1f8(%rsp), %rdi
leaq 0x14f9c8(%rip), %rsi    # 0x18fb37
movb $0x6, %al
callq 0x22f30
movq 0x5408(%rsp), %rdi
leaq 0x14f8ba(%rip), %rsi    # 0x18fa3f
movb $0x0, %al
callq 0x22f30
movl $0x0, 0x53b0(%rsp)
movl 0x53b0(%rsp), %eax
movl %eax, 0x18c(%rsp)
movq 0x5410(%rsp), %rdi
callq 0x2baf0
movl %eax, %ecx
movl 0x18c(%rsp), %eax
cmpl %ecx, %eax
jge 0x4027c
movq 0x5410(%rsp), %rdi
movl 0x53b0(%rsp), %esi
callq 0x2bb10
movq %rax, 0x180(%rsp)
jmp 0x401e1
movq 0x180(%rsp), %rax
movq %rax, 0x53a8(%rsp)
movq 0x5418(%rsp), %rdi
movq 0x53a8(%rsp), %rsi
movq 0x5408(%rsp), %rdx
callq 0x3feb0
movl %eax, 0x17c(%rsp)
jmp 0x40217
movl 0x17c(%rsp), %eax
cmpl $0x0, %eax
jne 0x40264
movl $0x0, 0x5424(%rsp)
movl $0x1, 0x53a4(%rsp)
jmp 0x40326
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x53b8(%rsp)
movl %eax, 0x53b4(%rsp)
leaq 0x53c0(%rsp), %rdi
callq 0x2c1a0
jmp 0x4129c
jmp 0x40266
movl 0x53b0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x53b0(%rsp)
jmp 0x40197
movq 0x5408(%rsp), %rdi
leaq 0x14f7c3(%rip), %rsi    # 0x18fa4e
xorl %eax, %eax
movb %al, 0x177(%rsp)
callq 0x22f30
movb 0x177(%rsp), %al
movq 0x5408(%rsp), %rdi
leaq 0x14f857(%rip), %rsi    # 0x18fb06
callq 0x22f30
movq 0x5410(%rsp), %rdi
callq 0x4fdd0
movl %eax, 0x178(%rsp)
jmp 0x402ca
movq 0x5410(%rsp), %rdi
callq 0x49480
movq %rax, %rdi
callq 0x2baf0
movl %eax, %ecx
movl 0x178(%rsp), %eax
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x40305
movq 0x5408(%rsp), %rdi
leaq 0x150292(%rip), %rsi    # 0x190590
movb $0x0, %al
callq 0x22f30
movq 0x5408(%rsp), %rdi
leaq 0x15532e(%rip), %rsi    # 0x195642
movb $0x0, %al
callq 0x22f30
movl $0x0, 0x53a4(%rsp)
leaq 0x53c0(%rsp), %rdi
callq 0x2c1a0
movl 0x53a4(%rsp), %eax
testl %eax, %eax
je 0x40345
jmp 0x40340
jmp 0x4128d
jmp 0x41282
movq 0x5410(%rsp), %rdi
callq 0x2b800
movq %rax, %rdi
leaq 0x14f6f4(%rip), %rsi    # 0x18fa55
movl $0x7, %edx
callq 0x22450
cmpl $0x0, %eax
je 0x4039a
movq 0x5410(%rsp), %rdi
callq 0x2b800
movq %rax, %rdi
leaq 0x14f6d6(%rip), %rsi    # 0x18fa5d
movl $0x5, %edx
callq 0x22450
cmpl $0x0, %eax
jne 0x40898
leaq 0x43a0(%rsp), %rax
movq %rax, 0x168(%rsp)
movq 0x5410(%rsp), %rdi
callq 0x2b800
movq 0x168(%rsp), %rdi
movq %rax, %rsi
movl $0xfff, %edx            # imm = 0xFFF
callq 0x224b0
leaq 0x43a0(%rsp), %rdi
movl $0x23, %esi
callq 0x22350
movq %rax, 0x4398(%rsp)
cmpq $0x0, 0x4398(%rsp)
jne 0x40401
movl $0x0, 0x5424(%rsp)
jmp 0x4128d
movq 0x4398(%rsp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, 0x4398(%rsp)
movb $0x0, (%rax)
leaq 0x2390(%rsp), %rdi
movq 0x4398(%rsp), %rsi
movl $0xfff, %edx            # imm = 0xFFF
callq 0x224b0
leaq 0x2390(%rsp), %rdi
leaq 0x14e377(%rip), %rsi    # 0x18e7bb
callq 0x230a0
movq %rax, 0x2388(%rsp)
cmpq $0x0, 0x2388(%rsp)
jne 0x4046c
movl $0x0, 0x5424(%rsp)
jmp 0x4128d
xorl %eax, %eax
movl %eax, %edi
leaq 0x1551cb(%rip), %rsi    # 0x195642
callq 0x230a0
movq %rax, 0x2380(%rsp)
cmpq $0x0, 0x2380(%rsp)
jne 0x4049f
movl $0x0, 0x5424(%rsp)
jmp 0x4128d
leaq 0x3390(%rsp), %rdi
movq 0x2388(%rsp), %rdx
movq 0x2380(%rsp), %rcx
leaq 0x14f5a5(%rip), %rsi    # 0x18fa63
movb $0x0, %al
callq 0x22160
movq 0x5410(%rsp), %rsi
leaq 0x2340(%rsp), %rdi
callq 0x56da0
leaq 0x2340(%rsp), %rax
movq %rax, 0x2378(%rsp)
movl 0x1bb870(%rip), %eax    # 0x1fbd60
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, 0x1bb865(%rip)    # 0x1fbd60
cmpl $0x0, %eax
jle 0x40516
movq 0x5408(%rsp), %rdi
leaq 0x14eea5(%rip), %rsi    # 0x18f3b4
movb $0x0, %al
callq 0x22f30
movq 0x5408(%rsp), %rdi
leaq 0x14f549(%rip), %rsi    # 0x18fa6e
xorl %eax, %eax
movl %eax, 0x154(%rsp)
movb %al, 0x153(%rsp)
callq 0x22f30
movb 0x153(%rsp), %al
movq 0x5408(%rsp), %rdi
movq 0x4398(%rsp), %rdx
leaq 0x14f51f(%rip), %rsi    # 0x18fa77
callq 0x22f30
movb 0x153(%rsp), %al
movq 0x5408(%rsp), %rdi
leaq 0x14f518(%rip), %rsi    # 0x18fa8b
leaq 0x43a0(%rsp), %rdx
callq 0x22f30
movb 0x153(%rsp), %al
movq 0x5408(%rsp), %rdi
leaq 0x14f50b(%rip), %rsi    # 0x18faa1
leaq 0x3390(%rsp), %rdx
callq 0x22f30
movb 0x153(%rsp), %al
movq 0x5408(%rsp), %rdi
leaq 0x14f501(%rip), %rsi    # 0x18faba
callq 0x22f30
movl 0x154(%rsp), %esi
movq 0x5408(%rsp), %rax
movq %rax, 0x158(%rsp)
movq 0x2378(%rsp), %rdi
callq 0x4fd70
movq %rax, 0x160(%rsp)
jmp 0x405ec
movq 0x160(%rsp), %rdi
xorl %esi, %esi
callq 0x4fdb0
movsd %xmm0, 0x148(%rsp)
jmp 0x40606
movq 0x2378(%rsp), %rdi
xorl %esi, %esi
callq 0x4fd70
movq %rax, 0x140(%rsp)
jmp 0x4061f
movq 0x140(%rsp), %rdi
movl $0x1, %esi
callq 0x4fdb0
movsd %xmm0, 0x138(%rsp)
jmp 0x4063c
movq 0x2378(%rsp), %rdi
xorl %esi, %esi
callq 0x4fd70
movq %rax, 0x130(%rsp)
jmp 0x40655
movq 0x130(%rsp), %rdi
movl $0x2, %esi
callq 0x4fdb0
movsd %xmm0, 0x128(%rsp)
jmp 0x40672
movq 0x2378(%rsp), %rdi
movl $0x1, %esi
callq 0x4fd70
movq %rax, 0x120(%rsp)
jmp 0x4068e
movq 0x120(%rsp), %rdi
xorl %esi, %esi
callq 0x4fdb0
movsd %xmm0, 0x118(%rsp)
jmp 0x406a8
movq 0x2378(%rsp), %rdi
movl $0x1, %esi
callq 0x4fd70
movq %rax, 0x110(%rsp)
jmp 0x406c4
movq 0x110(%rsp), %rdi
movl $0x1, %esi
callq 0x4fdb0
movsd %xmm0, 0x108(%rsp)
jmp 0x406e1
movq 0x2378(%rsp), %rdi
movl $0x1, %esi
callq 0x4fd70
movq %rax, 0x100(%rsp)
jmp 0x406fd
movq 0x100(%rsp), %rdi
movl $0x2, %esi
callq 0x4fdb0
movsd %xmm0, 0xf8(%rsp)
jmp 0x4071a
movsd 0xf8(%rsp), %xmm5
movsd 0x108(%rsp), %xmm4
movsd 0x118(%rsp), %xmm3
movsd 0x128(%rsp), %xmm2
movsd 0x138(%rsp), %xmm1
movsd 0x148(%rsp), %xmm0
movq 0x158(%rsp), %rdi
leaq 0x14f36f(%rip), %rsi    # 0x18face
movb $0x6, %al
callq 0x22f30
movq 0x5408(%rsp), %rdi
leaq 0x14f38d(%rip), %rsi    # 0x18fb02
movb $0x0, %al
callq 0x22f30
movl $0x0, 0x233c(%rsp)
movl 0x233c(%rsp), %eax
movl %eax, 0xf4(%rsp)
movq 0x5410(%rsp), %rdi
callq 0x2baf0
movl %eax, %ecx
movl 0xf4(%rsp), %eax
cmpl %ecx, %eax
jge 0x40869
movq 0x5410(%rsp), %rdi
movl 0x233c(%rsp), %esi
callq 0x2bb10
movq %rax, 0xe8(%rsp)
jmp 0x407d1
movq 0xe8(%rsp), %rax
movq %rax, 0x2330(%rsp)
movq 0x5418(%rsp), %rdi
movq 0x2330(%rsp), %rsi
movq 0x5408(%rsp), %rdx
callq 0x3feb0
movl %eax, 0xe4(%rsp)
jmp 0x40807
movl 0xe4(%rsp), %eax
cmpl $0x0, %eax
jne 0x40851
movl $0x0, 0x5424(%rsp)
movl $0x1, 0x53a4(%rsp)
jmp 0x40874
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x53b8(%rsp)
movl %eax, 0x53b4(%rsp)
leaq 0x2340(%rsp), %rdi
callq 0x2c1a0
jmp 0x4129c
jmp 0x40853
movl 0x233c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x233c(%rsp)
jmp 0x40787
movl $0x0, 0x53a4(%rsp)
leaq 0x2340(%rsp), %rdi
callq 0x2c1a0
movl 0x53a4(%rsp), %eax
testl %eax, %eax
je 0x40893
jmp 0x4088e
jmp 0x4128d
jmp 0x41280
movq 0x5410(%rsp), %rdi
callq 0x2b800
movq %rax, %rdi
leaq 0x14f25b(%rip), %rsi    # 0x18fb0a
movl $0x7, %edx
callq 0x22450
cmpl $0x0, %eax
jne 0x41238
leaq 0x1330(%rsp), %rax
movq %rax, 0xd8(%rsp)
movq 0x5410(%rsp), %rdi
callq 0x2b800
movq 0xd8(%rsp), %rdi
movq %rax, %rsi
movl $0xfff, %edx            # imm = 0xFFF
callq 0x224b0
leaq 0x1330(%rsp), %rdi
movl $0x23, %esi
callq 0x22350
movq %rax, 0x1328(%rsp)
cmpq $0x0, 0x1328(%rsp)
jne 0x40929
movl $0x0, 0x5424(%rsp)
jmp 0x4128d
movq 0x1328(%rsp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, 0x1328(%rsp)
movb $0x0, (%rax)
movq 0x5410(%rsp), %rdi
callq 0x2bb40
cmpl $0x0, %eax
jne 0x40965
movl $0x0, 0x5424(%rsp)
jmp 0x4128d
movq 0x5410(%rsp), %rdi
xorl %esi, %esi
callq 0x2bb60
movq %rax, 0x318(%rsp)
movq 0x318(%rsp), %rdi
callq 0x2bb90
movq %rax, 0x310(%rsp)
movq 0x310(%rsp), %rdi
callq 0x48320
cmpq $0x0, %rax
jne 0x409b4
movl $0x0, 0x5424(%rsp)
jmp 0x4128d
movq 0x310(%rsp), %rdi
callq 0x2b510
movq %rax, %rdi
callq 0x2b800
movq %rax, %rdi
leaq 0x14f13f(%rip), %rsi    # 0x18fb12
movl $0x6, %edx
callq 0x22450
cmpl $0x0, %eax
je 0x409f2
movl $0x0, 0x5424(%rsp)
jmp 0x4128d
movq 0x310(%rsp), %rdi
callq 0x2b510
movq %rax, %rdi
callq 0x2b800
movq %rax, %rsi
addq $0x6, %rsi
leaq 0x320(%rsp), %rdi
movl $0xfff, %edx            # imm = 0xFFF
callq 0x224b0
movq 0x5410(%rsp), %rsi
leaq 0x2d0(%rsp), %rdi
movq %rdi, 0xd0(%rsp)
callq 0x56da0
movq 0xd0(%rsp), %rax
movq %rax, 0x308(%rsp)
movq 0x5410(%rsp), %rdi
callq 0x2bae0
movq %rax, %rdi
callq 0x4e7f0
movq %rax, %rsi
leaq 0x250(%rsp), %rdi
callq 0xc6120
jmp 0x40a74
movq 0x5410(%rsp), %rdi
callq 0x2bae0
movq %rax, %rdi
movq (%rdi), %rax
movq 0x18(%rax), %rax
callq *%rax
movl %eax, 0xcc(%rsp)
jmp 0x40a96
movl 0xcc(%rsp), %eax
movl %eax, 0x24c(%rsp)
movl 0x1bb2b6(%rip), %eax    # 0x1fbd60
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, 0x1bb2ab(%rip)    # 0x1fbd60
cmpl $0x0, %eax
jle 0x40af8
movq 0x5408(%rsp), %rdi
leaq 0x14e8eb(%rip), %rsi    # 0x18f3b4
movb $0x0, %al
callq 0x22f30
jmp 0x40af8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x53b8(%rsp)
movl %eax, 0x53b4(%rsp)
leaq 0x2d0(%rsp), %rdi
callq 0x2c1a0
jmp 0x4129c
movq 0x5408(%rsp), %rdi
leaq 0x14ef67(%rip), %rsi    # 0x18fa6e
xorl %eax, %eax
movl %eax, 0xb4(%rsp)
movb %al, 0xb3(%rsp)
callq 0x22f30
movb 0xb3(%rsp), %al
movq 0x5408(%rsp), %rdi
movq 0x1328(%rsp), %rdx
leaq 0x14ef3d(%rip), %rsi    # 0x18fa77
callq 0x22f30
movb 0xb3(%rsp), %al
movq 0x5408(%rsp), %rdi
leaq 0x14ef36(%rip), %rsi    # 0x18fa8b
leaq 0x1330(%rsp), %rdx
callq 0x22f30
movb 0xb3(%rsp), %al
movq 0x5408(%rsp), %rdi
leaq 0x14ef29(%rip), %rsi    # 0x18faa1
leaq 0x320(%rsp), %rdx
callq 0x22f30
movb 0xb3(%rsp), %al
movq 0x5408(%rsp), %rdi
movl 0x24c(%rsp), %edx
leaq 0x14ef77(%rip), %rsi    # 0x18fb19
callq 0x22f30
movl 0xb4(%rsp), %esi
movq 0x5408(%rsp), %rax
movq %rax, 0xb8(%rsp)
movq 0x308(%rsp), %rdi
callq 0x4fd70
movq %rax, 0xc0(%rsp)
jmp 0x40bd5
movq 0xc0(%rsp), %rdi
xorl %esi, %esi
callq 0x4fdb0
movsd %xmm0, 0xa8(%rsp)
jmp 0x40bef
movq 0x308(%rsp), %rdi
xorl %esi, %esi
callq 0x4fd70
movq %rax, 0xa0(%rsp)
jmp 0x40c08
movq 0xa0(%rsp), %rdi
movl $0x1, %esi
callq 0x4fdb0
movsd %xmm0, 0x98(%rsp)
jmp 0x40c25
movq 0x308(%rsp), %rdi
xorl %esi, %esi
callq 0x4fd70
movq %rax, 0x90(%rsp)
jmp 0x40c3e
movq 0x90(%rsp), %rdi
movl $0x2, %esi
callq 0x4fdb0
movsd %xmm0, 0x88(%rsp)
jmp 0x40c5b
movq 0x308(%rsp), %rdi
movl $0x1, %esi
callq 0x4fd70
movq %rax, 0x80(%rsp)
jmp 0x40c77
movq 0x80(%rsp), %rdi
xorl %esi, %esi
callq 0x4fdb0
movsd %xmm0, 0x78(%rsp)
jmp 0x40c8e
movq 0x308(%rsp), %rdi
movl $0x1, %esi
callq 0x4fd70
movq %rax, 0x70(%rsp)
jmp 0x40ca7
movq 0x70(%rsp), %rdi
movl $0x1, %esi
callq 0x4fdb0
movsd %xmm0, 0x68(%rsp)
jmp 0x40cbe
movq 0x308(%rsp), %rdi
movl $0x1, %esi
callq 0x4fd70
movq %rax, 0x60(%rsp)
jmp 0x40cd7
movq 0x60(%rsp), %rdi
movl $0x2, %esi
callq 0x4fdb0
movsd %xmm0, 0x58(%rsp)
jmp 0x40cee
movsd 0x58(%rsp), %xmm5
movsd 0x68(%rsp), %xmm4
movsd 0x78(%rsp), %xmm3
movsd 0x88(%rsp), %xmm2
movsd 0x98(%rsp), %xmm1
movsd 0xa8(%rsp), %xmm0
movq 0xb8(%rsp), %rdi
leaq 0x14ee09(%rip), %rsi    # 0x18fb33
movb $0x6, %al
callq 0x22f30
movq 0x318(%rsp), %rdi
callq 0x49da0
cmpl $0x0, %eax
jle 0x410ec
movq 0x5408(%rsp), %rdi
leaq 0x14ee12(%rip), %rsi    # 0x18fb68
movb $0x0, %al
callq 0x22f30
movl $0x0, 0x248(%rsp)
movl 0x248(%rsp), %eax
movl %eax, 0x54(%rsp)
movq 0x318(%rsp), %rdi
callq 0x49da0
movl %eax, %ecx
movl 0x54(%rsp), %eax
cmpl %ecx, %eax
jge 0x410d6
movq 0x318(%rsp), %rdi
movl 0x248(%rsp), %esi
callq 0x49dc0
movq %rax, 0x48(%rsp)
jmp 0x40da9
movq 0x48(%rsp), %rax
movq %rax, 0x240(%rsp)
cmpq $0x0, 0x240(%rsp)
je 0x40dd5
movq 0x240(%rsp), %rdi
callq 0x48250
movq %rax, 0x40(%rsp)
jmp 0x40de3
leaq 0x14eda2(%rip), %rax    # 0x18fb7e
movq %rax, 0x40(%rsp)
jmp 0x40de3
movq 0x40(%rsp), %rax
movq %rax, 0x238(%rsp)
cmpq $0x0, 0x240(%rsp)
je 0x40e0f
movq 0x240(%rsp), %rdi
callq 0x2be70
movq %rax, 0x38(%rsp)
jmp 0x40e18
xorl %eax, %eax
movq %rax, 0x38(%rsp)
jmp 0x40e18
movq 0x38(%rsp), %rax
movq %rax, 0x230(%rsp)
cmpq $0x0, 0x240(%rsp)
je 0x40e44
movq 0x240(%rsp), %rdi
callq 0x48b20
movq %rax, 0x30(%rsp)
jmp 0x40e4d
xorl %eax, %eax
movq %rax, 0x30(%rsp)
jmp 0x40e4d
movq 0x30(%rsp), %rax
movq %rax, 0x228(%rsp)
cmpl $0x0, 0x248(%rsp)
jle 0x40e7a
movq 0x5408(%rsp), %rdi
leaq 0x14ed10(%rip), %rsi    # 0x18fb83
movb $0x0, %al
callq 0x22f30
movq 0x5408(%rsp), %rdi
movq 0x238(%rsp), %rdx
leaq 0x14ecf5(%rip), %rsi    # 0x18fb86
movb $0x0, %al
callq 0x22f30
cmpq $0x0, 0x230(%rsp)
je 0x4105c
movq 0x230(%rsp), %rdi
callq 0x48930
movq %rax, 0x220(%rsp)
movq 0x220(%rsp), %rdi
callq 0x2b4a0
movsd 0x14d70f(%rip), %xmm1   # 0x18e5e0
mulsd %xmm1, %xmm0
movsd 0x14d143(%rip), %xmm1   # 0x18e020
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x21c(%rsp)
movq 0x220(%rsp), %rdi
callq 0x2b4b0
movsd 0x14d6df(%rip), %xmm1   # 0x18e5e0
mulsd %xmm1, %xmm0
movsd 0x14d113(%rip), %xmm1   # 0x18e020
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x218(%rsp)
movq 0x220(%rsp), %rdi
callq 0x2b4c0
movsd 0x14d6af(%rip), %xmm1   # 0x18e5e0
mulsd %xmm1, %xmm0
movsd 0x14d0e3(%rip), %xmm1   # 0x18e020
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x214(%rsp)
cmpl $0xff, 0x21c(%rsp)
jge 0x40f82
cmpl $0x0, 0x21c(%rsp)
jle 0x40f70
movl 0x21c(%rsp), %eax
movl %eax, 0x2c(%rsp)
jmp 0x40f78
xorl %eax, %eax
movl %eax, 0x2c(%rsp)
jmp 0x40f78
movl 0x2c(%rsp), %eax
movl %eax, 0x28(%rsp)
jmp 0x40f8d
movl $0xff, %eax
movl %eax, 0x28(%rsp)
jmp 0x40f8d
movl 0x28(%rsp), %eax
movl %eax, 0x21c(%rsp)
cmpl $0xff, 0x218(%rsp)
jge 0x40fce
cmpl $0x0, 0x218(%rsp)
jle 0x40fbc
movl 0x218(%rsp), %eax
movl %eax, 0x24(%rsp)
jmp 0x40fc4
xorl %eax, %eax
movl %eax, 0x24(%rsp)
jmp 0x40fc4
movl 0x24(%rsp), %eax
movl %eax, 0x20(%rsp)
jmp 0x40fd9
movl $0xff, %eax
movl %eax, 0x20(%rsp)
jmp 0x40fd9
movl 0x20(%rsp), %eax
movl %eax, 0x218(%rsp)
cmpl $0xff, 0x214(%rsp)
jge 0x4101a
cmpl $0x0, 0x214(%rsp)
jle 0x41008
movl 0x214(%rsp), %eax
movl %eax, 0x1c(%rsp)
jmp 0x41010
xorl %eax, %eax
movl %eax, 0x1c(%rsp)
jmp 0x41010
movl 0x1c(%rsp), %eax
movl %eax, 0x18(%rsp)
jmp 0x41025
movl $0xff, %eax
movl %eax, 0x18(%rsp)
jmp 0x41025
movl 0x18(%rsp), %eax
movl %eax, 0x214(%rsp)
movq 0x5408(%rsp), %rdi
movl 0x21c(%rsp), %edx
movl 0x218(%rsp), %ecx
movl 0x214(%rsp), %r8d
leaq 0x14eb3f(%rip), %rsi    # 0x18fb94
movb $0x0, %al
callq 0x22f30
cmpq $0x0, 0x228(%rsp)
je 0x410aa
movq 0x228(%rsp), %rdi
callq 0x482e0
cmpq $0x0, %rax
je 0x410aa
movq 0x5408(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x228(%rsp), %rdi
callq 0x482e0
movq 0x10(%rsp), %rdi
movq %rax, %rdx
leaq 0x14eb0d(%rip), %rsi    # 0x18fbb0
movb $0x0, %al
callq 0x22f30
movq 0x5408(%rsp), %rdi
leaq 0x14ea4e(%rip), %rsi    # 0x18fb07
movb $0x0, %al
callq 0x22f30
movl 0x248(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x248(%rsp)
jmp 0x40d68
movq 0x5408(%rsp), %rdi
leaq 0x14e2df(%rip), %rsi    # 0x18f3c4
movb $0x0, %al
callq 0x22f30
leaq 0x250(%rsp), %rdi
callq 0xc6470
movl %eax, 0xc(%rsp)
jmp 0x410ff
movl 0xc(%rsp), %eax
cmpl $0x0, %eax
jne 0x411fd
movq 0x5408(%rsp), %rdi
leaq 0x14eaa6(%rip), %rsi    # 0x18fbc1
movb $0x0, %al
callq 0x22f30
movl $0x0, 0x210(%rsp)
cmpl $0x4, 0x210(%rsp)
jge 0x411e7
movl $0x0, 0x20c(%rsp)
cmpl $0x4, 0x20c(%rsp)
jge 0x411cf
cmpl $0x0, 0x20c(%rsp)
jg 0x41164
cmpl $0x0, 0x210(%rsp)
jle 0x4117a
movq 0x5408(%rsp), %rdi
leaq 0x14ea10(%rip), %rsi    # 0x18fb83
movb $0x0, %al
callq 0x22f30
movq 0x5408(%rsp), %rax
movq %rax, (%rsp)
movl 0x20c(%rsp), %esi
leaq 0x250(%rsp), %rdi
callq 0x49600
movq (%rsp), %rdi
movslq 0x210(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
leaq 0x14ea25(%rip), %rsi    # 0x18fbd7
movb $0x1, %al
callq 0x22f30
movl 0x20c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x20c(%rsp)
jmp 0x41146
jmp 0x411d1
movl 0x210(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x210(%rsp)
jmp 0x4112d
movq 0x5408(%rsp), %rdi
leaq 0x14e1ce(%rip), %rsi    # 0x18f3c4
movb $0x0, %al
callq 0x22f30
movq 0x5408(%rsp), %rdi
leaq 0x14e9ce(%rip), %rsi    # 0x18fbda
movb $0x0, %al
callq 0x22f30
movq 0x5408(%rsp), %rdi
leaq 0x14e8e0(%rip), %rsi    # 0x18fb02
movb $0x0, %al
callq 0x22f30
leaq 0x2d0(%rsp), %rdi
callq 0x2c1a0
jmp 0x4127e
movq 0x5410(%rsp), %rdi
callq 0x2b800
movq %rax, %rdi
leaq 0x14e99e(%rip), %rsi    # 0x18fbed
movl $0x4, %edx
callq 0x22450
cmpl $0x0, %eax
jne 0x4127c
movq 0x5410(%rsp), %rdi
callq 0x2b800
movq %rax, %rsi
leaq 0x14e97d(%rip), %rdi    # 0x18fbf2
movb $0x0, %al
callq 0xf3b00
jmp 0x4127e
jmp 0x41280
jmp 0x41282
movl $0x1, 0x5424(%rsp)
movl 0x5424(%rsp), %eax
addq $0x5428, %rsp           # imm = 0x5428
retq
movq 0x53b8(%rsp), %rdi
callq 0x23290
nopl (%rax)