#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__ EINTR;
int /*<<< orphan*/ EXIT_FAILURE;
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ warnx(char *, scalar_t__);
size_t write(int, void const *, size_t);
void write_exactly(int fd, const void *buf, size_t count) {
  size_t written = 0;
  while (written < count) {
    size_t rc = write(fd, buf + written, count - written);
    if (rc < 0 && errno == EINTR)
      continue;
    for (; !(rc >= 0); exit(EXIT_FAILURE))
      warnx("write_exactly() %d", errno);
    written += rc;
  }
}


x86 assembly (clang, -O0):
<write_exactly>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11f0 <write_exactly+0xc0>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jae    119f <write_exactly+0x6f>
mov    0x2e59(%rip),%rax
mov    (%rax),%rax
mov    0x2e3f(%rip),%rcx
cmp    (%rcx),%rax
jne    119f <write_exactly+0x6f>
jmp    114b <write_exactly+0x1b>
jmp    11a4 <write_exactly+0x74>
cmpq   $0x0,-0x28(%rbp)
setae  %al
xor    $0xff,%al
test   $0x1,%al
jne    11bb <write_exactly+0x8b>
jmp    11df <write_exactly+0xaf>
mov    0x2e1e(%rip),%rax
mov    (%rax),%rsi
lea    0xe34(%rip),%rdi
call   1040 <warnx@plt>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
mov    -0x28(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    114b <write_exactly+0x1b>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_exactly>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdx,%rdx
je     114a <write_exactly+0x3a>
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
lea    (%r14,%rbp,1),%rsi
mov    %rbx,%rdx
sub    %rbp,%rdx
mov    %r15d,%edi
call   1030 <write@plt>
add    %rax,%rbp
cmp    %rbx,%rbp
jb     1130 <write_exactly+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@.str = [19 x i8] c"write_exactly() %d\00"
@EXIT_FAILURE = global i32 0
define void @write_exactly(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
br label %9
9: ; preds = %40, %28, %3
%10 = load i64, i64* %7
%11 = load i64, i64* %6
%12 = icmp ult i64 %10, %11
br i1 %12, label %13, label %44
13: ; preds = %9
%14 = load i32, i32* %4
%15 = load i8*, i8** %5
%16 = load i64, i64* %7
%17 = getelementptr i8, i8* %15, i64 %16
%18 = load i64, i64* %6
%19 = load i64, i64* %7
%20 = sub i64 %18, %19
%21 = call i64 @write(i32 %14, i8* %17, i64 %20)
store i64 %21, i64* %8
%22 = load i64, i64* %8
%23 = icmp ult i64 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %13
%25 = load i64, i64* @errno
%26 = load i64, i64* @EINTR
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %29
28: ; preds = %24
br label %9
29: ; preds = %24, %13
br label %30
30: ; preds = %29
%31 = load i64, i64* %8
%32 = icmp uge i64 %31, 0
%33 = xor i1 %32, true
br i1 %33, label %34, label %40
34: ; preds = %30
%35 = load i64, i64* @errno
%36 = call i32 @warnx(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i64 %35)
br label %37
37: ; preds = %34
%38 = load i32, i32* @EXIT_FAILURE
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %30
%41 = load i64, i64* %8
%42 = load i64, i64* %7
%43 = add i64 %42, %41
store i64 %43, i64* %7
br label %9
44: ; preds = %9
ret void
}

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