// File: /workspace/github_cpp_makefile_star2_O1/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 $0x38, %rsp
movq %rcx, 0x20(%rsp)
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %r15
leaq 0x10(%rsp), %rbx
movl $0x1, %edi
movq %rbx, %rsi
callq 0x2100
movq (%rbx), %r13
movq 0x8(%rbx), %r12
leaq 0x10(%rsp), %rsi
movl $0x1, %edi
callq 0x2100
movq %r15, 0x30(%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 0x6a5a
leaq 0x10(%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 0x30(%rsp), %rdi
leaq 0xf(%rsp), %rsi
callq 0x5872
testq %rax, %rax
js 0x69ad
je 0x69b9
movzbl 0xf(%rsp), %eax
movl $0x2, %ecx
cmpl $0xa, %eax
je 0x69f2
cmpl $0xd, %eax
je 0x69f2
movl $0x1, %edi
leaq 0x10(%rsp), %r13
movq %r13, %rsi
callq 0x2100
imulq $0x3e8, 0x10(%rsp), %rcx # imm = 0x3E8
movq %r15, %rax
imulq 0x18(%rsp)
movq %rdx, %r12
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x12, %r12
addq %rax, %r12
addq %rcx, %r12
movb 0xf(%rsp), %al
movslq %ebx, %rbx
movb %al, (%r14,%rbx)
movb $0x0, 0x1(%r14,%rbx)
incq %rbx
jmp 0x69be
movl $0x1, %ecx
movq %rax, 0x28(%rsp)
jmp 0x69f2
leaq 0x10(%rsp), %r13
movl $0x1, %edi
movq %r13, %rsi
callq 0x2100
imulq $0x3e8, 0x10(%rsp), %rcx # imm = 0x3E8
movq %r15, %rax
imulq 0x18(%rsp)
movq %rdx, %r13
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x12, %r13
addq %rax, %r13
addq %rcx, %r13
xorl %ecx, %ecx
testl %ecx, %ecx
jne 0x6a0d
cmpl %ebp, %ebx
jge 0x6a12
movq %r13, %rax
subq %r12, %rax
cmpq 0x20(%rsp), %rax
jb 0x6924
jmp 0x6a12
cmpl $0x2, %ecx
jne 0x6a55
testl %ebx, %ebx
jle 0x6a3d
movl %ebx, %ebx
movzbl -0x1(%r14,%rbx), %eax
cmpl $0xd, %eax
je 0x6a28
cmpl $0xa, %eax
jne 0x6a3d
movb $0x0, -0x1(%r14,%rbx)
leaq -0x1(%rbx), %rax
cmpq $0x1, %rbx
movq %rax, %rbx
jg 0x6a18
xorl %ebx, %ebx
subq %r12, %r13
cmpq 0x20(%rsp), %r13
movslq %ebx, %rcx
movq $-0x6e, %rax
cmovbq %rcx, %rax
jmp 0x6a5a
movq 0x28(%rsp), %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq