#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 */
scalar_t__ EINTR;
scalar_t__ errno;
scalar_t__ write(int, void const *, size_t);
ssize_t forcewrite(int fd, const void *ptr, size_t size) {
  size_t size_left = size;
  ssize_t size_written;
  while (size_left > 0) {
    size_written = write(fd, ptr, size_left);
    if (size_written < 0) {
      if (errno == EINTR)
        size_written = 0;
      else
        return -1;
    } else if (size_written == 0)
      return -1;
    size_left -= size_written;
    ptr = (char *)ptr + size_written;
  }
  return size - size_left;
}


x86 assembly (clang, -O0):
<forcewrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jbe    11cb <forcewrite+0xbb>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jge    118e <forcewrite+0x7e>
mov    0x2e84(%rip),%rax
mov    (%rax),%rax
mov    0x2e72(%rip),%rcx
cmp    (%rcx),%rax
jne    117c <forcewrite+0x6c>
movq   $0x0,-0x30(%rbp)
jmp    1189 <forcewrite+0x79>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11d7 <forcewrite+0xc7>
jmp    11ab <forcewrite+0x9b>
cmpq   $0x0,-0x30(%rbp)
jne    11a6 <forcewrite+0x96>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11d7 <forcewrite+0xc7>
jmp    11ab <forcewrite+0x9b>
mov    -0x30(%rbp),%rcx
mov    -0x28(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    112b <forcewrite+0x1b>
mov    -0x20(%rbp),%rax
sub    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<forcewrite>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     116b <forcewrite+0x5b>
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%r15d
mov    0x2eb0(%rip),%r12
mov    0x2ea1(%rip),%r13
mov    %rdx,%rbp
jmp    114a <forcewrite+0x3a>
nopl   0x0(%rax)
je     1170 <forcewrite+0x60>
add    %rax,%rbx
sub    %rax,%rbp
je     1177 <forcewrite+0x67>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1030 <write@plt>
test   %rax,%rax
jns    1140 <forcewrite+0x30>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
jne    1170 <forcewrite+0x60>
xor    %eax,%eax
jmp    1142 <forcewrite+0x32>
xor    %r14d,%r14d
jmp    1177 <forcewrite+0x67>
mov    $0xffffffffffffffff,%r14
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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