// File: /workspace/github_c_makefile_star2_O2/odknt[P]st/st.c
// Function: ttywriteraw
void
ttywriteraw(const char *s, size_t n)
{
	fd_set wfd, rfd;
	ssize_t r;
	size_t lim = 256;

	/*
	 * Remember that we are using a pty, which might be a modem line.
	 * Writing too much will clog the line. That's why we are doing this
	 * dance.
	 * FIXME: Migrate the world to Plan 9.
	 */
	while (n > 0) {
		FD_ZERO(&wfd);
		FD_ZERO(&rfd);
		FD_SET(cmdfd, &wfd);
		FD_SET(cmdfd, &rfd);

		/* Check if we can write. */
		if (pselect(cmdfd+1, &rfd, &wfd, NULL, NULL, NULL) < 0) {
			if (errno == EINTR)
				continue;
			die("select failed: %s\n", strerror(errno));
		}
		if (FD_ISSET(cmdfd, &wfd)) {
			/*
			 * Only write the bytes written by ttywrite() or the
			 * default of 256. This seems to be a reasonable value
			 * for a serial line. Bigger values might clog the I/O.
			 */
			if ((r = write(cmdfd, s, (n < lim)? n : lim)) < 0)
				goto write_error;
			if (r < n) {
				/*
				 * We weren't able to write out everything.
				 * This means the buffer is getting full
				 * again. Empty it.
				 */
				if (n < lim)
					lim = ttyread();
				n -= r;
				s += r;
			} else {
				/* All bytes have been written. */
				break;
			}
		}
		if (FD_ISSET(cmdfd, &rfd))
			lim = ttyread();
	}
	return;

write_error:
	die("write error on tty: %s\n", strerror(errno));
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq %rsi, %r13
movq %rdi, 0x10(%rsp)
movl $0x100, %eax            # imm = 0x100
movq %rax, 0x8(%rsp)
pushq $0x40
popq %rbx
pushq $0x1
popq %r12
leaq 0x18(%rsp), %r14
leaq 0x98(%rsp), %r15
testq %r13, %r13
je 0x7d00
xorl %eax, %eax
cmpq $0x10, %rax
jne 0x7c0f
xorl %eax, %eax
cmpq $0x10, %rax
je 0x7c1d
andq $0x0, 0x18(%rsp,%rax,8)
incq %rax
jmp 0x7bfe
andq $0x0, 0x98(%rsp,%rax,8)
incq %rax
jmp 0x7bf6
movl 0x14731(%rip), %edi     # 0x1c354
movl %edi, %eax
cltd
idivl %ebx
movslq %eax, %rdx
shll $0x6, %eax
movl %edi, %ecx
subl %eax, %ecx
movq %r12, %rax
shlq %cl, %rax
orq %rax, 0x98(%rsp,%rdx,8)
orq %rax, 0x18(%rsp,%rdx,8)
incl %edi
movq %r14, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x6370
testl %eax, %eax
jns 0x7c74
callq 0x66e0
movl (%rax), %edi
movl $0x0, %eax
cmpl $0x4, %edi
je 0x7bf6
jmp 0x7d12
movl 0x146da(%rip), %edi     # 0x1c354
movl %edi, %eax
cltd
idivl %ebx
movl %edx, %ecx
cltq
movq 0x98(%rsp,%rax,8), %rsi
movq %r12, %rdx
shlq %cl, %rdx
btq %rcx, %rsi
jae 0x7ce6
movq 0x8(%rsp), %rdx
cmpq %rdx, %r13
cmovbq %r13, %rdx
movq 0x10(%rsp), %rsi
callq 0x69e0
testq %rax, %rax
js 0x7d28
movq %rax, %rbp
movq %r13, %rax
subq %rbp, %r13
jbe 0x7d00
cmpq 0x8(%rsp), %rax
jae 0x7cce
callq 0x790c
movq %rax, 0x8(%rsp)
movl 0x14680(%rip), %eax     # 0x1c354
cltd
idivl %ebx
movl %edx, %ecx
addq %rbp, 0x10(%rsp)
cltq
movq %r12, %rdx
shlq %cl, %rdx
testq %rdx, 0x18(%rsp,%rax,8)
je 0x7beb
callq 0x790c
movq %rax, 0x8(%rsp)
jmp 0x7beb
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x6570
leaq 0x8ae4(%rip), %rdi      # 0x10802
movq %rax, %rsi
xorl %eax, %eax
callq 0x6bf6
callq 0x66e0
movl (%rax), %edi
callq 0x6570
leaq 0x8ada(%rip), %rdi      # 0x10815
jmp 0x7d1e