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

/* Variables and functions */
int read(int, char *, int);
int write(int, char *, int);
int read_write(int fd_from, int fd_to) {
  char buf[1024];
  int len;
  if ((len = read(fd_from, buf, sizeof(buf))) == -1 || len == 0) {
    return -1;
  }
  int p = 0;
  while (p < len) {
    int r;
    if ((r = write(fd_to, &buf[p], len - p)) == -1) {
      return -1;
    }
    p += r;
  }
  return len;
}


x86 assembly (clang, -O0):
<read_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %eax,-0x414(%rbp)
cmp    $0xffffffff,%eax
je     1161 <read_write+0x41>
cmpl   $0x0,-0x414(%rbp)
jne    116d <read_write+0x4d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e9 <read_write+0xc9>
movl   $0x0,-0x418(%rbp)
mov    -0x418(%rbp),%eax
cmp    -0x414(%rbp),%eax
jge    11e0 <read_write+0xc0>
mov    -0xc(%rbp),%edi
movslq -0x418(%rbp),%rax
lea    -0x410(%rbp),%rsi
add    %rax,%rsi
mov    -0x414(%rbp),%edx
sub    -0x418(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x41c(%rbp)
cmp    $0xffffffff,%eax
jne    11c9 <read_write+0xa9>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e9 <read_write+0xc9>
mov    -0x41c(%rbp),%eax
add    -0x418(%rbp),%eax
mov    %eax,-0x418(%rbp)
jmp    1177 <read_write+0x57>
mov    -0x414(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %esi,%r15d
mov    %rsp,%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %eax,%ebp
lea    0x1(%rbp),%eax
mov    $0xffffffff,%r14d
cmp    $0x2,%eax
jb     1171 <read_write+0x51>
xor    %ebx,%ebx
nop
mov    %ebp,%edx
sub    %ebx,%edx
jle    116e <read_write+0x4e>
movslq %ebx,%rax
lea    (%rsp,%rax,1),%rsi
mov    %r15d,%edi
call   1030 <write@plt>
add    %eax,%ebx
cmp    $0xffffffff,%eax
jne    1150 <read_write+0x30>
jmp    1171 <read_write+0x51>
mov    %ebp,%r14d
mov    %r14d,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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