// File: /workspace/github_c_makefile_star2_O2/deplinenoise[P]tundra/src/TerminalIo.cpp
// Function: t2::FlushOutputQueue(int)
static void FlushOutputQueue(int job_id)
{
	int i;
	int count = 0;
	int result;
	LineBuffer *buffers[kLineBufCount];

	TTY_PRINTF(("linebuf: flush queue for job %d\n", job_id));

	for (i = 0; i < s_QueuedLinebufCount; ++i)
	{
		LineBuffer *b = s_QueuedLinebufs[i];
		if (job_id == b->m_JobId) {
			buffers[count++] = b;
		}
	}

	TTY_PRINTF(("found %d lines (out of %d total) buffered for job %d\n", count, s_QueuedLinebufCount, job_id));

	/* Flush these buffers to the TTY */

	qsort(&buffers[0], count, sizeof(buffers[0]), CompareBuffers);

	for (i = 0; i < count; ++i)
	{
		LineBuffer *b = buffers[i];
		result = write(b->m_IsStderr ? STDERR_FILENO : STDOUT_FILENO, b->m_Data, b->m_Len);
		(void) result; /* tty disappeared; not fatal */
	}

	/* Compact the queue, removing the buffers from this job. */
	for (i = 0; i < s_QueuedLinebufCount; )
	{
		LineBuffer *b = s_QueuedLinebufs[i];

		if (b->m_JobId == job_id)
		{
			/* Move the last pointer to this slot, decrement the queued count.
			 *
			 * This works even for the pathological case of a single queued
			 * value, it will just overwrite itself at index 0.
			 */
			s_QueuedLinebufs[i] = s_QueuedLinebufs[--s_QueuedLinebufCount];
			s_FreeLinebufs[s_FreeLinebufCount++] = b;

#if TTY_DEBUG
			int k, j;
			for (j = 0; j < s_QueuedLinebufCount; ++j)
			{
				for (k = 0; k < s_FreeLinebufCount; ++k)
				{
					assert(s_QueuedLinebufs[j] != s_FreeLinebufs[k]);
				}
			}
#endif
		}
		else
			++i;
	}

	if (1 == count)
		CondSignal(&s_CanPrint);
	else if (count > 1)
		CondBroadcast(&s_CanPrint);

#if TTY_DEBUG
	TTY_PRINTF(("%d records still queued: [", s_QueuedLinebufCount));
	for (i = 0; i < s_QueuedLinebufCount; ++i) {
		TTY_PRINTF(("%d ", s_QueuedLinebufs[i]->m_JobId));
	}
	TTY_PRINTF(("]\n"));
#endif
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x208, %rsp            # imm = 0x208
movl 0x8b81a(%rip), %eax     # 0x9dee4
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
movl %edi, %ebx
shlq $0x3, %rax
leaq 0x8b862(%rip), %r15     # 0x9df40
xorl %ebp, %ebp
movslq %ebp, %r14
cmpq %rcx, %rax
je 0x126fe
movq (%rcx,%r15), %rdx
cmpl %ebx, (%rdx)
jne 0x126f8
leal 0x1(%r14), %ebp
movq %rdx, (%rsp,%r14,8)
addq $0x8, %rcx
jmp 0x126e0
leaq 0x206(%rip), %rcx       # 0x1290b
movq %rsp, %rdi
pushq $0x8
popq %rdx
movq %r14, %rsi
callq 0x3620
xorl %r12d, %r12d
testl %r14d, %r14d
movl $0x0, %r14d
cmovgl %ebp, %r14d
pushq $0x2
popq %r13
cmpq %r12, %r14
je 0x1274c
movq (%rsp,%r12,8), %rax
cmpl $0x1, 0x8(%rax)
movl %r13d, %edi
sbbl $0x0, %edi
movq 0x10(%rax), %rsi
movslq 0xc(%rax), %rdx
callq 0x3120
incq %r12
jmp 0x12727
movl 0x8b792(%rip), %esi     # 0x9dee4
xorl %eax, %eax
movl 0x8b786(%rip), %edx     # 0x9dee0
leaq 0xb57f(%rip), %rcx      # 0x1dce0
movslq %eax, %rdi
leaq (%r15,%rdi,8), %rdi
cmpl %esi, %eax
jge 0x127a0
movq (%rdi), %r8
cmpl %ebx, (%r8)
je 0x1277c
incl %eax
addq $0x8, %rdi
jmp 0x12768
movslq %esi, %r9
decl %esi
movl %esi, 0x8b75d(%rip)     # 0x9dee4
movq -0x8(%r15,%r9,8), %r9
movq %r9, (%rdi)
movslq %edx, %rdi
incl %edx
movl %edx, 0x8b746(%rip)     # 0x9dee0
movq %r8, (%rcx,%rdi,8)
jmp 0x12761
cmpl $0x1, %ebp
jne 0x127b3
leaq 0x8b764(%rip), %rdi     # 0x9df10
callq 0x5563
jmp 0x127c1
jle 0x127c1
leaq 0x8b754(%rip), %rdi     # 0x9df10
callq 0x54ce
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq