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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int lseek(int, char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *, ...);
int read(int, char *, int);
char *strerror(int /*<<< orphan*/);
int write(int, char const *, int);
void copy_data(int to_fd, off_t to_offset, int from_fd, off_t from_offset,
               ssize_t size) {
  char buf[4096];
  int ret;
  int to_write;
  const char *ptr;
  ret = lseek(to_fd, to_offset, SEEK_SET);
  if (ret < 0) {
    printf("error: "
           "failed to seek output file to %lx"
           ": %s"
           "\n",
           to_offset, strerror(errno));
    exit(-1);
  };
  ret = lseek(from_fd, from_offset, SEEK_SET);
  if (ret < 0) {
    printf("error: "
           "failed to seek input file to %lx"
           ": %s"
           "\n",
           from_offset, strerror(errno));
    exit(-1);
  };
  while (size != 0) {
    ret = read(from_fd, buf, sizeof(buf));
    if (ret < 0) {
      printf("error: "
             "failed to read from input file"
             ": %s"
             "\n",
             strerror(errno));
      exit(-1);
    };
    if (ret == 0 && size > 0) {
      printf("error: "
             "eof while reading input file"
             ": %s"
             "\n",
             strerror(errno));
      exit(-1);
    };
    if (ret == 0)
      return;
    to_write = ret;
    ptr = buf;
    if (size > 0)
      size -= to_write;
    while (to_write > 0) {
      ret = write(to_fd, ptr, to_write);
      if (ret < 0) {
        printf("error: "
               "failed to write to output file"
               ": %s"
               "\n",
               strerror(errno));
        exit(-1);
      };
      to_write -= ret;
      ptr += ret;
    }
  }
}


