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

/* Variables and functions */
scalar_t__ check(int /*<<< orphan*/, char *);
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ read(int, char *, int);
scalar_t__ write(int, char *, scalar_t__);
void copy_fd_len(int in, int out, size_t len) {
  ssize_t read_cnt;
  char buf[4096];
  while (
      (read_cnt = check(read(in, buf, (sizeof(buf) < len ? sizeof(buf) : len)),
                        "read(copy_fd_len)")) > 0) {
    len -= read_cnt;
    if (write(out, buf, read_cnt) != read_cnt) {
      err(1, "write(copy_fd_len)");
    }
  }
}


x86 assembly (clang, -O0):
<copy_fd_len>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x102c(%rbp)
lea    -0x1020(%rbp),%rax
mov    %rax,-0x1028(%rbp)
mov    $0x1000,%eax
cmp    -0x10(%rbp),%rax
jae    118c <copy_fd_len+0x4c>
mov    $0x1000,%eax
mov    %rax,-0x1038(%rbp)
jmp    1197 <copy_fd_len+0x57>
mov    -0x10(%rbp),%rax
mov    %rax,-0x1038(%rbp)
mov    -0x1028(%rbp),%rsi
mov    -0x102c(%rbp),%edi
mov    -0x1038(%rbp),%rax
mov    %eax,%edx
call   1040 <read@plt>
mov    %eax,%edi
lea    0xe45(%rip),%rsi
call   1060 <check@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jle    1210 <copy_fd_len+0xd0>
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
cmp    -0x18(%rbp),%rax
je     120b <copy_fd_len+0xcb>
mov    $0x1,%edi
lea    0xe0c(%rip),%rsi
call   1050 <err@plt>
jmp    1155 <copy_fd_len+0x15>
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_fd_len>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1008,%rsp
mov    %rdx,%rbx
mov    %esi,%r14d
mov    %edi,%r15d
cmp    $0x1000,%rdx
mov    $0x1000,%edx
cmovb  %ebx,%edx
mov    %rsp,%rsi
call   1040 <read@plt>
lea    0xe88(%rip),%rsi
mov    %eax,%edi
call   1060 <check@plt>
test   %rax,%rax
jle    11f5 <copy_fd_len+0xb5>
mov    %rax,%r13
mov    %rsp,%rbp
lea    0xe6f(%rip),%r12
jmp    11cc <copy_fd_len+0x8c>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmp    $0x1000,%rbx
mov    $0x1000,%edx
cmovb  %ebx,%edx
mov    %r15d,%edi
mov    %rbp,%rsi
call   1040 <read@plt>
mov    %eax,%edi
mov    %r12,%rsi
call   1060 <check@plt>
mov    %rax,%r13
test   %rax,%rax
jle    11f5 <copy_fd_len+0xb5>
sub    %r13,%rbx
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %r13,%rdx
call   1030 <write@plt>
cmp    %r13,%rax
je     11a0 <copy_fd_len+0x60>
mov    $0x1,%edi
lea    0xe24(%rip),%rsi
call   1050 <err@plt>
jmp    11a0 <copy_fd_len+0x60>
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [18 x i8] c"read(copy_fd_len)\00"
@.str.1 = [19 x i8] c"write(copy_fd_len)\00"
define void @copy_fd_len(i32 %0, i32 %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
%8 = alloca [4096 x i8]
store i32 %0, i32* %4
store i32 %1, i32* %5
store i64 %2, i64* %6
br label %9
9: ; preds = %35, %3
%10 = load i32, i32* %4
%11 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%12 = load i64, i64* %6
%13 = icmp ult i64 4096, %12
br i1 %13, label %14, label %15
14: ; preds = %9
br label %17
15: ; preds = %9
%16 = load i64, i64* %6
br label %17
17: ; preds = %15, %14
%18 = phi i64 [ 4096, %14 ], [ %16, %15 ]
%19 = trunc i64 %18 to i32
%20 = call i32 @read(i32 %10, i8* %11, i32 %19)
%21 = call i64 @check(i32 %20, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
store i64 %21, i64* %7
%22 = icmp sgt i64 %21, 0
br i1 %22, label %23, label %36
23: ; preds = %17
%24 = load i64, i64* %7
%25 = load i64, i64* %6
%26 = sub i64 %25, %24
store i64 %26, i64* %6
%27 = load i32, i32* %5
%28 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%29 = load i64, i64* %7
%30 = call i64 @write(i32 %27, i8* %28, i64 %29)
%31 = load i64, i64* %7
%32 = icmp ne i64 %30, %31
br i1 %32, label %33, label %35
33: ; preds = %23
%34 = call i32 @err(i32 1, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %35
35: ; preds = %33, %23
br label %9
36: ; preds = %17
ret void
}

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