// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x30, %rsp
movl %edi, -0x4(%rbp)
movl %esi, -0x8(%rbp)
movl %edx, -0xc(%rbp)
movq %rcx, -0x18(%rbp)
movl %r8d, -0x1c(%rbp)
movq $0x0, -0x28(%rbp)
leaq 0x8dce0(%rip), %rdi     # 0xafee8
callq 0x69b0
cmpl $0x0, -0x1c(%rbp)
jle 0x223b2
jmp 0x22219
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl 0xd144(%rip), %eax      # 0x2f368
je 0x22231
movl -0x4(%rbp), %eax
cmpl 0xd139(%rip), %eax      # 0x2f368
jne 0x22233
jmp 0x2225d
callq 0x223d0
movq %rax, %rcx
movq %rcx, -0x28(%rbp)
xorl %eax, %eax
cmpq %rcx, %rax
je 0x22248
jmp 0x2225d
leaq 0x8dcc1(%rip), %rdi     # 0xaff10
leaq 0x8dc92(%rip), %rsi     # 0xafee8
callq 0x6b80
jmp 0x22219
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl 0xd100(%rip), %eax      # 0x2f368
jne 0x22281
jmp 0x2226c
jmp 0x2226e
movl -0x4(%rbp), %eax
movl %eax, 0xd0f1(%rip)      # 0x2f368
movl -0x4(%rbp), %edi
callq 0x22450
jmp 0x222b8
movl -0x4(%rbp), %eax
cmpl 0xd0de(%rip), %eax      # 0x2f368
je 0x222b6
cmpq $0x0, -0x28(%rbp)
je 0x22295
jmp 0x222b4
leaq 0x348a(%rip), %rdi      # 0x25726
leaq 0x3487(%rip), %rsi      # 0x2572a
movl $0x11c, %edx            # imm = 0x11C
leaq 0x348e(%rip), %rcx      # 0x2573d
callq 0x3230
jmp 0x222b6
jmp 0x222b8
cmpq $0x0, -0x28(%rbp)
jne 0x222f5
leaq 0x8dc22(%rip), %rdi     # 0xafee8
callq 0x69f0
jmp 0x222cd
movl -0x8(%rbp), %ecx
movl $0x1, %edi
movl $0x2, %eax
cmpl $0x0, %ecx
cmovnel %eax, %edi
movq -0x18(%rbp), %rsi
movslq -0x1c(%rbp), %rdx
callq 0x3120
movl %eax, -0x20(%rbp)
jmp 0x223be
jmp 0x222f7
jmp 0x222f9
movl -0x4(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movl -0xc(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x4(%rax)
cmpl $0x1fff, -0x1c(%rbp)    # imm = 0x1FFF
jle 0x2231f
movl $0x1fff, %eax           # imm = 0x1FFF
movl %eax, -0x2c(%rbp)
jmp 0x22325
movl -0x1c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0xc(%rax)
movl -0x8(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x18(%rbp), %rsi
movq -0x28(%rbp), %rax
movslq 0xc(%rax), %rdx
callq 0x32a0
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x28(%rbp), %rcx
movslq 0xc(%rcx), %rcx
movb $0x0, (%rax,%rcx)
movq -0x28(%rbp), %rax
movl 0xc(%rax), %ecx
movl -0x1c(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x1c(%rbp)
movq -0x28(%rbp), %rax
movl 0xc(%rax), %ecx
movq -0x18(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x18(%rbp)
movq -0x28(%rbp), %rdx
movl 0x8db50(%rip), %eax     # 0xafee4
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, 0x8db45(%rip)     # 0xafee4
movslq %eax, %rcx
leaq 0x8db97(%rip), %rax     # 0xaff40
movq %rdx, (%rax,%rcx,8)
jmp 0x2220d
leaq 0x8db2f(%rip), %rdi     # 0xafee8
callq 0x69f0
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)