x86 assembly (clang, -O0):
<copy_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1050,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e4d(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
mov    %eax,-0x1034(%rbp)
cmpl   $0x0,-0x1034(%rbp)
jge    11e0 <copy_data+0x80>
mov    -0x10(%rbp),%rax
mov    %rax,-0x1048(%rbp)
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x1048(%rbp),%rsi
mov    %rax,%rdx
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    0x2dea(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
mov    %eax,-0x1034(%rbp)
cmpl   $0x0,-0x1034(%rbp)
jge    1243 <copy_data+0xe3>
mov    -0x20(%rbp),%rax
mov    %rax,-0x1050(%rbp)
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x1050(%rbp),%rsi
mov    %rax,%rdx
lea    0xdfc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
jmp    1248 <copy_data+0xe8>
cmpq   $0x0,-0x28(%rbp)
je     13c8 <copy_data+0x268>
mov    -0x14(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
mov    %eax,-0x1034(%rbp)
cmpl   $0x0,-0x1034(%rbp)
jge    12a3 <copy_data+0x143>
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xdc9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x1034(%rbp)
jne    12e4 <copy_data+0x184>
cmpq   $0x0,-0x28(%rbp)
jle    12e4 <copy_data+0x184>
mov    0x2d1e(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xdb3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x1034(%rbp)
jne    12f6 <copy_data+0x196>
jmp    13c8 <copy_data+0x268>
mov    -0x1034(%rbp),%eax
mov    %eax,-0x1038(%rbp)
lea    -0x1030(%rbp),%rax
mov    %rax,-0x1040(%rbp)
cmpq   $0x0,-0x28(%rbp)
jle    132d <copy_data+0x1cd>
movslq -0x1038(%rbp),%rcx
mov    -0x28(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
jmp    1332 <copy_data+0x1d2>
cmpl   $0x0,-0x1038(%rbp)
jle    13c3 <copy_data+0x263>
mov    -0x4(%rbp),%edi
mov    -0x1040(%rbp),%rsi
mov    -0x1038(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1034(%rbp)
cmpl   $0x0,-0x1034(%rbp)
jge    1390 <copy_data+0x230>
mov    0x2c72(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xd30(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x1034(%rbp),%ecx
mov    -0x1038(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1038(%rbp)
mov    -0x1034(%rbp),%ecx
mov    -0x1040(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x1040(%rbp)
jmp    1332 <copy_data+0x1d2>
jmp    1248 <copy_data+0xe8>
add    $0x1050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_data>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1008,%rsp
mov    %r8,%r12
mov    %rcx,%r15
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%r13d
mov    0x2e51(%rip),%rbp
mov    0x0(%rbp),%edx
call   1050 <lseek@plt>
test   %eax,%eax
js     1288 <copy_data+0x128>
mov    0x0(%rbp),%edx
mov    %r14d,%edi
mov    %r15,%rsi
call   1050 <lseek@plt>
test   %eax,%eax
js     12a2 <copy_data+0x142>
test   %r12,%r12
je     121f <copy_data+0xbf>
mov    %rsp,%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
test   %eax,%eax
js     125a <copy_data+0xfa>
mov    %eax,%ebx
jne    11e5 <copy_data+0x85>
test   %r12,%r12
jg     1271 <copy_data+0x111>
test   %ebx,%ebx
je     121f <copy_data+0xbf>
test   %r12,%r12
mov    %ebx,%eax
mov    $0x0,%ecx
cmovle %rcx,%rax
sub    %rax,%r12
mov    %r15,%rbp
nopl   (%rax)
mov    %r13d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
test   %eax,%eax
js     1231 <copy_data+0xd1>
mov    %eax,%ecx
add    %rcx,%rbp
sub    %eax,%ebx
jg     1200 <copy_data+0xa0>
test   %r12,%r12
jne    11c0 <copy_data+0x60>
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe69(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdec(%rip),%rdi
jmp    1246 <copy_data+0xe6>
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe00(%rip),%rdi
jmp    1246 <copy_data+0xe6>
mov    0x2d51(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xd63(%rip),%rdi
mov    %rbx,%rsi
jmp    12ba <copy_data+0x15a>
mov    0x2d37(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xd77(%rip),%rdi
mov    %r15,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@SEEK_SET = global i32 0
@.str = [46 x i8] c"error: failed to seek output file to %lx: %s\0A\00"
@errno = global i32 0
@.str.1 = [45 x i8] c"error: failed to seek input file to %lx: %s\0A\00"
@.str.2 = [43 x i8] c"error: failed to read from input file: %s\0A\00"
@.str.3 = [41 x i8] c"error: eof while reading input file: %s\0A\00"
@.str.4 = [43 x i8] c"error: failed to write to output file: %s\0A\00"
define void @copy_data(i32 %0, i8* %1, i32 %2, i8* %3, i64 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca [4096 x i8]
%12 = alloca i32
%13 = alloca i32
%14 = alloca i8*
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i8* %3, i8** %9
store i64 %4, i64* %10
%15 = load i32, i32* %6
%16 = load i8*, i8** %7
%17 = load i32, i32* @SEEK_SET
%18 = call i32 @lseek(i32 %15, i8* %16, i32 %17)
store i32 %18, i32* %12
%19 = load i32, i32* %12
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %5
%22 = load i8*, i8** %7
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i8* %22, i8* %24)
%26 = call i32 @exit(i32 -1)
unreachable
27: ; preds = %5
%28 = load i32, i32* %8
%29 = load i8*, i8** %9
%30 = load i32, i32* @SEEK_SET
%31 = call i32 @lseek(i32 %28, i8* %29, i32 %30)
store i32 %31, i32* %12
%32 = load i32, i32* %12
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %40
34: ; preds = %27
%35 = load i8*, i8** %9
%36 = load i32, i32* @errno
%37 = call i8* @strerror(i32 %36)
%38 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i8* %35, i8* %37)
%39 = call i32 @exit(i32 -1)
unreachable
40: ; preds = %27
br label %41
41: ; preds = %104, %40
%42 = load i64, i64* %10
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %105
44: ; preds = %41
%45 = load i32, i32* %8
%46 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%47 = call i32 @read(i32 %45, i8* %46, i32 4096)
store i32 %47, i32* %12
%48 = load i32, i32* %12
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %55
50: ; preds = %44
%51 = load i32, i32* @errno
%52 = call i8* @strerror(i32 %51)
%53 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i8* %52)
%54 = call i32 @exit(i32 -1)
unreachable
55: ; preds = %44
%56 = load i32, i32* %12
%57 = icmp eq i32 %56, 0
br i1 %57, label %58, label %66
58: ; preds = %55
%59 = load i64, i64* %10
%60 = icmp sgt i64 %59, 0
br i1 %60, label %61, label %66
61: ; preds = %58
%62 = load i32, i32* @errno
%63 = call i8* @strerror(i32 %62)
%64 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i8* %63)
%65 = call i32 @exit(i32 -1)
unreachable
66: ; preds = %58, %55
%67 = load i32, i32* %12
%68 = icmp eq i32 %67, 0
br i1 %68, label %69, label %70
69: ; preds = %66
br label %105
70: ; preds = %66
%71 = load i32, i32* %12
store i32 %71, i32* %13
%72 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
store i8* %72, i8** %14
%73 = load i64, i64* %10
%74 = icmp sgt i64 %73, 0
br i1 %74, label %75, label %80
75: ; preds = %70
%76 = load i32, i32* %13
%77 = sext i32 %76 to i64
%78 = load i64, i64* %10
%79 = sub nsw i64 %78, %77
store i64 %79, i64* %10
br label %80
80: ; preds = %75, %70
br label %81
81: ; preds = %96, %80
%82 = load i32, i32* %13
%83 = icmp sgt i32 %82, 0
br i1 %83, label %84, label %104
84: ; preds = %81
%85 = load i32, i32* %6
%86 = load i8*, i8** %14
%87 = load i32, i32* %13
%88 = call i32 @write(i32 %85, i8* %86, i32 %87)
store i32 %88, i32* %12
%89 = load i32, i32* %12
%90 = icmp slt i32 %89, 0
br i1 %90, label %91, label %96
91: ; preds = %84
%92 = load i32, i32* @errno
%93 = call i8* @strerror(i32 %92)
%94 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i8* %93)
%95 = call i32 @exit(i32 -1)
unreachable
96: ; preds = %84
%97 = load i32, i32* %12
%98 = load i32, i32* %13
%99 = sub nsw i32 %98, %97
store i32 %99, i32* %13
%100 = load i32, i32* %12
%101 = load i8*, i8** %14
%102 = sext i32 %100 to i64
%103 = getelementptr inbounds i8, i8* %101, i64 %102
store i8* %103, i8** %14
br label %81
104: ; preds = %81
br label %41
105: ; preds = %69, %41
ret void
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@.str = [46 x i8] c"error: failed to seek output file to %lx: %s\0A\00"
@errno = global i32 0
@.str.1 = [45 x i8] c"error: failed to seek input file to %lx: %s\0A\00"
@.str.2 = [43 x i8] c"error: failed to read from input file: %s\0A\00"
@.str.3 = [41 x i8] c"error: eof while reading input file: %s\0A\00"
@.str.4 = [43 x i8] c"error: failed to write to output file: %s\0A\00"
define void @copy_data(i32 %0, i8* %1, i32 %2, i8* %3, i64 %4) {
%6 = alloca [4096 x i8]
%7 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%8 = load i32, i32* @SEEK_SET
%9 = tail call i32 @lseek(i32 %0, i8* %1, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %5
%12 = load i32, i32* @errno
%13 = tail call i8* @strerror(i32 %12)
%14 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %13)
%15 = tail call i32 @exit(i32 -1)
unreachable
16: ; preds = %5
%17 = load i32, i32* @SEEK_SET
%18 = tail call i32 @lseek(i32 %2, i8* %3, i32 %17)
%19 = icmp slt i32 %18, 0
br i1 %19, label %22, label %20
20: ; preds = %16
%21 = icmp eq i64 %4, 0
br i1 %21, label %67, label %29
22: ; preds = %16
%23 = load i32, i32* @errno
%24 = tail call i8* @strerror(i32 %23)
%25 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i8* %3, i8* %24)
%26 = tail call i32 @exit(i32 -1)
unreachable
27: ; preds = %62
%28 = icmp eq i64 %51, 0
br i1 %28, label %67, label %29
29: ; preds = %20, %27
%30 = phi i64 [ %51, %27 ], [ %4, %20 ]
%31 = call i32 @read(i32 %2, i8* %7, i32 4096)
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %38
33: ; preds = %29
%34 = load i32, i32* @errno
%35 = tail call i8* @strerror(i32 %34)
%36 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i8* %35)
%37 = tail call i32 @exit(i32 -1)
unreachable
38: ; preds = %29
%39 = icmp eq i32 %31, 0
%40 = icmp sgt i64 %30, 0
%41 = select i1 %39, i1 %40, i1 false
br i1 %41, label %42, label %47
42: ; preds = %38
%43 = load i32, i32* @errno
%44 = tail call i8* @strerror(i32 %43)
%45 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i8* %44)
%46 = tail call i32 @exit(i32 -1)
unreachable
47: ; preds = %38
br i1 %39, label %67, label %48
48: ; preds = %47
%49 = zext i32 %31 to i64
%50 = select i1 %40, i64 %49, i64 0
%51 = sub nsw i64 %30, %50
br label %52
52: ; preds = %48, %62
%53 = phi i8* [ %65, %62 ], [ %7, %48 ]
%54 = phi i32 [ %63, %62 ], [ %31, %48 ]
%55 = call i32 @write(i32 %0, i8* %53, i32 %54)
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %62
57: ; preds = %52
%58 = load i32, i32* @errno
%59 = tail call i8* @strerror(i32 %58)
%60 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i8* %59)
%61 = tail call i32 @exit(i32 -1)
unreachable
62: ; preds = %52
%63 = sub nsw i32 %54, %55
%64 = zext i32 %55 to i64
%65 = getelementptr inbounds i8, i8* %53, i64 %64
%66 = icmp sgt i32 %63, 0
br i1 %66, label %52, label %27
67: ; preds = %47, %27, %20
ret void
}