#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*/ cprintf(char *, int);
int mxc_serial_getc(char *);
unsigned int readline(char *const buf, const unsigned int size) {
  unsigned int i = 0;
  int err = 0;
  while (i < size - 1 && err == 0) {
    err = mxc_serial_getc(&buf[i]);
    if (err == 0) {
      if (buf[i] == '\n' || buf[i] == '\r') {
        break;
      }
      ++i;
    } else if (err == -1) {
      if (i == 0) {
        break;
      } else {
        err = 0;
        continue;
      }
    } else {
      cprintf("getc error: %x\n", err);
      break;
    }
  }
  buf[i] = '\0';
  return i;
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%ecx
mov    -0xc(%rbp),%edx
sub    $0x1,%edx
xor    %eax,%eax
cmp    %edx,%ecx
mov    %al,-0x15(%rbp)
jae    115d <readline+0x3d>
cmpl   $0x0,-0x14(%rbp)
sete   %al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
test   $0x1,%al
jne    116d <readline+0x4d>
jmp    1202 <readline+0xe2>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%eax
add    %rax,%rdi
call   1040 <mxc_serial_getc@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    11c4 <readline+0xa4>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
je     11b1 <readline+0x91>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
movsbl (%rax,%rcx,1),%eax
cmp    $0xd,%eax
jne    11b6 <readline+0x96>
jmp    1202 <readline+0xe2>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11fd <readline+0xdd>
cmpl   $0xffffffff,-0x14(%rbp)
jne    11e9 <readline+0xc9>
cmpl   $0x0,-0x10(%rbp)
jne    11dd <readline+0xbd>
jmp    1202 <readline+0xe2>
movl   $0x0,-0x14(%rbp)
jmp    113d <readline+0x1d>
mov    -0x14(%rbp),%esi
lea    0xe0d(%rip),%rdi
call   1030 <cprintf@plt>
jmp    1202 <readline+0xe2>
jmp    113d <readline+0x1d>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r15d
mov    %rdi,%r14
add    $0xffffffff,%r15d
je     1191 <readline+0x71>
mov    $0x1,%bpl
xor    %r12d,%r12d
mov    %r14,%rbx
nopl   (%rax)
mov    %rbx,%rdi
call   1040 <mxc_serial_getc@plt>
cmp    $0xffffffff,%eax
jne    1155 <readline+0x35>
test   $0x1,%bpl
je     1140 <readline+0x20>
jmp    117b <readline+0x5b>
test   %eax,%eax
jne    1180 <readline+0x60>
movzbl (%r14,%r12,1),%eax
cmp    $0xa,%al
je     118e <readline+0x6e>
cmp    $0xd,%al
je     118e <readline+0x6e>
lea    (%r14,%r12,1),%rbx
add    $0x1,%rbx
add    $0x1,%r12
xor    %ebp,%ebp
cmp    %r15,%r12
jne    1140 <readline+0x20>
jmp    1191 <readline+0x71>
xor    %r15d,%r15d
jmp    1191 <readline+0x71>
lea    0xe79(%rip),%rdi
mov    %eax,%esi
call   1030 <cprintf@plt>
mov    %r12d,%r15d
mov    %r15d,%eax
movb   $0x0,(%r14,%rax,1)
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"getc error: %x\0A\00"
define i32 @readline(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 0, i32* %5
store i32 0, i32* %6
br label %7
7: ; preds = %56, %52, %2
%8 = load i32, i32* %5
%9 = load i32, i32* %4
%10 = sub i32 %9, 1
%11 = icmp ult i32 %8, %10
br i1 %11, label %12, label %15
12: ; preds = %7
%13 = load i32, i32* %6
%14 = icmp eq i32 %13, 0
br label %15
15: ; preds = %12, %7
%16 = phi i1 [ false, %7 ], [ %14, %12 ]
br i1 %16, label %17, label %57
17: ; preds = %15
%18 = load i8*, i8** %3
%19 = load i32, i32* %5
%20 = zext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
%22 = call i32 @mxc_serial_getc(i8* %21)
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %45
25: ; preds = %17
%26 = load i8*, i8** %3
%27 = load i32, i32* %5
%28 = zext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp eq i32 %31, 10
br i1 %32, label %41, label %33
33: ; preds = %25
%34 = load i8*, i8** %3
%35 = load i32, i32* %5
%36 = zext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp eq i32 %39, 13
br i1 %40, label %41, label %42
41: ; preds = %33, %25
br label %57
42: ; preds = %33
%43 = load i32, i32* %5
%44 = add i32 %43, 1
store i32 %44, i32* %5
br label %56
45: ; preds = %17
%46 = load i32, i32* %6
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %53
48: ; preds = %45
%49 = load i32, i32* %5
%50 = icmp eq i32 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %48
br label %57
52: ; preds = %48
store i32 0, i32* %6
br label %7
53: ; preds = %45
%54 = load i32, i32* %6
%55 = call i32 @cprintf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %54)
br label %57
56: ; preds = %42
br label %7
57: ; preds = %53, %51, %41, %15
%58 = load i8*, i8** %3
%59 = load i32, i32* %5
%60 = zext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %58, i64 %60
store i8 0, i8* %61
%62 = load i32, i32* %5
ret i32 %62
}

LLVM IR (-O2):
@.str = [16 x i8] c"getc error: %x\0A\00"
define i32 @readline(i8* %0, i32 %1) {
%3 = add i32 %1, -1
%4 = zext i32 %3 to i64
%5 = icmp eq i32 %3, 0
br i1 %5, label %24, label %6
6: ; preds = %2, %14
%7 = phi i1 [ false, %14 ], [ true, %2 ]
%8 = phi i8* [ %16, %14 ], [ %0, %2 ]
%9 = phi i64 [ %15, %14 ], [ 0, %2 ]
br label %10
10: ; preds = %6, %18
%11 = tail call i32 @mxc_serial_getc(i8* %8)
switch i32 %11, label %19 [
i32 0, label %12
i32 -1, label %18
]
12: ; preds = %10
%13 = load i8, i8* %8
switch i8 %13, label %14 [
i8 10, label %22
i8 13, label %22
]
14: ; preds = %12
%15 = add nuw nsw i64 %9, 1
%16 = getelementptr inbounds i8, i8* %0, i64 %15
%17 = icmp eq i64 %15, %4
br i1 %17, label %24, label %6
18: ; preds = %10
br i1 %7, label %24, label %10
19: ; preds = %10
%20 = trunc i64 %9 to i32
%21 = tail call i32 @cprintf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %11)
br label %24
22: ; preds = %12, %12
%23 = trunc i64 %9 to i32
br label %24
24: ; preds = %14, %18, %2, %22, %19
%25 = phi i32 [ %20, %19 ], [ %23, %22 ], [ %3, %2 ], [ 0, %18 ], [ %3, %14 ]
%26 = zext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %0, i64 %26
store i8 0, i8* %27
ret i32 %25
}