#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;
int console_rxChar();
int /*<<< orphan*/ errno;
int _usbdm_read(int file, char *ptr, int len) {
  if (file != STDIN_FILENO) {
    errno = EBADF;
    return -1;
  }
  int done = 0;
  int ch;
  do {
    ch = console_rxChar();
    *ptr++ = (char)ch;
  } while ((++done < len) && (ch != '\n'));
  return done;
}


x86 assembly (clang, -O0):
<_usbdm_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%eax
mov    0x2ea4(%rip),%rcx
cmp    (%rcx),%eax
je     1152 <_usbdm_read+0x42>
mov    0x2e9d(%rip),%rax
mov    (%rax),%ecx
mov    0x2e9c(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11ab <_usbdm_read+0x9b>
movl   $0x0,-0x18(%rbp)
mov    $0x0,%al
call   1030 <console_rxChar@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x18(%rbp),%ecx
add    $0x1,%ecx
mov    %ecx,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x14(%rbp),%ecx
mov    %al,-0x1d(%rbp)
jge    119a <_usbdm_read+0x8a>
cmpl   $0xa,-0x1c(%rbp)
setne  %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    1159 <_usbdm_read+0x49>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_usbdm_read>:
push   %rbp
push   %r14
push   %rbx
mov    0x2eb5(%rip),%rax
cmp    %edi,(%rax)
jne    114a <_usbdm_read+0x3a>
mov    %edx,%r14d
mov    %rsi,%rbx
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <console_rxChar@plt>
mov    %al,(%rbx,%rbp,1)
add    $0x1,%rbp
cmp    %r14d,%ebp
jge    1161 <_usbdm_read+0x51>
cmp    $0xa,%eax
jne    1130 <_usbdm_read+0x20>
jmp    1161 <_usbdm_read+0x51>
mov    0x2e87(%rip),%rax
mov    (%rax),%eax
mov    0x2e86(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@EBADF = global i32 0
@errno = global i32 0
define i32 @_usbdm_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 %8, label %6
6: ; preds = %3
%7 = load i32, i32* @EBADF
store i32 %7, i32* @errno
br label %18
8: ; preds = %3, %8
%9 = phi i8* [ %13, %8 ], [ %1, %3 ]
%10 = phi i32 [ %14, %8 ], [ 0, %3 ]
%11 = tail call i32 (...) @console_rxChar()
%12 = trunc i32 %11 to i8
%13 = getelementptr inbounds i8, i8* %9, i64 1
store i8 %12, i8* %9
%14 = add nuw nsw i32 %10, 1
%15 = icmp slt i32 %14, %2
%16 = icmp ne i32 %11, 10
%17 = select i1 %15, i1 %16, i1 false
br i1 %17, label %8, label %18
18: ; preds = %8, %6
%19 = phi i32 [ -1, %6 ], [ %14, %8 ]
ret i32 %19
}