#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 /*<<< orphan*/ UartRxEmpty();
char UartRxGetChar();
int UartRxGetLine(char *line, int length) {
  char c;
  while ((length--) && (!UartRxEmpty())) {
    c = UartRxGetChar();
    if ((c == '\r') || (c == '\n'))
      return 1;
    else {
      *line++ = c;
      *line = 0;
    }
  }
  if (length == 0)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<UartRxGetLine>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x14(%rbp),%ecx
mov    %ecx,%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x16(%rbp)
je     115a <UartRxGetLine+0x3a>
mov    $0x0,%al
call   1030 <UartRxEmpty@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0x16(%rbp)
mov    -0x16(%rbp),%al
test   $0x1,%al
jne    116a <UartRxGetLine+0x4a>
jmp    11ba <UartRxGetLine+0x9a>
mov    $0x0,%al
call   1040 <UartRxGetChar@plt>
mov    %al,-0x15(%rbp)
movsbl -0x15(%rbp),%eax
cmp    $0xd,%eax
je     118e <UartRxGetLine+0x6e>
movsbl -0x15(%rbp),%eax
cmp    $0xa,%eax
jne    119a <UartRxGetLine+0x7a>
movl   $0x1,-0x4(%rbp)
jmp    11d7 <UartRxGetLine+0xb7>
mov    -0x15(%rbp),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
jmp    112f <UartRxGetLine+0xf>
cmpl   $0x0,-0x14(%rbp)
jne    11d0 <UartRxGetLine+0xb0>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d7 <UartRxGetLine+0xb7>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<UartRxGetLine>:
push   %rbp
push   %r14
push   %rbx
mov    $0xffffffff,%r14d
test   %esi,%esi
je     1175 <UartRxGetLine+0x55>
mov    %esi,%ebp
mov    %rdi,%rbx
add    $0xffffffff,%ebp
add    $0x1,%rbx
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <UartRxEmpty@plt>
test   %eax,%eax
jne    1172 <UartRxGetLine+0x52>
xor    %eax,%eax
call   1040 <UartRxGetChar@plt>
cmp    $0xa,%al
je     116b <UartRxGetLine+0x4b>
cmp    $0xd,%al
je     116b <UartRxGetLine+0x4b>
mov    %al,-0x1(%rbx)
movb   $0x0,(%rbx)
add    $0x1,%rbx
add    $0xffffffff,%ebp
jb     1140 <UartRxGetLine+0x20>
jmp    1175 <UartRxGetLine+0x55>
mov    $0x1,%eax
jmp    117b <UartRxGetLine+0x5b>
mov    %ebp,%r14d
cmp    $0x1,%r14d
sbb    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @UartRxGetLine(i8* nocapture writeonly %0, i32 %1) {
%3 = icmp eq i32 %1, 0
br i1 %3, label %15, label %4
4: ; preds = %2, %12
%5 = phi i32 [ %7, %12 ], [ %1, %2 ]
%6 = phi i8* [ %13, %12 ], [ %0, %2 ]
%7 = add nsw i32 %5, -1
%8 = tail call i32 (...) @UartRxEmpty()
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %4
%11 = tail call signext i8 (...) @UartRxGetChar()
switch i8 %11, label %12 [
i8 13, label %19
i8 10, label %19
]
12: ; preds = %10
%13 = getelementptr inbounds i8, i8* %6, i64 1
store i8 %11, i8* %6
store i8 0, i8* %13
%14 = icmp eq i32 %7, 0
br i1 %14, label %15, label %4
15: ; preds = %4, %12, %2
%16 = phi i32 [ -1, %2 ], [ -1, %12 ], [ %7, %4 ]
%17 = icmp eq i32 %16, 0
%18 = sext i1 %17 to i32
br label %19
19: ; preds = %10, %10, %15
%20 = phi i32 [ %18, %15 ], [ 1, %10 ], [ 1, %10 ]
ret i32 %20
}