#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;
scalar_t__ EPIPE;
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
size_t write(int, unsigned char const *, size_t);
int v4lconvert_helper_write(int fd, const void *b, size_t count,
                            char *progname) {
  const unsigned char *buf = b;
  size_t ret, written = 0;
  while (written < count) {
    ret = write(fd, buf + written, count - written);
    if (ret == -1) {
      if (errno == EINTR)
        continue;
      if (errno == EPIPE)
        exit(0);
      fprintf(stderr, "%s: error writing: %s\n", progname, strerror(errno));
      return -1;
    }
    written += ret;
  }
  return 0;
}


x86 assembly (clang, -O0):
<v4lconvert_helper_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1232 <v4lconvert_helper_write+0xf2>
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rsi
add    -0x38(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x38(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0xffffffffffffffff,-0x30(%rbp)
jne    1221 <v4lconvert_helper_write+0xe1>
mov    0x2e2d(%rip),%rax
mov    (%rax),%rax
mov    0x2e1b(%rip),%rcx
cmp    (%rcx),%rax
jne    11bb <v4lconvert_helper_write+0x7b>
jmp    1167 <v4lconvert_helper_write+0x27>
mov    0x2e0e(%rip),%rax
mov    (%rax),%rax
mov    0x2e0c(%rip),%rcx
cmp    (%rcx),%rax
jne    11dc <v4lconvert_helper_write+0x9c>
xor    %edi,%edi
call   1050 <exit@plt>
mov    0x2dfd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2dd9(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xdf0(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1239 <v4lconvert_helper_write+0xf9>
mov    -0x30(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    1167 <v4lconvert_helper_write+0x27>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<v4lconvert_helper_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %rdx,%rdx
je     11b5 <v4lconvert_helper_write+0x75>
mov    %edi,%r13d
mov    %rcx,(%rsp)
xor    %r14d,%r14d
mov    0x2e6c(%rip),%r15
mov    0x2e5d(%rip),%r12
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %rdx,0x10(%rsp)
mov    %rsi,0x8(%rsp)
nopl   0x0(%rax,%rax,1)
mov    %r13d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
cmp    $0xffffffffffffffff,%rax
jne    119f <v4lconvert_helper_write+0x5f>
mov    (%r15),%rdi
cmp    (%r12),%rdi
je     1180 <v4lconvert_helper_write+0x40>
jmp    11b9 <v4lconvert_helper_write+0x79>
add    %rax,%r14
mov    0x8(%rsp),%rsi
lea    (%rsi,%r14,1),%rbp
mov    0x10(%rsp),%rbx
sub    %r14,%rbx
ja     1180 <v4lconvert_helper_write+0x40>
xor    %eax,%eax
jmp    11ed <v4lconvert_helper_write+0xad>
mov    0x2e18(%rip),%rax
cmp    (%rax),%rdi
je     11fc <v4lconvert_helper_write+0xbc>
mov    0x2e14(%rip),%rax
mov    (%rax),%ebx
call   1060 <strerror@plt>
lea    0xe26(%rip),%rsi
mov    %ebx,%edi
mov    (%rsp),%rdx
mov    %rax,%rcx
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %edi,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@EPIPE = global i64 0
@stderr = global i32 0
@.str = [23 x i8] c"%s: error writing: %s\0A\00"
define i32 @v4lconvert_helper_write(i32 %0, i8* %1, i64 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i64
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i8* %3, i8** %9
%13 = load i8*, i8** %7
store i8* %13, i8** %10
store i64 0, i64* %12
br label %14
14: ; preds = %46, %33, %4
%15 = load i64, i64* %12
%16 = load i64, i64* %8
%17 = icmp ult i64 %15, %16
br i1 %17, label %18, label %50
18: ; preds = %14
%19 = load i32, i32* %6
%20 = load i8*, i8** %10
%21 = load i64, i64* %12
%22 = getelementptr inbounds i8, i8* %20, i64 %21
%23 = load i64, i64* %8
%24 = load i64, i64* %12
%25 = sub i64 %23, %24
%26 = call i64 @write(i32 %19, i8* %22, i64 %25)
store i64 %26, i64* %11
%27 = load i64, i64* %11
%28 = icmp eq i64 %27, -1
br i1 %28, label %29, label %46
29: ; preds = %18
%30 = load i64, i64* @errno
%31 = load i64, i64* @EINTR
%32 = icmp eq i64 %30, %31
br i1 %32, label %33, label %34
33: ; preds = %29
br label %14
34: ; preds = %29
%35 = load i64, i64* @errno
%36 = load i64, i64* @EPIPE
%37 = icmp eq i64 %35, %36
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = call i32 @exit(i32 0)
unreachable
40: ; preds = %34
%41 = load i32, i32* @stderr
%42 = load i8*, i8** %9
%43 = load i64, i64* @errno
%44 = call i8* @strerror(i64 %43)
%45 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %42, i8* %44)
store i32 -1, i32* %5
br label %51
46: ; preds = %18
%47 = load i64, i64* %11
%48 = load i64, i64* %12
%49 = add i64 %48, %47
store i64 %49, i64* %12
br label %14
50: ; preds = %14
store i32 0, i32* %5
br label %51
51: ; preds = %50, %40
%52 = load i32, i32* %5
ret i32 %52
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@EPIPE = global i64 0
@stderr = global i32 0
@.str = [23 x i8] c"%s: error writing: %s\0A\00"
define i32 @v4lconvert_helper_write(i32 %0, i8* nocapture %1, i64 %2, i8* %3) {
%5 = icmp eq i64 %2, 0
br i1 %5, label %31, label %6
6: ; preds = %4, %26
%7 = phi i64 [ %30, %26 ], [ %2, %4 ]
%8 = phi i8* [ %29, %26 ], [ %1, %4 ]
%9 = phi i64 [ %27, %26 ], [ 0, %4 ]
br label %10
10: ; preds = %6, %13
%11 = tail call i64 @write(i32 %0, i8* %8, i64 %7)
%12 = icmp eq i64 %11, -1
br i1 %12, label %13, label %26
13: ; preds = %10
%14 = load i64, i64* @errno
%15 = load i64, i64* @EINTR
%16 = icmp eq i64 %14, %15
br i1 %16, label %10, label %17
17: ; preds = %13
%18 = load i64, i64* @EPIPE
%19 = icmp eq i64 %14, %18
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = tail call i32 @exit(i32 0)
unreachable
22: ; preds = %17
%23 = load i32, i32* @stderr
%24 = tail call i8* @strerror(i64 %14)
%25 = tail call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %3, i8* %24)
br label %31
26: ; preds = %10
%27 = add i64 %11, %9
%28 = icmp ult i64 %27, %2
%29 = getelementptr inbounds i8, i8* %1, i64 %27
%30 = sub i64 %2, %27
br i1 %28, label %6, label %31
31: ; preds = %26, %4, %22
%32 = phi i32 [ -1, %22 ], [ 0, %4 ], [ 0, %26 ]
ret i32 %32
}