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

/* Variables and functions */
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int write(int, char *, int);
int pts_copy(int fd_from, int fd_to) {
  char buff[1024];
  int len;
  if ((len = read(fd_from, buff, sizeof(buff))) < 0) {
    perror("read()");
    return -1;
  }
  if (write(fd_to, buff, len) < len) {
    perror("write()");
    return -1;
  }
  return len;
}


x86 assembly (clang, -O0):
<pts_copy>:
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    $0x0,%eax
jge    117c <pts_copy+0x4c>
lea    0xe95(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11be <pts_copy+0x8e>
mov    -0xc(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x414(%rbp),%edx
call   1030 <write@plt>
cmp    -0x414(%rbp),%eax
jge    11b5 <pts_copy+0x85>
lea    0xe63(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11be <pts_copy+0x8e>
mov    -0x414(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pts_copy>:
push   %rbp
push   %rbx
sub    $0x408,%rsp
mov    %esi,%ebp
mov    %rsp,%rsi
mov    $0x400,%edx
call   1040 <read@plt>
test   %eax,%eax
js     116a <pts_copy+0x3a>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %ebp,%edi
mov    %eax,%edx
call   1030 <write@plt>
cmp    %ebx,%eax
jl     1173 <pts_copy+0x43>
mov    %ebx,%eax
add    $0x408,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe8f(%rip),%rdi
jmp    117a <pts_copy+0x4a>
lea    0xe8d(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%ebx
jmp    115e <pts_copy+0x2e>

LLVM IR (-O0):
@.str = [7 x i8] c"read()\00"
@.str.1 = [8 x i8] c"write()\00"
define i32 @pts_copy(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [1024 x i8]
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = load i32, i32* %4
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%10 = call i32 @read(i32 %8, i8* %9, i32 1024)
store i32 %10, i32* %7
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %25
14: ; preds = %2
%15 = load i32, i32* %5
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%17 = load i32, i32* %7
%18 = call i32 @write(i32 %15, i8* %16, i32 %17)
%19 = load i32, i32* %7
%20 = icmp slt i32 %18, %19
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %25
23: ; preds = %14
%24 = load i32, i32* %7
store i32 %24, i32* %3
br label %25
25: ; preds = %23, %21, %12
%26 = load i32, i32* %3
ret i32 %26
}

LLVM IR (-O2):
@.str = [7 x i8] c"read()\00"
@.str.1 = [8 x i8] c"write()\00"
define i32 @pts_copy(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 = icmp slt i32 %5, 0
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = call i32 @write(i32 %1, i8* %4, i32 %5)
%9 = icmp slt i32 %8, %5
br i1 %9, label %10, label %13
10: ; preds = %7, %2
%11 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), %2 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), %7 ]
%12 = tail call i32 @perror(i8* %11)
br label %13
13: ; preds = %10, %7
%14 = phi i32 [ %5, %7 ], [ -1, %10 ]
ret i32 %14
}