#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;
typedef int /*<<< orphan*/ data_buff;

/* Variables and functions */
scalar_t__ EIO;
scalar_t__ errno;
int read(int, char *, int);
int write(int, char *, int);
ssize_t feed(int read_from_fd, int write_to_fd) {
  static char data_buff[2048];
  ssize_t n, written;
  if ((n = read(read_from_fd, data_buff, sizeof(data_buff))) < 0) {
    return errno == EIO ? 0 : -1;
    return -1;
  }
  if (n == 0)
    return 0;
  if ((written = write(write_to_fd, data_buff, n)) != n) {
    if (written >= 0)
      errno = EIO;
    return -1;
  }
  return n;
}


x86 assembly (clang, -O0):
<feed>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
lea    0x2f08(%rip),%rsi
mov    $0x800,%edx
call   1040 <read@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    1177 <feed+0x57>
mov    0x2e83(%rip),%rax
mov    (%rax),%rdx
mov    0x2e81(%rip),%rax
mov    (%rax),%rsi
mov    $0xffffffff,%eax
xor    %ecx,%ecx
cmp    %rsi,%rdx
cmove  %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    11db <feed+0xbb>
cmpl   $0x0,-0x10(%rbp)
jne    118d <feed+0x6d>
movl   $0x0,-0x4(%rbp)
jmp    11db <feed+0xbb>
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%edx
lea    0x2ea6(%rip),%rsi
call   1030 <write@plt>
mov    %eax,-0x14(%rbp)
cmp    -0x10(%rbp),%eax
je     11d5 <feed+0xb5>
cmpl   $0x0,-0x14(%rbp)
jl     11c9 <feed+0xa9>
mov    0x2e24(%rip),%rax
mov    (%rax),%rcx
mov    0x2e12(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11db <feed+0xbb>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<feed>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
lea    0x2f14(%rip),%rsi
mov    $0x800,%edx
call   1040 <read@plt>
test   %eax,%eax
js     1171 <feed+0x51>
je     118e <feed+0x6e>
mov    %eax,%ebx
lea    0x2efb(%rip),%rsi
mov    %ebp,%edi
mov    %eax,%edx
call   1030 <write@plt>
cmp    %ebx,%eax
je     1190 <feed+0x70>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     1190 <feed+0x70>
mov    0x2e7e(%rip),%rax
mov    (%rax),%rax
mov    0x2e6c(%rip),%rcx
mov    %rax,(%rcx)
jmp    1190 <feed+0x70>
mov    0x2e60(%rip),%rax
mov    (%rax),%rax
mov    0x2e5e(%rip),%rcx
xor    %ebx,%ebx
cmp    (%rcx),%rax
setne  %bl
neg    %ebx
jmp    1190 <feed+0x70>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@feed.data_buff = internal global [2048 x i8] zeroinitializer
@errno = global i64 0
@EIO = global i64 0
define i32 @feed(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = load i32, i32* %4
%9 = call i32 @read(i32 %8, i8* getelementptr inbounds ([2048 x i8], [2048 x i8]* @feed.data_buff, i64 0, i64 0), i32 2048)
store i32 %9, i32* %6
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %17
11: ; preds = %2
%12 = load i64, i64* @errno
%13 = load i64, i64* @EIO
%14 = icmp eq i64 %12, %13
%15 = zext i1 %14 to i64
%16 = select i1 %14, i32 0, i32 -1
store i32 %16, i32* %3
br label %35
17: ; preds = %2
%18 = load i32, i32* %6
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %17
store i32 0, i32* %3
br label %35
21: ; preds = %17
%22 = load i32, i32* %5
%23 = load i32, i32* %6
%24 = call i32 @write(i32 %22, i8* getelementptr inbounds ([2048 x i8], [2048 x i8]* @feed.data_buff, i64 0, i64 0), i32 %23)
store i32 %24, i32* %7
%25 = load i32, i32* %6
%26 = icmp ne i32 %24, %25
br i1 %26, label %27, label %33
27: ; preds = %21
%28 = load i32, i32* %7
%29 = icmp sge i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = load i64, i64* @EIO
store i64 %31, i64* @errno
br label %32
32: ; preds = %30, %27
store i32 -1, i32* %3
br label %35
33: ; preds = %21
%34 = load i32, i32* %6
store i32 %34, i32* %3
br label %35
35: ; preds = %33, %32, %20, %11
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
@feed.data_buff = internal global [2048 x i8] zeroinitializer
@errno = global i64 0
@EIO = global i64 0
define i32 @feed(i32 %0, i32 %1) {
%3 = tail call i32 @read(i32 %0, i8* getelementptr inbounds ([2048 x i8], [2048 x i8]* @feed.data_buff, i64 0, i64 0), i32 2048)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %10
5: ; preds = %2
%6 = load i64, i64* @errno
%7 = load i64, i64* @EIO
%8 = icmp ne i64 %6, %7
%9 = sext i1 %8 to i32
br label %19
10: ; preds = %2
%11 = icmp eq i32 %3, 0
br i1 %11, label %19, label %12
12: ; preds = %10
%13 = tail call i32 @write(i32 %1, i8* getelementptr inbounds ([2048 x i8], [2048 x i8]* @feed.data_buff, i64 0, i64 0), i32 %3)
%14 = icmp eq i32 %13, %3
br i1 %14, label %19, label %15
15: ; preds = %12
%16 = icmp sgt i32 %13, -1
br i1 %16, label %17, label %19
17: ; preds = %15
%18 = load i64, i64* @EIO
store i64 %18, i64* @errno
br label %19
19: ; preds = %12, %15, %17, %10, %5
%20 = phi i32 [ %9, %5 ], [ 0, %10 ], [ -1, %17 ], [ -1, %15 ], [ %3, %12 ]
ret i32 %20
}