#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 *);
int /*<<< orphan*/ error_text(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));
      error_text("Could not write to device");
      return -1;
    }
    if (rc == 0) {
      error_reason("Write failed");
      error_text("Could not write to device");
      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    1203 <misc_write+0xc3>
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    11be <misc_write+0x7e>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    %rax,%rdi
call   1030 <error_reason@plt>
lea    0xe53(%rip),%rdi
call   1050 <error_text@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120a <misc_write+0xca>
cmpq   $0x0,-0x28(%rbp)
jne    11ed <misc_write+0xad>
lea    0xe4a(%rip),%rdi
call   1030 <error_reason@plt>
lea    0xe24(%rip),%rdi
call   1050 <error_text@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120a <misc_write+0xca>
jmp    11f2 <misc_write+0xb2>
mov    -0x28(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    115b <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     1185 <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     1189 <misc_write+0x49>
cmp    $0xffffffffffffffff,%rax
je     1192 <misc_write+0x52>
add    %rax,%rbp
cmp    %rbx,%rbp
jb     1160 <misc_write+0x20>
xor    %eax,%eax
jmp    11b9 <misc_write+0x79>
lea    0xe8a(%rip),%rdi
jmp    11a3 <misc_write+0x63>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    %rax,%rdi
call   1030 <error_reason@plt>
lea    0xe51(%rip),%rdi
call   1050 <error_text@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@.str = [26 x i8] c"Could not write to device\00"
@.str.1 = [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 = %39, %3
%11 = load i64, i64* %8
%12 = load i64, i64* %7
%13 = icmp ult i64 %11, %12
br i1 %13, label %14, label %43
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 %32
27: ; preds = %14
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 @error_reason(i8* %29)
%31 = call i32 @error_text(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %44
32: ; preds = %14
%33 = load i64, i64* %9
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %32
%36 = call i32 @error_reason(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%37 = call i32 @error_text(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %44
38: ; preds = %32
br label %39
39: ; preds = %38
%40 = load i64, i64* %9
%41 = load i64, i64* %8
%42 = add i64 %41, %40
store i64 %42, i64* %8
br label %10
43: ; preds = %10
store i32 0, i32* %4
br label %44
44: ; preds = %43, %35, %27
%45 = load i32, i32* %4
ret i32 %45
}

LLVM IR (-O2):
@errno = global i32 0
@.str = [26 x i8] c"Could not write to device\00"
@.str.1 = [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 %23, 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 %23
19: ; preds = %7, %13
%20 = phi i8* [ %15, %13 ], [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), %7 ]
%21 = tail call i32 @error_reason(i8* %20)
%22 = tail call i32 @error_text(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %16, %19, %3
%24 = phi i32 [ 0, %3 ], [ -1, %19 ], [ 0, %16 ]
ret i32 %24
}