// File: /workspace/github_c_makefile_star2_O1/j-hoppe[P]tu58fs/monitor.c
// Function: monitor_puts_echocheck
int monitor_puts_echocheck(monitor_t *_this, char *s, int timeout_us) {
	int i, m, n;
	if (s == NULL || strlen(s) == 0)
		return ERROR_OK;
	n = strlen(s);

	// wait max "timeout" for every char
	for (i = 0; i < n; i++) {
		// send a single char
		char c = s[i];
		int echo_found;

		m = write(_this->serial->fd, &c, 1);
		if (m <= 0)
			return error_set(ERROR_MONITOR, "serial_putc() failed");
		monitor_trace(&c, TRACE_IN, 0, "monitor_puts_echocheck");

		// wait until echo or timeout
		echo_found = 0;
		timeout_set(timeout_us);

		while (!echo_found && !timeout_reached()) {
			char tmpbuff[2];

			fcntl(_this->serial->fd, F_SETFL, FNDELAY);
			tmpbuff[0] = 0; // clear
			m = read(_this->serial->fd, tmpbuff, 1);
			if (m > 0)
				monitor_trace(tmpbuff, m, TRACE_IN, "monitor_puts_echocheck");
			// else if (m < 0)
			//	monitor_trace(NULL, 0, TRACE_STATUS, "monitor_puts_echocheck");

			if (m == 1) {
				if (_this->fecho)
					fputc(tmpbuff[0], _this->fecho);

				echo_found = 1;
				if (c != tmpbuff[0]) {
					monitor_trace_dump(ferr);
					return error_set(ERROR_MONITOR,
							"monitor_puts_echoed(): sent 0x%x, received 0x%x.", c, tmpbuff[0]);
				}
			}
		}
		if (!echo_found) {
			monitor_trace(NULL, 0, TRACE_STATUS, "monitor_puts_echoed(): no echo");
			monitor_trace_dump(ferr);
			return error_set(ERROR_MONITOR,
					"monitor_puts_echoed(): sent 0x%x, no echo after %0.1f ms.", c,
					timeout_us / 1000.0);
		}
	}
	return ERROR_OK;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %edx, 0xc(%rsp)
xorl %ebp, %ebp
testq %rsi, %rsi
je 0x10f53
movq %rsi, %rbx
movq %rdi, %r15
movq %rsi, %rdi
callq 0x31a0
testl %eax, %eax
jle 0x10f53
cvtsi2sdl 0xc(%rsp), %xmm0
divsd 0x6226(%rip), %xmm0     # 0x17030
movsd %xmm0, 0x10(%rsp)
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorl %r12d, %r12d
movq %rbx, 0x20(%rsp)
movq %rax, 0x18(%rsp)
movsbl (%rbx,%r12), %r13d
movb %r13b, 0x7(%rsp)
movq 0x10(%r15), %rax
movl (%rax), %edi
movl $0x1, %edx
leaq 0x7(%rsp), %rsi
callq 0x3150
testl %eax, %eax
jle 0x10ef1
movl 0xc(%rsp), %edi
callq 0xa47d
xorl %ebx, %ebx
testl %ebx, %ebx
setne %r14b
jne 0x10f30
callq 0xa4b5
testl %eax, %eax
jne 0x10f0d
movq 0x10(%r15), %rax
movl (%rax), %edi
xorl %ebx, %ebx
movl $0x4, %esi
movl $0x800, %edx            # imm = 0x800
xorl %eax, %eax
callq 0x3130
movb $0x0, 0x5(%rsp)
movq 0x10(%r15), %rax
movl (%rax), %edi
movl $0x1, %edx
leaq 0x5(%rsp), %rsi
callq 0x32c0
movb $0x1, %bpl
cmpl $0x1, %eax
jne 0x10ee6
movq 0x20(%r15), %rsi
testq %rsi, %rsi
je 0x10ebb
movsbl 0x5(%rsp), %edi
callq 0x32a0
movsbl 0x5(%rsp), %ecx
movl $0x1, %ebx
cmpb %cl, %r13b
je 0x10ee6
xorl %ebp, %ebp
movl $0xfffffff6, %edi       # imm = 0xFFFFFFF6
leaq 0x61e5(%rip), %rsi      # 0x170bd
movl %r13d, %edx
xorl %eax, %eax
callq 0x9f8f
movl %eax, 0x8(%rsp)
testb %bpl, %bpl
jne 0x10e54
jmp 0x10f30
xorl %r14d, %r14d
movl $0xfffffff6, %edi       # imm = 0xFFFFFFF6
leaq 0x61a8(%rip), %rsi      # 0x170a8
xorl %eax, %eax
callq 0x9f8f
movl %eax, 0x8(%rsp)
jmp 0x10f30
movl $0xfffffff6, %edi       # imm = 0xFFFFFFF6
leaq 0x61d5(%rip), %rsi      # 0x170ee
movl %r13d, %edx
movsd 0x10(%rsp), %xmm0
movb $0x1, %al
callq 0x9f8f
movl %eax, 0x8(%rsp)
xorl %r14d, %r14d
testb %r14b, %r14b
je 0x10f4f
incq %r12
movq 0x18(%rsp), %rax
cmpq %rax, %r12
movq 0x20(%rsp), %rbx
jne 0x10e22
xorl %ebp, %ebp
jmp 0x10f53
movl 0x8(%rsp), %ebp
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq