#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 PIPE_BUF;
int r_read(int, char *, int);
scalar_t__ r_write(int, char *, int);
int copyblock(int fromfd, int tofd, size_t size) {
  char buf[PIPE_BUF];
  int bytesread;
  int totalbytes = 0;
  int blocksize = PIPE_BUF;
  int bytesleft;
  while (totalbytes < size) {
    bytesleft = size - totalbytes;
    if (bytesleft < PIPE_BUF) {
      blocksize = bytesleft;
    } else {
      blocksize = PIPE_BUF;
    }
    if ((bytesread = r_read(fromfd, buf, blocksize)) < 0)
      return -1;
    if (bytesread == 0)
      return totalbytes;
    if (r_write(tofd, buf, bytesread) < 0)
      return -1;
    totalbytes += bytesread;
  }
  return totalbytes;
}


x86 assembly (clang, -O0):
<copyblock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2ea7(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x30(%rbp)
mov    0x2e74(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
movslq -0x30(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    123a <copyblock+0x11a>
mov    -0x18(%rbp),%rax
movslq -0x30(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
mov    0x2e49(%rip),%rcx
cmp    (%rcx),%eax
jge    11aa <copyblock+0x8a>
mov    -0x38(%rbp),%eax
mov    %eax,-0x34(%rbp)
jmp    11b6 <copyblock+0x96>
mov    0x2e2f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x48(%rbp),%rsi
mov    -0x8(%rbp),%edi
mov    -0x34(%rbp),%edx
call   1040 <r_read@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    11e4 <copyblock+0xc4>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    1247 <copyblock+0x127>
cmpl   $0x0,-0x2c(%rbp)
jne    1200 <copyblock+0xe0>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    1247 <copyblock+0x127>
mov    -0x48(%rbp),%rsi
mov    -0xc(%rbp),%edi
mov    -0x2c(%rbp),%edx
call   1030 <r_write@plt>
cmp    $0x0,%rax
jge    122c <copyblock+0x10c>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    1247 <copyblock+0x127>
mov    -0x2c(%rbp),%eax
add    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
jmp    1171 <copyblock+0x51>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copyblock>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %esi,-0x2c(%rbp)
mov    0x2ea5(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rbx
lea    0xf(%rdx),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
test   %r12,%r12
je     11d0 <copyblock+0xb0>
mov    %edi,%r15d
cmp    %r12d,%edx
cmovg  %r12d,%edx
mov    %rbx,%rsi
call   1040 <r_read@plt>
mov    %eax,%r13d
mov    $0xffffffff,%eax
test   %r13d,%r13d
js     11d7 <copyblock+0xb7>
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
nop
test   %r13d,%r13d
je     11d4 <copyblock+0xb4>
mov    -0x2c(%rbp),%edi
mov    %rbx,%rsi
mov    %r13d,%edx
call   1030 <r_write@plt>
test   %rax,%rax
js     11c9 <copyblock+0xa9>
add    %r13d,%r14d
movslq %r14d,%rax
cmp    %r12,%rax
jae    11d4 <copyblock+0xb4>
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
mov    %r12d,%edx
sub    %r14d,%edx
cmp    %eax,%edx
cmovge %eax,%edx
mov    %r15d,%edi
mov    %rbx,%rsi
call   1040 <r_read@plt>
mov    %eax,%r13d
test   %eax,%eax
jns    1180 <copyblock+0x60>
mov    $0xffffffff,%eax
jmp    11d7 <copyblock+0xb7>
xor    %eax,%eax
jmp    11d7 <copyblock+0xb7>
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PIPE_BUF = global i32 0
define i32 @copyblock(i32 %0, i32 %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i64 %2, i64* %7
%15 = load i32, i32* @PIPE_BUF
%16 = zext i32 %15 to i64
%17 = call i8* @llvm.stacksave()
store i8* %17, i8** %8
%18 = alloca i8, i64 %16
store i64 %16, i64* %9
store i32 0, i32* %11
%19 = load i32, i32* @PIPE_BUF
store i32 %19, i32* %12
br label %20
20: ; preds = %55, %3
%21 = load i32, i32* %11
%22 = sext i32 %21 to i64
%23 = load i64, i64* %7
%24 = icmp ult i64 %22, %23
br i1 %24, label %25, label %59
25: ; preds = %20
%26 = load i64, i64* %7
%27 = load i32, i32* %11
%28 = sext i32 %27 to i64
%29 = sub i64 %26, %28
%30 = trunc i64 %29 to i32
store i32 %30, i32* %13
%31 = load i32, i32* %13
%32 = load i32, i32* @PIPE_BUF
%33 = icmp slt i32 %31, %32
br i1 %33, label %34, label %36
34: ; preds = %25
%35 = load i32, i32* %13
store i32 %35, i32* %12
br label %38
36: ; preds = %25
%37 = load i32, i32* @PIPE_BUF
store i32 %37, i32* %12
br label %38
38: ; preds = %36, %34
%39 = load i32, i32* %5
%40 = load i32, i32* %12
%41 = call i32 @r_read(i32 %39, i8* %18, i32 %40)
store i32 %41, i32* %10
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %38
store i32 -1, i32* %4
store i32 1, i32* %14
br label %61
44: ; preds = %38
%45 = load i32, i32* %10
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %44
%48 = load i32, i32* %11
store i32 %48, i32* %4
store i32 1, i32* %14
br label %61
49: ; preds = %44
%50 = load i32, i32* %6
%51 = load i32, i32* %10
%52 = call i64 @r_write(i32 %50, i8* %18, i32 %51)
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %49
store i32 -1, i32* %4
store i32 1, i32* %14
br label %61
55: ; preds = %49
%56 = load i32, i32* %10
%57 = load i32, i32* %11
%58 = add nsw i32 %57, %56
store i32 %58, i32* %11
br label %20
59: ; preds = %20
%60 = load i32, i32* %11
store i32 %60, i32* %4
store i32 1, i32* %14
br label %61
61: ; preds = %59, %54, %47, %43
%62 = load i8*, i8** %8
%63 = load i32, i32* %4
ret i32 %63
}

LLVM IR (-O2):
@PIPE_BUF = global i32 0
define i32 @copyblock(i32 %0, i32 %1, i64 %2) {
%4 = load i32, i32* @PIPE_BUF
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = trunc i64 %2 to i32
%8 = icmp eq i64 %2, 0
br i1 %8, label %32, label %9
9: ; preds = %3
%10 = icmp sgt i32 %4, %7
%11 = select i1 %10, i32 %7, i32 %4
%12 = call i32 @r_read(i32 %0, i8* %6, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %32, label %24
14: ; preds = %28
%15 = sext i32 %31 to i64
%16 = icmp ult i64 %15, %2
br i1 %16, label %17, label %32
17: ; preds = %14
%18 = load i32, i32* @PIPE_BUF
%19 = sub i32 %7, %31
%20 = icmp slt i32 %19, %18
%21 = select i1 %20, i32 %19, i32 %18
%22 = call i32 @r_read(i32 %0, i8* %6, i32 %21)
%23 = icmp slt i32 %22, 0
br i1 %23, label %32, label %24
24: ; preds = %9, %17
%25 = phi i32 [ %22, %17 ], [ %12, %9 ]
%26 = phi i32 [ %31, %17 ], [ 0, %9 ]
%27 = icmp eq i32 %25, 0
br i1 %27, label %32, label %28
28: ; preds = %24
%29 = call i64 @r_write(i32 %1, i8* %6, i32 %25)
%30 = icmp slt i64 %29, 0
%31 = add nsw i32 %25, %26
br i1 %30, label %32, label %14
32: ; preds = %14, %28, %24, %17, %9, %3
%33 = phi i32 [ 0, %3 ], [ -1, %9 ], [ -1, %17 ], [ %26, %24 ], [ -1, %28 ], [ %31, %14 ]
ret i32 %33
}