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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ lseek(int, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int write(int, char *, int);
void copy(int out, int in, off_t offset, off_t size) {
  char ibuf[4096];
  int remaining, cur, count;
  if (lseek(in, offset, SEEK_SET) < 0) {
    perror("copy: lseek");
    exit(1);
  }
  remaining = size;
  while (remaining) {
    cur = remaining;
    if (cur > sizeof ibuf)
      cur = sizeof ibuf;
    remaining -= cur;
    if ((count = read(in, ibuf, cur)) != cur) {
      fprintf(stderr, "copy: read: %s\n",
              count ? strerror(errno) : "premature end of file");
      exit(1);
    }
    if ((count = write(out, ibuf, cur)) != cur) {
      perror("copy: write");
      exit(1);
    }
  }
}


x86 assembly (clang, -O0):
<copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %ecx,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    0x2e3c(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
cmp    $0x0,%rax
jge    11bb <copy+0x4b>
lea    0xe54(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x1014(%rbp)
cmpl   $0x0,-0x1014(%rbp)
je     12e3 <copy+0x173>
mov    -0x1014(%rbp),%eax
mov    %eax,-0x1018(%rbp)
movslq -0x1018(%rbp),%rax
cmp    $0x1000,%rax
jbe    11fa <copy+0x8a>
movl   $0x1000,-0x1018(%rbp)
mov    -0x1018(%rbp),%ecx
mov    -0x1014(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1014(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    -0x1018(%rbp),%edx
call   1050 <read@plt>
mov    %eax,-0x101c(%rbp)
cmp    -0x1018(%rbp),%eax
je     12a1 <copy+0x131>
mov    0x2da4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1020(%rbp)
cmpl   $0x0,-0x101c(%rbp)
je     126b <copy+0xfb>
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,-0x1028(%rbp)
jmp    127e <copy+0x10e>
lea    0xdaa(%rip),%rax
mov    %rax,-0x1028(%rbp)
jmp    127e <copy+0x10e>
mov    -0x1020(%rbp),%edi
mov    -0x1028(%rbp),%rdx
lea    0xd7a(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x4(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    -0x1018(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x101c(%rbp)
cmp    -0x1018(%rbp),%eax
je     12de <copy+0x16e>
lea    0xd63(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    11c4 <copy+0x54>
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1000,%rsp
mov    %ecx,%ebx
mov    %esi,%r15d
mov    %edi,%r14d
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
mov    %esi,%edi
mov    %edx,%esi
mov    %eax,%edx
call   1040 <lseek@plt>
test   %rax,%rax
js     124b <copy+0xdb>
mov    %rsp,%r12
nopw   0x0(%rax,%rax,1)
test   %ebx,%ebx
je     11fc <copy+0x8c>
cmp    $0x1000,%ebx
mov    $0x1000,%ebp
cmovb  %ebx,%ebp
mov    %r15d,%edi
mov    %r12,%rsi
mov    %ebp,%edx
call   1050 <read@plt>
cmp    %ebp,%eax
jne    120c <copy+0x9c>
sub    %ebp,%ebx
mov    %r14d,%edi
mov    %r12,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
je     11b0 <copy+0x40>
lea    0xe45(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
add    $0x1000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dcd(%rip),%rcx
mov    (%rcx),%ebx
test   %eax,%eax
jne    1222 <copy+0xb2>
lea    0xdfc(%rip),%rdx
jmp    1233 <copy+0xc3>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rdx
lea    0xdd2(%rip),%rsi
mov    %ebx,%edi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xdae(%rip),%rdi
jmp    11ed <copy+0x7d>

LLVM IR (-O0):
@SEEK_SET = global i32 0
@.str = [12 x i8] c"copy: lseek\00"
@stderr = global i32 0
@.str.1 = [16 x i8] c"copy: read: %s\0A\00"
@errno = global i32 0
@.str.2 = [22 x i8] c"premature end of file\00"
@.str.3 = [12 x i8] c"copy: write\00"
define void @copy(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [4096 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%13 = load i32, i32* %6
%14 = load i32, i32* %7
%15 = load i32, i32* @SEEK_SET
%16 = call i64 @lseek(i32 %13, i32 %14, i32 %15)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %4
%19 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %4
%22 = load i32, i32* %8
store i32 %22, i32* %10
br label %23
23: ; preds = %64, %21
%24 = load i32, i32* %10
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %65
26: ; preds = %23
%27 = load i32, i32* %10
store i32 %27, i32* %11
%28 = load i32, i32* %11
%29 = sext i32 %28 to i64
%30 = icmp ugt i64 %29, 4096
br i1 %30, label %31, label %32
31: ; preds = %26
store i32 4096, i32* %11
br label %32
32: ; preds = %31, %26
%33 = load i32, i32* %11
%34 = load i32, i32* %10
%35 = sub nsw i32 %34, %33
store i32 %35, i32* %10
%36 = load i32, i32* %6
%37 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%38 = load i32, i32* %11
%39 = call i32 @read(i32 %36, i8* %37, i32 %38)
store i32 %39, i32* %12
%40 = load i32, i32* %11
%41 = icmp ne i32 %39, %40
br i1 %41, label %42, label %54
42: ; preds = %32
%43 = load i32, i32* @stderr
%44 = load i32, i32* %12
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %42
%47 = load i32, i32* @errno
%48 = call i8* @strerror(i32 %47)
br label %50
49: ; preds = %42
br label %50
50: ; preds = %49, %46
%51 = phi i8* [ %48, %46 ], [ getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), %49 ]
%52 = call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %51)
%53 = call i32 @exit(i32 1)
unreachable
54: ; preds = %32
%55 = load i32, i32* %5
%56 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%57 = load i32, i32* %11
%58 = call i32 @write(i32 %55, i8* %56, i32 %57)
store i32 %58, i32* %12
%59 = load i32, i32* %11
%60 = icmp ne i32 %58, %59
br i1 %60, label %61, label %64
61: ; preds = %54
%62 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%63 = call i32 @exit(i32 1)
unreachable
64: ; preds = %54
br label %23
65: ; preds = %23
ret void
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@.str = [12 x i8] c"copy: lseek\00"
@stderr = global i32 0
@.str.1 = [16 x i8] c"copy: read: %s\0A\00"
@errno = global i32 0
@.str.2 = [22 x i8] c"premature end of file\00"
@.str.3 = [12 x i8] c"copy: write\00"
define void @copy(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca [4096 x i8]
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%7 = load i32, i32* @SEEK_SET
%8 = tail call i64 @lseek(i32 %1, i32 %2, i32 %7)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %4
%11 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %4, %31
%14 = phi i32 [ %32, %31 ], [ %3, %4 ]
%15 = icmp eq i32 %14, 0
br i1 %15, label %38, label %16
16: ; preds = %13
%17 = icmp ult i32 %14, 4096
%18 = select i1 %17, i32 %14, i32 4096
%19 = call i32 @read(i32 %1, i8* %6, i32 %18)
%20 = icmp eq i32 %19, %18
br i1 %20, label %31, label %21
21: ; preds = %16
%22 = load i32, i32* @stderr
%23 = icmp eq i32 %19, 0
br i1 %23, label %27, label %24
24: ; preds = %21
%25 = load i32, i32* @errno
%26 = tail call i8* @strerror(i32 %25)
br label %27
27: ; preds = %21, %24
%28 = phi i8* [ %26, %24 ], [ getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), %21 ]
%29 = tail call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %28)
%30 = tail call i32 @exit(i32 1)
unreachable
31: ; preds = %16
%32 = sub nsw i32 %14, %18
%33 = call i32 @write(i32 %0, i8* %6, i32 %18)
%34 = icmp eq i32 %33, %18
br i1 %34, label %13, label %35
35: ; preds = %31
%36 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%37 = tail call i32 @exit(i32 1)
unreachable
38: ; preds = %13
ret void
}