#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*/ perror(char *);
int read(int, char *, unsigned int);
scalar_t__ write(int, char *, int);
int 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");
      return -1;
    }
    if (write(od, idata, 4096) < 0) {
      perror("write");
      return -1;
    }
    n += 4096;
  }
  r = read(fd, idata, len - n);
  if (r < 0) {
    perror("read");
    return -1;
  }
  if (write(od, idata, r) < 0) {
    perror("write");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<copy_partial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movl   $0x0,-0x1014(%rbp)
mov    -0x1014(%rbp),%eax
add    $0x1000,%eax
cmp    -0x10(%rbp),%eax
jae    11e5 <copy_partial+0xb5>
mov    -0x8(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
cmp    $0x1000,%eax
je     1199 <copy_partial+0x69>
lea    0xe78(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1266 <copy_partial+0x136>
mov    -0xc(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    11cf <copy_partial+0x9f>
lea    0xe47(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1266 <copy_partial+0x136>
mov    -0x1014(%rbp),%eax
add    $0x1000,%eax
mov    %eax,-0x1014(%rbp)
jmp    114e <copy_partial+0x1e>
mov    -0x8(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    -0x10(%rbp),%edx
sub    -0x1014(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x1018(%rbp)
cmpl   $0x0,-0x1018(%rbp)
jge    1228 <copy_partial+0xf8>
lea    0xde9(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1266 <copy_partial+0x136>
mov    -0xc(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    -0x1018(%rbp),%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    125f <copy_partial+0x12f>
lea    0xdb7(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1266 <copy_partial+0x136>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_partial>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1008,%rsp
mov    %edx,%ebx
mov    %esi,%r15d
mov    %edi,%r12d
xor    %ecx,%ecx
mov    %rsp,%rbp
lea    0xeab(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nop
lea    0x1000(%rcx),%r13d
cmp    %ebx,%r13d
jae    11a4 <copy_partial+0x74>
mov    %r12d,%edi
mov    %rbp,%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
cmp    $0x1000,%eax
jne    11d5 <copy_partial+0xa5>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0x1000,%edx
call   1030 <write@plt>
mov    %r13d,%ecx
test   %rax,%rax
jns    1160 <copy_partial+0x30>
lea    0xe63(%rip),%r14
jmp    11d5 <copy_partial+0xa5>
sub    %ecx,%ebx
mov    %rsp,%rsi
mov    %r12d,%edi
mov    %ebx,%edx
call   1040 <read@plt>
test   %eax,%eax
js     11d5 <copy_partial+0xa5>
mov    %rsp,%rsi
mov    %r15d,%edi
mov    %eax,%edx
call   1030 <write@plt>
mov    %rax,%rcx
xor    %eax,%eax
lea    0xe35(%rip),%r14
test   %rcx,%rcx
jns    11e2 <copy_partial+0xb2>
mov    %r14,%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [5 x i8] c"read\00"
@.str.1 = [6 x i8] c"write\00"
define i32 @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 = %13, %3
%7 = phi i32 [ 0, %3 ], [ %8, %13 ]
%8 = add i32 %7, 4096
%9 = icmp ult i32 %8, %2
br i1 %9, label %10, label %16
10: ; preds = %6
%11 = call i32 @read(i32 %0, i8* %5, i32 4096)
%12 = icmp eq i32 %11, 4096
br i1 %12, label %13, label %23
13: ; preds = %10
%14 = call i64 @write(i32 %1, i8* %5, i32 4096)
%15 = icmp slt i64 %14, 0
br i1 %15, label %23, label %6
16: ; preds = %6
%17 = sub i32 %2, %7
%18 = call i32 @read(i32 %0, i8* %5, i32 %17)
%19 = icmp slt i32 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %16
%21 = call i64 @write(i32 %1, i8* %5, i32 %18)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %13, %10, %20, %16
%24 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %16 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), %20 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %10 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), %13 ]
%25 = tail call i32 @perror(i8* %24)
br label %26
26: ; preds = %23, %20
%27 = phi i32 [ 0, %20 ], [ -1, %23 ]
ret i32 %27
}