// File: /workspace/github_c_makefile_star2_O0/Tarsnap[P]kivaloo/liball/../lib/datastruct/onlinequantile.c
// Function: onlinequantile_add
int
onlinequantile_add(struct onlinequantile * Q, double x)
{
	size_t i;
	double r;

	/* Figure out where the quantile falls after expansion. */
	hazenquantile(Q->N + 1, Q->q, &i, &r);

	/* Which heap needs to grow? */
	if (i + 1 > Q->N_smaller) {
		/* We need to expand the "smaller than quantile" heap. */

		/* Sanity-check. */
		assert(Q->N_smaller == i);

		if (x <= Q->larger_min) {
			/* Add to "smaller" heap. */
			if (doubleheap_add(Q->smaller, -x))
				goto err0;
			Q->N_smaller += 1;

			/* Update smaller_max if necessary. */
			if (Q->smaller_max < x)
				Q->smaller_max = x;
		} else {
			/*
			 * This value is going into the "larger" heap, but we
			 * want the "smaller" heap to be growing; so we need
			 * to take the least element from "larger" and move it
			 * into "smaller".
			 */

			/* Sanity-check. */
			assert(Q->N_smaller < Q->N);

			/* Add element to "smaller" heap. */
			if (doubleheap_add(Q->smaller, -Q->larger_min))
				goto err0;
			Q->N_smaller += 1;
			Q->smaller_max = Q->larger_min;

			/*
			 * Now replace the value we removed from the "larger"
			 * heap with our new value; and figure out what the
			 * new minimum value in that heap is.
			 */
			doubleheap_setmin(Q->larger, x);
			doubleheap_getmin(Q->larger, &Q->larger_min);
		}
	} else {
		/* We need to expand the "larger than quantile" heap. */

		/* Sanity-check. */
		assert(Q->N_smaller - 1 == i);

		/* Where does the new value land? */
		if (x >= Q->smaller_max) {
			/* Add to "larger" heap. */
			if (doubleheap_add(Q->larger, x))
				goto err0;

			/* Update larger_min if necessary. */
			if (Q->larger_min > x)
				Q->larger_min = x;
		} else {
			/*
			 * This value is going into the "smaller" heap, but we
			 * want the "larger" heap to be growing; so we need
			 * to take the greatest element from "smaller" and move
			 * it into "larger".
			 */

			/* Sanity-check. */
			assert(Q->N_smaller > 0);

			/* Add element to "larger" heap. */
			if (doubleheap_add(Q->larger, Q->smaller_max))
				goto err0;
			Q->larger_min = Q->smaller_max;

			/*
			 * Now replace the value we removed from the "smaller"
			 * heap with our new value; and figure out what the
			 * new maximum value in that heap is.
			 */
			doubleheap_setmin(Q->smaller, -x);
			doubleheap_getmin(Q->smaller, &Q->smaller_max);
			Q->smaller_max = - Q->smaller_max;
		}
	}

	/* We have added a new data point (somewhere). */
	Q->N += 1;

	/* Success! */
	return (0);

err0:
	/* Failure! */
	return (-1);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movsd %xmm0, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
addq $0x1, %rdi
movq -0x10(%rbp), %rax
movsd 0x30(%rax), %xmm0
leaq -0x20(%rbp), %rsi
leaq -0x28(%rbp), %rdx
callq 0x21d0
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq -0x10(%rbp), %rcx
cmpq 0x28(%rcx), %rax
jbe 0x25cc
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
cmpq -0x20(%rbp), %rax
jne 0x2479
jmp 0x2498
leaq 0x1f0f(%rip), %rdi      # 0x438f
leaq 0x1e97(%rip), %rsi      # 0x431e
movl $0x7a, %edx
leaq 0x1f0e(%rip), %rcx      # 0x43a1
callq 0x10d0
movsd -0x18(%rbp), %xmm1
movq -0x10(%rbp), %rax
movsd 0x10(%rax), %xmm0
ucomisd %xmm1, %xmm0
jb 0x2516
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movsd -0x18(%rbp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
callq 0x2a00
cmpl $0x0, %eax
je 0x24df
jmp 0x272c
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x28(%rax)
movq -0x10(%rbp), %rax
movsd 0x18(%rax), %xmm1
movsd -0x18(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x2511
movsd -0x18(%rbp), %xmm0
movq -0x10(%rbp), %rax
movsd %xmm0, 0x18(%rax)
jmp 0x25c7
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x20(%rcx), %rax
jae 0x252a
jmp 0x2549
leaq 0x1e4a(%rip), %rdi      # 0x437b
leaq 0x1de6(%rip), %rsi      # 0x431e
movl $0x8e, %edx
leaq 0x1e5d(%rip), %rcx      # 0x43a1
callq 0x10d0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x10(%rbp), %rax
movsd 0x10(%rax), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
callq 0x2a00
cmpl $0x0, %eax
je 0x2580
jmp 0x272c
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x28(%rax)
movq -0x10(%rbp), %rax
movsd 0x10(%rax), %xmm0
movq -0x10(%rbp), %rax
movsd %xmm0, 0x18(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movsd -0x18(%rbp), %xmm0
callq 0x2b80
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rsi
addq $0x10, %rsi
callq 0x2b30
jmp 0x2713
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
subq $0x1, %rax
cmpq -0x20(%rbp), %rax
jne 0x25e0
jmp 0x25ff
leaq 0x1d21(%rip), %rdi      # 0x4308
leaq 0x1d30(%rip), %rsi      # 0x431e
movl $0xa2, %edx
leaq 0x1da7(%rip), %rcx      # 0x43a1
callq 0x10d0
movsd -0x18(%rbp), %xmm0
movq -0x10(%rbp), %rax
ucomisd 0x18(%rax), %xmm0
jb 0x264d
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movsd -0x18(%rbp), %xmm0
callq 0x2a00
cmpl $0x0, %eax
je 0x262a
jmp 0x272c
movq -0x10(%rbp), %rax
movsd 0x10(%rax), %xmm0
ucomisd -0x18(%rbp), %xmm0
jbe 0x2648
movsd -0x18(%rbp), %xmm0
movq -0x10(%rbp), %rax
movsd %xmm0, 0x10(%rax)
jmp 0x2711
movq -0x10(%rbp), %rax
cmpq $0x0, 0x28(%rax)
jbe 0x265a
jmp 0x2679
leaq 0x1d78(%rip), %rdi      # 0x43d9
leaq 0x1cb6(%rip), %rsi      # 0x431e
movl $0xb6, %edx
leaq 0x1d2d(%rip), %rcx      # 0x43a1
callq 0x10d0
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movsd 0x18(%rax), %xmm0
callq 0x2a00
cmpl $0x0, %eax
je 0x2698
jmp 0x272c
movq -0x10(%rbp), %rax
movsd 0x18(%rax), %xmm0
movq -0x10(%rbp), %rax
movsd %xmm0, 0x10(%rax)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movsd -0x18(%rbp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
callq 0x2b80
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x10(%rbp), %rsi
addq $0x18, %rsi
callq 0x2b30
movq -0x10(%rbp), %rax
movsd 0x18(%rax), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movq -0x10(%rbp), %rax
movsd %xmm0, 0x18(%rax)
jmp 0x2713
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x20(%rax)
movl $0x0, -0x4(%rbp)
jmp 0x2733
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax)