#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__ off_t;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ free(void *);
scalar_t__ lseek(int, scalar_t__, int /*<<< orphan*/);
void *malloc(int);
size_t read(int, void *, size_t);
size_t write(int, void *, size_t);
int xerror(char *);
int xfilecopy(int fdIn, int fdOut, off_t offsetIn, off_t offsetOut,
              size_t bytes) {
  if ((offsetIn != (off_t)-1) && (lseek(fdIn, offsetIn, SEEK_SET) == (off_t)-1))
    return xerror("Seek failed");
  if ((offsetOut != (off_t)-1) &&
      (lseek(fdOut, offsetOut, SEEK_SET) == (off_t)-1))
    return xerror("Seek failed");
  int CHUNK = 256 * 1024;
  void *buf = malloc(CHUNK);
  if (buf == NULL)
    return xerror("malloc failed");
  size_t left = bytes;
  while (left > 0) {
    size_t wanted = (left < CHUNK) ? left : CHUNK;
    size_t r = read(fdIn, buf, wanted);
    if (r <= 0)
      return xerror("Read failed");
    if (write(fdOut, buf, r) != r)
      return xerror("Write failed");
    left -= r;
  }
  free(buf);
  return 1;
}


x86 assembly (clang, -O0):
<xfilecopy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
cmpq   $0xffffffffffffffff,-0x18(%rbp)
je     11b8 <xfilecopy+0x58>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    0x2e4d(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
cmp    $0xffffffffffffffff,%rax
jne    11b8 <xfilecopy+0x58>
lea    0xe55(%rip),%rdi
call   1060 <xerror@plt>
mov    %eax,-0x4(%rbp)
jmp    12ee <xfilecopy+0x18e>
cmpq   $0xffffffffffffffff,-0x20(%rbp)
je     11f6 <xfilecopy+0x96>
mov    -0xc(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    0x2e0f(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
cmp    $0xffffffffffffffff,%rax
jne    11f6 <xfilecopy+0x96>
lea    0xe17(%rip),%rdi
call   1060 <xerror@plt>
mov    %eax,-0x4(%rbp)
jmp    12ee <xfilecopy+0x18e>
movl   $0x40000,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1228 <xfilecopy+0xc8>
lea    0xdf1(%rip),%rdi
call   1060 <xerror@plt>
mov    %eax,-0x4(%rbp)
jmp    12ee <xfilecopy+0x18e>
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jbe    12de <xfilecopy+0x17e>
mov    -0x40(%rbp),%rax
movslq -0x2c(%rbp),%rcx
cmp    %rcx,%rax
jae    1259 <xfilecopy+0xf9>
mov    -0x40(%rbp),%rax
mov    %rax,-0x58(%rbp)
jmp    1261 <xfilecopy+0x101>
movslq -0x2c(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x48(%rbp),%rdx
call   1070 <read@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
ja     129c <xfilecopy+0x13c>
lea    0xd8b(%rip),%rdi
call   1060 <xerror@plt>
mov    %eax,-0x4(%rbp)
jmp    12ee <xfilecopy+0x18e>
mov    -0xc(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x50(%rbp),%rdx
call   1040 <write@plt>
cmp    -0x50(%rbp),%rax
je     12ca <xfilecopy+0x16a>
lea    0xd69(%rip),%rdi
call   1060 <xerror@plt>
mov    %eax,-0x4(%rbp)
jmp    12ee <xfilecopy+0x18e>
mov    -0x50(%rbp),%rcx
mov    -0x40(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x40(%rbp)
jmp    1230 <xfilecopy+0xd0>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xfilecopy>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %r8,%rbx
mov    %rcx,%rbp
mov    %esi,%r14d
mov    %edi,%r15d
cmp    $0xffffffffffffffff,%rdx
je     119a <xfilecopy+0x3a>
mov    0x2e5f(%rip),%rax
mov    (%rax),%eax
mov    %r15d,%edi
mov    %rdx,%rsi
mov    %eax,%edx
call   1050 <lseek@plt>
cmp    $0xffffffffffffffff,%rax
je     1231 <xfilecopy+0xd1>
cmp    $0xffffffffffffffff,%rbp
je     11ba <xfilecopy+0x5a>
mov    0x2e39(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
mov    %rbp,%rsi
call   1050 <lseek@plt>
cmp    $0xffffffffffffffff,%rax
je     1231 <xfilecopy+0xd1>
mov    $0x40000,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     123a <xfilecopy+0xda>
mov    %rax,%r12
test   %rbx,%rbx
je     121b <xfilecopy+0xbb>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmp    $0x40000,%rbx
mov    $0x40000,%edx
cmovb  %rbx,%rdx
mov    %r15d,%edi
mov    %r12,%rsi
call   1070 <read@plt>
test   %rax,%rax
je     1243 <xfilecopy+0xe3>
mov    %rax,%rbp
mov    %r14d,%edi
mov    %r12,%rsi
mov    %rax,%rdx
call   1040 <write@plt>
cmp    %rbp,%rax
jne    124c <xfilecopy+0xec>
sub    %rbp,%rbx
jne    11e0 <xfilecopy+0x80>
mov    %r12,%rdi
call   1030 <free@plt>
mov    $0x1,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdc8(%rip),%rdi
jmp    1253 <xfilecopy+0xf3>
lea    0xdcb(%rip),%rdi
jmp    1253 <xfilecopy+0xf3>
lea    0xdd0(%rip),%rdi
jmp    1253 <xfilecopy+0xf3>
lea    0xdd3(%rip),%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <xerror@plt>

LLVM IR (-O0):
@SEEK_SET = global i32 0
@.str = [12 x i8] c"Seek failed\00"
@.str.1 = [14 x i8] c"malloc failed\00"
@.str.2 = [12 x i8] c"Read failed\00"
@.str.3 = [13 x i8] c"Write failed\00"
define i32 @xfilecopy(i32 %0, i32 %1, i64 %2, i64 %3, i64 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i64
%15 = alloca i64
%16 = alloca i64
store i32 %0, i32* %7
store i32 %1, i32* %8
store i64 %2, i64* %9
store i64 %3, i64* %10
store i64 %4, i64* %11
%17 = load i64, i64* %9
%18 = icmp ne i64 %17, -1
br i1 %18, label %19, label %27
19: ; preds = %5
%20 = load i32, i32* %7
%21 = load i64, i64* %9
%22 = load i32, i32* @SEEK_SET
%23 = call i64 @lseek(i32 %20, i64 %21, i32 %22)
%24 = icmp eq i64 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %19
%26 = call i32 @xerror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32 %26, i32* %6
br label %86
27: ; preds = %19, %5
%28 = load i64, i64* %10
%29 = icmp ne i64 %28, -1
br i1 %29, label %30, label %38
30: ; preds = %27
%31 = load i32, i32* %8
%32 = load i64, i64* %10
%33 = load i32, i32* @SEEK_SET
%34 = call i64 @lseek(i32 %31, i64 %32, i32 %33)
%35 = icmp eq i64 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %30
%37 = call i32 @xerror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32 %37, i32* %6
br label %86
38: ; preds = %30, %27
store i32 262144, i32* %12
%39 = load i32, i32* %12
%40 = call i8* @malloc(i32 %39)
store i8* %40, i8** %13
%41 = load i8*, i8** %13
%42 = icmp eq i8* %41, null
br i1 %42, label %43, label %45
43: ; preds = %38
%44 = call i32 @xerror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i32 %44, i32* %6
br label %86
45: ; preds = %38
%46 = load i64, i64* %11
store i64 %46, i64* %14
br label %47
47: ; preds = %79, %45
%48 = load i64, i64* %14
%49 = icmp ugt i64 %48, 0
br i1 %49, label %50, label %83
50: ; preds = %47
%51 = load i64, i64* %14
%52 = load i32, i32* %12
%53 = sext i32 %52 to i64
%54 = icmp ult i64 %51, %53
br i1 %54, label %55, label %57
55: ; preds = %50
%56 = load i64, i64* %14
br label %60
57: ; preds = %50
%58 = load i32, i32* %12
%59 = sext i32 %58 to i64
br label %60
60: ; preds = %57, %55
%61 = phi i64 [ %56, %55 ], [ %59, %57 ]
store i64 %61, i64* %15
%62 = load i32, i32* %7
%63 = load i8*, i8** %13
%64 = load i64, i64* %15
%65 = call i64 @read(i32 %62, i8* %63, i64 %64)
store i64 %65, i64* %16
%66 = load i64, i64* %16
%67 = icmp ule i64 %66, 0
br i1 %67, label %68, label %70
68: ; preds = %60
%69 = call i32 @xerror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
store i32 %69, i32* %6
br label %86
70: ; preds = %60
%71 = load i32, i32* %8
%72 = load i8*, i8** %13
%73 = load i64, i64* %16
%74 = call i64 @write(i32 %71, i8* %72, i64 %73)
%75 = load i64, i64* %16
%76 = icmp ne i64 %74, %75
br i1 %76, label %77, label %79
77: ; preds = %70
%78 = call i32 @xerror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
store i32 %78, i32* %6
br label %86
79: ; preds = %70
%80 = load i64, i64* %16
%81 = load i64, i64* %14
%82 = sub i64 %81, %80
store i64 %82, i64* %14
br label %47
83: ; preds = %47
%84 = load i8*, i8** %13
%85 = call i32 @free(i8* %84)
store i32 1, i32* %6
br label %86
86: ; preds = %83, %77, %68, %43, %36, %25
%87 = load i32, i32* %6
ret i32 %87
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@.str = [12 x i8] c"Seek failed\00"
@.str.1 = [14 x i8] c"malloc failed\00"
@.str.2 = [12 x i8] c"Read failed\00"
@.str.3 = [13 x i8] c"Write failed\00"
define i32 @xfilecopy(i32 %0, i32 %1, i64 %2, i64 %3, i64 %4) {
%6 = icmp eq i64 %2, -1
br i1 %6, label %13, label %7
7: ; preds = %5
%8 = load i32, i32* @SEEK_SET
%9 = tail call i64 @lseek(i32 %0, i64 %2, i32 %8)
%10 = icmp eq i64 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = tail call i32 @xerror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %46
13: ; preds = %7, %5
%14 = icmp eq i64 %3, -1
br i1 %14, label %21, label %15
15: ; preds = %13
%16 = load i32, i32* @SEEK_SET
%17 = tail call i64 @lseek(i32 %1, i64 %3, i32 %16)
%18 = icmp eq i64 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = tail call i32 @xerror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %46
21: ; preds = %15, %13
%22 = tail call dereferenceable_or_null(262144) i8* @malloc(i32 262144)
%23 = icmp eq i8* %22, null
br i1 %23, label %26, label %24
24: ; preds = %21
%25 = icmp eq i64 %4, 0
br i1 %25, label %44, label %30
26: ; preds = %21
%27 = tail call i32 @xerror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %46
28: ; preds = %38
%29 = icmp eq i64 %41, 0
br i1 %29, label %44, label %30
30: ; preds = %24, %28
%31 = phi i64 [ %41, %28 ], [ %4, %24 ]
%32 = icmp ult i64 %31, 262144
%33 = select i1 %32, i64 %31, i64 262144
%34 = tail call i64 @read(i32 %0, i8* %22, i64 %33)
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %30
%37 = tail call i32 @xerror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %46
38: ; preds = %30
%39 = tail call i64 @write(i32 %1, i8* %22, i64 %34)
%40 = icmp eq i64 %39, %34
%41 = sub i64 %31, %34
br i1 %40, label %28, label %42
42: ; preds = %38
%43 = tail call i32 @xerror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %46
44: ; preds = %28, %24
%45 = tail call i32 @free(i8* %22)
br label %46
46: ; preds = %42, %36, %26, %44, %19, %11
%47 = phi i32 [ %12, %11 ], [ %20, %19 ], [ %27, %26 ], [ 1, %44 ], [ %43, %42 ], [ %37, %36 ]
ret i32 %47
}