// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x230, %rsp            # imm = 0x230
movl %edi, -0x4(%rbp)
movl $0x0, -0xc(%rbp)
jmp 0x22467
movl $0x0, -0x8(%rbp)
movl -0x8(%rbp), %eax
cmpl 0x8da6d(%rip), %eax     # 0xafee4
jge 0x224c6
movslq -0x8(%rbp), %rcx
leaq 0x8dabc(%rip), %rax     # 0xaff40
movq (%rax,%rcx,8), %rax
movq %rax, -0x218(%rbp)
movl -0x4(%rbp), %eax
movq -0x218(%rbp), %rcx
cmpl (%rcx), %eax
jne 0x224b9
movq -0x218(%rbp), %rcx
movl -0xc(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0xc(%rbp)
cltq
movq %rcx, -0x210(%rbp,%rax,8)
jmp 0x224bb
movl -0x8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8(%rbp)
jmp 0x2246e
jmp 0x224c8
jmp 0x224ca
leaq -0x210(%rbp), %rdi
movslq -0xc(%rbp), %rsi
movl $0x8, %edx
leaq 0x2cf(%rip), %rcx       # 0x227b0
callq 0x3600
movl $0x0, -0x8(%rbp)
movl -0x8(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x2254b
movslq -0x8(%rbp), %rax
movq -0x210(%rbp,%rax,8), %rax
movq %rax, -0x220(%rbp)
movq -0x220(%rbp), %rax
movl 0x8(%rax), %ecx
movl $0x1, %edi
movl $0x2, %eax
cmpl $0x0, %ecx
cmovnel %eax, %edi
movq -0x220(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x220(%rbp), %rax
movslq 0xc(%rax), %rdx
callq 0x3120
movl %eax, -0x10(%rbp)
movl -0x8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8(%rbp)
jmp 0x224ed
movl $0x0, -0x8(%rbp)
movl -0x8(%rbp), %eax
cmpl 0x8d989(%rip), %eax     # 0xafee4
jge 0x225e7
movslq -0x8(%rbp), %rcx
leaq 0x8d9d4(%rip), %rax     # 0xaff40
movq (%rax,%rcx,8), %rax
movq %rax, -0x228(%rbp)
movq -0x228(%rbp), %rax
movl (%rax), %eax
cmpl -0x4(%rbp), %eax
jne 0x225d9
movl 0x8d959(%rip), %eax     # 0xafee4
addl $-0x1, %eax
movl %eax, 0x8d950(%rip)     # 0xafee4
movslq %eax, %rcx
leaq 0x8d9a2(%rip), %rax     # 0xaff40
movq (%rax,%rcx,8), %rdx
movslq -0x8(%rbp), %rcx
leaq 0x8d993(%rip), %rax     # 0xaff40
movq %rdx, (%rax,%rcx,8)
movq -0x228(%rbp), %rdx
movl 0x8d922(%rip), %eax     # 0xafee0
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, 0x8d917(%rip)     # 0xafee0
movslq %eax, %rcx
leaq 0xd70d(%rip), %rax      # 0x2fce0
movq %rdx, (%rax,%rcx,8)
jmp 0x225e2
movl -0x8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8(%rbp)
jmp 0x22552
movl $0x1, %eax
cmpl -0xc(%rbp), %eax
jne 0x225ff
leaq 0x8d918(%rip), %rdi     # 0xaff10
callq 0x6da0
jmp 0x22613
cmpl $0x1, -0xc(%rbp)
jle 0x22611
leaq 0x8d904(%rip), %rdi     # 0xaff10
callq 0x6a30
jmp 0x22613
addq $0x230, %rsp            # imm = 0x230
popq %rbp
retq
nopl (%rax)