#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 scalar_t__ ssize_t;
typedef int /*<<< orphan*/ off_t;

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, size_t const, size_t,
                           size_t);
int /*<<< orphan*/ perror(int /*<<< orphan*/ *);
scalar_t__ pwrite(int, void *, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int write_bytes(int out_fd, void *buf, const size_t bytes, off_t out_offset) {
  size_t bytes_written = 0;
  size_t bytes_left = bytes;
  while (bytes_left > 0) {
    ssize_t this_bytes = pwrite(out_fd, buf, bytes_left, out_offset);
    if (this_bytes < 0) {
      fprintf(stderr,
              "Error: During writing out %zu bytes, encountered an error "
              "attempting to write bytes [%zu -- %zu]\n",
              bytes, bytes_written, bytes_written + bytes_left);
      perror(NULL);
      return EXIT_FAILURE;
    }
    bytes_written += (size_t)this_bytes;
    out_offset += this_bytes;
    bytes_left -= (size_t)this_bytes;
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<write_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jbe    11f0 <write_bytes+0xc0>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1040 <pwrite@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jge    11c2 <write_bytes+0x92>
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    -0x28(%rbp),%r8
add    -0x30(%rbp),%r8
lea    0xe5d(%rip),%rsi
call   1030 <fprintf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <perror@plt>
mov    0x2e20(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11fc <write_bytes+0xcc>
mov    -0x38(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x38(%rbp),%rcx
movslq -0x1c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x1c(%rbp)
mov    -0x38(%rbp),%rcx
mov    -0x30(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x30(%rbp)
jmp    1156 <write_bytes+0x26>
mov    0x2dd9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_bytes>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e8e(%rip),%rax
test   %rdx,%rdx
je     1186 <write_bytes+0x56>
mov    %ecx,%ebp
mov    %rdx,%r14
mov    %rsi,%r12
mov    %edi,%r13d
xor    %r15d,%r15d
mov    %rdx,%rbx
nopl   0x0(%rax,%rax,1)
mov    %r13d,%edi
mov    %r12,%rsi
mov    %rbx,%rdx
mov    %ebp,%ecx
call   1040 <pwrite@plt>
test   %rax,%rax
js     1197 <write_bytes+0x67>
add    %rax,%r15
add    %eax,%ebp
sub    %rax,%rbx
jne    1160 <write_bytes+0x30>
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
add    %r15,%rbx
lea    0xe56(%rip),%rsi
mov    %r14,%rdx
mov    %r15,%rcx
mov    %rbx,%r8
call   1030 <fprintf@plt>
xor    %edi,%edi
call   1050 <perror@plt>
mov    0x2e12(%rip),%rax
jmp    1186 <write_bytes+0x56>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [98 x i8] c"Error: During writing out %zu bytes, encountered an error attempting to write bytes [%zu -- %zu]\0A\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @write_bytes(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i32 %3, i32* %9
store i64 0, i64* %10
%13 = load i64, i64* %8
store i64 %13, i64* %11
br label %14
14: ; preds = %35, %4
%15 = load i64, i64* %11
%16 = icmp ugt i64 %15, 0
br i1 %16, label %17, label %47
17: ; preds = %14
%18 = load i32, i32* %6
%19 = load i8*, i8** %7
%20 = load i64, i64* %11
%21 = load i32, i32* %9
%22 = call i64 @pwrite(i32 %18, i8* %19, i64 %20, i32 %21)
store i64 %22, i64* %12
%23 = load i64, i64* %12
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %35
25: ; preds = %17
%26 = load i32, i32* @stderr
%27 = load i64, i64* %8
%28 = load i64, i64* %10
%29 = load i64, i64* %10
%30 = load i64, i64* %11
%31 = add i64 %29, %30
%32 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([98 x i8], [98 x i8]* @.str, i64 0, i64 0), i64 %27, i64 %28, i64 %31)
%33 = call i32 @perror(i32* null)
%34 = load i32, i32* @EXIT_FAILURE
store i32 %34, i32* %5
br label %49
35: ; preds = %17
%36 = load i64, i64* %12
%37 = load i64, i64* %10
%38 = add i64 %37, %36
store i64 %38, i64* %10
%39 = load i64, i64* %12
%40 = load i32, i32* %9
%41 = sext i32 %40 to i64
%42 = add nsw i64 %41, %39
%43 = trunc i64 %42 to i32
store i32 %43, i32* %9
%44 = load i64, i64* %12
%45 = load i64, i64* %11
%46 = sub i64 %45, %44
store i64 %46, i64* %11
br label %14
47: ; preds = %14
%48 = load i32, i32* @EXIT_SUCCESS
store i32 %48, i32* %5
br label %49
49: ; preds = %47, %25
%50 = load i32, i32* %5
ret i32 %50
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [98 x i8] c"Error: During writing out %zu bytes, encountered an error attempting to write bytes [%zu -- %zu]\0A\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @write_bytes(i32 %0, i8* nocapture %1, i64 %2, i32 %3) {
%5 = icmp eq i64 %2, 0
br i1 %5, label %23, label %6
6: ; preds = %4, %17
%7 = phi i64 [ %21, %17 ], [ %2, %4 ]
%8 = phi i64 [ %18, %17 ], [ 0, %4 ]
%9 = phi i32 [ %20, %17 ], [ %3, %4 ]
%10 = tail call i64 @pwrite(i32 %0, i8* %1, i64 %7, i32 %9)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %6
%13 = load i32, i32* @stderr
%14 = add i64 %7, %8
%15 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([98 x i8], [98 x i8]* @.str, i64 0, i64 0), i64 %2, i64 %8, i64 %14)
%16 = tail call i32 @perror(i32* null)
br label %23
17: ; preds = %6
%18 = add i64 %10, %8
%19 = trunc i64 %10 to i32
%20 = add i32 %9, %19
%21 = sub i64 %7, %10
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %6
23: ; preds = %17, %4, %12
%24 = phi i32* [ @EXIT_FAILURE, %12 ], [ @EXIT_SUCCESS, %4 ], [ @EXIT_SUCCESS, %17 ]
%25 = load i32, i32* %24
ret i32 %25
}