// File: /workspace/github_c_makefile_star2_O1/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 %r12
pushq %rbx
subq $0x200, %rsp            # imm = 0x200
movl %edi, %ebx
movslq 0x8974f(%rip), %rax     # 0x9dee4
leaq 0x897a4(%rip), %r15     # 0x9df40
testq %rax, %rax
jle 0x147c5
shlq $0x3, %rax
xorl %ecx, %ecx
xorl %ebp, %ebp
movq (%rcx,%r15), %rdx
cmpl %ebx, (%rdx)
jne 0x147ba
movslq %ebp, %rsi
incl %ebp
movq %rdx, (%rsp,%rsi,8)
addq $0x8, %rcx
cmpq %rcx, %rax
jne 0x147a9
jmp 0x147c7
xorl %ebp, %ebp
movslq %ebp, %r14
leaq 0x256(%rip), %rcx       # 0x14a27
movq %rsp, %rdi
movl $0x8, %edx
movq %r14, %rsi
callq 0x3620
testl %r14d, %r14d
jle 0x14811
movl %ebp, %r14d
xorl %r12d, %r12d
movq (%rsp,%r12,8), %rax
cmpl $0x1, 0x8(%rax)
movl $0x2, %edi
sbbl $0x0, %edi
movq 0x10(%rax), %rsi
movslq 0xc(%rax), %rdx
callq 0x3120
incq %r12
cmpq %r12, %r14
jne 0x147ec
movl 0x896cd(%rip), %esi     # 0x9dee4
testl %esi, %esi
jle 0x14860
xorl %eax, %eax
movl 0x896bd(%rip), %edx     # 0x9dee0
leaq 0x94b6(%rip), %rcx      # 0x1dce0
movslq %eax, %r8
movq (%r15,%r8,8), %rdi
cmpl %ebx, (%rdi)
jne 0x1485a
movslq %esi, %r9
decl %esi
movl %esi, 0x896a4(%rip)     # 0x9dee4
movq -0x8(%r15,%r9,8), %r9
movq %r9, (%r15,%r8,8)
movslq %edx, %r8
incl %edx
movl %edx, 0x8968c(%rip)     # 0x9dee0
movq %rdi, (%rcx,%r8,8)
jmp 0x1485c
incl %eax
cmpl %esi, %eax
jl 0x1482a
cmpl $0x1, %ebp
jne 0x1487e
leaq 0x896a4(%rip), %rdi     # 0x9df10
callq 0x3040
testl %eax, %eax
je 0x14890
leaq 0x12dd(%rip), %rdi      # 0x15b59
jmp 0x148a7
jle 0x14890
leaq 0x89689(%rip), %rdi     # 0x9df10
callq 0x3110
testl %eax, %eax
jne 0x148a0
addq $0x200, %rsp            # imm = 0x200
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1207(%rip), %rdi      # 0x15aae
xorl %eax, %eax
callq 0x5c85