#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int PIPE_BUF;
scalar_t__ feof(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int fread(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ r_write(int, char *, int);
int fcopyblock(FILE *fromfp, 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 = fread(buf, 1, blocksize, fromfp)) < blocksize) {
      if (ferror(fromfp))
        return -1;
    }
    if (r_write(tofd, buf, bytesread) < 0)
      return -1;
    if (feof(fromfp))
      return 0;
    totalbytes += bytesread;
  }
  return totalbytes;
}


x86 assembly (clang, -O0):
<fcopyblock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e86(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x38(%rbp)
mov    0x2e53(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
movslq -0x38(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1283 <fcopyblock+0x143>
mov    -0x20(%rbp),%rax
movslq -0x38(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x40(%rbp)
mov    -0x40(%rbp),%eax
mov    0x2e28(%rip),%rcx
cmp    (%rcx),%eax
jge    11cb <fcopyblock+0x8b>
mov    -0x40(%rbp),%eax
mov    %eax,-0x3c(%rbp)
jmp    11d7 <fcopyblock+0x97>
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x50(%rbp),%rdi
mov    -0x3c(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %eax,-0x34(%rbp)
cmp    -0x3c(%rbp),%eax
jge    1223 <fcopyblock+0xe3>
mov    -0x10(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     121e <fcopyblock+0xde>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1290 <fcopyblock+0x150>
jmp    1223 <fcopyblock+0xe3>
mov    -0x50(%rbp),%rsi
mov    -0x14(%rbp),%edi
mov    -0x34(%rbp),%edx
call   1060 <r_write@plt>
cmp    $0x0,%rax
jge    124f <fcopyblock+0x10f>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1290 <fcopyblock+0x150>
mov    -0x10(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%rax
je     1275 <fcopyblock+0x135>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1290 <fcopyblock+0x150>
mov    -0x34(%rbp),%eax
add    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
jmp    1192 <fcopyblock+0x52>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fcopyblock>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,-0x2c(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r13
lea    0xf(%rax),%rsi
and    $0xfffffffffffffff0,%rsi
sub    %rsi,%r13
mov    %r13,%rsp
test   %rdx,%rdx
je     11ef <fcopyblock+0xaf>
mov    %rdi,%rbx
xor    %r12d,%r12d
mov    %rdx,-0x38(%rbp)
nopl   (%rax)
mov    %edx,%r15d
sub    %r12d,%r15d
cmp    %eax,%r15d
cmovge %eax,%r15d
mov    %r13,%rdi
mov    $0x1,%esi
mov    %r15d,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %eax,%r14d
cmp    %r15d,%eax
jge    11b5 <fcopyblock+0x75>
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
jne    11f4 <fcopyblock+0xb4>
mov    -0x2c(%rbp),%edi
mov    %r13,%rsi
mov    %r14d,%edx
call   1060 <r_write@plt>
test   %rax,%rax
js     11f4 <fcopyblock+0xb4>
mov    %rbx,%rdi
call   1050 <feof@plt>
test   %rax,%rax
jne    11ef <fcopyblock+0xaf>
add    %r14d,%r12d
movslq %r12d,%rax
mov    -0x38(%rbp),%rdx
cmp    %rdx,%rax
jae    11fa <fcopyblock+0xba>
mov    0x2df5(%rip),%rax
mov    (%rax),%eax
jmp    1180 <fcopyblock+0x40>
xor    %r12d,%r12d
jmp    11fa <fcopyblock+0xba>
mov    $0xffffffff,%r12d
mov    %r12d,%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 @fcopyblock(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 = %61, %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 %65
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* %12
%40 = load i32*, i32** %5
%41 = call i32 @fread(i8* %18, i32 1, i32 %39, i32* %40)
store i32 %41, i32* %10
%42 = load i32, i32* %12
%43 = icmp slt i32 %41, %42
br i1 %43, label %44, label %50
44: ; preds = %38
%45 = load i32*, i32** %5
%46 = call i64 @ferror(i32* %45)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %44
store i32 -1, i32* %4
store i32 1, i32* %14
br label %67
49: ; preds = %44
br label %50
50: ; preds = %49, %38
%51 = load i32, i32* %6
%52 = load i32, i32* %10
%53 = call i64 @r_write(i32 %51, i8* %18, i32 %52)
%54 = icmp slt i64 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %50
store i32 -1, i32* %4
store i32 1, i32* %14
br label %67
56: ; preds = %50
%57 = load i32*, i32** %5
%58 = call i64 @feof(i32* %57)
%59 = icmp ne i64 %58, 0
br i1 %59, label %60, label %61
60: ; preds = %56
store i32 0, i32* %4
store i32 1, i32* %14
br label %67
61: ; preds = %56
%62 = load i32, i32* %10
%63 = load i32, i32* %11
%64 = add nsw i32 %63, %62
store i32 %64, i32* %11
br label %20
65: ; preds = %20
%66 = load i32, i32* %11
store i32 %66, i32* %4
store i32 1, i32* %14
br label %67
67: ; preds = %65, %60, %55, %48
%68 = load i8*, i8** %8
%69 = load i32, i32* %4
ret i32 %69
}

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