// File: /workspace/github_c_makefile_star2_O1/Tarsnap[P]kivaloo/perf/perfstats.c
// Function: flush
static int
flush(struct perfstats * P)
{
	STR logline;
	struct reqstats * r;
	struct reqstats ** rs;
	double p50, p99;
	char * s;
	size_t linelen;
	size_t nrs;
	size_t i, j;

	/* Export statistics from the elastic array. */
	if (reqstatslist_exportdup(P->stats, &rs, &nrs))
		goto err0;

	/* Sort the statistics by request type. */
	for (i = 0; i < nrs; i++) {
		for (j = i + 1; j < nrs; j++) {
			if (rs[i]->reqtype > rs[j]->reqtype) {
				r = rs[i];
				rs[i] = rs[j];
				rs[j] = r;
			}
		}
	}

	/* Gather statistics for each request type. */
	if ((logline = str_init(0)) == NULL)
		goto err1;
	for (i = 0; i < nrs; i++) {
		r = rs[i];
		onlinequantile_get(r->p50, &p50);
		onlinequantile_get(r->p99, &p99);
		if (asprintf(&s, "|%s|%06zu|%08.3f|%08.3f|%08.3f|%08.3f",
		    reqtypes_lookup(r->reqtype), r->N, 1000 * r->mu,
		        1000 * p50, 1000 * p99, 1000 * r->p100) == -1)
			goto err2;
		if (str_append(logline, s, strlen(s)))
			goto err3;
		free(s);
	}
	if (str_append(logline, "\0", 1))
		goto err2;
	if (str_export(logline, &s, &linelen))
		goto err2;
	logging_printf(P->L, "%s", s);
	free(s);

	/* Free the statistics and empty the elastic array. */
	for (i = 0; i < nrs; i++) {
		r = rs[i];
		onlinequantile_free(r->p99);
		onlinequantile_free(r->p50);
		free(r);
	}
	free(rs);
	reqstatslist_resize(P->stats, 0);

	/* Success! */
	return (0);

err3:
	free(s);
err2:
	str_free(logline);
err1:
	free(rs);
err0:
	/* Failure! */
	return (-1);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
movq 0x20(%rdi), %rdi
movq %rsp, %rsi
leaq 0x8(%rsp), %rdx
movl $0x8, %ecx
callq 0x46a0
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x4102
movq 0x8(%rsp), %rax
testq %rax, %rax
je 0x3fc3
xorl %ecx, %ecx
movq %rcx, %rdx
incq %rcx
cmpq %rax, %rcx
jae 0x3fbe
movq %rcx, %rsi
movq (%rsp), %r8
movq (%r8,%rdx,8), %rdi
movl (%rdi), %r10d
movq (%r8,%rsi,8), %r9
cmpl (%r9), %r10d
jbe 0x3fb6
movq %r9, (%r8,%rdx,8)
movq (%rsp), %r8
movq %rdi, (%r8,%rsi,8)
incq %rsi
cmpq %rax, %rsi
jb 0x3f96
cmpq %rax, %rcx
jne 0x3f88
movl $0x1, %esi
xorl %edi, %edi
callq 0x4394
testq %rax, %rax
je 0x40f9
movq %rax, %r15
movq %rbx, 0x18(%rsp)
cmpq $0x0, 0x8(%rsp)
je 0x40b3
xorl %r12d, %r12d
leaq 0x20(%rsp), %r13
leaq 0x8495(%rip), %rbp      # 0xc490
leaq 0x10(%rsp), %rbx
movq (%rsp), %rax
movq (%rax,%r12,8), %r14
movq 0x18(%r14), %rdi
leaq 0x28(%rsp), %rsi
callq 0x9193
movq 0x20(%r14), %rdi
movq %r13, %rsi
callq 0x9193
movl (%r14), %edi
callq 0x4194
movq 0x8(%r14), %rcx
movsd 0x10(%r14), %xmm0
movsd 0x842c(%rip), %xmm4     # 0xc468
mulsd %xmm4, %xmm0
movsd 0x28(%rsp), %xmm1
mulsd %xmm4, %xmm1
movsd 0x20(%rsp), %xmm2
mulsd %xmm4, %xmm2
movsd 0x28(%r14), %xmm3
mulsd %xmm4, %xmm3
movq %rbx, %rdi
movq %rbp, %rsi
movq %rax, %rdx
movb $0x4, %al
callq 0x6ed4
cmpl $-0x1, %eax
je 0x40eb
movq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x2150
movl $0x1, %ecx
movq %r15, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x450c
movl %eax, %r14d
movq 0x10(%rsp), %rdi
callq 0x2040
testl %r14d, %r14d
jne 0x40eb
incq %r12
cmpq 0x8(%rsp), %r12
jb 0x4000
leaq 0x83fc(%rip), %rsi      # 0xc4b6
movl $0x1, %edx
movl $0x1, %ecx
movq %r15, %rdi
callq 0x450c
testl %eax, %eax
jne 0x40eb
leaq 0x10(%rsp), %rsi
leaq 0x30(%rsp), %rdx
movl $0x1, %ecx
movq %r15, %rdi
callq 0x4651
testl %eax, %eax
je 0x4114
movq %r15, %rdi
callq 0x4445
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movq (%rsp), %rdi
callq 0x2040
movl %r12d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%rsp), %r15
movq (%r15), %rdi
movq 0x10(%rsp), %rdx
leaq 0x7f97(%rip), %rsi      # 0xc0bf
xorl %r12d, %r12d
xorl %eax, %eax
callq 0x9bb7
movq 0x10(%rsp), %rdi
callq 0x2040
cmpq $0x0, 0x8(%rsp)
je 0x4173
xorl %r14d, %r14d
movq (%rsp), %rax
movq (%rax,%r14,8), %rbx
movq 0x20(%rbx), %rdi
callq 0x9757
movq 0x18(%rbx), %rdi
callq 0x9757
movq %rbx, %rdi
callq 0x2040
incq %r14
cmpq 0x8(%rsp), %r14
jb 0x4147
movq (%rsp), %rdi
callq 0x2040
movq 0x20(%r15), %rdi
movl $0x8, %edx
xorl %esi, %esi
callq 0x4411
jmp 0x4102
nopl (%rax)