#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 */
int uart0_getc();
char *uart0_gets(char *s, int len) {
  int i = 1;
  int c;
  do {
    c = uart0_getc();
    if (c == -1)
      return (char *)0;
    *s = c;
    s++;
    i++;
  } while (i < len && c != '\n');
  *s = '\0';
  return s;
}


x86 assembly (clang, -O0):
<uart0_gets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
mov    $0x0,%al
call   1030 <uart0_getc@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1147 <uart0_gets+0x37>
movq   $0x0,-0x8(%rbp)
jmp    119c <uart0_gets+0x8c>
mov    -0x1c(%rbp),%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%ecx
xor    %eax,%eax
cmp    -0x14(%rbp),%ecx
mov    %al,-0x1d(%rbp)
jge    1182 <uart0_gets+0x72>
cmpl   $0xa,-0x1c(%rbp)
setne  %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    1126 <uart0_gets+0x16>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<uart0_gets>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %rdi,%rbx
mov    $0x2,%ebp
nop
xor    %eax,%eax
call   1030 <uart0_getc@plt>
cmp    $0xffffffff,%eax
je     1144 <uart0_gets+0x34>
mov    %al,(%rbx)
add    $0x1,%rbx
cmp    %r14d,%ebp
jge    113f <uart0_gets+0x2f>
add    $0x1,%ebp
cmp    $0xa,%eax
jne    1120 <uart0_gets+0x10>
movb   $0x0,(%rbx)
jmp    1146 <uart0_gets+0x36>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @uart0_gets(i8* writeonly %0, i32 %1) {
br label %3
3: ; preds = %8, %2
%4 = phi i8* [ %0, %2 ], [ %10, %8 ]
%5 = phi i32 [ 1, %2 ], [ %11, %8 ]
%6 = tail call i32 (...) @uart0_getc()
%7 = icmp eq i32 %6, -1
br i1 %7, label %16, label %8
8: ; preds = %3
%9 = trunc i32 %6 to i8
store i8 %9, i8* %4
%10 = getelementptr inbounds i8, i8* %4, i64 1
%11 = add nuw nsw i32 %5, 1
%12 = icmp slt i32 %11, %1
%13 = icmp ne i32 %6, 10
%14 = select i1 %12, i1 %13, i1 false
br i1 %14, label %3, label %15
15: ; preds = %8
store i8 0, i8* %10
br label %16
16: ; preds = %3, %15
%17 = phi i8* [ %10, %15 ], [ null, %3 ]
ret i8* %17
}