// File: /workspace/github_c_makefile_star2_O3/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 0x46a7
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x4114
movq 0x8(%rsp), %rax
testq %rax, %rax
je 0x3fd5
xorl %ecx, %ecx
movq %rcx, %rdx
incq %rcx
cmpq %rax, %rcx
jae 0x3fd0
movq %rcx, %rsi
movq (%rsp), %r8
movq (%r8,%rdx,8), %rdi
movl (%rdi), %r10d
movq (%r8,%rsi,8), %r9
cmpl (%r9), %r10d
jbe 0x3fc8
movq %r9, (%r8,%rdx,8)
movq (%rsp), %r8
movq %rdi, (%r8,%rsi,8)
incq %rsi
cmpq %rax, %rsi
jb 0x3fa8
cmpq %rax, %rcx
jne 0x3f9a
movl $0x1, %esi
xorl %edi, %edi
callq 0x43a4
testq %rax, %rax
je 0x410b
movq %rax, %r15
movq %rbx, 0x18(%rsp)
cmpq $0x0, 0x8(%rsp)
je 0x40c5
xorl %r12d, %r12d
leaq 0x20(%rsp), %r13
leaq 0x9423(%rip), %rbp      # 0xd430
leaq 0x10(%rsp), %rbx
movq (%rsp), %rax
movq (%rax,%r12,8), %r14
movq 0x18(%r14), %rdi
leaq 0x28(%rsp), %rsi
callq 0x9222
movq 0x20(%r14), %rdi
movq %r13, %rsi
callq 0x9222
movl (%r14), %edi
callq 0x41a4
movq 0x8(%r14), %rcx
movsd 0x10(%r14), %xmm0
movsd 0x93ba(%rip), %xmm4     # 0xd408
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 0x6f2c
cmpl $-0x1, %eax
je 0x40fd
movq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x2150
movl $0x1, %ecx
movq %r15, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x4513
movl %eax, %r14d
movq 0x10(%rsp), %rdi
callq 0x2040
testl %r14d, %r14d
jne 0x40fd
incq %r12
cmpq 0x8(%rsp), %r12
jb 0x4012
leaq 0x938a(%rip), %rsi      # 0xd456
movl $0x1, %edx
movl $0x1, %ecx
movq %r15, %rdi
callq 0x4513
testl %eax, %eax
jne 0x40fd
leaq 0x10(%rsp), %rsi
leaq 0x30(%rsp), %rdx
movl $0x1, %ecx
movq %r15, %rdi
callq 0x4658
testl %eax, %eax
je 0x4126
movq %r15, %rdi
callq 0x444c
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 0x8f85(%rip), %rsi      # 0xd0bf
xorl %r12d, %r12d
xorl %eax, %eax
callq 0x9c13
movq 0x10(%rsp), %rdi
callq 0x2040
cmpq $0x0, 0x8(%rsp)
je 0x4185
xorl %r14d, %r14d
movq (%rsp), %rax
movq (%rax,%r14,8), %rbx
movq 0x20(%rbx), %rdi
callq 0x97b3
movq 0x18(%rbx), %rdi
callq 0x97b3
movq %rbx, %rdi
callq 0x2040
incq %r14
cmpq 0x8(%rsp), %r14
jb 0x4159
movq (%rsp), %rdi
callq 0x2040
movq 0x20(%r15), %rdi
movl $0x8, %edx
xorl %esi, %esi
callq 0x4418
jmp 0x4114
nop