// File: /workspace/github_c_makefile_star2_O3/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 0xe5a2(%rip), %rdi      # 0x1d690
leaq 0x7401(%rip), %rsi      # 0x164f6
callq 0x33c0
testq %rax, %rax
je 0xf285
movq %rax, %r15
leaq 0x7d84(%rip), %rdi      # 0x16e91
movq %rax, %rsi
callq 0x33a0
leaq 0x7d78(%rip), %rdi      # 0x16e94
movq %r15, %rsi
callq 0x33a0
leaq 0x7dd8(%rip), %rdi      # 0x16f03
movq %r15, %rsi
callq 0x33a0
leaq 0x7dda(%rip), %rdi      # 0x16f14
movq %r15, %rsi
callq 0x33a0
movl 0xe558(%rip), %eax      # 0x1d6a0
testl %eax, %eax
jle 0xf24d
xorl %r12d, %r12d
movq 0xe53e(%rip), %rcx      # 0x1d698
leaq (%r12,%r12,8), %rdx
cmpl $0x0, 0x40(%rcx,%rdx,8)
jle 0xf23e
leaq (%rcx,%rdx,8), %rax
movq %rax, 0x8(%rsp)
movl $0x18, %r13d
xorl %ebx, %ebx
movq %r12, %rbp
movq %r15, %r12
movq 0x8(%rsp), %rax
movq 0x38(%rax), %r15
movq -0x18(%r15,%r13), %rdi
callq 0x5f5f
movapd %xmm0, %xmm1
movsd 0x6c41(%rip), %xmm0     # 0x15de0
mulsd %xmm0, %xmm1
movsd %xmm1, 0x18(%rsp)
movq -0x10(%r15,%r13), %rdi
callq 0x5f5f
mulsd 0x6c25(%rip), %xmm0     # 0x15de0
movsd %xmm0, 0x10(%rsp)
movq -0x8(%r15,%r13), %rdi
leaq 0x420(%rsp), %r14
movq %r14, %rsi
callq 0xf2b0
movq (%r15,%r13), %rdi
movq %r12, %r15
movq %rbp, %r12
leaq 0x20(%rsp), %rbp
movq %rbp, %rsi
callq 0xf2b0
movsd 0x10(%rsp), %xmm1
movapd %xmm1, %xmm2
mulsd 0x7c39(%rip), %xmm2     # 0x16e38
movq %r15, %rdi
leaq 0x7d71(%rip), %rsi      # 0x16f7a
movl %r12d, %edx
movsd 0x18(%rsp), %xmm0
movq %r14, %rcx
movq %rbp, %r8
movb $0x3, %al
callq 0x3480
incq %rbx
movq 0x8(%rsp), %rax
movslq 0x40(%rax), %rax
addq $0x20, %r13
cmpq %rax, %rbx
jl 0xf17a
movl 0xe462(%rip), %eax      # 0x1d6a0
incq %r12
movslq %eax, %rcx
cmpq %rcx, %r12
jl 0xf153
leaq 0x7d9a(%rip), %rdi      # 0x16fee
movq %r15, %rsi
callq 0x33a0
leaq 0x7d14(%rip), %rdi      # 0x16f77
movq %r15, %rsi
callq 0x33a0
movq %r15, %rdi
callq 0x3340
addq $0x828, %rsp            # imm = 0x828
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xe404(%rip), %rdx      # 0x1d690
leaq 0x7bc5(%rip), %rsi      # 0x16e58
movl $0x2, %edi
xorl %eax, %eax
addq $0x828, %rsp            # imm = 0x828
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5d74