#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;
int /*<<< orphan*/ printf(char *, scalar_t__, int);
scalar_t__ write(int, void const *, size_t);
int write_full(int fd, const void *buf, size_t count) {
  ssize_t ret = 0;
  ssize_t total = 0;
  while (count) {
    ret = write(fd, buf, count);
    if (ret < 0) {
      if (errno == EINTR) {
        continue;
      }
      printf("write error errno=%d fd=%d\n", errno, fd);
      break;
    }
    count -= ret;
    buf += ret;
    total += ret;
  }
  return total;
}


x86 assembly (clang, -O0):
<write_full>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11d6 <write_full+0xb6>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11aa <write_full+0x8a>
mov    0x2e6c(%rip),%rax
mov    (%rax),%rax
mov    0x2e5a(%rip),%rcx
cmp    (%rcx),%rax
jne    118c <write_full+0x6c>
jmp    1143 <write_full+0x23>
mov    0x2e4d(%rip),%rax
mov    (%rax),%rsi
mov    -0x4(%rbp),%edx
lea    0xe60(%rip),%rdi
call   1040 <printf@plt>
jmp    11d6 <write_full+0xb6>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x20(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    1143 <write_full+0x23>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_full>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     118d <write_full+0x6d>
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %edi,%r15d
xor    %r14d,%r14d
mov    0x2e9d(%rip),%r12
mov    0x2e8e(%rip),%r13
nopw   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
test   %rax,%rax
jns    116f <write_full+0x4f>
mov    (%r12),%rsi
cmp    0x0(%r13),%rsi
je     1150 <write_full+0x30>
jmp    117c <write_full+0x5c>
add    %rax,%rbp
add    %rax,%r14
sub    %rax,%rbx
jne    1150 <write_full+0x30>
jmp    1190 <write_full+0x70>
lea    0xe7d(%rip),%rdi
mov    %r15d,%edx
call   1040 <printf@plt>
jmp    1190 <write_full+0x70>
xor    %r14d,%r14d
mov    %r14d,%eax
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
@.str = [28 x i8] c"write error errno=%d fd=%d\0A\00"
define i32 @write_full(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i64 0, i64* %7
store i64 0, i64* %8
br label %9
9: ; preds = %28, %23, %3
%10 = load i64, i64* %6
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %38
12: ; preds = %9
%13 = load i32, i32* %4
%14 = load i8*, i8** %5
%15 = load i64, i64* %6
%16 = call i64 @write(i32 %13, i8* %14, i64 %15)
store i64 %16, i64* %7
%17 = load i64, i64* %7
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %28
19: ; preds = %12
%20 = load i64, i64* @errno
%21 = load i64, i64* @EINTR
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %24
23: ; preds = %19
br label %9
24: ; preds = %19
%25 = load i64, i64* @errno
%26 = load i32, i32* %4
%27 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i64 %25, i32 %26)
br label %38
28: ; preds = %12
%29 = load i64, i64* %7
%30 = load i64, i64* %6
%31 = sub i64 %30, %29
store i64 %31, i64* %6
%32 = load i64, i64* %7
%33 = load i8*, i8** %5
%34 = getelementptr i8, i8* %33, i64 %32
store i8* %34, i8** %5
%35 = load i64, i64* %7
%36 = load i64, i64* %8
%37 = add nsw i64 %36, %35
store i64 %37, i64* %8
br label %9
38: ; preds = %24, %9
%39 = load i64, i64* %8
%40 = trunc i64 %39 to i32
ret i32 %40
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@.str = [28 x i8] c"write error errno=%d fd=%d\0A\00"
define i32 @write_full(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %23, label %5
5: ; preds = %3, %18
%6 = phi i64 [ %21, %18 ], [ 0, %3 ]
%7 = phi i64 [ %19, %18 ], [ %2, %3 ]
%8 = phi i8* [ %20, %18 ], [ %1, %3 ]
br label %9
9: ; preds = %5, %12
%10 = tail call i64 @write(i32 %0, i8* %8, i64 %7)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %18
12: ; preds = %9
%13 = load i64, i64* @errno
%14 = load i64, i64* @EINTR
%15 = icmp eq i64 %13, %14
br i1 %15, label %9, label %16
16: ; preds = %12
%17 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i64 %13, i32 %0)
br label %23
18: ; preds = %9
%19 = sub i64 %7, %10
%20 = getelementptr i8, i8* %8, i64 %10
%21 = add nsw i64 %10, %6
%22 = icmp eq i64 %19, 0
br i1 %22, label %23, label %5
23: ; preds = %18, %3, %16
%24 = phi i64 [ %6, %16 ], [ 0, %3 ], [ %21, %18 ]
%25 = trunc i64 %24 to i32
ret i32 %25
}