#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 */
int /*<<< orphan*/ log_write_err(int);
int write(int, char *, int);
int write_fully(int fd, char *buf, int count) {
  do {
    int written = write(fd, buf, count);
    if (written < 0) {
      log_write_err(fd);
      return written;
    }
    buf += written;
    count -= written;
  } while (count > 0);
  return 0;
}


x86 assembly (clang, -O0):
<write_fully>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1161 <write_fully+0x41>
mov    -0x8(%rbp),%edi
call   1040 <log_write_err@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    118e <write_fully+0x6e>
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jg     1132 <write_fully+0x12>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_fully>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r14d
nop
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
mov    %eax,%r15d
test   %eax,%eax
js     1154 <write_fully+0x34>
mov    %r15d,%eax
add    %rax,%rbx
sub    %r15d,%ebp
jg     1130 <write_fully+0x10>
xor    %r15d,%r15d
jmp    115c <write_fully+0x3c>
mov    %r14d,%edi
call   1040 <log_write_err@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @write_fully(i32 %0, i8* nocapture %1, i32 %2) {
br label %4
4: ; preds = %11, %3
%5 = phi i8* [ %1, %3 ], [ %13, %11 ]
%6 = phi i32 [ %2, %3 ], [ %14, %11 ]
%7 = tail call i32 @write(i32 %0, i8* %5, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %4
%10 = tail call i32 @log_write_err(i32 %0)
br label %16
11: ; preds = %4
%12 = zext i32 %7 to i64
%13 = getelementptr inbounds i8, i8* %5, i64 %12
%14 = sub nsw i32 %6, %7
%15 = icmp sgt i32 %14, 0
br i1 %15, label %4, label %16
16: ; preds = %11, %9
%17 = phi i32 [ %7, %9 ], [ 0, %11 ]
ret i32 %17
}