#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
scalar_t__ uart_isrxready();
char uart_readc();
int uart_readline(char *buffer, int max_len) {
  int i = 0;
  while (uart_isrxready()) {
    buffer[i] = uart_readc();
    if (buffer[i] == '\n' || i >= max_len) {
      break;
    }
    i++;
  }
  buffer[i] = '\0';
  return i;
}


x86 assembly (clang, -O0):
<uart_readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    $0x0,%al
call   1030 <uart_isrxready@plt>
cmp    $0x0,%rax
je     118f <uart_readline+0x6f>
mov    $0x0,%al
call   1040 <uart_readc@plt>
mov    %al,%dl
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
je     117c <uart_readline+0x5c>
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jl     1181 <uart_readline+0x61>
jmp    118f <uart_readline+0x6f>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1136 <uart_readline+0x16>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<uart_readline>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%r14
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <uart_isrxready@plt>
test   %rax,%rax
je     1164 <uart_readline+0x44>
movslq %ebp,%rbp
xor    %ebx,%ebx
nopl   0x0(%rax)
xor    %eax,%eax
call   1040 <uart_readc@plt>
mov    %al,(%r14,%rbx,1)
cmp    $0xa,%al
je     1164 <uart_readline+0x44>
cmp    %rbp,%rbx
jge    1164 <uart_readline+0x44>
add    $0x1,%rbx
xor    %eax,%eax
call   1030 <uart_isrxready@plt>
test   %rax,%rax
jne    1140 <uart_readline+0x20>
mov    %ebx,%eax
movb   $0x0,(%r14,%rax,1)
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @uart_readline(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 0, i32* %5
br label %6
6: ; preds = %27, %2
%7 = call i64 (...) @uart_isrxready()
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %30
9: ; preds = %6
%10 = call signext i8 (...) @uart_readc()
%11 = load i8*, i8** %3
%12 = load i32, i32* %5
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %11, i64 %13
store i8 %10, i8* %14
%15 = load i8*, i8** %3
%16 = load i32, i32* %5
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %15, i64 %17
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp eq i32 %20, 10
br i1 %21, label %26, label %22
22: ; preds = %9
%23 = load i32, i32* %5
%24 = load i32, i32* %4
%25 = icmp sge i32 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %22, %9
br label %30
27: ; preds = %22
%28 = load i32, i32* %5
%29 = add nsw i32 %28, 1
store i32 %29, i32* %5
br label %6
30: ; preds = %26, %6
%31 = load i8*, i8** %3
%32 = load i32, i32* %5
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
store i8 0, i8* %34
%35 = load i32, i32* %5
ret i32 %35
}

LLVM IR (-O2):
define i32 @uart_readline(i8* nocapture writeonly %0, i32 %1) {
%3 = tail call i64 (...) @uart_isrxready()
%4 = icmp eq i64 %3, 0
br i1 %4, label %21, label %5
5: ; preds = %2
%6 = sext i32 %1 to i64
br label %7
7: ; preds = %5, %14
%8 = phi i64 [ 0, %5 ], [ %15, %14 ]
%9 = tail call signext i8 (...) @uart_readc()
%10 = getelementptr inbounds i8, i8* %0, i64 %8
store i8 %9, i8* %10
%11 = icmp ne i8 %9, 10
%12 = icmp slt i64 %8, %6
%13 = select i1 %11, i1 %12, i1 false
br i1 %13, label %14, label %18
14: ; preds = %7
%15 = add nuw nsw i64 %8, 1
%16 = tail call i64 (...) @uart_isrxready()
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %7
18: ; preds = %7, %14
%19 = phi i64 [ %15, %14 ], [ %8, %7 ]
%20 = trunc i64 %19 to i32
br label %21
21: ; preds = %18, %2
%22 = phi i32 [ 0, %2 ], [ %20, %18 ]
%23 = zext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %0, i64 %23
store i8 0, i8* %24
ret i32 %22
}