ffcopy(from, to) {
  char buf[512], *p;
  register n, k;
  for (;;) {
    n = read(from, buf, sizeof(buf));
    if (n < 0) {
      error("Cannot read file");
      return (-1);
    }
    if (n == 0)
      return (0);
    p = buf;
    while (n > 0) {
      k = write(to, p, (unsigned)n);
      if (k <= 0) {
        error("Cannot write file");
        return (-1);
      }
      n -= k;
      p += k;
    }
  }
}


x86 assembly (clang, -O0):
<ffcopy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    $0x200,%edx
mov    $0x0,%al
call   1040 <read@plt>
mov    %eax,-0x21c(%rbp)
cmpl   $0x0,-0x21c(%rbp)
jge    1184 <ffcopy+0x54>
lea    0xe8f(%rip),%rdi
mov    $0x0,%al
call   1050 <error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1234 <ffcopy+0x104>
cmpl   $0x0,-0x21c(%rbp)
jne    119d <ffcopy+0x6d>
movl   $0x0,-0x4(%rbp)
jmp    1234 <ffcopy+0x104>
lea    -0x210(%rbp),%rax
mov    %rax,-0x218(%rbp)
cmpl   $0x0,-0x21c(%rbp)
jle    122f <ffcopy+0xff>
mov    -0xc(%rbp),%edi
mov    -0x218(%rbp),%rsi
mov    -0x21c(%rbp),%edx
mov    $0x0,%al
call   1030 <write@plt>
mov    %eax,-0x220(%rbp)
cmpl   $0x0,-0x220(%rbp)
jg     11fc <ffcopy+0xcc>
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1050 <error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1234 <ffcopy+0x104>
mov    -0x220(%rbp),%ecx
mov    -0x21c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x21c(%rbp)
mov    -0x220(%rbp),%ecx
mov    -0x218(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x218(%rbp)
jmp    11ab <ffcopy+0x7b>
jmp    1141 <ffcopy+0x11>
mov    -0x4(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ffcopy>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    %esi,%ebx
mov    %edi,%r12d
mov    %rsp,%r15
mov    $0x200,%edx
mov    %r15,%rsi
xor    %eax,%eax
call   1040 <read@plt>
lea    0xea1(%rip),%rdi
test   %eax,%eax
js     11b6 <ffcopy+0x86>
mov    %eax,%ebp
xor    %r13d,%r13d
test   %ebp,%ebp
je     11c3 <ffcopy+0x93>
mov    %r15,%r14
nop
mov    %ebx,%edi
mov    %r15,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1030 <write@plt>
test   %eax,%eax
jle    11af <ffcopy+0x7f>
mov    %eax,%ecx
add    %rcx,%r15
sub    %eax,%ebp
jg     1170 <ffcopy+0x40>
mov    $0x200,%edx
mov    %r12d,%edi
mov    %r14,%r15
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1168 <ffcopy+0x38>
lea    0xe53(%rip),%rdi
jmp    11b6 <ffcopy+0x86>
lea    0xe5b(%rip),%rdi
xor    %eax,%eax
call   1050 <error@plt>
mov    $0xffffffff,%r13d
mov    %r13d,%eax
add    $0x208,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"Cannot read file\00"
@.str.1 = [18 x i8] c"Cannot write file\00"
define i32 @ffcopy(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [512 x i8]
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
br label %10
10: ; preds = %44, %2
%11 = load i32, i32* %4
%12 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%13 = call i32 (i32, i8*, i64, ...) bitcast (i32 (...)* @read to i32 (i32, i8*, i64, ...)*)(i32 %11, i8* %12, i64 512)
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %10
%17 = call i32 (i8*, ...) bitcast (i32 (...)* @error to i32 (i8*, ...)*)(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %45
18: ; preds = %10
%19 = load i32, i32* %8
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %18
store i32 0, i32* %3
br label %45
22: ; preds = %18
%23 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
store i8* %23, i8** %7
br label %24
24: ; preds = %36, %22
%25 = load i32, i32* %8
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %44
27: ; preds = %24
%28 = load i32, i32* %5
%29 = load i8*, i8** %7
%30 = load i32, i32* %8
%31 = call i32 (i32, i8*, i32, ...) bitcast (i32 (...)* @write to i32 (i32, i8*, i32, ...)*)(i32 %28, i8* %29, i32 %30)
store i32 %31, i32* %9
%32 = load i32, i32* %9
%33 = icmp sle i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %27
%35 = call i32 (i8*, ...) bitcast (i32 (...)* @error to i32 (i8*, ...)*)(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %45
36: ; preds = %27
%37 = load i32, i32* %9
%38 = load i32, i32* %8
%39 = sub nsw i32 %38, %37
store i32 %39, i32* %8
%40 = load i32, i32* %9
%41 = load i8*, i8** %7
%42 = sext i32 %40 to i64
%43 = getelementptr inbounds i8, i8* %41, i64 %42
store i8* %43, i8** %7
br label %24
44: ; preds = %24
br label %10
45: ; preds = %34, %21, %16
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
@.str = [17 x i8] c"Cannot read file\00"
@.str.1 = [18 x i8] c"Cannot write file\00"
define i32 @ffcopy(i32 %0, i32 %1) {
%3 = alloca [512 x i8]
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%5 = call i32 (i32, i8*, i64, ...) bitcast (i32 (...)* @read to i32 (i32, i8*, i64, ...)*)(i32 %0, i8* %4, i64 512)
%6 = icmp slt i32 %5, 0
br i1 %6, label %23, label %10
7: ; preds = %18
%8 = call i32 (i32, i8*, i64, ...) bitcast (i32 (...)* @read to i32 (i32, i8*, i64, ...)*)(i32 %0, i8* %4, i64 512)
%9 = icmp slt i32 %8, 0
br i1 %9, label %23, label %10
10: ; preds = %2, %7
%11 = phi i32 [ %8, %7 ], [ %5, %2 ]
%12 = icmp eq i32 %11, 0
br i1 %12, label %26, label %13
13: ; preds = %10, %18
%14 = phi i32 [ %19, %18 ], [ %11, %10 ]
%15 = phi i8* [ %21, %18 ], [ %4, %10 ]
%16 = call i32 (i32, i8*, i32, ...) bitcast (i32 (...)* @write to i32 (i32, i8*, i32, ...)*)(i32 %1, i8* %15, i32 %14)
%17 = icmp slt i32 %16, 1
br i1 %17, label %23, label %18
18: ; preds = %13
%19 = sub nsw i32 %14, %16
%20 = zext i32 %16 to i64
%21 = getelementptr inbounds i8, i8* %15, i64 %20
%22 = icmp sgt i32 %19, 0
br i1 %22, label %13, label %7
23: ; preds = %7, %13, %2
%24 = phi i8* [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), %2 ], [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), %13 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), %7 ]
%25 = call i32 (i8*, ...) bitcast (i32 (...)* @error to i32 (i8*, ...)*)(i8* %24)
br label %26
26: ; preds = %10, %23
%27 = phi i32 [ -1, %23 ], [ 0, %10 ]
ret i32 %27
}