// File: /workspace/github_c_makefile_star2_O2/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
leaq 0x8(%rsp), %rsi
leaq 0x10(%rsp), %rdx
pushq $0x8
popq %rcx
callq 0x3f80
pushq $-0x1
popq %r14
testl %eax, %eax
jne 0x3a49
movq 0x10(%rsp), %rax
xorl %ecx, %ecx
cmpq %rax, %rcx
je 0x3926
leaq 0x1(%rcx), %rdx
movq %rdx, %rsi
cmpq %rax, %rsi
jae 0x3921
movq 0x8(%rsp), %r8
movq (%r8,%rcx,8), %rdi
movl (%rdi), %r10d
movq (%r8,%rsi,8), %r9
cmpl (%r9), %r10d
jbe 0x391c
movq %r9, (%r8,%rcx,8)
movq 0x8(%rsp), %r8
movq %rdi, (%r8,%rsi,8)
incq %rsi
jmp 0x38f5
movq %rdx, %rcx
jmp 0x38e9
pushq $0x1
popq %rsi
xorl %edi, %edi
callq 0x3c9c
testq %rax, %rax
je 0x3a3f
movq %rax, %r15
movq %rbx, 0x18(%rsp)
xorl %r12d, %r12d
leaq 0x20(%rsp), %r13
leaq 0x7ae0(%rip), %rbp      # 0xb430
movq %rsp, %rbx
cmpq 0x10(%rsp), %r12
jae 0x3a06
movq 0x8(%rsp), %rax
movq (%rax,%r12,8), %r14
movq 0x18(%r14), %rdi
leaq 0x28(%rsp), %rsi
callq 0x844c
movq 0x20(%r14), %rdi
movq %r13, %rsi
callq 0x844c
movl (%r14), %edi
callq 0x3ad0
movq 0x8(%r14), %rcx
movsd 0x10(%r14), %xmm0
movsd 0x7a6d(%rip), %xmm4     # 0xb408
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 0x6124
cmpl $-0x1, %eax
je 0x3a33
movq (%rsp), %r14
movq %r14, %rdi
callq 0x2150
movq %r15, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x3ac8
movl %eax, %r14d
movq (%rsp), %rdi
callq 0x2040
incq %r12
testl %r14d, %r14d
je 0x3953
jmp 0x3a33
leaq 0x7a49(%rip), %rsi      # 0xb456
pushq $0x1
popq %rdx
movq %r15, %rdi
callq 0x3ac8
testl %eax, %eax
jne 0x3a33
movq %rsp, %rsi
leaq 0x30(%rsp), %rdx
pushq $0x1
popq %rcx
movq %r15, %rdi
callq 0x3f30
testl %eax, %eax
je 0x3a5b
movq %r15, %rdi
callq 0x3d33
pushq $-0x1
popq %r14
movq 0x8(%rsp), %rdi
callq 0x2040
movl %r14d, %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 (%rsp), %rdx
leaq 0x7651(%rip), %rsi      # 0xb0bf
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x8ccc
movq (%rsp), %rdi
callq 0x2040
movq 0x8(%rsp), %rdi
cmpq 0x10(%rsp), %r14
jae 0x3ab0
movq (%rdi,%r14,8), %rbx
movq 0x20(%rbx), %rdi
callq 0x8892
movq 0x18(%rbx), %rdi
callq 0x8892
movq %rbx, %rdi
callq 0x2040
incq %r14
jmp 0x3a81
callq 0x2040
movq 0x20(%r15), %rdi
xorl %r14d, %r14d
pushq $0x8
popq %rdx
xorl %esi, %esi
callq 0x3d01
jmp 0x3a49