#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*/ EBADF;
int STDIN_FILENO;
char _uart_getc();
int /*<<< orphan*/ errno;
int _read(int file, char *buf, int len) {
  if (STDIN_FILENO == file) {
    int i;
    for (i = 0; i < len; i++) {
      buf[i] = _uart_getc();
      if ('\n' == buf[i]) {
        return i;
      }
    }
    return i;
  } else {
    errno = EBADF;
    return -1;
  }
}


x86 assembly (clang, -O0):
<_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    0x2ea7(%rip),%rax
mov    (%rax),%eax
cmp    -0x8(%rbp),%eax
jne    119d <_read+0x8d>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1192 <_read+0x82>
mov    $0x0,%al
call   1030 <_uart_getc@plt>
mov    %al,%dl
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%ecx
mov    $0xa,%eax
cmp    %ecx,%eax
jne    117f <_read+0x6f>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b6 <_read+0xa6>
jmp    1184 <_read+0x74>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    113b <_read+0x2b>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b6 <_read+0xa6>
mov    0x2e34(%rip),%rax
mov    (%rax),%ecx
mov    0x2e33(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_read>:
push   %r15
push   %r14
push   %rbx
mov    0x2eb4(%rip),%rax
cmp    %edi,(%rax)
jne    114d <_read+0x3d>
test   %edx,%edx
jle    1166 <_read+0x56>
mov    %rsi,%r14
mov    %edx,%r15d
xor    %ebx,%ebx
nopl   0x0(%rax)
xor    %eax,%eax
call   1030 <_uart_getc@plt>
mov    %al,(%r14,%rbx,1)
cmp    $0xa,%al
je     1168 <_read+0x58>
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1130 <_read+0x20>
mov    %r15d,%ebx
jmp    1168 <_read+0x58>
mov    0x2e84(%rip),%rax
mov    (%rax),%eax
mov    0x2e83(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebx
jmp    1168 <_read+0x58>
xor    %ebx,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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