#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 readwrite(int fromfd, int tofd) {
  char buf[PIPE_BUF];
  int bytesread;
  if ((bytesread = r_read(fromfd, buf, PIPE_BUF)) < 0)
    return -1;
  if (bytesread == 0)
    return 0;
  if (r_write(tofd, buf, bytesread) < 0)
    return -1;
  return bytesread;
}


x86 assembly (clang, -O0):
<readwrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2eab(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x30(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e7c(%rip),%rax
mov    (%rax),%edx
call   1040 <r_read@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    118a <readwrite+0x6a>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x28(%rbp)
jmp    11e0 <readwrite+0xc0>
cmpl   $0x0,-0x24(%rbp)
jne    11a7 <readwrite+0x87>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x28(%rbp)
jmp    11e0 <readwrite+0xc0>
mov    -0x30(%rbp),%rsi
mov    -0xc(%rbp),%edi
mov    -0x24(%rbp),%edx
call   1030 <r_write@plt>
cmp    $0x0,%rax
jge    11d3 <readwrite+0xb3>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x28(%rbp)
jmp    11e0 <readwrite+0xc0>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readwrite>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    0x2eac(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rbx
mov    %rdx,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rsi
call   1040 <r_read@plt>
test   %eax,%eax
js     1176 <readwrite+0x56>
je     117d <readwrite+0x5d>
mov    %eax,%r15d
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <r_write@plt>
test   %rax,%rax
mov    $0xffffffff,%eax
cmovns %r15d,%eax
jmp    117f <readwrite+0x5f>
mov    $0xffffffff,%eax
jmp    117f <readwrite+0x5f>
xor    %eax,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PIPE_BUF = global i32 0
define i32 @readwrite(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%10 = load i32, i32* @PIPE_BUF
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %6
%13 = alloca i8, i64 %11
store i64 %11, i64* %7
%14 = load i32, i32* %4
%15 = load i32, i32* @PIPE_BUF
%16 = call i32 @r_read(i32 %14, i8* %13, i32 %15)
store i32 %16, i32* %8
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %2
store i32 -1, i32* %3
store i32 1, i32* %9
br label %31
19: ; preds = %2
%20 = load i32, i32* %8
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %19
store i32 0, i32* %3
store i32 1, i32* %9
br label %31
23: ; preds = %19
%24 = load i32, i32* %5
%25 = load i32, i32* %8
%26 = call i64 @r_write(i32 %24, i8* %13, i32 %25)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %23
store i32 -1, i32* %3
store i32 1, i32* %9
br label %31
29: ; preds = %23
%30 = load i32, i32* %8
store i32 %30, i32* %3
store i32 1, i32* %9
br label %31
31: ; preds = %29, %28, %22, %18
%32 = load i8*, i8** %6
%33 = load i32, i32* %3
ret i32 %33
}

LLVM IR (-O2):
@PIPE_BUF = global i32 0
define i32 @readwrite(i32 %0, i32 %1) {
%3 = load i32, i32* @PIPE_BUF
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = call i32 @r_read(i32 %0, i8* %5, i32 %3)
%7 = icmp slt i32 %6, 0
br i1 %7, label %14, label %8
8: ; preds = %2
%9 = icmp eq i32 %6, 0
br i1 %9, label %14, label %10
10: ; preds = %8
%11 = call i64 @r_write(i32 %1, i8* %5, i32 %6)
%12 = icmp slt i64 %11, 0
%13 = select i1 %12, i32 -1, i32 %6
br label %14
14: ; preds = %10, %8, %2
%15 = phi i32 [ -1, %2 ], [ 0, %8 ], [ %13, %10 ]
ret i32 %15
}