// File: /workspace/github_c_makefile_star2_O3/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 %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x200, %rsp            # imm = 0x200
movl %edi, %ebx
movslq 0x89ecb(%rip), %rax     # 0x9dee4
leaq 0x89f20(%rip), %r15     # 0x9df40
testq %rax, %rax
jle 0x14095
shlq $0x3, %rax
xorl %ecx, %ecx
xorl %r12d, %r12d
movq (%rcx,%r15), %rdx
cmpl %ebx, (%rdx)
jne 0x14040
movslq %r12d, %rsi
incl %r12d
movq %rdx, (%rsp,%rsi,8)
addq $0x8, %rcx
cmpq %rcx, %rax
jne 0x1402e
movslq %r12d, %r14
leaq 0x273(%rip), %rcx       # 0x142c6
movq %rsp, %rdi
movl $0x8, %edx
movq %r14, %rsi
callq 0x3620
testl %r14d, %r14d
jle 0x140ae
movl %r12d, %r14d
xorl %r13d, %r13d
movq (%rsp,%r13,8), %rax
cmpl $0x1, 0x8(%rax)
movl $0x2, %edi
sbbl $0x0, %edi
movq 0x10(%rax), %rsi
movslq 0xc(%rax), %rdx
callq 0x3120
incq %r13
cmpq %r13, %r14
jne 0x1406e
jmp 0x140ae
leaq 0x22a(%rip), %rcx       # 0x142c6
xorl %r12d, %r12d
movq %rsp, %rdi
movl $0x8, %edx
xorl %esi, %esi
callq 0x3620
movl 0x89e30(%rip), %esi     # 0x9dee4
testl %esi, %esi
jle 0x140fd
xorl %eax, %eax
movl 0x89e20(%rip), %edx     # 0x9dee0
leaq 0x9c19(%rip), %rcx      # 0x1dce0
movslq %eax, %r8
movq (%r15,%r8,8), %rdi
cmpl %ebx, (%rdi)
jne 0x140f7
movslq %esi, %r9
decl %esi
movl %esi, 0x89e07(%rip)     # 0x9dee4
movq -0x8(%r15,%r9,8), %r9
movq %r9, (%r15,%r8,8)
movslq %edx, %r8
incl %edx
movl %edx, 0x89def(%rip)     # 0x9dee0
movq %rdi, (%rcx,%r8,8)
jmp 0x140f9
incl %eax
cmpl %esi, %eax
jl 0x140c7
cmpl $0x1, %r12d
jne 0x1411c
leaq 0x89e06(%rip), %rdi     # 0x9df10
callq 0x3040
testl %eax, %eax
je 0x1412e
leaq 0x1a27(%rip), %rdi      # 0x15b41
jmp 0x14146
jle 0x1412e
leaq 0x89deb(%rip), %rdi     # 0x9df10
callq 0x3110
testl %eax, %eax
jne 0x1413f
addq $0x200, %rsp            # imm = 0x200
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
leaq 0x1950(%rip), %rdi      # 0x15a96
xorl %eax, %eax
callq 0x5b09