#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 */

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ errno;
size_t write(int, char *, size_t);
int write_full(int fd, char *data, size_t count) {
  size_t written;
restart:
  while ((written = write(fd, data, count)) > 0) {
    data += written;
    count -= written;
  }
  if (written < 0) {
    if (errno == EAGAIN)
      goto restart;
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<write_full>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
jmp    1128 <write_full+0x18>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jbe    1166 <write_full+0x56>
mov    -0x20(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    1128 <write_full+0x18>
cmpq   $0x0,-0x20(%rbp)
jae    119c <write_full+0x8c>
mov    0x2e68(%rip),%rax
mov    (%rax),%rax
mov    0x2e4e(%rip),%rcx
cmp    (%rcx),%rax
jne    1190 <write_full+0x80>
jmp    1123 <write_full+0x13>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a3 <write_full+0x93>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_full>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
nopl   0x0(%rax)
call   1030 <write@plt>
test   %rax,%rax
je     113a <write_full+0x2a>
add    %rax,%rbx
sub    %rax,%r14
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
jmp    1120 <write_full+0x10>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@errno = global i64 0
@EAGAIN = global i64 0
define i32 @write_full(i32 %0, i8* nocapture %1, i64 %2) {
%4 = tail call i64 @write(i32 %0, i8* %1, i64 %2)
%5 = icmp eq i64 %4, 0
br i1 %5, label %14, label %6
6: ; preds = %3, %6
%7 = phi i64 [ %12, %6 ], [ %4, %3 ]
%8 = phi i64 [ %11, %6 ], [ %2, %3 ]
%9 = phi i8* [ %10, %6 ], [ %1, %3 ]
%10 = getelementptr inbounds i8, i8* %9, i64 %7
%11 = sub i64 %8, %7
%12 = tail call i64 @write(i32 %0, i8* %10, i64 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %6
14: ; preds = %6, %3
ret i32 0
}