// File: /workspace/github_c_makefile_star2_O2/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), %rbx
movq 0x40(%r15), %r14
leaq -0x1(%r14), %r12
movq 0x80(%r15), %r13
movq %r15, %rdi
callq 0x29cd
testl %eax, %eax
leaq 0x19bb(%rip), %rax      # 0x6013
leaq 0x19b0(%rip), %rcx      # 0x600f
cmoveq %rcx, %rax
movq %rax, (%rsp)
movq %rbx, %rdi
movq %r12, %rsi
leaq 0x1944(%rip), %rdx      # 0x5fb8
movq %r13, %rcx
movq 0x18(%rsp), %r8
movq 0x20(%rsp), %r9
xorl %eax, %eax
callq 0x20e0
movslq %eax, %r13
cmpq %r13, %r12
jbe 0x482c
leaq (%r14,%rbx), %r12
decq %r12
addq %rbx, %r13
movq 0x70(%r15), %rax
movq %rax, %r14
imulq $0x28, 0x80(%r15), %rcx
addq %rcx, %rax
addq $-0x28, %rax
cmpq %rax, %r14
ja 0x472a
movl (%r14), %eax
cmpl $0x3, %eax
je 0x46f5
cmpl $0x2, %eax
je 0x46e0
cmpl $0x1, %eax
jne 0x471d
movq %r12, %rbx
subq %r13, %rbx
movq %r13, %rdi
movq %rbx, %rsi
leaq 0x193c(%rip), %rdx      # 0x601a
jmp 0x4708
movq %r12, %rbx
subq %r13, %rbx
movq %r13, %rdi
movq %rbx, %rsi
leaq 0x1937(%rip), %rdx      # 0x602a
jmp 0x4708
movq %r12, %rbx
subq %r13, %rbx
movq %r13, %rdi
movq %rbx, %rsi
leaq 0x1932(%rip), %rdx      # 0x603a
xorl %eax, %eax
callq 0x20e0
cltq
cmpq %rax, %rbx
jbe 0x482c
addq %rax, %r13
addq $0x28, %r14
movq 0x70(%r15), %rax
jmp 0x46a5
decq %r13
movq %r12, %rbx
subq %r13, %rbx
movq %r13, %rdi
movq %rbx, %rsi
leaq 0x1914(%rip), %rdx      # 0x6054
xorl %eax, %eax
callq 0x20e0
cltq
cmpq %rax, %rbx
jbe 0x482c
addq %rax, %r13
movq 0x70(%r15), %rax
movq %rax, %r14
imulq $0x28, 0x80(%r15), %rcx
addq %rcx, %rax
addq $-0x28, %rax
cmpq %rax, %r14
ja 0x4809
movq %r12, %rbx
subq %r13, %rbx
movq %r13, %rdi
movq %rbx, %rsi
leaq 0x18d8(%rip), %rdx      # 0x605f
xorl %eax, %eax
callq 0x20e0
cltq
cmpq %rax, %rbx
jbe 0x482c
addq %rax, %r13
xorl %ebp, %ebp
cmpq 0x28(%r15), %rbp
jae 0x47d5
movq %r12, %rbx
subq %r13, %rbx
movq 0x20(%r14), %rax
movq (%rax,%rbp,8), %rcx
movq %r13, %rdi
movq %rbx, %rsi
leaq 0x18a2(%rip), %rdx      # 0x6061
xorl %eax, %eax
callq 0x20e0
cltq
cmpq %rax, %rbx
jbe 0x482c
addq %rax, %r13
incq %rbp
jmp 0x479e
decq %r13
movq %r12, %rbx
subq %r13, %rbx
movq %r13, %rdi
movq %rbx, %rsi
leaq 0x187b(%rip), %rdx      # 0x6066
xorl %eax, %eax
callq 0x20e0
cltq
cmpq %rax, %rbx
jbe 0x482c
addq %rax, %r13
addq $0x28, %r14
movq 0x70(%r15), %rax
jmp 0x475c
decq %r13
subq %r13, %r12
xorl %ebx, %ebx
movq %r13, %rdi
movq %r12, %rsi
leaq 0x184b(%rip), %rdx      # 0x6069
xorl %eax, %eax
callq 0x20e0
cltq
cmpq %rax, %r12
ja 0x484f
movq 0x40(%r15), %rdx
addq %rdx, %rdx
movq %rdx, 0x40(%r15)
movq %r15, %rdi
movq 0x10(%rsp), %rsi
callq 0x2e84
testl %eax, %eax
je 0x4634
pushq $0x1
popq %rbx
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq