// File: /workspace/github_c_makefile_star2_O1/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), %r12
movq 0x40(%r15), %r14
leaq -0x1(%r14), %r13
movq 0x80(%r15), %rbx
movq %r15, %rdi
callq 0x2ae8
testl %eax, %eax
leaq 0x166f(%rip), %rax      # 0x601b
leaq 0x1664(%rip), %rcx      # 0x6017
cmoveq %rcx, %rax
movq %rax, (%rsp)
movq %r12, %rdi
movq %r13, %rsi
leaq 0x15f8(%rip), %rdx      # 0x5fc0
movq %rbx, %rcx
movq 0x18(%rsp), %r8
movq 0x20(%rsp), %r9
xorl %eax, %eax
callq 0x20e0
movslq %eax, %rbx
cmpq %rbx, %r13
jbe 0x4c61
leaq (%r14,%r12), %rax
decq %rax
movq %rax, 0x8(%rsp)
addq %r12, %rbx
movq 0x70(%r15), %r14
movq 0x80(%r15), %rax
leaq (%rax,%rax,4), %rax
leaq (%r14,%rax,8), %rax
addq $-0x28, %rax
cmpq %rax, %r14
seta %bpl
ja 0x4aab
movl (%r14), %eax
cmpl $0x3, %eax
je 0x4a5b
cmpl $0x2, %eax
je 0x4a44
cmpl $0x1, %eax
jne 0x4a81
movq 0x8(%rsp), %r12
subq %rbx, %r12
movq %rbx, %rdi
movq %r12, %rsi
leaq 0x15e0(%rip), %rdx      # 0x6022
jmp 0x4a70
movq 0x8(%rsp), %r12
subq %rbx, %r12
movq %rbx, %rdi
movq %r12, %rsi
leaq 0x15d9(%rip), %rdx      # 0x6032
jmp 0x4a70
movq 0x8(%rsp), %r12
subq %rbx, %r12
movq %rbx, %rdi
movq %r12, %rsi
leaq 0x15d2(%rip), %rdx      # 0x6042
xorl %eax, %eax
callq 0x20e0
cltq
cmpq %rax, %r12
jbe 0x4aa1
addq %rax, %rbx
addq $0x28, %r14
movq 0x70(%r15), %rax
movq 0x80(%r15), %rcx
leaq (%rcx,%rcx,4), %rcx
leaq (%rax,%rcx,8), %rax
addq $-0x28, %rax
jmp 0x4a0e
testb $0x1, %bpl
je 0x4c61
decq %rbx
movq 0x8(%rsp), %r12
subq %rbx, %r12
movq %rbx, %rdi
movq %r12, %rsi
leaq 0x1599(%rip), %rdx      # 0x605c
xorl %eax, %eax
callq 0x20e0
cltq
cmpq %rax, %r12
jbe 0x4c61
addq %rax, %rbx
movq 0x70(%r15), %r14
movq 0x80(%r15), %rax
leaq (%rax,%rax,4), %rax
leaq (%r14,%rax,8), %rax
addq $-0x28, %rax
movl $0x7, %ecx
cmpq %rax, %r14
ja 0x4c2e
leaq 0x1563(%rip), %r13      # 0x6067
movq 0x8(%rsp), %r12
subq %rbx, %r12
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
xorl %eax, %eax
callq 0x20e0
cltq
cmpq %rax, %r12
jbe 0x4c29
addq %rax, %rbx
cmpq $0x0, 0x28(%r15)
setne %bpl
movl $0xa, %ecx
je 0x4bd0
movq 0x8(%rsp), %r12
subq %rbx, %r12
movq 0x20(%r14), %rax
movq (%rax), %rcx
movq %rbx, %rdi
movq %r12, %rsi
leaq 0x150f(%rip), %rdx      # 0x6069
xorl %eax, %eax
callq 0x20e0
cltq
cmpq %rax, %r12
jbe 0x4bbd
movl $0x1, %r13d
movq %rbx, %rcx
movq %rax, %rbx
addq %rcx, %rbx
cmpq 0x28(%r15), %r13
setb %bpl
jae 0x4bc4
movq 0x8(%rsp), %r12
subq %rbx, %r12
movq 0x20(%r14), %rax
movq (%rax,%r13,8), %rcx
movq %rbx, %rdi
movq %r12, %rsi
leaq 0x14cb(%rip), %rdx      # 0x6069
xorl %eax, %eax
callq 0x20e0
cltq
incq %r13
cmpq %rax, %r12
ja 0x4b6e
movl $0x2, %ecx
leaq 0x14ac(%rip), %r13      # 0x6067
jmp 0x4bd0
movl $0x2, %ecx
jmp 0x4bd0
leaq 0x149c(%rip), %r13      # 0x6067
movl $0xa, %ecx
testb %bpl, %bpl
jne 0x4c2e
decq %rbx
movq 0x8(%rsp), %r12
subq %rbx, %r12
movq %rbx, %rdi
movq %r12, %rsi
leaq 0x1481(%rip), %rdx      # 0x606e
xorl %eax, %eax
callq 0x20e0
cltq
cmpq %rax, %r12
jbe 0x4c29
addq %rax, %rbx
addq $0x28, %r14
movq 0x70(%r15), %rax
movq 0x80(%r15), %rcx
leaq (%rcx,%rcx,4), %rcx
leaq (%rax,%rcx,8), %rax
addq $-0x28, %rax
cmpq %rax, %r14
jbe 0x4b04
movl $0x7, %ecx
jmp 0x4c2e
movl $0x2, %ecx
cmpl $0x2, %ecx
je 0x4c61
cmpl $0x7, %ecx
jne 0x4c87
decq %rbx
movq 0x8(%rsp), %r14
subq %rbx, %r14
xorl %r12d, %r12d
movq %rbx, %rdi
movq %r14, %rsi
leaq 0x141e(%rip), %rdx      # 0x6071
xorl %eax, %eax
callq 0x20e0
cltq
cmpq %rax, %r14
ja 0x4c87
movq 0x40(%r15), %rdx
addq %rdx, %rdx
movq %rdx, 0x40(%r15)
movq %r15, %rdi
movq 0x10(%rsp), %rsi
callq 0x3030
testl %eax, %eax
je 0x4988
movl $0x1, %r12d
movl %r12d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)