#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 vmfile {
  int dummy;
};
typedef scalar_t__ ssize_t;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ ENODATA;
scalar_t__ errno;
size_t vmfile_ravail(struct vmfile *);
int /*<<< orphan*/ vmfile_rloc(struct vmfile *);
int /*<<< orphan*/ vmfile_rseek(struct vmfile *, scalar_t__);
scalar_t__ write(int, int /*<<< orphan*/, size_t);
int vmfile_write(struct vmfile *vmb, int fd) {
  ssize_t res;
  size_t rav;
  while ((rav = vmfile_ravail(vmb)) > 0) {
    res = write(fd, vmfile_rloc(vmb), rav);
    if (res < 0)
      return (EAGAIN == errno ? 0 : -1);
    else if (res > 0)
      vmfile_rseek(vmb, res);
    else
      return errno = ENODATA, -1;
  }
  return 1;
}


x86 assembly (clang, -O0):
<vmfile_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <vmfile_ravail@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jbe    1202 <vmfile_write+0xc2>
mov    -0x14(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <vmfile_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 <vmfile_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 <vmfile_write+0xc9>
cmpq   $0x0,-0x20(%rbp)
jle    11d8 <vmfile_write+0x98>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <vmfile_rseek@plt>
jmp    11f8 <vmfile_write+0xb8>
mov    0x2e09(%rip),%rax
mov    (%rax),%rcx
mov    0x2def(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1209 <vmfile_write+0xc9>
jmp    11fd <vmfile_write+0xbd>
jmp    114f <vmfile_write+0xf>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vmfile_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%rbx
call   1040 <vmfile_ravail@plt>
mov    $0x1,%r14d
test   %rax,%rax
je     11d3 <vmfile_write+0x93>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1060 <vmfile_rloc@plt>
mov    %r15d,%edi
mov    %eax,%esi
mov    %rbp,%rdx
call   1030 <write@plt>
test   %rax,%rax
js     1199 <vmfile_write+0x59>
je     11b9 <vmfile_write+0x79>
mov    %rbx,%rdi
mov    %rax,%rsi
call   1050 <vmfile_rseek@plt>
mov    %rbx,%rdi
call   1040 <vmfile_ravail@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    1160 <vmfile_write+0x20>
jmp    11d3 <vmfile_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 <vmfile_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.vmfile = type { i32 }
@EAGAIN = global i64 0
@errno = global i64 0
@ENODATA = global i64 0
define i32 @vmfile_write(%struct.vmfile* %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.vmfile*
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
store %struct.vmfile* %0, %struct.vmfile** %4
store i32 %1, i32* %5
br label %8
8: ; preds = %36, %2
%9 = load %struct.vmfile*, %struct.vmfile** %4
%10 = call i64 @vmfile_ravail(%struct.vmfile* %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.vmfile*, %struct.vmfile** %4
%15 = call i32 @vmfile_rloc(%struct.vmfile* %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.vmfile*, %struct.vmfile** %4
%31 = load i64, i64* %6
%32 = call i32 @vmfile_rseek(%struct.vmfile* %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.vmfile = type { i32 }
@EAGAIN = global i64 0
@errno = global i64 0
@ENODATA = global i64 0
define i32 @vmfile_write(%struct.vmfile* %0, i32 %1) {
%3 = tail call i64 @vmfile_ravail(%struct.vmfile* %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 @vmfile_rloc(%struct.vmfile* %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 @vmfile_rseek(%struct.vmfile* %0, i64 %8)
%19 = tail call i64 @vmfile_ravail(%struct.vmfile* %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
}