// File: /workspace/github_c_makefile_star2_O2/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
xorl %ebx, %ebx
testq %rsi, %rsi
je 0xfe42
movl %edx, %ebp
movq %rsi, %r12
movq %rdi, %r15
movq %rsi, %rdi
callq 0x3190
testq %rax, %rax
je 0xfe42
movl %ebp, 0xc(%rsp)
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
leaq 0x9(%rsp), %rsi
pushq $0x1
popq %rbp
leaq 0xa(%rsp), %r14
xorl %ebx, %ebx
movq %r12, 0x18(%rsp)
movq %rax, 0x10(%rsp)
cmpq %rax, %rbx
je 0xfe53
movsbl (%r12,%rbx), %r13d
movb %r13b, 0x9(%rsp)
movq 0x10(%r15), %rax
movl (%rax), %edi
movq %rbp, %rdx
callq 0x3140
testl %eax, %eax
jle 0xfe57
movq %rbx, 0x20(%rsp)
movl 0xc(%rsp), %edi
callq 0x9cf1
xorl %ebx, %ebx
testl %ebx, %ebx
jne 0xfde9
callq 0x9d08
testl %eax, %eax
jne 0xfe05
movq 0x10(%r15), %rax
movl (%rax), %edi
xorl %ebx, %ebx
pushq $0x4
popq %rsi
movl $0x800, %edx            # imm = 0x800
xorl %eax, %eax
callq 0x3120
movb $0x0, 0xa(%rsp)
movq 0x10(%r15), %rax
movl (%rax), %edi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x32b0
cmpl $0x1, %eax
jne 0xfd89
movq 0x20(%r15), %rsi
movsbl 0xa(%rsp), %r12d
testq %rsi, %rsi
je 0xfddf
movl %r12d, %edi
callq 0x3290
pushq $0x1
popq %rbx
cmpb %r12b, %r13b
je 0xfd89
jmp 0xfe29
movq 0x20(%rsp), %rbx
incq %rbx
movq 0x18(%rsp), %r12
movq 0x10(%rsp), %rax
leaq 0x9(%rsp), %rsi
jmp 0xfd50
cvtsi2sdl 0xc(%rsp), %xmm0
divsd 0x6265(%rip), %xmm0     # 0x16078
leaq 0x631c(%rip), %rsi      # 0x16136
pushq $-0xa
popq %rdi
movl %r13d, %edx
movb $0x1, %al
callq 0x982c
jmp 0xfe40
leaq 0x62d5(%rip), %rsi      # 0x16105
pushq $-0xa
popq %rdi
movl %r13d, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x982c
movl %eax, %ebx
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebx, %ebx
jmp 0xfe42
leaq 0x6292(%rip), %rsi      # 0x160f0
pushq $-0xa
popq %rdi
xorl %eax, %eax
callq 0x982c
jmp 0xfe40