// File: /workspace/github_cpp_makefile_star2_O3/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
leaq 0x8(%rsp), %rbx
movl $0x1, %edi
movq %rbx, %rsi
callq 0x2100
movq (%rbx), %r13
movq 0x8(%rbx), %r12
leaq 0x8(%rsp), %rsi
movl $0x1, %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
movq $-0x16, %rax
jne 0x6861
leaq 0x8(%rsp), %rcx
movq 0x8(%rcx), %rax
imulq $0x3e8, (%rcx), %rcx    # imm = 0x3E8
movabsq $0x431bde82d7b634db, %r15 # imm = 0x431BDE82D7B634DB
imulq %r15
movq %r13, %rsi
movq %rdx, %r13
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x12, %r13
addq %rax, %r13
addq %rcx, %r13
imulq $0x3e8, %rsi, %rcx      # imm = 0x3E8
movq %r12, %rax
imulq %r15
movq %rdx, %r12
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x12, %r12
addq %rax, %r12
addq %rcx, %r12
movb $0x0, (%r14)
decl %ebp
xorl %ebx, %ebx
movl $0x1, %edx
movl $0x32, %ecx
movq 0x20(%rsp), %rdi
leaq 0x7(%rsp), %rsi
callq 0x578a
testq %rax, %rax
js 0x6861
je 0x67d4
movzbl 0x7(%rsp), %eax
cmpl $0xa, %eax
je 0x6820
cmpl $0xd, %eax
je 0x6820
movl $0x1, %edi
leaq 0x8(%rsp), %r13
movq %r13, %rsi
callq 0x2100
imulq $0x3e8, 0x8(%rsp), %rcx # imm = 0x3E8
movq %r15, %rax
imulq 0x10(%rsp)
movq %rdx, %r12
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x12, %r12
addq %rax, %r12
addq %rcx, %r12
movb 0x7(%rsp), %al
movslq %ebx, %rbx
movb %al, (%r14,%rbx)
movb $0x0, 0x1(%r14,%rbx)
incq %rbx
jmp 0x67d9
leaq 0x8(%rsp), %r13
movl $0x1, %edi
movq %r13, %rsi
callq 0x2100
imulq $0x3e8, 0x8(%rsp), %rcx # imm = 0x3E8
movq %r15, %rax
imulq 0x10(%rsp)
movq %rdx, %r13
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x12, %r13
addq %rax, %r13
addq %rcx, %r13
cmpl %ebp, %ebx
jge 0x6820
movq %r13, %rax
subq %r12, %rax
cmpq 0x18(%rsp), %rax
jb 0x674c
testl %ebx, %ebx
jle 0x684b
movl %ebx, %ebx
movzbl -0x1(%r14,%rbx), %eax
cmpl $0xd, %eax
je 0x6836
cmpl $0xa, %eax
jne 0x684b
movb $0x0, -0x1(%r14,%rbx)
leaq -0x1(%rbx), %rax
cmpq $0x1, %rbx
movq %rax, %rbx
jg 0x6826
xorl %ebx, %ebx
subq %r12, %r13
cmpq 0x18(%rsp), %r13
movslq %ebx, %rcx
movq $-0x6e, %rax
cmovbq %rcx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq