#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 ssize_t;

/* Variables and functions */
int read(int, char *, int);
int write(int, char *, int);
int fallback_rw(int from_fd, int to_fd, char *buf, int buf_size) {
  ssize_t written = 0;
  ssize_t amount = read(from_fd, buf, buf_size);
  if (amount == -1) {
    return -1;
  }
  while (amount > 0) {
    written = write(to_fd, buf + written, amount);
    if (written == -1)
      return -2;
    amount -= written;
  }
  return written;
}


x86 assembly (clang, -O0):
<fallback_rw>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1164 <fallback_rw+0x44>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b8 <fallback_rw+0x98>
jmp    1169 <fallback_rw+0x49>
cmpl   $0x0,-0x24(%rbp)
jle    11b2 <fallback_rw+0x92>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
movslq -0x20(%rbp),%rax
add    %rax,%rsi
mov    -0x24(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11a2 <fallback_rw+0x82>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11b8 <fallback_rw+0x98>
mov    -0x20(%rbp),%ecx
mov    -0x24(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x24(%rbp)
jmp    1169 <fallback_rw+0x49>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fallback_rw>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%rbx
mov    %esi,%r14d
mov    %rdx,%rsi
mov    %ecx,%edx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
je     116d <fallback_rw+0x4d>
mov    %eax,%ebp
xor    %eax,%eax
test   %ebp,%ebp
jle    1179 <fallback_rw+0x59>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movslq %eax,%rsi
add    %rbx,%rsi
mov    %r14d,%edi
mov    %ebp,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     1174 <fallback_rw+0x54>
sub    %eax,%ebp
test   %ebp,%ebp
jg     1150 <fallback_rw+0x30>
jmp    1179 <fallback_rw+0x59>
mov    $0xffffffff,%eax
jmp    1179 <fallback_rw+0x59>
mov    $0xfffffffe,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @fallback_rw(i32 %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
store i32 0, i32* %10
%12 = load i32, i32* %6
%13 = load i8*, i8** %8
%14 = load i32, i32* %9
%15 = call i32 @read(i32 %12, i8* %13, i32 %14)
store i32 %15, i32* %11
%16 = load i32, i32* %11
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %19
18: ; preds = %4
store i32 -1, i32* %5
br label %40
19: ; preds = %4
br label %20
20: ; preds = %34, %19
%21 = load i32, i32* %11
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %38
23: ; preds = %20
%24 = load i32, i32* %7
%25 = load i8*, i8** %8
%26 = load i32, i32* %10
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
%29 = load i32, i32* %11
%30 = call i32 @write(i32 %24, i8* %28, i32 %29)
store i32 %30, i32* %10
%31 = load i32, i32* %10
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %34
33: ; preds = %23
store i32 -2, i32* %5
br label %40
34: ; preds = %23
%35 = load i32, i32* %10
%36 = load i32, i32* %11
%37 = sub nsw i32 %36, %35
store i32 %37, i32* %11
br label %20
38: ; preds = %20
%39 = load i32, i32* %10
store i32 %39, i32* %5
br label %40
40: ; preds = %38, %33, %18
%41 = load i32, i32* %5
ret i32 %41
}

LLVM IR (-O2):
define i32 @fallback_rw(i32 %0, i32 %1, i8* nocapture %2, i32 %3) {
%5 = tail call i32 @read(i32 %0, i8* %2, i32 %3)
%6 = icmp eq i32 %5, -1
br i1 %6, label %19, label %7
7: ; preds = %4
%8 = icmp sgt i32 %5, 0
br i1 %8, label %11, label %19
9: ; preds = %11
%10 = icmp sgt i32 %18, 0
br i1 %10, label %11, label %19
11: ; preds = %7, %9
%12 = phi i32 [ %18, %9 ], [ %5, %7 ]
%13 = phi i32 [ %16, %9 ], [ 0, %7 ]
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %2, i64 %14
%16 = tail call i32 @write(i32 %1, i8* %15, i32 %12)
%17 = icmp eq i32 %16, -1
%18 = sub nsw i32 %12, %16
br i1 %17, label %19, label %9
19: ; preds = %11, %9, %7, %4
%20 = phi i32 [ -1, %4 ], [ 0, %7 ], [ -2, %11 ], [ %16, %9 ]
ret i32 %20
}