#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ feof(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int fread(void *, int, int, int /*<<< orphan*/ *);
int stdio_read(void *data, void *buf, int len, int *err) {
  FILE *f = (FILE *)data;
  int n;
  if (len == 0)
    return 0;
  n = fread(buf, 1, len, f);
  if (n != len) {
    *err = -11;
    if (feof(f))
      *err = -1;
    if (ferror(f))
      *err = -11;
  }
  return n;
}


x86 assembly (clang, -O0):
<stdio_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1165 <stdio_read+0x35>
movl   $0x0,-0x4(%rbp)
jmp    11d8 <stdio_read+0xa8>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     11d2 <stdio_read+0xa2>
mov    -0x28(%rbp),%rax
movl   $0xfffffff5,(%rax)
mov    -0x30(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%rax
je     11b0 <stdio_read+0x80>
mov    -0x28(%rbp),%rax
movl   $0xffffffff,(%rax)
mov    -0x30(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     11cd <stdio_read+0x9d>
mov    -0x28(%rbp),%rax
movl   $0xfffffff5,(%rax)
jmp    11d2 <stdio_read+0xa2>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stdio_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edx,%edx
je     118e <stdio_read+0x5e>
mov    %rcx,%r14
mov    %edx,%ebp
mov    %rdi,%rbx
mov    %rsi,%rdi
mov    $0x1,%esi
mov    %rbx,%rcx
call   1040 <fread@plt>
cmp    %ebp,%eax
je     1190 <stdio_read+0x60>
mov    %eax,%r15d
movl   $0xfffffff5,(%r14)
mov    %rbx,%rdi
call   1050 <feof@plt>
test   %rax,%rax
je     1175 <stdio_read+0x45>
movl   $0xffffffff,(%r14)
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     1189 <stdio_read+0x59>
movl   $0xfffffff5,(%r14)
mov    %r15d,%ebp
jmp    1190 <stdio_read+0x60>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @stdio_read(i8* %0, i8* %1, i32 %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32* %3, i32** %9
%12 = load i8*, i8** %6
%13 = bitcast i8* %12 to i32*
store i32* %13, i32** %10
%14 = load i32, i32* %8
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %4
store i32 0, i32* %5
br label %41
17: ; preds = %4
%18 = load i8*, i8** %7
%19 = load i32, i32* %8
%20 = load i32*, i32** %10
%21 = call i32 @fread(i8* %18, i32 1, i32 %19, i32* %20)
store i32 %21, i32* %11
%22 = load i32, i32* %11
%23 = load i32, i32* %8
%24 = icmp ne i32 %22, %23
br i1 %24, label %25, label %39
25: ; preds = %17
%26 = load i32*, i32** %9
store i32 -11, i32* %26
%27 = load i32*, i32** %10
%28 = call i64 @feof(i32* %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = load i32*, i32** %9
store i32 -1, i32* %31
br label %32
32: ; preds = %30, %25
%33 = load i32*, i32** %10
%34 = call i64 @ferror(i32* %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = load i32*, i32** %9
store i32 -11, i32* %37
br label %38
38: ; preds = %36, %32
br label %39
39: ; preds = %38, %17
%40 = load i32, i32* %11
store i32 %40, i32* %5
br label %41
41: ; preds = %39, %16
%42 = load i32, i32* %5
ret i32 %42
}

LLVM IR (-O2):
define i32 @stdio_read(i8* nocapture %0, i8* nocapture %1, i32 %2, i32* nocapture writeonly %3) {
%5 = bitcast i8* %0 to i32*
%6 = icmp eq i32 %2, 0
br i1 %6, label %18, label %7
7: ; preds = %4
%8 = tail call i32 @fread(i8* %1, i32 1, i32 %2, i32* %5)
%9 = icmp eq i32 %8, %2
br i1 %9, label %18, label %10
10: ; preds = %7
store i32 -11, i32* %3
%11 = tail call i64 @feof(i32* %5)
%12 = icmp eq i64 %11, 0
br i1 %12, label %14, label %13
13: ; preds = %10
store i32 -1, i32* %3
br label %14
14: ; preds = %13, %10
%15 = tail call i64 @ferror(i32* %5)
%16 = icmp eq i64 %15, 0
br i1 %16, label %18, label %17
17: ; preds = %14
store i32 -11, i32* %3
br label %18
18: ; preds = %7, %17, %14, %4
%19 = phi i32 [ 0, %4 ], [ %8, %14 ], [ %8, %17 ], [ %2, %7 ]
ret i32 %19
}