#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 */
scalar_t__ EAGAIN;
scalar_t__ errno;
size_t getpagesize();
int read(int, char *, size_t);
scalar_t__ write(int, char *, int);
int copy(int from, int to) {
  size_t bsize = getpagesize();
  char buf[bsize];
  int nread = read(from, buf, bsize);
  if (nread < 0 && errno != EAGAIN) {
    return -1;
  }
  if (nread > 0) {
    if (write(to, buf, nread) < 0) {
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
xor    %eax,%eax
call   1050 <getpagesize@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x38(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rdx
call   1040 <read@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11b6 <copy+0x86>
mov    0x2e50(%rip),%rax
mov    (%rax),%rax
mov    0x2e36(%rip),%rcx
cmp    (%rcx),%rax
je     11b6 <copy+0x86>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x30(%rbp)
jmp    11ff <copy+0xcf>
cmpl   $0x0,-0x2c(%rbp)
jle    11f1 <copy+0xc1>
mov    -0x38(%rbp),%rsi
mov    -0xc(%rbp),%edi
mov    -0x2c(%rbp),%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    11ec <copy+0xbc>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x30(%rbp)
jmp    11ff <copy+0xcf>
jmp    11f1 <copy+0xc1>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %edi,%r15d
xor    %eax,%eax
call   1050 <getpagesize@plt>
mov    %rsp,%rbx
lea    0xf(%rax),%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rbx
mov    %rbx,%rsp
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1040 <read@plt>
test   %eax,%eax
js     1185 <copy+0x55>
je     11a0 <copy+0x70>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %rax,%rax
jns    11a0 <copy+0x70>
mov    $0xffffffff,%eax
jmp    11a2 <copy+0x72>
mov    0x2e54(%rip),%rax
mov    (%rax),%rcx
mov    0x2e3a(%rip),%rdx
mov    $0xffffffff,%eax
cmp    (%rdx),%rcx
jne    11a2 <copy+0x72>
xor    %eax,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EAGAIN = global i64 0
define i32 @copy(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%11 = call i64 (...) @getpagesize()
store i64 %11, i64* %6
%12 = load i64, i64* %6
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %7
%14 = alloca i8, i64 %12
store i64 %12, i64* %8
%15 = load i32, i32* %4
%16 = load i64, i64* %6
%17 = call i32 @read(i32 %15, i8* %14, i64 %16)
store i32 %17, i32* %9
%18 = load i32, i32* %9
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %2
%21 = load i64, i64* @errno
%22 = load i64, i64* @EAGAIN
%23 = icmp ne i64 %21, %22
br i1 %23, label %24, label %25
24: ; preds = %20
store i32 -1, i32* %3
store i32 1, i32* %10
br label %36
25: ; preds = %20, %2
%26 = load i32, i32* %9
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %35
28: ; preds = %25
%29 = load i32, i32* %5
%30 = load i32, i32* %9
%31 = call i64 @write(i32 %29, i8* %14, i32 %30)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %28
store i32 -1, i32* %3
store i32 1, i32* %10
br label %36
34: ; preds = %28
br label %35
35: ; preds = %34, %25
store i32 0, i32* %3
store i32 1, i32* %10
br label %36
36: ; preds = %35, %33, %24
%37 = load i8*, i8** %7
%38 = load i32, i32* %3
ret i32 %38
}

LLVM IR (-O2):
@errno = global i64 0
@EAGAIN = global i64 0
define i32 @copy(i32 %0, i32 %1) {
%3 = tail call i64 (...) @getpagesize()
%4 = alloca i8, i64 %3
%5 = call i32 @read(i32 %0, i8* %4, i64 %3)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %11
7: ; preds = %2
%8 = load i64, i64* @errno
%9 = load i64, i64* @EAGAIN
%10 = icmp eq i64 %8, %9
br i1 %10, label %16, label %17
11: ; preds = %2
%12 = icmp eq i32 %5, 0
br i1 %12, label %16, label %13
13: ; preds = %11
%14 = call i64 @write(i32 %1, i8* %4, i32 %5)
%15 = icmp slt i64 %14, 0
br i1 %15, label %17, label %16
16: ; preds = %7, %13, %11
br label %17
17: ; preds = %13, %7, %16
%18 = phi i32 [ 0, %16 ], [ -1, %7 ], [ -1, %13 ]
ret i32 %18
}