#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 scalar_t__ off_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int write(int, char *, int);
void relay_copy(int fdin, int fdout) {
  char buf[(1 << 16)];
  off_t len;
  size_t off;
  ssize_t nr, nw;
  printf("copy...\n");
  len = 0;
  while (1) {
    nr = read(fdin, buf, sizeof(buf));
    if (nr == -1)
      err(1, "read");
    if (nr == 0)
      break;
    len += nr;
    off = 0;
    do {
      nw = write(fdout, buf + off, nr);
      if (nw == -1)
        err(1, "write");
      off += nw;
      nr -= nw;
    } while (nr);
  }
  printf("len %lld\n", len);
}


x86 assembly (clang, -O0):
<relay_copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10030,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
lea    0xea8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x10018(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x10010(%rbp),%rsi
mov    $0x10000,%edx
call   1050 <read@plt>
mov    %eax,-0x10024(%rbp)
cmpl   $0xffffffff,-0x10024(%rbp)
jne    11a2 <relay_copy+0x62>
mov    $0x1,%edi
lea    0xe6c(%rip),%rsi
call   1060 <err@plt>
cmpl   $0x0,-0x10024(%rbp)
jne    11b4 <relay_copy+0x74>
jmp    124f <relay_copy+0x10f>
movslq -0x10024(%rbp),%rax
add    -0x10018(%rbp),%rax
mov    %rax,-0x10018(%rbp)
movq   $0x0,-0x10020(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10010(%rbp),%rsi
add    -0x10020(%rbp),%rsi
mov    -0x10024(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x10028(%rbp)
cmpl   $0xffffffff,-0x10028(%rbp)
jne    1214 <relay_copy+0xd4>
mov    $0x1,%edi
lea    0xdff(%rip),%rsi
call   1060 <err@plt>
movslq -0x10028(%rbp),%rax
add    -0x10020(%rbp),%rax
mov    %rax,-0x10020(%rbp)
mov    -0x10028(%rbp),%ecx
mov    -0x10024(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x10024(%rbp)
cmpl   $0x0,-0x10024(%rbp)
jne    11d4 <relay_copy+0x94>
jmp    116a <relay_copy+0x2a>
mov    -0x10018(%rbp),%rsi
lea    0xdb7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x10030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<relay_copy>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10018,%rsp
mov    %esi,%ebx
mov    %edi,0x4(%rsp)
lea    0xea7(%rip),%rdi
call   1030 <puts@plt>
xor    %r14d,%r14d
lea    0xe88(%rip),%rbp
jmp    118b <relay_copy+0x3b>
nop
mov    0x8(%rsp),%rax
add    %r14,%rax
mov    %rax,%r14
mov    0x4(%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0x10000,%edx
call   1060 <read@plt>
mov    %eax,%r12d
cmp    $0xffffffff,%eax
je     11b0 <relay_copy+0x60>
test   %r12d,%r12d
jne    11c1 <relay_copy+0x71>
jmp    1204 <relay_copy+0xb4>
nopl   (%rax)
mov    $0x1,%edi
lea    0xe44(%rip),%rsi
call   1070 <err@plt>
mov    %r14,0x8(%rsp)
movslq %r12d,%r14
xor    %r13d,%r13d
jmp    11db <relay_copy+0x8b>
xchg   %ax,%ax
movslq %r15d,%rax
add    %rax,%r13
sub    %r15d,%r12d
je     1180 <relay_copy+0x30>
lea    (%rsp,%r13,1),%rsi
add    $0x10,%rsi
mov    %ebx,%edi
mov    %r12d,%edx
call   1040 <write@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
jne    11d0 <relay_copy+0x80>
mov    $0x1,%edi
mov    %rbp,%rsi
call   1070 <err@plt>
jmp    11d0 <relay_copy+0x80>
lea    0xe00(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
add    $0x10018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <printf@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"copy...\0A\00"
@.str.1 = [5 x i8] c"read\00"
@.str.2 = [6 x i8] c"write\00"
@.str.3 = [10 x i8] c"len %lld\0A\00"
define void @relay_copy(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [65536 x i8]
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i64 0, i64* %6
br label %11
11: ; preds = %2, %50
%12 = load i32, i32* %3
%13 = getelementptr inbounds [65536 x i8], [65536 x i8]* %5, i64 0, i64 0
%14 = call i32 @read(i32 %12, i8* %13, i32 65536)
store i32 %14, i32* %8
%15 = load i32, i32* %8
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %11
%20 = load i32, i32* %8
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %19
br label %51
23: ; preds = %19
%24 = load i32, i32* %8
%25 = sext i32 %24 to i64
%26 = load i64, i64* %6
%27 = add nsw i64 %26, %25
store i64 %27, i64* %6
store i64 0, i64* %7
br label %28
28: ; preds = %47, %23
%29 = load i32, i32* %4
%30 = getelementptr inbounds [65536 x i8], [65536 x i8]* %5, i64 0, i64 0
%31 = load i64, i64* %7
%32 = getelementptr inbounds i8, i8* %30, i64 %31
%33 = load i32, i32* %8
%34 = call i32 @write(i32 %29, i8* %32, i32 %33)
store i32 %34, i32* %9
%35 = load i32, i32* %9
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %39
37: ; preds = %28
%38 = call i32 @err(i32 1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %39
39: ; preds = %37, %28
%40 = load i32, i32* %9
%41 = sext i32 %40 to i64
%42 = load i64, i64* %7
%43 = add i64 %42, %41
store i64 %43, i64* %7
%44 = load i32, i32* %9
%45 = load i32, i32* %8
%46 = sub nsw i32 %45, %44
store i32 %46, i32* %8
br label %47
47: ; preds = %39
%48 = load i32, i32* %8
%49 = icmp ne i32 %48, 0
br i1 %49, label %28, label %50
50: ; preds = %47
br label %11
51: ; preds = %22
%52 = load i64, i64* %6
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i64 %52)
ret void
}

LLVM IR (-O2):
@.str.1 = [5 x i8] c"read\00"
@.str.2 = [6 x i8] c"write\00"
@.str.3 = [10 x i8] c"len %lld\0A\00"
@str = [8 x i8] c"copy...\00"
define void @relay_copy(i32 %0, i32 %1) {
%3 = alloca [65536 x i8]
%4 = getelementptr inbounds [65536 x i8], [65536 x i8]* %3, i64 0, i64 0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0))
br label %8
6: ; preds = %23
%7 = add nsw i64 %9, %14
br label %8
8: ; preds = %6, %2
%9 = phi i64 [ 0, %2 ], [ %7, %6 ]
%10 = call i32 @read(i32 %0, i8* %4, i32 65536)
switch i32 %10, label %13 [
i32 -1, label %11
i32 0, label %28
]
11: ; preds = %8
%12 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %11, %8
%14 = sext i32 %10 to i64
br label %15
15: ; preds = %23, %13
%16 = phi i64 [ 0, %13 ], [ %25, %23 ]
%17 = phi i32 [ %10, %13 ], [ %26, %23 ]
%18 = getelementptr inbounds [65536 x i8], [65536 x i8]* %3, i64 0, i64 %16
%19 = call i32 @write(i32 %1, i8* %18, i32 %17)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %15
%22 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %23
23: ; preds = %21, %15
%24 = sext i32 %19 to i64
%25 = add i64 %16, %24
%26 = sub nsw i32 %17, %19
%27 = icmp eq i32 %26, 0
br i1 %27, label %6, label %15
28: ; preds = %8
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i64 %9)
ret void
}