#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 */

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int read(int, char *, unsigned int);
scalar_t__ write(int, char *, int);
void copy_partial(int fd, int od, unsigned int len) {
  char idata[4096];
  unsigned int n = 0;
  int r;
  while (n + 4096 < len) {
    if (read(fd, idata, 4096) != 4096) {
      ;
      perror("read");
      exit(1);
    }
    if (write(od, idata, 4096) < 0) {
      perror("write");
      exit(1);
    }
    n += 4096;
  }
  r = read(fd, idata, len - n);
  if (r < 0) {
    perror("read");
    exit(1);
  }
  if (write(od, idata, r) < 0) {
    perror("write");
    exit(1);
  }
}


x86 assembly (clang, -O0):
<copy_partial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
movl   $0x0,-0x1014(%rbp)
mov    -0x1014(%rbp),%eax
add    $0x1000,%eax
cmp    -0xc(%rbp),%eax
jae    11f1 <copy_partial+0xb1>
mov    -0x4(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
cmp    $0x1000,%eax
je     11a7 <copy_partial+0x67>
lea    0xe68(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x8(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    11db <copy_partial+0x9b>
lea    0xe39(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x1014(%rbp),%eax
add    $0x1000,%eax
mov    %eax,-0x1014(%rbp)
jmp    115e <copy_partial+0x1e>
mov    -0x4(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    -0xc(%rbp),%edx
sub    -0x1014(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x1018(%rbp)
cmpl   $0x0,-0x1018(%rbp)
jge    1232 <copy_partial+0xf2>
lea    0xddd(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x8(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    -0x1018(%rbp),%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    1267 <copy_partial+0x127>
lea    0xdad(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_partial>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1000,%rsp
mov    %edx,%r12d
mov    %esi,%r14d
mov    %edi,%r15d
xor    %ecx,%ecx
mov    %rsp,%rbx
nopl   (%rax)
lea    0x1000(%rcx),%ebp
cmp    %r12d,%ebp
jae    11a2 <copy_partial+0x62>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
cmp    $0x1000,%eax
jne    11d9 <copy_partial+0x99>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1030 <write@plt>
mov    %ebp,%ecx
test   %rax,%rax
jns    1160 <copy_partial+0x20>
lea    0xe65(%rip),%rdi
jmp    11e0 <copy_partial+0xa0>
sub    %ecx,%r12d
mov    %rsp,%rsi
mov    %r15d,%edi
mov    %r12d,%edx
call   1040 <read@plt>
test   %eax,%eax
js     11d9 <copy_partial+0x99>
mov    %rsp,%rsi
mov    %r14d,%edi
mov    %eax,%edx
call   1030 <write@plt>
test   %rax,%rax
js     1199 <copy_partial+0x59>
add    $0x1000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe20(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"read\00"
@.str.1 = [6 x i8] c"write\00"
define void @copy_partial(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [4096 x i8]
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i32 0, i32* %8
br label %10
10: ; preds = %31, %3
%11 = load i32, i32* %8
%12 = add i32 %11, 4096
%13 = load i32, i32* %6
%14 = icmp ult i32 %12, %13
br i1 %14, label %15, label %34
15: ; preds = %10
%16 = load i32, i32* %4
%17 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%18 = call i32 @read(i32 %16, i8* %17, i32 4096)
%19 = icmp ne i32 %18, 4096
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %15
%24 = load i32, i32* %5
%25 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%26 = call i64 @write(i32 %24, i8* %25, i32 4096)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %23
%29 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %23
%32 = load i32, i32* %8
%33 = add i32 %32, 4096
store i32 %33, i32* %8
br label %10
34: ; preds = %10
%35 = load i32, i32* %4
%36 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%37 = load i32, i32* %6
%38 = load i32, i32* %8
%39 = sub i32 %37, %38
%40 = call i32 @read(i32 %35, i8* %36, i32 %39)
store i32 %40, i32* %9
%41 = load i32, i32* %9
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %34
%44 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %34
%47 = load i32, i32* %5
%48 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%49 = load i32, i32* %9
%50 = call i64 @write(i32 %47, i8* %48, i32 %49)
%51 = icmp slt i64 %50, 0
br i1 %51, label %52, label %55
52: ; preds = %46
%53 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%54 = call i32 @exit(i32 1)
unreachable
55: ; preds = %46
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"read\00"
@.str.1 = [6 x i8] c"write\00"
define void @copy_partial(i32 %0, i32 %1, i32 %2) {
%4 = alloca [4096 x i8]
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
br label %6
6: ; preds = %16, %3
%7 = phi i32 [ 0, %3 ], [ %8, %16 ]
%8 = add i32 %7, 4096
%9 = icmp ult i32 %8, %2
br i1 %9, label %10, label %22
10: ; preds = %6
%11 = call i32 @read(i32 %0, i8* %5, i32 4096)
%12 = icmp eq i32 %11, 4096
br i1 %12, label %16, label %13
13: ; preds = %10
%14 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %10
%17 = call i64 @write(i32 %1, i8* %5, i32 4096)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %6
19: ; preds = %16
%20 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %6
%23 = sub i32 %2, %7
%24 = call i32 @read(i32 %0, i8* %5, i32 %23)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %22
%30 = call i64 @write(i32 %1, i8* %5, i32 %24)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%34 = tail call i32 @exit(i32 1)
unreachable
35: ; preds = %29
ret void
}