// File: /workspace/github_c_makefile_star2_O3/deplinenoise[P]tundra/src/TerminalIo.cpp
// Function: t2::TerminalIoEmit(int, int, int, char const*, int)
void TerminalIoEmit(int job_id, int is_stderr, int sort_key, const char *data, int len)
{
	int result;
	LineBuffer *buf = NULL;

  MutexLock(&s_LineLock);

	while (len > 0)
	{
		/* Wait for a line buffer to become available, or for the tty to become free. */
		for (;;)
		{
			/* If we already have the tty, or we can get it: break */
			if (-1 == s_PrintingJobIndex || job_id == s_PrintingJobIndex)
				break;

			/* If we can allocate a buffer: break */

			if (NULL != (buf = AllocLineBuffer()))
				break;

			/* Otherwise wait */
      CondWait(&s_CanPrint, &s_LineLock);
		}

		if (-1 == s_PrintingJobIndex)
		{
			/* Let this job own the output channel */
			TTY_PRINTF(("job %d is taking the tty\n", job_id));
			s_PrintingJobIndex = job_id;

			FlushOutputQueue(job_id);
		}
		else if (job_id != s_PrintingJobIndex)
		{
			assert(buf);
		}

		if (!buf)
		{
			/* This thread owns the TTY, so just print. We don't need to keep the
			 * mutex locked as this job will not finsh and reset the currently
			 * printing job until later. Releasing the mutex now means other
			 * threads can come in and buffer their data. */

			MutexUnlock(&s_LineLock);

			TTY_PRINTF(("copying %d bytes of data from job_id %d, stderr=%d, sort_key %d\n",
						len, job_id, is_stderr, sort_key));

			result = write(is_stderr ? STDERR_FILENO : STDOUT_FILENO, data, len);
			(void) result; /* tty disappeared; not fatal */
			return; /* finish the loop immediately */
		}
		else
		{
			/* We can't print this data as we don't own the TTY so we buffer it. */
			TTY_PRINTF(("buffering %d bytes of data from job_id %d, stderr=%d, sort_key %d\n",
						len, job_id, is_stderr, sort_key));

			/* PERF: Could release mutex around this memcpy, not sure if it's a win. */
			buf->m_JobId = job_id;
			buf->m_SortKey = sort_key;
			buf->m_Len = len > kLineBufSize - 1 ? kLineBufSize - 1 : len;
			buf->m_IsStderr = is_stderr;
			memcpy(buf->m_Data, data, buf->m_Len);
			buf->m_Data[buf->m_Len] = '\0';

			len -= buf->m_Len;
			data += buf->m_Len;

			/* Queue this line for output */
			s_QueuedLinebufs[s_QueuedLinebufCount++] = buf;
		}
	}

	MutexUnlock(&s_LineLock);

	return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %r8d, %ebp
movq %rcx, %rbx
movl %edx, 0x4(%rsp)
movl %esi, (%rsp)
movl %edi, %r12d
leaq 0x8a0dc(%rip), %rdi     # 0x9dee8
callq 0x34b0
testl %eax, %eax
jne 0x13fee
testl %ebp, %ebp
jle 0x13f4b
xorl %r15d, %r15d
leaq 0x8a0e5(%rip), %r13     # 0x9df10
leaq 0x8a0b6(%rip), %r14     # 0x9dee8
movl 0x9540(%rip), %eax      # 0x1d378
cmpl $-0x1, %eax
sete %cl
cmpl %r12d, %eax
sete %al
movl %ecx, %edx
orb %al, %dl
jne 0x13eb8
movslq 0x8a08f(%rip), %rax     # 0x9dee0
movl 0x8a08d(%rip), %ecx     # 0x9dee4
addl %eax, %ecx
cmpl $0x40, %ecx
jne 0x13fa2
testl %eax, %eax
je 0x13e81
leaq -0x1(%rax), %rcx
movl %ecx, 0x8a070(%rip)     # 0x9dee0
leaq 0x9e69(%rip), %rcx      # 0x1dce0
movq -0x8(%rcx,%rax,8), %r15
testq %r15, %r15
jne 0x13ee4
movq %r13, %rdi
movq %r14, %rsi
callq 0x3520
testl %eax, %eax
jne 0x13fc1
movl 0x94de(%rip), %edx      # 0x1d378
cmpl %r12d, %edx
sete %al
cmpl $-0x1, %edx
sete %cl
je 0x13eb5
movl $0x0, %r15d
cmpl %r12d, %edx
jne 0x13e4a
jmp 0x13eb8
xorl %r15d, %r15d
testb %cl, %cl
je 0x13ecd
movl %r12d, 0x94b5(%rip)     # 0x1d378
movl %r12d, %edi
callq 0x14000
jmp 0x13edb
testq %r15, %r15
setne %cl
orb %al, %cl
je 0x13fcf
testq %r15, %r15
je 0x13f6e
movl %r12d, (%r15)
movl $0x1fff, %eax           # imm = 0x1FFF
cmpl %eax, %ebp
movl $0x1fff, %edx           # imm = 0x1FFF
cmovll %ebp, %edx
movl 0x4(%rsp), %eax
movl %eax, 0x4(%r15)
movl %edx, 0xc(%r15)
movl (%rsp), %eax
movl %eax, 0x8(%r15)
movq 0x10(%r15), %rdi
movq %rbx, %rsi
callq 0x3290
movq 0x10(%r15), %rax
movslq 0xc(%r15), %rcx
movb $0x0, (%rax,%rcx)
movslq 0xc(%r15), %rax
addq %rax, %rbx
movslq 0x89fb5(%rip), %rcx     # 0x9dee4
leal 0x1(%rcx), %edx
movl %edx, 0x89fac(%rip)     # 0x9dee4
leaq 0x8a001(%rip), %rdx     # 0x9df40
movq %r15, (%rdx,%rcx,8)
subl %eax, %ebp
jg 0x13e32
leaq 0x89f96(%rip), %rdi     # 0x9dee8
callq 0x3250
testl %eax, %eax
jne 0x13ff7
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x89f73(%rip), %rdi     # 0x9dee8
callq 0x3250
testl %eax, %eax
jne 0x13ff7
cmpl $0x1, (%rsp)
movl $0x2, %edi
sbbl $0x0, %edi
movl %ebp, %edx
movq %rbx, %rsi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3120
leaq 0x3463(%rip), %rdi      # 0x1740c
leaq 0x340f(%rip), %rsi      # 0x173bf
leaq 0x3490(%rip), %rcx      # 0x17447
movl $0x98, %edx
callq 0x3220
leaq 0x1db5(%rip), %rdi      # 0x15d7d
xorl %eax, %eax
callq 0x5b09
leaq 0x33e5(%rip), %rdi      # 0x173bb
leaq 0x33e2(%rip), %rsi      # 0x173bf
leaq 0x33ee(%rip), %rcx      # 0x173d2
movl $0x11c, %edx            # imm = 0x11C
callq 0x3220
leaq 0x1a67(%rip), %rdi      # 0x15a5c
jmp 0x13fc8
leaq 0x1a7a(%rip), %rdi      # 0x15a78
jmp 0x13fc8