#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 size_t 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__ pread(int, void *, size_t const, size_t);
int /*<<< orphan*/ stderr;
int write_bytes(int, void *, scalar_t__, size_t);
int copy_bytes_with_preadwrite(int in_fd, int out_fd, off_t in_offset,
                               off_t out_offset, const size_t bytes,
                               void *userbuf, const size_t bufsize) {
  if (bytes == 0) {
    return EXIT_SUCCESS;
  }
  size_t bytes_left = bytes;
  size_t total_bytes_written = 0;
  size_t total_bytes_read = 0;
  while (bytes_left > 0) {
    const size_t bytes_to_read = bufsize > bytes_left ? bytes_left : bufsize;
    ssize_t bytes_read = pread(in_fd, userbuf, bytes_to_read, in_offset);
    if (bytes_read < 0) {
      fprintf(stderr,
              "Error: During read of %zu bytes, encountered while reading "
              "bytes [%zu -- %zu].\n",
              bytes, total_bytes_read, total_bytes_read + bytes_to_read);
      perror(NULL);
      return EXIT_FAILURE;
    }
    in_offset += (size_t)bytes_read;
    total_bytes_read += (size_t)bytes_read;
    int write_status = write_bytes(out_fd, userbuf, bytes_read, out_offset);
    if (write_status != EXIT_SUCCESS) {
      return write_status;
    }
    out_offset += (size_t)bytes_read;
    total_bytes_written += (size_t)bytes_read;
    bytes_left -= (size_t)bytes_read;
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<copy_bytes_with_preadwrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    0x10(%rbp),%rax
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    117e <copy_bytes_with_preadwrite+0x3e>
mov    0x2e5c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12b2 <copy_bytes_with_preadwrite+0x172>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
cmpq   $0x0,-0x38(%rbp)
jbe    12a6 <copy_bytes_with_preadwrite+0x166>
mov    0x10(%rbp),%rax
cmp    -0x38(%rbp),%rax
jbe    11bc <copy_bytes_with_preadwrite+0x7c>
mov    -0x38(%rbp),%rax
mov    %rax,-0x68(%rbp)
jmp    11c4 <copy_bytes_with_preadwrite+0x84>
mov    0x10(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x68(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x50(%rbp),%rdx
mov    -0x18(%rbp),%rcx
call   1060 <pread@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
jge    122e <copy_bytes_with_preadwrite+0xee>
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
mov    -0x48(%rbp),%rcx
mov    -0x48(%rbp),%r8
add    -0x50(%rbp),%r8
lea    0xdf1(%rip),%rsi
call   1030 <fprintf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <perror@plt>
mov    0x2db4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12b2 <copy_bytes_with_preadwrite+0x172>
mov    -0x58(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x58(%rbp),%rax
add    -0x48(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x58(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1040 <write_bytes@plt>
mov    %eax,-0x5c(%rbp)
mov    -0x5c(%rbp),%eax
mov    0x2d69(%rip),%rcx
cmp    (%rcx),%eax
je     127a <copy_bytes_with_preadwrite+0x13a>
mov    -0x5c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12b2 <copy_bytes_with_preadwrite+0x172>
mov    -0x58(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x58(%rbp),%rax
add    -0x40(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x58(%rbp),%rcx
mov    -0x38(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x38(%rbp)
jmp    1196 <copy_bytes_with_preadwrite+0x56>
mov    0x2d23(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_bytes_with_preadwrite>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,(%rsp)
mov    %edi,0x4(%rsp)
test   %r8,%r8
je     11d2 <copy_bytes_with_preadwrite+0x92>
mov    %r9,%r12
mov    %rcx,%rbx
mov    %rdx,%r13
xor    %ecx,%ecx
mov    %r8,0x10(%rsp)
mov    %r8,%r15
nopl   (%rax)
mov    %rcx,0x8(%rsp)
mov    0x50(%rsp),%r14
cmp    %r14,%r15
cmovb  %r15,%r14
mov    0x4(%rsp),%edi
mov    %r12,%rsi
mov    %r14,%rdx
mov    %r13,%rcx
call   1060 <pread@plt>
test   %rax,%rax
js     11ea <copy_bytes_with_preadwrite+0xaa>
mov    %rax,%rbp
mov    (%rsp),%edi
mov    %r12,%r14
mov    %r12,%rsi
mov    %rax,%rdx
mov    %rbx,%rcx
call   1040 <write_bytes@plt>
mov    0x2e1a(%rip),%rcx
cmp    (%rcx),%eax
jne    11db <copy_bytes_with_preadwrite+0x9b>
mov    0x8(%rsp),%rcx
add    %rbp,%rcx
add    %rbp,%r13
add    %rbp,%rbx
sub    %rbp,%r15
mov    %r14,%r12
jne    1170 <copy_bytes_with_preadwrite+0x30>
jmp    11db <copy_bytes_with_preadwrite+0x9b>
mov    0x2df7(%rip),%rax
mov    (%rax),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2def(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rsp),%rcx
add    %rcx,%r14
lea    0xdfe(%rip),%rsi
mov    0x10(%rsp),%rdx
mov    %r14,%r8
call   1030 <fprintf@plt>
xor    %edi,%edi
call   1050 <perror@plt>
mov    0x2dbb(%rip),%rax
jmp    11d9 <copy_bytes_with_preadwrite+0x99>

LLVM IR (-O0):
@EXIT_SUCCESS = global i32 0
@stderr = global i32 0
@.str = [80 x i8] c"Error: During read of %zu bytes, encountered while reading bytes [%zu -- %zu].\0A\00"
@EXIT_FAILURE = global i32 0
define i32 @copy_bytes_with_preadwrite(i32 %0, i32 %1, i64 %2, i64 %3, i64 %4, i8* %5, i64 %6) {
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i64
%12 = alloca i64
%13 = alloca i64
%14 = alloca i8*
%15 = alloca i64
%16 = alloca i64
%17 = alloca i64
%18 = alloca i64
%19 = alloca i64
%20 = alloca i64
%21 = alloca i32
store i32 %0, i32* %9
store i32 %1, i32* %10
store i64 %2, i64* %11
store i64 %3, i64* %12
store i64 %4, i64* %13
store i8* %5, i8** %14
store i64 %6, i64* %15
%22 = load i64, i64* %13
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %7
%25 = load i32, i32* @EXIT_SUCCESS
store i32 %25, i32* %8
br label %87
26: ; preds = %7
%27 = load i64, i64* %13
store i64 %27, i64* %16
store i64 0, i64* %17
store i64 0, i64* %18
br label %28
28: ; preds = %75, %26
%29 = load i64, i64* %16
%30 = icmp ugt i64 %29, 0
br i1 %30, label %31, label %85
31: ; preds = %28
%32 = load i64, i64* %15
%33 = load i64, i64* %16
%34 = icmp ugt i64 %32, %33
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = load i64, i64* %16
br label %39
37: ; preds = %31
%38 = load i64, i64* %15
br label %39
39: ; preds = %37, %35
%40 = phi i64 [ %36, %35 ], [ %38, %37 ]
store i64 %40, i64* %19
%41 = load i32, i32* %9
%42 = load i8*, i8** %14
%43 = load i64, i64* %19
%44 = load i64, i64* %11
%45 = call i64 @pread(i32 %41, i8* %42, i64 %43, i64 %44)
store i64 %45, i64* %20
%46 = load i64, i64* %20
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %58
48: ; preds = %39
%49 = load i32, i32* @stderr
%50 = load i64, i64* %13
%51 = load i64, i64* %18
%52 = load i64, i64* %18
%53 = load i64, i64* %19
%54 = add i64 %52, %53
%55 = call i32 @fprintf(i32 %49, i8* getelementptr inbounds ([80 x i8], [80 x i8]* @.str, i64 0, i64 0), i64 %50, i64 %51, i64 %54)
%56 = call i32 @perror(i32* null)
%57 = load i32, i32* @EXIT_FAILURE
store i32 %57, i32* %8
br label %87
58: ; preds = %39
%59 = load i64, i64* %20
%60 = load i64, i64* %11
%61 = add i64 %60, %59
store i64 %61, i64* %11
%62 = load i64, i64* %20
%63 = load i64, i64* %18
%64 = add i64 %63, %62
store i64 %64, i64* %18
%65 = load i32, i32* %10
%66 = load i8*, i8** %14
%67 = load i64, i64* %20
%68 = load i64, i64* %12
%69 = call i32 @write_bytes(i32 %65, i8* %66, i64 %67, i64 %68)
store i32 %69, i32* %21
%70 = load i32, i32* %21
%71 = load i32, i32* @EXIT_SUCCESS
%72 = icmp ne i32 %70, %71
br i1 %72, label %73, label %75
73: ; preds = %58
%74 = load i32, i32* %21
store i32 %74, i32* %8
br label %87
75: ; preds = %58
%76 = load i64, i64* %20
%77 = load i64, i64* %12
%78 = add i64 %77, %76
store i64 %78, i64* %12
%79 = load i64, i64* %20
%80 = load i64, i64* %17
%81 = add i64 %80, %79
store i64 %81, i64* %17
%82 = load i64, i64* %20
%83 = load i64, i64* %16
%84 = sub i64 %83, %82
store i64 %84, i64* %16
br label %28
85: ; preds = %28
%86 = load i32, i32* @EXIT_SUCCESS
store i32 %86, i32* %8
br label %87
87: ; preds = %85, %73, %48, %24
%88 = load i32, i32* %8
ret i32 %88
}

LLVM IR (-O2):
@EXIT_SUCCESS = global i32 0
@stderr = global i32 0
@.str = [80 x i8] c"Error: During read of %zu bytes, encountered while reading bytes [%zu -- %zu].\0A\00"
@EXIT_FAILURE = global i32 0
define i32 @copy_bytes_with_preadwrite(i32 %0, i32 %1, i64 %2, i64 %3, i64 %4, i8* %5, i64 %6) {
%8 = icmp eq i64 %4, 0
br i1 %8, label %9, label %11
9: ; preds = %7
%10 = load i32, i32* @EXIT_SUCCESS
br label %36
11: ; preds = %7, %30
%12 = phi i64 [ %32, %30 ], [ %2, %7 ]
%13 = phi i64 [ %33, %30 ], [ %3, %7 ]
%14 = phi i64 [ %31, %30 ], [ 0, %7 ]
%15 = phi i64 [ %34, %30 ], [ %4, %7 ]
%16 = icmp ult i64 %15, %6
%17 = select i1 %16, i64 %15, i64 %6
%18 = tail call i64 @pread(i32 %0, i8* %5, i64 %17, i64 %12)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %11
%21 = load i32, i32* @stderr
%22 = add i64 %17, %14
%23 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([80 x i8], [80 x i8]* @.str, i64 0, i64 0), i64 %4, i64 %14, i64 %22)
%24 = tail call i32 @perror(i32* null)
%25 = load i32, i32* @EXIT_FAILURE
br label %36
26: ; preds = %11
%27 = tail call i32 @write_bytes(i32 %1, i8* %5, i64 %18, i64 %13)
%28 = load i32, i32* @EXIT_SUCCESS
%29 = icmp eq i32 %27, %28
br i1 %29, label %30, label %36
30: ; preds = %26
%31 = add i64 %18, %14
%32 = add i64 %18, %12
%33 = add i64 %18, %13
%34 = sub i64 %15, %18
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %11
36: ; preds = %26, %30, %20, %9
%37 = phi i32 [ %10, %9 ], [ %25, %20 ], [ %27, %30 ], [ %27, %26 ]
ret i32 %37
}