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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
scalar_t__ lseek(int, size_t, int /*<<< orphan*/);
scalar_t__ xread_partial(int, unsigned char *, size_t);
scalar_t__ xwrite(int, unsigned char *, scalar_t__);
ssize_t xcopy(int ofd, int ifd, size_t offset, size_t len) {
  unsigned char buf[8192];
  ssize_t n;
  n = lseek(ifd, offset, SEEK_SET);
  if (n < 0)
    return n;
  while (len) {
    n = xread_partial(ifd, buf, len < 8192 ? len : 8192);
    if (n < 0)
      return n;
    len -= n;
    n = xwrite(ofd, buf, n);
    if (n < 0)
      return n;
  }
  return 0;
}


x86 assembly (clang, -O0):
<xcopy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2040,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x10(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    0x2e89(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
mov    %rax,-0x2028(%rbp)
cmpq   $0x0,-0x2028(%rbp)
jge    1183 <xcopy+0x53>
mov    -0x2028(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1271 <xcopy+0x141>
jmp    1188 <xcopy+0x58>
cmpq   $0x0,-0x20(%rbp)
je     1269 <xcopy+0x139>
mov    -0x10(%rbp),%eax
mov    %eax,-0x2034(%rbp)
lea    -0x2020(%rbp),%rax
mov    %rax,-0x2030(%rbp)
cmpq   $0x2000,-0x20(%rbp)
jae    11c8 <xcopy+0x98>
mov    -0x20(%rbp),%rax
mov    %rax,-0x2040(%rbp)
jmp    11d9 <xcopy+0xa9>
mov    $0x2000,%eax
mov    %rax,-0x2040(%rbp)
jmp    11d9 <xcopy+0xa9>
mov    -0x2030(%rbp),%rsi
mov    -0x2034(%rbp),%edi
mov    -0x2040(%rbp),%rdx
call   1050 <xread_partial@plt>
mov    %rax,-0x2028(%rbp)
cmpq   $0x0,-0x2028(%rbp)
jge    1217 <xcopy+0xe7>
mov    -0x2028(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1271 <xcopy+0x141>
mov    -0x2028(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x2020(%rbp),%rsi
mov    -0x2028(%rbp),%rdx
call   1030 <xwrite@plt>
mov    %rax,-0x2028(%rbp)
cmpq   $0x0,-0x2028(%rbp)
jge    1264 <xcopy+0x134>
mov    -0x2028(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1271 <xcopy+0x141>
jmp    1188 <xcopy+0x58>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x2040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xcopy>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x2008,%rsp
mov    %rcx,%rbx
mov    %esi,%r15d
mov    %edi,%r14d
mov    0x2e93(%rip),%rax
mov    (%rax),%eax
mov    %esi,%edi
mov    %rdx,%rsi
mov    %eax,%edx
call   1040 <lseek@plt>
test   %rax,%rax
js     11af <xcopy+0x7f>
mov    %rsp,%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
test   %rbx,%rbx
je     11ad <xcopy+0x7d>
cmp    $0x2000,%rbx
mov    $0x2000,%edx
cmovb  %rbx,%rdx
mov    %r15d,%edi
mov    %rbp,%rsi
call   1050 <xread_partial@plt>
test   %rax,%rax
js     11af <xcopy+0x7f>
sub    %rax,%rbx
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %rax,%rdx
call   1030 <xwrite@plt>
test   %rax,%rax
jns    1170 <xcopy+0x40>
jmp    11af <xcopy+0x7f>
xor    %eax,%eax
add    $0x2008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_SET = global i32 0
define i64 @xcopy(i32 %0, i32 %1, i64 %2, i64 %3) {
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
%9 = alloca i64
%10 = alloca [8192 x i8]
%11 = alloca i64
store i32 %0, i32* %6
store i32 %1, i32* %7
store i64 %2, i64* %8
store i64 %3, i64* %9
%12 = load i32, i32* %7
%13 = load i64, i64* %8
%14 = load i32, i32* @SEEK_SET
%15 = call i64 @lseek(i32 %12, i64 %13, i32 %14)
store i64 %15, i64* %11
%16 = load i64, i64* %11
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %4
%19 = load i64, i64* %11
store i64 %19, i64* %5
br label %53
20: ; preds = %4
br label %21
21: ; preds = %51, %20
%22 = load i64, i64* %9
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %52
24: ; preds = %21
%25 = load i32, i32* %7
%26 = getelementptr inbounds [8192 x i8], [8192 x i8]* %10, i64 0, i64 0
%27 = load i64, i64* %9
%28 = icmp ult i64 %27, 8192
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = load i64, i64* %9
br label %32
31: ; preds = %24
br label %32
32: ; preds = %31, %29
%33 = phi i64 [ %30, %29 ], [ 8192, %31 ]
%34 = call i64 @xread_partial(i32 %25, i8* %26, i64 %33)
store i64 %34, i64* %11
%35 = load i64, i64* %11
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %32
%38 = load i64, i64* %11
store i64 %38, i64* %5
br label %53
39: ; preds = %32
%40 = load i64, i64* %11
%41 = load i64, i64* %9
%42 = sub i64 %41, %40
store i64 %42, i64* %9
%43 = load i32, i32* %6
%44 = getelementptr inbounds [8192 x i8], [8192 x i8]* %10, i64 0, i64 0
%45 = load i64, i64* %11
%46 = call i64 @xwrite(i32 %43, i8* %44, i64 %45)
store i64 %46, i64* %11
%47 = load i64, i64* %11
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %39
%50 = load i64, i64* %11
store i64 %50, i64* %5
br label %53
51: ; preds = %39
br label %21
52: ; preds = %21
store i64 0, i64* %5
br label %53
53: ; preds = %52, %49, %37, %18
%54 = load i64, i64* %5
ret i64 %54
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
define i64 @xcopy(i32 %0, i32 %1, i64 %2, i64 %3) {
%5 = alloca [8192 x i8]
%6 = getelementptr inbounds [8192 x i8], [8192 x i8]* %5, i64 0, i64 0
%7 = load i32, i32* @SEEK_SET
%8 = tail call i64 @lseek(i32 %1, i64 %2, i32 %7)
%9 = icmp slt i64 %8, 0
br i1 %9, label %22, label %10
10: ; preds = %4, %18
%11 = phi i64 [ %19, %18 ], [ %3, %4 ]
%12 = icmp eq i64 %11, 0
br i1 %12, label %22, label %13
13: ; preds = %10
%14 = icmp ult i64 %11, 8192
%15 = select i1 %14, i64 %11, i64 8192
%16 = call i64 @xread_partial(i32 %1, i8* %6, i64 %15)
%17 = icmp slt i64 %16, 0
br i1 %17, label %22, label %18
18: ; preds = %13
%19 = sub i64 %11, %16
%20 = call i64 @xwrite(i32 %0, i8* %6, i64 %16)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %10
22: ; preds = %10, %18, %13, %4
%23 = phi i64 [ %8, %4 ], [ 0, %10 ], [ %20, %18 ], [ %16, %13 ]
ret i64 %23
}