// File: /workspace/github_c_makefile_star2_O1/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 0x8997c(%rip), %rdi     # 0x9dee8
callq 0x34b0
testl %eax, %eax
jne 0x1476b
testl %ebp, %ebp
jle 0x146c8
xorl %r15d, %r15d
leaq 0x89985(%rip), %r13     # 0x9df10
leaq 0x89956(%rip), %r14     # 0x9dee8
movl 0x8de0(%rip), %eax      # 0x1d378
cmpl $-0x1, %eax
sete %cl
cmpl %r12d, %eax
sete %al
movl %ecx, %edx
orb %al, %dl
je 0x145c7
testb $0x1, %cl
je 0x1463e
movl %r12d, 0x8dbe(%rip)     # 0x1d378
movl %r12d, %edi
callq 0x1477d
jmp 0x1464e
movslq 0x89912(%rip), %rdx     # 0x9dee0
movl 0x89910(%rip), %esi     # 0x9dee4
addl %edx, %esi
cmpl $0x40, %esi
jne 0x1471f
testl %edx, %edx
je 0x145fb
leaq -0x1(%rdx), %rsi
movl %esi, 0x898f3(%rip)     # 0x9dee0
leaq 0x96ec(%rip), %rsi      # 0x1dce0
movq -0x8(%rsi,%rdx,8), %r15
jmp 0x145fe
xorl %r15d, %r15d
testq %r15, %r15
jne 0x145aa
movq %r13, %rdi
movq %r14, %rsi
callq 0x3520
testl %eax, %eax
jne 0x1473e
movl 0x8d5c(%rip), %edx      # 0x1d378
cmpl %r12d, %edx
sete %al
cmpl $-0x1, %edx
sete %cl
je 0x146c0
movl $0x0, %r15d
cmpl %r12d, %edx
jne 0x145c7
jmp 0x145aa
testq %r15, %r15
setne %cl
orb %cl, %al
testb $0x1, %al
je 0x1474c
testq %r15, %r15
je 0x146eb
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 0x89842(%rip), %rcx     # 0x9dee4
leal 0x1(%rcx), %edx
movl %edx, 0x89839(%rip)     # 0x9dee4
leaq 0x8988e(%rip), %rdx     # 0x9df40
movq %r15, (%rdx,%rcx,8)
subl %eax, %ebp
jg 0x14592
jmp 0x146c8
xorl %r15d, %r15d
jmp 0x145aa
leaq 0x89819(%rip), %rdi     # 0x9dee8
callq 0x3250
testl %eax, %eax
jne 0x14774
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x897f6(%rip), %rdi     # 0x9dee8
callq 0x3250
testl %eax, %eax
jne 0x14774
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 0x2cb1(%rip), %rdi      # 0x173d7
leaq 0x2c5d(%rip), %rsi      # 0x1738a
leaq 0x2cde(%rip), %rcx      # 0x17412
movl $0x98, %edx
callq 0x3220
leaq 0x1650(%rip), %rdi      # 0x15d95
xorl %eax, %eax
callq 0x5c85
leaq 0x2c33(%rip), %rdi      # 0x17386
leaq 0x2c30(%rip), %rsi      # 0x1738a
leaq 0x2c3c(%rip), %rcx      # 0x1739d
movl $0x11c, %edx            # imm = 0x11C
callq 0x3220
leaq 0x1302(%rip), %rdi      # 0x15a74
jmp 0x14745
leaq 0x1315(%rip), %rdi      # 0x15a90
jmp 0x14745