#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*/ EAGAIN;
int /*<<< orphan*/ EBADF;
#define STDIN_FILENO 128
int /*<<< orphan*/ errno;
int serial_read(char *, int);
int _read(int file, char *ptr, int len) {
  int num = 0;
  switch (file) {
  case STDIN_FILENO:
    num = serial_read(ptr, len);
    if (num < len)
      errno = EAGAIN;
    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,-0x18(%rbp)
mov    -0x8(%rbp),%eax
sub    $0x80,%eax
jne    116e <_read+0x5e>
jmp    113c <_read+0x2c>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <serial_read@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1169 <_read+0x59>
mov    0x2e6a(%rip),%rax
mov    (%rax),%ecx
mov    0x2e79(%rip),%rax
mov    %ecx,(%rax)
jmp    118c <_read+0x7c>
mov    0x2e63(%rip),%rax
mov    (%rax),%ecx
mov    0x2e62(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1192 <_read+0x82>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_read>:
push   %rbx
cmp    $0x80,%edi
jne    1132 <_read+0x22>
mov    %edx,%ebx
mov    %rsi,%rdi
mov    %edx,%esi
call   1030 <serial_read@plt>
cmp    %ebx,%eax
jge    1149 <_read+0x39>
mov    0x2e98(%rip),%rcx
jmp    113e <_read+0x2e>
mov    $0xffffffff,%eax
mov    0x2e9a(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2e99(%rip),%rdx
mov    %ecx,(%rdx)
pop    %rbx
ret

LLVM IR (-O0):
@EAGAIN = global i32 0
@errno = global i32 0
@EBADF = 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
store i32 0, i32* %8
%9 = load i32, i32* %5
switch i32 %9, label %20 [
i32 128, label %10
]
10: ; preds = %3
%11 = load i8*, i8** %6
%12 = load i32, i32* %7
%13 = call i32 @serial_read(i8* %11, i32 %12)
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = load i32, i32* %7
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %19
17: ; preds = %10
%18 = load i32, i32* @EAGAIN
store i32 %18, i32* @errno
br label %19
19: ; preds = %17, %10
br label %22
20: ; preds = %3
%21 = load i32, i32* @EBADF
store i32 %21, i32* @errno
store i32 -1, i32* %4
br label %24
22: ; preds = %19
%23 = load i32, i32* %8
store i32 %23, i32* %4
br label %24
24: ; preds = %22, %20
%25 = load i32, i32* %4
ret i32 %25
}

LLVM IR (-O2):
@EAGAIN = global i32 0
@errno = global i32 0
@EBADF = global i32 0
define i32 @_read(i32 %0, i8* %1, i32 %2) {
%4 = icmp eq i32 %0, 128
br i1 %4, label %5, label %8
5: ; preds = %3
%6 = tail call i32 @serial_read(i8* %1, i32 %2)
%7 = icmp slt i32 %6, %2
br i1 %7, label %8, label %12
8: ; preds = %3, %5
%9 = phi i32* [ @EAGAIN, %5 ], [ @EBADF, %3 ]
%10 = phi i32 [ %6, %5 ], [ -1, %3 ]
%11 = load i32, i32* %9
store i32 %11, i32* @errno
br label %12
12: ; preds = %8, %5
%13 = phi i32 [ %6, %5 ], [ %10, %8 ]
ret i32 %13
}