#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 > (int)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     12e0 <copy+0x170>
mov    -0x1014(%rbp),%eax
mov    %eax,-0x1018(%rbp)
cmpl   $0x1000,-0x1018(%rbp)
jle    11f7 <copy+0x87>
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     129e <copy+0x12e>
mov    0x2da7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1020(%rbp)
cmpl   $0x0,-0x101c(%rbp)
je     1268 <copy+0xf8>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,-0x1028(%rbp)
jmp    127b <copy+0x10b>
lea    0xdad(%rip),%rax
mov    %rax,-0x1028(%rbp)
jmp    127b <copy+0x10b>
mov    -0x1020(%rbp),%edi
mov    -0x1028(%rbp),%rdx
lea    0xd7d(%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     12db <copy+0x16b>
lea    0xd66(%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
cmovl  %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 = %63, %21
%24 = load i32, i32* %10
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %64
26: ; preds = %23
%27 = load i32, i32* %10
store i32 %27, i32* %11
%28 = load i32, i32* %11
%29 = icmp sgt i32 %28, 4096
br i1 %29, label %30, label %31
30: ; preds = %26
store i32 4096, i32* %11
br label %31
31: ; preds = %30, %26
%32 = load i32, i32* %11
%33 = load i32, i32* %10
%34 = sub nsw i32 %33, %32
store i32 %34, i32* %10
%35 = load i32, i32* %6
%36 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%37 = load i32, i32* %11
%38 = call i32 @read(i32 %35, i8* %36, i32 %37)
store i32 %38, i32* %12
%39 = load i32, i32* %11
%40 = icmp ne i32 %38, %39
br i1 %40, label %41, label %53
41: ; preds = %31
%42 = load i32, i32* @stderr
%43 = load i32, i32* %12
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %41
%46 = load i32, i32* @errno
%47 = call i8* @strerror(i32 %46)
br label %49
48: ; preds = %41
br label %49
49: ; preds = %48, %45
%50 = phi i8* [ %47, %45 ], [ getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), %48 ]
%51 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %50)
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %31
%54 = load i32, i32* %5
%55 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%56 = load i32, i32* %11
%57 = call i32 @write(i32 %54, i8* %55, i32 %56)
store i32 %57, i32* %12
%58 = load i32, i32* %11
%59 = icmp ne i32 %57, %58
br i1 %59, label %60, label %63
60: ; preds = %53
%61 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%62 = call i32 @exit(i32 1)
unreachable
63: ; preds = %53
br label %23
64: ; 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 slt 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
}