#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 EINVAL;
int errno;
scalar_t__ read(int, void *, int);
int read_file(void *pvFile, void *pvBuf, size_t cbBuf) {
  int cb;
  errno = 0;
  cb = (int)read(*(int *)pvFile, pvBuf, (int)cbBuf);
  if (cb >= 0)
    return (int)cb;
  if (!errno)
    return -EINVAL;
  return -errno;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2eb5(%rip),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    %eax,%edx
call   1030 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     115e <read_file+0x4e>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    118f <read_file+0x7f>
mov    0x2e7b(%rip),%rax
cmpl   $0x0,(%rax)
jne    1181 <read_file+0x71>
xor    %eax,%eax
mov    0x2e61(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    118f <read_file+0x7f>
xor    %eax,%eax
mov    0x2e56(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbx
mov    0x2ec8(%rip),%rbx
movl   $0x0,(%rbx)
mov    (%rdi),%edi
call   1030 <read@plt>
test   %eax,%eax
js     112b <read_file+0x1b>
pop    %rbx
ret
mov    (%rbx),%eax
test   %eax,%eax
je     1135 <read_file+0x25>
neg    %eax
pop    %rbx
ret
mov    0x2e9c(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
pop    %rbx
ret

LLVM IR (-O0):
@errno = global i32 0
@EINVAL = global i32 0
define i32 @read_file(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 0, i32* @errno
%9 = load i8*, i8** %5
%10 = bitcast i8* %9 to i32*
%11 = load i32, i32* %10
%12 = load i8*, i8** %6
%13 = load i64, i64* %7
%14 = trunc i64 %13 to i32
%15 = call i64 @read(i32 %11, i8* %12, i32 %14)
%16 = trunc i64 %15 to i32
store i32 %16, i32* %8
%17 = load i32, i32* %8
%18 = icmp sge i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %3
%20 = load i32, i32* %8
store i32 %20, i32* %4
br label %30
21: ; preds = %3
%22 = load i32, i32* @errno
%23 = icmp ne i32 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %21
%25 = load i32, i32* @EINVAL
%26 = sub nsw i32 0, %25
store i32 %26, i32* %4
br label %30
27: ; preds = %21
%28 = load i32, i32* @errno
%29 = sub nsw i32 0, %28
store i32 %29, i32* %4
br label %30
30: ; preds = %27, %24, %19
%31 = load i32, i32* %4
ret i32 %31
}

LLVM IR (-O2):
@errno = global i32 0
@EINVAL = global i32 0
define i32 @read_file(i8* nocapture %0, i8* nocapture %1, i64 %2) {
store i32 0, i32* @errno
%4 = bitcast i8* %0 to i32*
%5 = load i32, i32* %4
%6 = trunc i64 %2 to i32
%7 = tail call i64 @read(i32 %5, i8* %1, i32 %6)
%8 = trunc i64 %7 to i32
%9 = icmp sgt i32 %8, -1
br i1 %9, label %18, label %10
10: ; preds = %3
%11 = load i32, i32* @errno
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = load i32, i32* @EINVAL
%15 = sub nsw i32 0, %14
br label %18
16: ; preds = %10
%17 = sub nsw i32 0, %11
br label %18
18: ; preds = %3, %16, %13
%19 = phi i32 [ %17, %16 ], [ %15, %13 ], [ %8, %3 ]
ret i32 %19
}