#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 */
struct vmbuf {
  int dummy;
};
typedef scalar_t__ ssize_t;

/* Variables and functions */
int EAGAIN;
int errno;
scalar_t__ read(int, int /*<<< orphan*/, scalar_t__);
scalar_t__ vmbuf_wavail(struct vmbuf *);
int /*<<< orphan*/ vmbuf_wloc(struct vmbuf *);
scalar_t__ vmbuf_wseek(struct vmbuf *, scalar_t__);
int vmbuf_read(struct vmbuf *vmbuf, int fd) {
  ssize_t res;
  ssize_t wavail;
  while (0 <
         (res = read(fd, vmbuf_wloc(vmbuf), wavail = vmbuf_wavail(vmbuf)))) {
    if (0 > vmbuf_wseek(vmbuf, res))
      return -1;
    if (res < wavail)
      return errno = 0, 1;
  }
  if (res < 0)
    return (EAGAIN == errno ? 1 : -1);
  return 0;
}


x86 assembly (clang, -O0):
<vmbuf_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <vmbuf_wloc@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <vmbuf_wavail@plt>
mov    -0x30(%rbp),%edi
mov    -0x2c(%rbp),%esi
mov    %rax,%rdx
mov    %rdx,-0x28(%rbp)
call   1030 <read@plt>
mov    %rax,%rcx
mov    %rcx,-0x20(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jge    11e1 <vmbuf_read+0xa1>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <vmbuf_wseek@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jle    11b5 <vmbuf_read+0x75>
movl   $0xffffffff,-0x4(%rbp)
jmp    121c <vmbuf_read+0xdc>
mov    -0x20(%rbp),%rax
cmp    -0x28(%rbp),%rax
jge    11dc <vmbuf_read+0x9c>
mov    0x2e16(%rip),%rax
movl   $0x0,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    121c <vmbuf_read+0xdc>
jmp    114f <vmbuf_read+0xf>
cmpq   $0x0,-0x20(%rbp)
jge    1215 <vmbuf_read+0xd5>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edx
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
mov    $0xffffffff,%eax
mov    $0x1,%ecx
cmp    %esi,%edx
cmove  %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    121c <vmbuf_read+0xdc>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vmbuf_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r15
nopl   (%rax)
mov    %r15,%rdi
call   1040 <vmbuf_wloc@plt>
mov    %eax,%ebx
mov    %r15,%rdi
call   1050 <vmbuf_wavail@plt>
mov    %rax,%rbp
mov    %r14d,%edi
mov    %ebx,%esi
mov    %rax,%rdx
call   1030 <read@plt>
mov    %rax,%rbx
test   %rax,%rax
jle    11a3 <vmbuf_read+0x63>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1060 <vmbuf_wseek@plt>
test   %rax,%rax
js     11c9 <vmbuf_read+0x89>
cmp    %rbp,%rbx
jge    1150 <vmbuf_read+0x10>
mov    0x2e4a(%rip),%rax
movl   $0x0,(%rax)
mov    $0x1,%eax
jmp    11ce <vmbuf_read+0x8e>
xor    %eax,%eax
test   %rbx,%rbx
jns    11ce <vmbuf_read+0x8e>
mov    0x2e1f(%rip),%rax
mov    (%rax),%eax
mov    0x2e26(%rip),%rcx
xor    %edx,%edx
cmp    (%rcx),%eax
sete   %dl
lea    (%rdx,%rdx,1),%eax
add    $0xffffffff,%eax
jmp    11ce <vmbuf_read+0x8e>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.vmbuf = type { i32 }
@errno = global i32 0
@EAGAIN = global i32 0
define i32 @vmbuf_read(%struct.vmbuf* %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.vmbuf*
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
store %struct.vmbuf* %0, %struct.vmbuf** %4
store i32 %1, i32* %5
br label %8
8: ; preds = %27, %2
%9 = load i32, i32* %5
%10 = load %struct.vmbuf*, %struct.vmbuf** %4
%11 = call i32 @vmbuf_wloc(%struct.vmbuf* %10)
%12 = load %struct.vmbuf*, %struct.vmbuf** %4
%13 = call i64 @vmbuf_wavail(%struct.vmbuf* %12)
store i64 %13, i64* %7
%14 = call i64 @read(i32 %9, i32 %11, i64 %13)
store i64 %14, i64* %6
%15 = icmp slt i64 0, %14
br i1 %15, label %16, label %28
16: ; preds = %8
%17 = load %struct.vmbuf*, %struct.vmbuf** %4
%18 = load i64, i64* %6
%19 = call i64 @vmbuf_wseek(%struct.vmbuf* %17, i64 %18)
%20 = icmp sgt i64 0, %19
br i1 %20, label %21, label %22
21: ; preds = %16
store i32 -1, i32* %3
br label %38
22: ; preds = %16
%23 = load i64, i64* %6
%24 = load i64, i64* %7
%25 = icmp slt i64 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %22
store i32 0, i32* @errno
store i32 1, i32* %3
br label %38
27: ; preds = %22
br label %8
28: ; preds = %8
%29 = load i64, i64* %6
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %37
31: ; preds = %28
%32 = load i32, i32* @EAGAIN
%33 = load i32, i32* @errno
%34 = icmp eq i32 %32, %33
%35 = zext i1 %34 to i64
%36 = select i1 %34, i32 1, i32 -1
store i32 %36, i32* %3
br label %38
37: ; preds = %28
store i32 0, i32* %3
br label %38
38: ; preds = %37, %31, %26, %21
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
%struct.vmbuf = type { i32 }
@errno = global i32 0
@EAGAIN = global i32 0
define i32 @vmbuf_read(%struct.vmbuf* %0, i32 %1) {
br label %3
3: ; preds = %11, %2
%4 = tail call i32 @vmbuf_wloc(%struct.vmbuf* %0)
%5 = tail call i64 @vmbuf_wavail(%struct.vmbuf* %0)
%6 = tail call i64 @read(i32 %1, i32 %4, i64 %5)
%7 = icmp sgt i64 %6, 0
br i1 %7, label %8, label %14
8: ; preds = %3
%9 = tail call i64 @vmbuf_wseek(%struct.vmbuf* %0, i64 %6)
%10 = icmp slt i64 %9, 0
br i1 %10, label %21, label %11
11: ; preds = %8
%12 = icmp slt i64 %6, %5
br i1 %12, label %13, label %3
13: ; preds = %11
store i32 0, i32* @errno
br label %21
14: ; preds = %3
%15 = icmp slt i64 %6, 0
br i1 %15, label %16, label %21
16: ; preds = %14
%17 = load i32, i32* @EAGAIN
%18 = load i32, i32* @errno
%19 = icmp eq i32 %17, %18
%20 = select i1 %19, i32 1, i32 -1
br label %21
21: ; preds = %8, %14, %16, %13
%22 = phi i32 [ 1, %13 ], [ %20, %16 ], [ 0, %14 ], [ -1, %8 ]
ret i32 %22
}