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


x86 assembly (clang, -O0):
<_read>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%eax
sub    $0x80,%eax
jne    116f <_read+0x6f>
jmp    1128 <_read+0x28>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    116a <_read+0x6a>
movb   $0x0,-0x1d(%rbp)
mov    -0x1d(%rbp),%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    112f <_read+0x2f>
jmp    118d <_read+0x8d>
mov    0x2e62(%rip),%rax
mov    (%rax),%ecx
mov    0x2e61(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1193 <_read+0x93>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<_read>:
push   %rbx
cmp    $0x80,%edi
jne    112d <_read+0x1d>
mov    %edx,%ebx
test   %edx,%edx
jle    1146 <_read+0x36>
mov    %ebx,%edx
mov    %rsi,%rdi
xor    %esi,%esi
call   1030 <memset@plt>
jmp    1148 <_read+0x38>
mov    0x2ea4(%rip),%rax
mov    (%rax),%eax
mov    0x2ea3(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebx
jmp    1148 <_read+0x38>
xor    %ebx,%ebx
mov    %ebx,%eax
pop    %rbx
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
%10 = alloca i8
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %9
%11 = load i32, i32* %5
switch i32 %11, label %27 [
i32 128, label %12
]
12: ; preds = %3
store i32 0, i32* %8
br label %13
13: ; preds = %23, %12
%14 = load i32, i32* %8
%15 = load i32, i32* %7
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %26
17: ; preds = %13
store i8 0, i8* %10
%18 = load i8, i8* %10
%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 = %17
%24 = load i32, i32* %8
%25 = add nsw i32 %24, 1
store i32 %25, i32* %8
br label %13
26: ; preds = %13
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 %9
5: ; preds = %3
%6 = icmp sgt i32 %2, 0
br i1 %6, label %7, label %11
7: ; preds = %5
%8 = zext i32 %2 to i64
br label %11
9: ; preds = %3
%10 = load i32, i32* @EBADF
store i32 %10, i32* @errno
br label %11
11: ; preds = %7, %5, %9
%12 = phi i32 [ -1, %9 ], [ 0, %5 ], [ %2, %7 ]
ret i32 %12
}