#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;

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


x86 assembly (clang, -O0):
<write_in_full>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
cmpq   $0x0,-0x20(%rbp)
jbe    11c0 <write_in_full+0xb0>
mov    -0xc(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <xwrite@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jge    116a <write_in_full+0x5a>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11c8 <write_in_full+0xb8>
cmpq   $0x0,-0x38(%rbp)
jne    1194 <write_in_full+0x84>
mov    0x2e5c(%rip),%rax
mov    (%rax),%ecx
mov    0x2e5b(%rip),%rax
mov    %ecx,(%rax)
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11c8 <write_in_full+0xb8>
mov    -0x38(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x38(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x38(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1133 <write_in_full+0x23>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_in_full>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdx,%rdx
je     1151 <write_in_full+0x41>
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %edi,%ebp
xor    %r14d,%r14d
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1030 <xwrite@plt>
test   %rax,%rax
js     1168 <write_in_full+0x58>
je     1156 <write_in_full+0x46>
add    %rax,%r15
add    %rax,%r14
sub    %rax,%rbx
jne    1130 <write_in_full+0x20>
jmp    116f <write_in_full+0x5f>
xor    %r14d,%r14d
jmp    116f <write_in_full+0x5f>
mov    0x2e7b(%rip),%rax
mov    (%rax),%eax
mov    0x2e7a(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffffffffffff,%r14
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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