// File: /workspace/github_cpp_makefile_star2_O2/AndreRenaud[P]simple_uart/simple_uart.c
// Function: simple_uart_read_line
ssize_t simple_uart_read_line(struct simple_uart *uart, char *buffer, int max_len, uint64_t ms_timeout)
{
    int pos = 0;
    uint64_t last = mseconds();
    uint64_t now = mseconds();

    if (!uart || !buffer || max_len <= 0)
        return -EINVAL;

    *buffer = '\0';

    do {
        char ch;
        ssize_t ret = simple_uart_read(uart, &ch, 1);
        if (ret < 0)
            return ret;
        if (ret > 0) {
            if (ch == '\n' || ch == '\r') {
                break;
            } else {
                last = mseconds();
                buffer[pos] = ch;
                buffer[pos + 1] = '\0';
                pos++;
            }
        }
        now = mseconds();
    } while (pos < max_len - 1 && now - last < ms_timeout);

    /* If we got a carriage return + line feed, just collapse them */
    while (pos > 0 && (buffer[pos - 1] == '\r' || buffer[pos - 1] == '\n')) {
        buffer[pos - 1] = '\0';
        pos--;
    }

    if (now - last >= ms_timeout)
        return -ETIMEDOUT;

    return pos;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rcx, 0x18(%rsp)
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %r15
pushq $0x1
popq %r12
leaq 0x8(%rsp), %rbx
movl %r12d, %edi
movq %rbx, %rsi
callq 0x2100
movq (%rbx), %r13
movq 0x8(%rbx), %rbx
leaq 0x8(%rsp), %rsi
movl %r12d, %edi
callq 0x2100
movq %r15, 0x20(%rsp)
testq %r15, %r15
sete %al
testq %r14, %r14
sete %cl
orb %al, %cl
testl %ebp, %ebp
setle %al
orb %cl, %al
je 0x60fe
pushq $-0x16
popq %rax
jmp 0x623a
leaq 0x8(%rsp), %rcx
movq 0x8(%rcx), %rax
imulq $0x3e8, (%rcx), %rcx    # imm = 0x3E8
movl $0xf4240, %esi          # imm = 0xF4240
cqto
idivq %rsi
movq %r13, %rdx
movq %rax, %r13
addq %rcx, %r13
imulq $0x3e8, %rdx, %rcx      # imm = 0x3E8
movq %rbx, %rax
cqto
idivq %rsi
movq %rax, %r12
addq %rcx, %r12
movb $0x0, (%r14)
decl %ebp
xorl %ebx, %ebx
movq 0x20(%rsp), %rdi
leaq 0x7(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x52a4
testq %rax, %rax
js 0x623a
je 0x61ba
movzbl 0x7(%rsp), %eax
cmpl $0xa, %eax
je 0x61fe
cmpl $0xd, %eax
je 0x61fe
pushq $0x1
popq %rdi
leaq 0x8(%rsp), %r13
movq %r13, %rsi
callq 0x2100
pushq $0x1
popq %rdi
imulq $0x3e8, 0x8(%rsp), %rcx # imm = 0x3E8
movq 0x10(%rsp), %rax
cqto
movl $0xf4240, %r15d         # imm = 0xF4240
idivq %r15
movq %rax, %r12
addq %rcx, %r12
movb 0x7(%rsp), %al
movslq %ebx, %rbx
movb %al, (%r14,%rbx)
movb $0x0, 0x1(%r14,%rbx)
incq %rbx
jmp 0x61c8
leaq 0x8(%rsp), %r13
movl $0xf4240, %r15d         # imm = 0xF4240
pushq $0x1
popq %rdi
movq %r13, %rsi
callq 0x2100
imulq $0x3e8, 0x8(%rsp), %rcx # imm = 0x3E8
movq 0x10(%rsp), %rax
cqto
idivq %r15
movq %rax, %r13
addq %rcx, %r13
cmpl %ebp, %ebx
jge 0x61fe
movq %r13, %rax
subq %r12, %rax
cmpq 0x18(%rsp), %rax
jb 0x613e
movl %ebx, %ecx
movl %ebx, %eax
sarl $0x1f, %eax
andl %ebx, %eax
testl %ecx, %ecx
jle 0x6228
movzbl -0x1(%r14,%rcx), %edx
cmpl $0xd, %edx
je 0x621b
cmpl $0xa, %edx
jne 0x6226
movb $0x0, -0x1(%r14,%rcx)
decq %rcx
jmp 0x6207
movl %ecx, %eax
subq %r12, %r13
cmpq 0x18(%rsp), %r13
movslq %eax, %rcx
pushq $-0x6e
popq %rax
cmovbq %rcx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq