// File: /workspace/github_c_makefile_star2_O2/deplinenoise[P]tundra/src/Profiler.cpp
// Function: t2::ProfilerWriteOutput()
void ProfilerWriteOutput()
  {
    FILE* f = fopen(s_ProfilerState.m_FileName, "w");
    if (!f)
    {
      Log(kWarning, "profiler: failed to write profiler output file into '%s'", s_ProfilerState.m_FileName);
      return;
    }

    // See https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/edit for
    // Chrome Tracing profiler format description

    fputs("{\n", f);
    // JSON does not support comments, so emit "how to use this" as a fake string value    
    fputs("\"instructions_readme\": \"1) Open Chrome, 2) go to chrome://tracing, 3) click Load, 4) navigate to this file.\",\n", f);
    fputs("\"traceEvents\":[\n", f);
    fputs("{ \"cat\":\"\", \"pid\":1, \"tid\":0, \"ts\":0, \"ph\":\"M\", \"name\":\"process_name\", \"args\": { \"name\":\"tundra\" } }\n", f);
    for (int i = 0; i < s_ProfilerState.m_ThreadCount; ++i)
    {
      const ProfilerThread& thread = s_ProfilerState.m_Threads[i];
      for (int j = 0; j < thread.m_EventCount; ++j)
      {
        const ProfilerEvent& evt = thread.m_Events[j];
        double timeUs = TimerToSeconds(evt.m_Time)*1.0e6;
        double durUs = TimerToSeconds(evt.m_Duration)*1.0e6;
        char name[1024];
        char info[1024];
        EscapeString(evt.m_Name, name, sizeof(name));
        EscapeString(evt.m_Info, info, sizeof(info));
        fprintf(f, ",{ \"pid\":1, \"tid\":%d, \"ts\":%.0f, \"dur\":%.0f, \"ph\":\"X\", \"name\": \"%s\", \"args\": { \"durationMS\":%.0f, \"detail\":\"%s\" }}\n", i, timeUs, durUs, name, durUs*0.001, info);
      }
    }
    fputs("\n]\n", f);
    fputs("}\n", f);

    fclose(f);
  }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x828, %rsp            # imm = 0x828
movq 0xf6c3(%rip), %rdi      # 0x1d690
leaq 0x6520(%rip), %rsi      # 0x144f4
callq 0x33c0
testq %rax, %rax
je 0xe13e
movq %rax, %rbx
leaq 0x6e9d(%rip), %rdi      # 0x14e89
movq %rax, %rsi
callq 0x33a0
leaq 0x6e91(%rip), %rdi      # 0x14e8c
movq %rbx, %rsi
callq 0x33a0
leaq 0x6ef1(%rip), %rdi      # 0x14efb
movq %rbx, %rsi
callq 0x33a0
leaq 0x6ef3(%rip), %rdi      # 0x14f0c
movq %rbx, 0x8(%rsp)
movq %rbx, %rsi
callq 0x33a0
xorl %r14d, %r14d
movslq 0xf670(%rip), %rax      # 0x1d6a0
cmpq %rax, %r14
jge 0xe101
imulq $0x48, %r14, %rbp
addq 0xf654(%rip), %rbp      # 0x1d698
pushq $0x18
popq %r13
xorl %ebx, %ebx
movslq 0x40(%rbp), %rax
cmpq %rax, %rbx
jge 0xe0f9
movq 0x38(%rbp), %r15
movq -0x18(%r15,%r13), %rdi
callq 0x5b6a
movapd %xmm0, %xmm1
movsd 0x5d7f(%rip), %xmm0     # 0x13df0
mulsd %xmm0, %xmm1
movsd %xmm1, 0x18(%rsp)
movq -0x10(%r15,%r13), %rdi
callq 0x5b6a
mulsd 0x5d63(%rip), %xmm0     # 0x13df0
movsd %xmm0, 0x10(%rsp)
movq -0x8(%r15,%r13), %rdi
leaq 0x420(%rsp), %r12
movq %r12, %rsi
callq 0xe167
movq (%r15,%r13), %rdi
leaq 0x20(%rsp), %r15
movq %r15, %rsi
callq 0xe167
movsd 0x10(%rsp), %xmm1
movapd %xmm1, %xmm2
mulsd 0x6d65(%rip), %xmm2     # 0x14e30
movq 0x8(%rsp), %rdi
leaq 0x6e9b(%rip), %rsi      # 0x14f72
movl %r14d, %edx
movsd 0x18(%rsp), %xmm0
movq %r12, %rcx
movq %r15, %r8
movb $0x3, %al
callq 0x3480
incq %rbx
addq $0x20, %r13
jmp 0xe04a
incq %r14
jmp 0xe029
leaq 0x6ede(%rip), %rdi      # 0x14fe6
movq 0x8(%rsp), %rbx
movq %rbx, %rsi
callq 0x33a0
leaq 0x6e53(%rip), %rdi      # 0x14f6f
movq %rbx, %rsi
callq 0x33a0
movq %rbx, %rdi
callq 0x3340
addq $0x828, %rsp            # imm = 0x828
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xf54b(%rip), %rdx      # 0x1d690
leaq 0x6d04(%rip), %rsi      # 0x14e50
pushq $0x2
popq %rdi
xorl %eax, %eax
addq $0x828, %rsp            # imm = 0x828
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5981