#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__ EAGAIN;
scalar_t__ errno;
scalar_t__ write(int, char *, size_t);
int write_full(int fd, char *data, size_t count) {
  ssize_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
jle    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)
jge    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   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2eb9(%rip),%r15
mov    0x2ea2(%rip),%r12
jmp    1136 <write_full+0x26>
add    %rax,%rbx
sub    %rax,%r14
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <write@plt>
test   %rax,%rax
jg     1130 <write_full+0x20>
test   %rax,%rax
jns    115d <write_full+0x4d>
mov    (%r15),%rax
cmp    (%r12),%rax
je     1136 <write_full+0x26>
mov    $0xffffffff,%eax
jmp    115f <write_full+0x4f>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
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 sgt 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 slt 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) {
br label %4
4: ; preds = %22, %3
%5 = phi i8* [ %1, %3 ], [ %18, %22 ]
%6 = phi i64 [ %2, %3 ], [ %19, %22 ]
%7 = tail call i64 @write(i32 %0, i8* %5, i64 %6)
%8 = icmp sgt i64 %7, 0
br i1 %8, label %9, label %17
9: ; preds = %4, %9
%10 = phi i64 [ %15, %9 ], [ %7, %4 ]
%11 = phi i64 [ %14, %9 ], [ %6, %4 ]
%12 = phi i8* [ %13, %9 ], [ %5, %4 ]
%13 = getelementptr inbounds i8, i8* %12, i64 %10
%14 = sub i64 %11, %10
%15 = tail call i64 @write(i32 %0, i8* %13, i64 %14)
%16 = icmp sgt i64 %15, 0
br i1 %16, label %9, label %17
17: ; preds = %9, %4
%18 = phi i8* [ %5, %4 ], [ %13, %9 ]
%19 = phi i64 [ %6, %4 ], [ %14, %9 ]
%20 = phi i64 [ %7, %4 ], [ %15, %9 ]
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %17
%23 = load i64, i64* @errno
%24 = load i64, i64* @EAGAIN
%25 = icmp eq i64 %23, %24
br i1 %25, label %4, label %26
26: ; preds = %17, %22
%27 = phi i32 [ -1, %22 ], [ 0, %17 ]
ret i32 %27
}