#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;
#define STDIN_FILENO 128
int /*<<< orphan*/ errno;
scalar_t__ usart_getchar();
int _read(int file, char *ptr, int len) {
  int n;
  int num = 0;
  switch (file) {
  case STDIN_FILENO:
    for (n = 0; n < len; n++) {
      *ptr++ = (char)usart_getchar();
      num++;
    }
    break;
  default:
    errno = EBADF;
    return -1;
  }
  return num;
}


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)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%eax
sub    $0x80,%eax
jne    1185 <_read+0x75>
jmp    113c <_read+0x2c>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1180 <_read+0x70>
mov    $0x0,%al
call   1030 <usart_getchar@plt>
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1143 <_read+0x33>
jmp    11a3 <_read+0x93>
mov    0x2e4c(%rip),%rax
mov    (%rax),%ecx
mov    0x2e4b(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11a9 <_read+0x99>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_read>:
push   %r15
push   %r14
push   %rbx
cmp    $0x80,%edi
jne    1146 <_read+0x36>
test   %edx,%edx
jle    1160 <_read+0x50>
mov    %rsi,%r14
mov    %edx,%r15d
xor    %ebx,%ebx
nopl   0x0(%rax)
xor    %eax,%eax
call   1030 <usart_getchar@plt>
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
cmp    %ebx,%r15d
jne    1130 <_read+0x20>
jmp    1163 <_read+0x53>
mov    0x2e8b(%rip),%rax
mov    (%rax),%eax
mov    0x2e8a(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%r15d
jmp    1163 <_read+0x53>
xor    %r15d,%r15d
mov    %r15d,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@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
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %9
%10 = load i32, i32* %5
switch i32 %10, label %27 [
i32 128, label %11
]
11: ; preds = %3
store i32 0, i32* %8
br label %12
12: ; preds = %23, %11
%13 = load i32, i32* %8
%14 = load i32, i32* %7
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %26
16: ; preds = %12
%17 = call i64 (...) @usart_getchar()
%18 = trunc i64 %17 to i8
%19 = load i8*, i8** %6
%20 = getelementptr inbounds i8, i8* %19, i32 1
store i8* %20, i8** %6
store i8 %18, i8* %19
%21 = load i32, i32* %9
%22 = add nsw i32 %21, 1
store i32 %22, i32* %9
br label %23
23: ; preds = %16
%24 = load i32, i32* %8
%25 = add nsw i32 %24, 1
store i32 %25, i32* %8
br label %12
26: ; preds = %12
br label %29
27: ; preds = %3
%28 = load i32, i32* @EBADF
store i32 %28, i32* @errno
store i32 -1, i32* %4
br label %31
29: ; preds = %26
%30 = load i32, i32* %9
store i32 %30, i32* %4
br label %31
31: ; preds = %29, %27
%32 = load i32, i32* %4
ret i32 %32
}

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