#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__ errno;
int /*<<< orphan*/ progressbar_error(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(scalar_t__);
int write(int, void *, int);
int write_bytes(int fd, void *buff, int bytes) {
  int res, count;
  for (count = 0; count < bytes; count += res) {
    res = write(fd, buff + count, bytes - count);
    if (res == -1) {
      if (errno != EINTR) {
        do {
          progressbar_error("Write failed because %s\n", strerror(errno));
        } while (0);
        return -1;
      }
      res = 0;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<write_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11dd <write_bytes+0xad>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11ca <write_bytes+0x9a>
mov    0x2e5e(%rip),%rax
mov    (%rax),%rax
mov    0x2e4c(%rip),%rcx
cmp    (%rcx),%rax
je     11c3 <write_bytes+0x93>
jmp    119a <write_bytes+0x6a>
mov    0x2e3f(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    %eax,%esi
lea    0xe4e(%rip),%rdi
call   1040 <progressbar_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e4 <write_bytes+0xb4>
movl   $0x0,-0x18(%rbp)
jmp    11cf <write_bytes+0x9f>
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1149 <write_bytes+0x19>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_bytes>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
jle    11a4 <write_bytes+0x74>
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebp,%ebp
mov    0x2e90(%rip),%r12
mov    0x2e81(%rip),%r13
jmp    1166 <write_bytes+0x36>
nopl   0x0(%rax)
add    %eax,%ebp
cmp    %ebx,%ebp
jge    11a4 <write_bytes+0x74>
movslq %ebp,%rax
lea    (%r14,%rax,1),%rsi
mov    %ebx,%edx
sub    %eax,%edx
mov    %r15d,%edi
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1160 <write_bytes+0x30>
mov    (%r12),%rdi
xor    %eax,%eax
cmp    0x0(%r13),%rdi
je     1160 <write_bytes+0x30>
call   1050 <strerror@plt>
lea    0xe6a(%rip),%rdi
mov    %eax,%esi
call   1040 <progressbar_error@plt>
mov    $0xffffffff,%eax
jmp    11a6 <write_bytes+0x76>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@.str = [25 x i8] c"Write failed because %s\0A\00"
define i32 @write_bytes(i32 %0, i8* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %23
5: ; preds = %3, %19
%6 = phi i32 [ %21, %19 ], [ 0, %3 ]
%7 = sext i32 %6 to i64
%8 = getelementptr i8, i8* %1, i64 %7
%9 = sub nsw i32 %2, %6
%10 = tail call i32 @write(i32 %0, i8* %8, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %19
12: ; preds = %5
%13 = load i64, i64* @errno
%14 = load i64, i64* @EINTR
%15 = icmp eq i64 %13, %14
br i1 %15, label %19, label %16
16: ; preds = %12
%17 = tail call i32 @strerror(i64 %13)
%18 = tail call i32 @progressbar_error(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %17)
br label %23
19: ; preds = %12, %5
%20 = phi i32 [ %10, %5 ], [ 0, %12 ]
%21 = add nsw i32 %20, %6
%22 = icmp slt i32 %21, %2
br i1 %22, label %5, label %23
23: ; preds = %19, %3, %16
%24 = phi i32 [ -1, %16 ], [ 0, %3 ], [ 0, %19 ]
ret i32 %24
}