#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 size_t ssize_t;

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error_reason(char *);
char *strerror(int /*<<< orphan*/);
size_t write(int, void *, size_t);
int misc_write(int fd, const void *data, size_t count) {
  size_t written;
  ssize_t rc;
  for (written = 0; written < count; written += rc) {
    rc = write(fd, ((void *)(((unsigned long)data) + ((unsigned long)written))),
               count - written);
    if (rc == -1) {
      error_reason(strerror(errno));
      return -1;
    }
    if (rc == 0) {
      error_reason("Write failed");
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<misc_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11db <misc_write+0xab>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x20(%rbp),%rdx
call   1040 <write@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    11a2 <misc_write+0x72>
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    %rax,%rdi
call   1030 <error_reason@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e2 <misc_write+0xb2>
cmpq   $0x0,-0x28(%rbp)
jne    11c5 <misc_write+0x95>
lea    0xe4c(%rip),%rdi
call   1030 <error_reason@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e2 <misc_write+0xb2>
jmp    11ca <misc_write+0x9a>
mov    -0x28(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    114b <misc_write+0x1b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<misc_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdx,%rdx
je     1175 <misc_write+0x45>
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   1040 <write@plt>
test   %rax,%rax
je     1179 <misc_write+0x49>
cmp    $0xffffffffffffffff,%rax
je     1182 <misc_write+0x52>
add    %rax,%rbp
cmp    %rbx,%rbp
jb     1150 <misc_write+0x20>
xor    %eax,%eax
jmp    119d <misc_write+0x6d>
lea    0xe80(%rip),%rdi
jmp    1193 <misc_write+0x63>
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    %rax,%rdi
call   1030 <error_reason@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@.str = [13 x i8] c"Write failed\00"
define i32 @misc_write(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i64 0, i64* %8
br label %10
10: ; preds = %37, %3
%11 = load i64, i64* %8
%12 = load i64, i64* %7
%13 = icmp ult i64 %11, %12
br i1 %13, label %14, label %41
14: ; preds = %10
%15 = load i32, i32* %5
%16 = load i8*, i8** %6
%17 = ptrtoint i8* %16 to i64
%18 = load i64, i64* %8
%19 = add i64 %17, %18
%20 = inttoptr i64 %19 to i8*
%21 = load i64, i64* %7
%22 = load i64, i64* %8
%23 = sub i64 %21, %22
%24 = call i64 @write(i32 %15, i8* %20, i64 %23)
store i64 %24, i64* %9
%25 = load i64, i64* %9
%26 = icmp eq i64 %25, -1
br i1 %26, label %27, label %31
27: ; preds = %14
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 @error_reason(i8* %29)
store i32 -1, i32* %4
br label %42
31: ; preds = %14
%32 = load i64, i64* %9
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = call i32 @error_reason(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %42
36: ; preds = %31
br label %37
37: ; preds = %36
%38 = load i64, i64* %9
%39 = load i64, i64* %8
%40 = add i64 %39, %38
store i64 %40, i64* %8
br label %10
41: ; preds = %10
store i32 0, i32* %4
br label %42
42: ; preds = %41, %34, %27
%43 = load i32, i32* %4
ret i32 %43
}

LLVM IR (-O2):
@errno = global i32 0
@.str = [13 x i8] c"Write failed\00"
define i32 @misc_write(i32 %0, i8* %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %22, label %5
5: ; preds = %3
%6 = ptrtoint i8* %1 to i64
br label %7
7: ; preds = %5, %16
%8 = phi i64 [ 0, %5 ], [ %17, %16 ]
%9 = add i64 %8, %6
%10 = inttoptr i64 %9 to i8*
%11 = sub i64 %2, %8
%12 = tail call i64 @write(i32 %0, i8* %10, i64 %11)
switch i64 %12, label %16 [
i64 -1, label %13
i64 0, label %19
]
13: ; preds = %7
%14 = load i32, i32* @errno
%15 = tail call i8* @strerror(i32 %14)
br label %19
16: ; preds = %7
%17 = add i64 %12, %8
%18 = icmp ult i64 %17, %2
br i1 %18, label %7, label %22
19: ; preds = %7, %13
%20 = phi i8* [ %15, %13 ], [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), %7 ]
%21 = tail call i32 @error_reason(i8* %20)
br label %22
22: ; preds = %16, %19, %3
%23 = phi i32 [ 0, %3 ], [ -1, %19 ], [ 0, %16 ]
ret i32 %23
}