// File: /workspace/github_c_makefile_star2_O1/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 0xdec6(%rip), %rdi      # 0x1d690
leaq 0x6d35(%rip), %rsi      # 0x16506
callq 0x33c0
testq %rax, %rax
je 0xf95e
movq %rax, %r15
leaq 0x76a8(%rip), %rdi      # 0x16e91
movq %rax, %rsi
callq 0x33a0
leaq 0x769c(%rip), %rdi      # 0x16e94
movq %r15, %rsi
callq 0x33a0
leaq 0x76fc(%rip), %rdi      # 0x16f03
movq %r15, %rsi
callq 0x33a0
leaq 0x76fe(%rip), %rdi      # 0x16f14
movq %r15, %rsi
callq 0x33a0
cmpl $0x0, 0xde7b(%rip)      # 0x1d6a0
jle 0xf926
xorl %r14d, %r14d
leaq 0x420(%rsp), %r12
movq 0xde5b(%rip), %rax      # 0x1d698
leaq (%r14,%r14,8), %rcx
cmpl $0x0, 0x40(%rax,%rcx,8)
jle 0xf913
leaq (%rax,%rcx,8), %rax
movq %rax, 0x8(%rsp)
movl $0x18, %r13d
xorl %ebx, %ebx
movq %r14, %rbp
movq %r15, %r14
movq 0x8(%rsp), %rax
movq 0x38(%rax), %r15
movq -0x18(%r15,%r13), %rdi
callq 0x60db
movapd %xmm0, %xmm1
movsd 0x656e(%rip), %xmm0     # 0x15df0
mulsd %xmm0, %xmm1
movsd %xmm1, 0x18(%rsp)
movq -0x10(%r15,%r13), %rdi
callq 0x60db
mulsd 0x6552(%rip), %xmm0     # 0x15df0
movsd %xmm0, 0x10(%rsp)
movq -0x8(%r15,%r13), %rdi
movq %r12, %rsi
callq 0xf989
movq (%r15,%r13), %rdi
movq %r14, %r15
movq %rbp, %r14
leaq 0x20(%rsp), %rbp
movq %rbp, %rsi
callq 0xf989
movsd 0x10(%rsp), %xmm1
movapd %xmm1, %xmm2
mulsd 0x755e(%rip), %xmm2     # 0x16e38
movq %r15, %rdi
leaq 0x7696(%rip), %rsi      # 0x16f7a
movl %r14d, %edx
movsd 0x18(%rsp), %xmm0
movq %r12, %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 0xf85d
incq %r14
movslq 0xdd83(%rip), %rax      # 0x1d6a0
cmpq %rax, %r14
jl 0xf836
leaq 0x76c1(%rip), %rdi      # 0x16fee
movq %r15, %rsi
callq 0x33a0
leaq 0x763b(%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 0xdd2b(%rip), %rdx      # 0x1d690
leaq 0x74ec(%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 0x5ef0