// File: /workspace/github_c_makefile_star2_O3/0xADE1A1DE[P]MeasureSuite/lib/src/json.c
// Function: generate_json
int generate_json(struct measuresuite *ms, uint64_t delta_in_seconds,
                  size_t check_result) {
  char *json = ms->json;
  char *json_end = ms->json + ms->json_len - 1;

  size_t size = 0; // available
  size_t len = 0;  // written

  PRINT("{\"stats\":"
        "{"
        "\"numFunctions\":%" PRIu64 ","
        "\"runtime\":%" PRIu64 "," // in seconds
        "\"incorrect\":%" PRIu64
        "," // index
            // if incorrect is '0', all functions calculate the same, otherwise
            // the index of which function is incorrect to the previous one.

        "\"timer\":\"%s\"" // string "RDTSCP" or "PMC"
        "},"
        "\"functions\":[",
        ms->num_functions, delta_in_seconds, check_result,
        ms_get_timer(ms) == PMC ? "PMC" : "RDTSCP");

  // print function meta data
  //

  FOR_EACH_FUNCTION {
    switch (fct->type) {
    case ASM:
#ifdef USE_ASSEMBLYLINE
      PRINT("{\"type\":\"ASM\", \"chunks\":%" PRIi32 "},", fct->chunks);
#endif
      break;
    case BIN:
      PRINT("{\"type\":\"BIN\"},");
      break;
    case ELF:
      PRINT("{\"type\":\"ELF\"},");
      break;
    case SHARED_OBJECT:
      PRINT("{\"type\":\"SHARED_OBJECT\"},");
      break;
    }
  }

  // overwrite comma
  json--;
  PRINT("],\"cycles\":[");

  // print cycles
  FOR_EACH_FUNCTION {

    PRINT("[");
    for (size_t run_i = 0; run_i < ms->num_batches; run_i++) {
      PRINT("%" PRIu64 ",", fct->cycle_results[run_i]);
    }
    // overwrite comma after last number
    json--;
    // close array and comma for next one
    PRINT("],");
  }

  // overwrite comma after last array
  json -= 1;
  PRINT("]}");

  return 0;

  // we did not have enough space.
enlarge:

  // enlarge
  ms->json_len *= 2;
  DEBUG("trying to realloc space for more json (new len: %lu) %lu, %lu\n",
        ms->json_len, ms->num_batches, ms->batch_size);
  DEBUG("JSON was: %s", ms->json);
  if (realloc_or_fail(ms, (void **)&(ms->json), ms->json_len)) {
    return 1;
  }

  // and try again recursively
  return generate_json(ms, delta_in_seconds, check_result);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, 0x20(%rsp)
movq %rsi, 0x18(%rsp)
movq %rdi, %r15
leaq 0x38(%rdi), %rax
movq %rax, 0x10(%rsp)
movq 0x38(%r15), %r14
movq 0x40(%r15), %r12
leaq -0x1(%r12), %r13
movq 0x80(%r15), %rbx
movq %r15, %rdi
callq 0x2a88
testl %eax, %eax
leaq 0x171e(%rip), %rax      # 0x604b
leaq 0x1713(%rip), %rcx      # 0x6047
cmoveq %rcx, %rax
movq %rax, (%rsp)
movq %r14, %rdi
movq %r13, %rsi
leaq 0x16a7(%rip), %rdx      # 0x5ff0
movq %rbx, %rcx
movq 0x18(%rsp), %r8
movq 0x20(%rsp), %r9
xorl %eax, %eax
callq 0x20e0
movslq %eax, %rbx
cmpq %rbx, %r13
jbe 0x4b35
leaq (%r12,%r14), %r13
decq %r13
addq %r14, %rbx
movq 0x70(%r15), %r12
movq 0x80(%r15), %rax
leaq (%rax,%rax,4), %rax
leaq (%r12,%rax,8), %rax
addq $-0x28, %rax
cmpq %rax, %r12
ja 0x4a18
movl (%r12), %eax
cmpl $0x3, %eax
je 0x49d0
cmpl $0x2, %eax
je 0x49bb
cmpl $0x1, %eax
jne 0x49f8
movq %r13, %r14
subq %rbx, %r14
movq %rbx, %rdi
movq %r14, %rsi
leaq 0x1699(%rip), %rdx      # 0x6052
jmp 0x49e3
movq %r13, %r14
subq %rbx, %r14
movq %rbx, %rdi
movq %r14, %rsi
leaq 0x1694(%rip), %rdx      # 0x6062
jmp 0x49e3
movq %r13, %r14
subq %rbx, %r14
movq %rbx, %rdi
movq %r14, %rsi
leaq 0x168f(%rip), %rdx      # 0x6072
xorl %eax, %eax
callq 0x20e0
cltq
cmpq %rax, %r14
jbe 0x4b35
addq %rax, %rbx
addq $0x28, %r12
movq 0x70(%r15), %rax
movq 0x80(%r15), %rcx
leaq (%rcx,%rcx,4), %rcx
leaq (%rax,%rcx,8), %rax
addq $-0x28, %rax
jmp 0x498a
decq %rbx
movq %r13, %r14
subq %rbx, %r14
movq %rbx, %rdi
movq %r14, %rsi
leaq 0x165e(%rip), %rdx      # 0x608c
xorl %eax, %eax
callq 0x20e0
cltq
cmpq %rax, %r14
jbe 0x4b35
addq %rax, %rbx
movq 0x70(%r15), %r12
movq 0x80(%r15), %rax
leaq (%rax,%rax,4), %rax
leaq (%r12,%rax,8), %rax
addq $-0x28, %rax
cmpq %rax, %r12
ja 0x4b11
movq %r13, %r14
subq %rbx, %r14
movq %rbx, %rdi
movq %r14, %rsi
leaq 0x1621(%rip), %rdx      # 0x6097
xorl %eax, %eax
callq 0x20e0
cltq
cmpq %rax, %r14
jbe 0x4b35
addq %rax, %rbx
cmpq $0x0, 0x28(%r15)
je 0x4aca
xorl %ebp, %ebp
movq %r13, %r14
subq %rbx, %r14
movq 0x20(%r12), %rax
movq (%rax,%rbp,8), %rcx
movq %rbx, %rdi
movq %r14, %rsi
leaq 0x15e9(%rip), %rdx      # 0x6099
xorl %eax, %eax
callq 0x20e0
cltq
cmpq %rax, %r14
jbe 0x4b35
addq %rax, %rbx
incq %rbp
cmpq 0x28(%r15), %rbp
jb 0x4a94
decq %rbx
movq %r13, %r14
subq %rbx, %r14
movq %rbx, %rdi
movq %r14, %rsi
leaq 0x15be(%rip), %rdx      # 0x609e
xorl %eax, %eax
callq 0x20e0
cltq
cmpq %rax, %r14
jbe 0x4b35
addq %rax, %rbx
addq $0x28, %r12
movq 0x70(%r15), %rax
movq 0x80(%r15), %rcx
leaq (%rcx,%rcx,4), %rcx
leaq (%rax,%rcx,8), %rax
addq $-0x28, %rax
jmp 0x4a5a
decq %rbx
subq %rbx, %r13
xorl %r14d, %r14d
movq %rbx, %rdi
movq %r13, %rsi
leaq 0x157a(%rip), %rdx      # 0x60a1
xorl %eax, %eax
callq 0x20e0
cltq
cmpq %rax, %r13
ja 0x4b5b
movq 0x40(%r15), %rdx
addq %rdx, %rdx
movq %rdx, 0x40(%r15)
movq %r15, %rdi
movq 0x10(%rsp), %rsi
callq 0x2fb4
testl %eax, %eax
je 0x4908
movl $0x1, %r14d
movl %r14d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)