// File: /workspace/github_c_makefile_star2_O3/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 $0x18, %rsp
xorl %ebp, %ebp
testq %rsi, %rsi
je 0x10e5b
movl %edx, %ebx
movq %rsi, %r14
movq %rdi, %r15
movq %rsi, %rdi
callq 0x3190
testl %eax, %eax
jle 0x10e5b
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
leaq 0x1(%rsp), %rsi
leaq 0x2(%rsp), %r13
xorl %r12d, %r12d
movq %rax, 0x10(%rsp)
movq %r14, 0x8(%rsp)
movsbl (%r14,%r12), %ebp
movb %bpl, 0x1(%rsp)
movq 0x10(%r15), %rax
movl (%rax), %edi
movl $0x1, %edx
movq %rsi, %r14
callq 0x3140
testl %eax, %eax
jle 0x10e6c
movl %ebx, %edi
callq 0xa460
callq 0xa498
testl %eax, %eax
jne 0x10e38
movq 0x10(%r15), %rax
movl (%rax), %edi
movl $0x4, %esi
movl $0x800, %edx            # imm = 0x800
xorl %eax, %eax
callq 0x3120
movb $0x0, 0x2(%rsp)
movq 0x10(%r15), %rax
movl (%rax), %edi
movl $0x1, %edx
movq %r13, %rsi
callq 0x32b0
cmpl $0x1, %eax
jne 0x10dbc
movl %ebx, 0x4(%rsp)
movq 0x20(%r15), %rsi
movsbl 0x2(%rsp), %ebx
testq %rsi, %rsi
je 0x10e12
movl %ebx, %edi
callq 0x3290
cmpb %bl, %bpl
jne 0x10e81
incq %r12
xorl %ebp, %ebp
movq 0x10(%rsp), %rax
cmpq %rax, %r12
movl 0x4(%rsp), %ebx
movq %r14, %rsi
movq 0x8(%rsp), %r14
jne 0x10d90
jmp 0x10e5b
cvtsi2sd %ebx, %xmm0
divsd 0x6284(%rip), %xmm0     # 0x170c8
leaq 0x633b(%rip), %rsi      # 0x17186
movl $0xfffffff6, %edi       # imm = 0xFFFFFFF6
movl %ebp, %edx
movb $0x1, %al
callq 0x9f73
movl %eax, %ebp
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x62cd(%rip), %rsi      # 0x17140
movl $0xfffffff6, %edi       # imm = 0xFFFFFFF6
xorl %eax, %eax
callq 0x9f73
jmp 0x10e59
leaq 0x62cd(%rip), %rsi      # 0x17155
movl $0xfffffff6, %edi       # imm = 0xFFFFFFF6
movl %ebp, %edx
movl %ebx, %ecx
xorl %eax, %eax
callq 0x9f73
jmp 0x10e59