#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 */
scalar_t__ EAGAIN;
scalar_t__ ENODATA;
scalar_t__ errno;
size_t vmbuf_ravail(struct vmbuf *);
int /*<<< orphan*/ vmbuf_rloc(struct vmbuf *);
int /*<<< orphan*/ vmbuf_rseek(struct vmbuf *, scalar_t__);
scalar_t__ write(int, int /*<<< orphan*/, size_t);
int vmbuf_write(struct vmbuf *vmbuf, int fd) {
  ssize_t res;
  size_t rav;
  while ((rav = vmbuf_ravail(vmbuf)) > 0) {
    res = write(fd, vmbuf_rloc(vmbuf), rav);
    if (res < 0)
      return (EAGAIN == errno ? 0 : -1);
    else if (res > 0)
      vmbuf_rseek(vmbuf, res);
    else
      return errno = ENODATA, -1;
  }
  return 1;
}


x86 assembly (clang, -O0):
<vmbuf_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <vmbuf_ravail@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jbe    1202 <vmbuf_write+0xc2>
mov    -0x14(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <vmbuf_rloc@plt>
mov    -0x2c(%rbp),%edi
mov    %eax,%esi
mov    -0x28(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11bb <vmbuf_write+0x7b>
mov    0x2e2f(%rip),%rax
mov    (%rax),%rdx
mov    0x2e35(%rip),%rax
mov    (%rax),%rsi
mov    $0xffffffff,%eax
xor    %ecx,%ecx
cmp    %rsi,%rdx
cmove  %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    1209 <vmbuf_write+0xc9>
cmpq   $0x0,-0x20(%rbp)
jle    11d8 <vmbuf_write+0x98>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <vmbuf_rseek@plt>
jmp    11f8 <vmbuf_write+0xb8>
mov    0x2e09(%rip),%rax
mov    (%rax),%rcx
mov    0x2def(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1209 <vmbuf_write+0xc9>
jmp    11fd <vmbuf_write+0xbd>
jmp    114f <vmbuf_write+0xf>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vmbuf_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%rbx
call   1050 <vmbuf_ravail@plt>
mov    $0x1,%r14d
test   %rax,%rax
je     11d3 <vmbuf_write+0x93>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1060 <vmbuf_rloc@plt>
mov    %r15d,%edi
mov    %eax,%esi
mov    %rbp,%rdx
call   1030 <write@plt>
test   %rax,%rax
js     1199 <vmbuf_write+0x59>
je     11b9 <vmbuf_write+0x79>
mov    %rbx,%rdi
mov    %rax,%rsi
call   1040 <vmbuf_rseek@plt>
mov    %rbx,%rdi
call   1050 <vmbuf_ravail@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    1160 <vmbuf_write+0x20>
jmp    11d3 <vmbuf_write+0x93>
mov    0x2e28(%rip),%rax
mov    (%rax),%rax
mov    0x2e2e(%rip),%rcx
xor    %r14d,%r14d
cmp    (%rcx),%rax
setne  %r14b
neg    %r14d
jmp    11d3 <vmbuf_write+0x93>
mov    0x2e28(%rip),%rax
mov    (%rax),%rax
mov    0x2e0e(%rip),%rcx
mov    %rax,(%rcx)
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.vmbuf = type { i32 }
@EAGAIN = global i64 0
@errno = global i64 0
@ENODATA = global i64 0
define i32 @vmbuf_write(%struct.vmbuf* %0, i32 %1) {
%3 = tail call i64 @vmbuf_ravail(%struct.vmbuf* %0)
%4 = icmp eq i64 %3, 0
br i1 %4, label %23, label %5
5: ; preds = %2, %17
%6 = phi i64 [ %19, %17 ], [ %3, %2 ]
%7 = tail call i32 @vmbuf_rloc(%struct.vmbuf* %0)
%8 = tail call i64 @write(i32 %1, i32 %7, i64 %6)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %5
%11 = load i64, i64* @EAGAIN
%12 = load i64, i64* @errno
%13 = icmp ne i64 %11, %12
%14 = sext i1 %13 to i32
br label %23
15: ; preds = %5
%16 = icmp eq i64 %8, 0
br i1 %16, label %21, label %17
17: ; preds = %15
%18 = tail call i32 @vmbuf_rseek(%struct.vmbuf* %0, i64 %8)
%19 = tail call i64 @vmbuf_ravail(%struct.vmbuf* %0)
%20 = icmp eq i64 %19, 0
br i1 %20, label %23, label %5
21: ; preds = %15
%22 = load i64, i64* @ENODATA
store i64 %22, i64* @errno
br label %23
23: ; preds = %17, %2, %21, %10
%24 = phi i32 [ %14, %10 ], [ -1, %21 ], [ 1, %2 ], [ 1, %17 ]
ret i32 %24
}