// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x176d9
movq -0x18(%rbp), %rdi
callq 0x3190
cmpq $0x0, %rax
jne 0x176e5
movl $0x0, -0x4(%rbp)
jmp 0x178d4
movq -0x18(%rbp), %rdi
callq 0x3190
movl %eax, -0x28(%rbp)
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x28(%rbp), %eax
jge 0x178cd
movq -0x18(%rbp), %rax
movslq -0x20(%rbp), %rcx
movb (%rax,%rcx), %al
movb %al, -0x29(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl (%rax), %edi
leaq -0x29(%rbp), %rsi
movl $0x1, %edx
callq 0x3140
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jg 0x1774e
movl $0xfffffff6, %edi       # imm = 0xFFFFFFF6
leaq 0x692b(%rip), %rsi      # 0x1e06a
movb $0x0, %al
callq 0xcd70
movl %eax, -0x4(%rbp)
jmp 0x178d4
leaq -0x29(%rbp), %rdi
movl $0x1, %esi
xorl %edx, %edx
leaq 0x691f(%rip), %rcx      # 0x1e07f
movb $0x0, %al
callq 0x17690
movl $0x0, -0x30(%rbp)
movl -0x1c(%rbp), %edi
callq 0xd330
xorl %eax, %eax
cmpl $0x0, -0x30(%rbp)
movb %al, -0x33(%rbp)
jne 0x17791
callq 0xd360
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
movb %al, -0x33(%rbp)
movb -0x33(%rbp), %al
testb $0x1, %al
jne 0x1779d
jmp 0x17862
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl (%rax), %edi
movl $0x4, %esi
movl $0x800, %edx            # imm = 0x800
movb $0x0, %al
callq 0x3120
movb $0x0, -0x32(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl (%rax), %edi
leaq -0x32(%rbp), %rsi
movl $0x1, %edx
callq 0x32a0
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jle 0x177f7
leaq -0x32(%rbp), %rdi
movl -0x24(%rbp), %esi
movl $0x1, %edx
leaq 0x688f(%rip), %rcx      # 0x1e07f
movb $0x0, %al
callq 0x17690
cmpl $0x1, -0x24(%rbp)
jne 0x1785d
movq -0x10(%rbp), %rax
cmpq $0x0, 0x20(%rax)
je 0x17819
movsbl -0x32(%rbp), %edi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rsi
callq 0x3290
movl $0x1, -0x30(%rbp)
movsbl -0x29(%rbp), %eax
movsbl -0x32(%rbp), %ecx
cmpl %ecx, %eax
je 0x1785b
leaq 0xdcfd(%rip), %rax      # 0x25530
movq (%rax), %rdi
callq 0x17230
movsbl -0x29(%rbp), %edx
movsbl -0x32(%rbp), %ecx
movl $0xfffffff6, %edi       # imm = 0xFFFFFFF6
leaq 0x6847(%rip), %rsi      # 0x1e096
movb $0x0, %al
callq 0xcd70
movl %eax, -0x4(%rbp)
jmp 0x178d4
jmp 0x1785d
jmp 0x17776
cmpl $0x0, -0x30(%rbp)
jne 0x178bd
xorl %eax, %eax
movl %eax, %edi
xorl %esi, %esi
movl $0x2, %edx
leaq 0x684d(%rip), %rcx      # 0x1e0c7
movb $0x0, %al
callq 0x17690
leaq 0xdca8(%rip), %rax      # 0x25530
movq (%rax), %rdi
callq 0x17230
movsbl -0x29(%rbp), %edx
cvtsi2sdl -0x1c(%rbp), %xmm0
movsd 0x6707(%rip), %xmm1     # 0x1dfa8
divsd %xmm1, %xmm0
movl $0xfffffff6, %edi       # imm = 0xFFFFFFF6
leaq 0x6835(%rip), %rsi      # 0x1e0e6
movb $0x1, %al
callq 0xcd70
movl %eax, -0x4(%rbp)
jmp 0x178d4
jmp 0x178bf
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x176f8
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)