#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 scalar_t__ ssize_t;
typedef int /*<<< orphan*/ off_t;

/* Variables and functions */
int /*<<< orphan*/ ENOSPC;
int /*<<< orphan*/ errno;
scalar_t__ xpwrite(int, char const *, size_t, int /*<<< orphan*/);
ssize_t pwrite_in_full(int fd, const void *buf, size_t count, off_t offset) {
  const char *p = buf;
  ssize_t total = 0;
  while (count > 0) {
    ssize_t nr;
    nr = xpwrite(fd, p, count, offset);
    if (nr < 0)
      return -1;
    if (nr == 0) {
      errno = ENOSPC;
      return -1;
    }
    count -= nr;
    total += nr;
    p += nr;
    offset += nr;
  }
  return total;
}


x86 assembly (clang, -O0):
<pwrite_in_full>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
cmpq   $0x0,-0x20(%rbp)
jbe    11d4 <pwrite_in_full+0xc4>
mov    -0xc(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x24(%rbp),%ecx
call   1030 <xpwrite@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jge    1170 <pwrite_in_full+0x60>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11dc <pwrite_in_full+0xcc>
cmpq   $0x0,-0x40(%rbp)
jne    119a <pwrite_in_full+0x8a>
mov    0x2e56(%rip),%rax
mov    (%rax),%ecx
mov    0x2e55(%rip),%rax
mov    %ecx,(%rax)
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11dc <pwrite_in_full+0xcc>
mov    -0x40(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x40(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x40(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x40(%rbp),%rcx
movslq -0x24(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x24(%rbp)
jmp    1136 <pwrite_in_full+0x26>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pwrite_in_full>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %rdx,%rdx
je     1158 <pwrite_in_full+0x48>
mov    %ecx,%r15d
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %edi,%r12d
xor    %r14d,%r14d
nopl   0x0(%rax)
mov    %r12d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
mov    %r15d,%ecx
call   1030 <xpwrite@plt>
test   %rax,%rax
js     116f <pwrite_in_full+0x5f>
je     115d <pwrite_in_full+0x4d>
add    %rax,%r14
add    %rax,%rbp
add    %eax,%r15d
sub    %rax,%rbx
jne    1130 <pwrite_in_full+0x20>
jmp    1176 <pwrite_in_full+0x66>
xor    %r14d,%r14d
jmp    1176 <pwrite_in_full+0x66>
mov    0x2e74(%rip),%rax
mov    (%rax),%eax
mov    0x2e73(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffffffffffff,%r14
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@ENOSPC = global i32 0
@errno = global i32 0
define i64 @pwrite_in_full(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i64
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i64
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i32 %3, i32* %9
%13 = load i8*, i8** %7
store i8* %13, i8** %10
store i64 0, i64* %11
br label %14
14: ; preds = %31, %4
%15 = load i64, i64* %8
%16 = icmp ugt i64 %15, 0
br i1 %16, label %17, label %46
17: ; preds = %14
%18 = load i32, i32* %6
%19 = load i8*, i8** %10
%20 = load i64, i64* %8
%21 = load i32, i32* %9
%22 = call i64 @xpwrite(i32 %18, i8* %19, i64 %20, i32 %21)
store i64 %22, i64* %12
%23 = load i64, i64* %12
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %17
store i64 -1, i64* %5
br label %48
26: ; preds = %17
%27 = load i64, i64* %12
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = load i32, i32* @ENOSPC
store i32 %30, i32* @errno
store i64 -1, i64* %5
br label %48
31: ; preds = %26
%32 = load i64, i64* %12
%33 = load i64, i64* %8
%34 = sub i64 %33, %32
store i64 %34, i64* %8
%35 = load i64, i64* %12
%36 = load i64, i64* %11
%37 = add nsw i64 %36, %35
store i64 %37, i64* %11
%38 = load i64, i64* %12
%39 = load i8*, i8** %10
%40 = getelementptr inbounds i8, i8* %39, i64 %38
store i8* %40, i8** %10
%41 = load i64, i64* %12
%42 = load i32, i32* %9
%43 = sext i32 %42 to i64
%44 = add nsw i64 %43, %41
%45 = trunc i64 %44 to i32
store i32 %45, i32* %9
br label %14
46: ; preds = %14
%47 = load i64, i64* %11
store i64 %47, i64* %5
br label %48
48: ; preds = %46, %29, %25
%49 = load i64, i64* %5
ret i64 %49
}

LLVM IR (-O2):
@ENOSPC = global i32 0
@errno = global i32 0
define i64 @pwrite_in_full(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = icmp eq i64 %2, 0
br i1 %5, label %24, label %6
6: ; preds = %4, %17
%7 = phi i64 [ %19, %17 ], [ 0, %4 ]
%8 = phi i8* [ %20, %17 ], [ %1, %4 ]
%9 = phi i32 [ %22, %17 ], [ %3, %4 ]
%10 = phi i64 [ %18, %17 ], [ %2, %4 ]
%11 = tail call i64 @xpwrite(i32 %0, i8* %8, i64 %10, i32 %9)
%12 = icmp slt i64 %11, 0
br i1 %12, label %24, label %13
13: ; preds = %6
%14 = icmp eq i64 %11, 0
br i1 %14, label %15, label %17
15: ; preds = %13
%16 = load i32, i32* @ENOSPC
store i32 %16, i32* @errno
br label %24
17: ; preds = %13
%18 = sub i64 %10, %11
%19 = add nsw i64 %11, %7
%20 = getelementptr inbounds i8, i8* %8, i64 %11
%21 = trunc i64 %11 to i32
%22 = add i32 %9, %21
%23 = icmp eq i64 %18, 0
br i1 %23, label %24, label %6
24: ; preds = %17, %6, %4, %15
%25 = phi i64 [ -1, %15 ], [ 0, %4 ], [ %19, %17 ], [ -1, %6 ]
ret i64 %25
}