#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;

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ read(int, char *, size_t);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
scalar_t__ write(int, char *, scalar_t__);
size_t sendfile_from_socket(int out_fd, int in_fd, size_t count) {
  char buffer[4096];
  while (count) {
    size_t todo;
    ssize_t bytes, written;
    todo = count > 4096 ? 4096 : count;
    bytes = read(in_fd, buffer, todo);
    if (bytes < 0) {
      fprintf(stderr, "read() failed: %s\n", strerror(errno));
      exit(1);
    }
    written = write(out_fd, buffer, bytes);
    if (written < bytes) {
      fprintf(stderr, "write() failed: %s\n", strerror(errno));
      exit(1);
    }
    if (bytes == 0)
      break;
    count -= bytes;
  }
  return count;
}


x86 assembly (clang, -O0):
<sendfile_from_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     12a6 <sendfile_from_socket+0x156>
cmpq   $0x1000,-0x10(%rbp)
jbe    118f <sendfile_from_socket+0x3f>
mov    $0x1000,%eax
mov    %rax,-0x1030(%rbp)
jmp    119a <sendfile_from_socket+0x4a>
mov    -0x10(%rbp),%rax
mov    %rax,-0x1030(%rbp)
mov    -0x1030(%rbp),%rax
mov    %rax,-0x1018(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    -0x1018(%rbp),%rdx
call   1040 <read@plt>
mov    %rax,-0x1020(%rbp)
cmpq   $0x0,-0x1020(%rbp)
jge    120f <sendfile_from_socket+0xbf>
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1034(%rbp)
mov    0x2def(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x1034(%rbp),%edi
mov    %rax,%rdx
lea    0xe00(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x4(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    -0x1020(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x1028(%rbp)
mov    -0x1028(%rbp),%rax
cmp    -0x1020(%rbp),%rax
jge    127c <sendfile_from_socket+0x12c>
mov    0x2d99(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1038(%rbp)
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x1038(%rbp),%edi
mov    %rax,%rdx
lea    0xda6(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
cmpq   $0x0,-0x1020(%rbp)
jne    128f <sendfile_from_socket+0x13f>
jmp    12a6 <sendfile_from_socket+0x156>
mov    -0x1020(%rbp),%rcx
mov    -0x10(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1165 <sendfile_from_socket+0x15>
mov    -0x10(%rbp),%rax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sendfile_from_socket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1000,%rsp
mov    %rdx,%rbx
mov    %esi,%r14d
mov    %edi,%r15d
mov    %rsp,%r12
nopl   0x0(%rax,%rax,1)
test   %rbx,%rbx
je     11b5 <sendfile_from_socket+0x65>
cmp    $0x1000,%rbx
mov    $0x1000,%edx
cmovb  %rbx,%rdx
mov    %r14d,%edi
mov    %r12,%rsi
call   1040 <read@plt>
test   %rax,%rax
js     11ca <sendfile_from_socket+0x7a>
mov    %rax,%rbp
mov    %r15d,%edi
mov    %r12,%rsi
mov    %rax,%rdx
call   1030 <write@plt>
cmp    %rbp,%rax
jl     11ea <sendfile_from_socket+0x9a>
sub    %rbp,%rbx
test   %rbp,%rbp
jne    1170 <sendfile_from_socket+0x20>
jmp    11b7 <sendfile_from_socket+0x67>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x1000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e0f(%rip),%rax
mov    (%rax),%ebx
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe18(%rip),%rsi
jmp    1208 <sendfile_from_socket+0xb8>
mov    0x2def(%rip),%rax
mov    (%rax),%ebx
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe0b(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [19 x i8] c"read() failed: %s\0A\00"
@errno = global i32 0
@.str.1 = [20 x i8] c"write() failed: %s\0A\00"
define i64 @sendfile_from_socket(i32 %0, i32 %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca [4096 x i8]
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
store i32 %0, i32* %4
store i32 %1, i32* %5
store i64 %2, i64* %6
br label %11
11: ; preds = %52, %3
%12 = load i64, i64* %6
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %56
14: ; preds = %11
%15 = load i64, i64* %6
%16 = icmp ugt i64 %15, 4096
br i1 %16, label %17, label %18
17: ; preds = %14
br label %20
18: ; preds = %14
%19 = load i64, i64* %6
br label %20
20: ; preds = %18, %17
%21 = phi i64 [ 4096, %17 ], [ %19, %18 ]
store i64 %21, i64* %8
%22 = load i32, i32* %5
%23 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%24 = load i64, i64* %8
%25 = call i64 @read(i32 %22, i8* %23, i64 %24)
store i64 %25, i64* %9
%26 = load i64, i64* %9
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %20
%29 = load i32, i32* @stderr
%30 = load i32, i32* @errno
%31 = call i8* @strerror(i32 %30)
%32 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %31)
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %20
%35 = load i32, i32* %4
%36 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%37 = load i64, i64* %9
%38 = call i64 @write(i32 %35, i8* %36, i64 %37)
store i64 %38, i64* %10
%39 = load i64, i64* %10
%40 = load i64, i64* %9
%41 = icmp slt i64 %39, %40
br i1 %41, label %42, label %48
42: ; preds = %34
%43 = load i32, i32* @stderr
%44 = load i32, i32* @errno
%45 = call i8* @strerror(i32 %44)
%46 = call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %45)
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %34
%49 = load i64, i64* %9
%50 = icmp eq i64 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %48
br label %56
52: ; preds = %48
%53 = load i64, i64* %9
%54 = load i64, i64* %6
%55 = sub i64 %54, %53
store i64 %55, i64* %6
br label %11
56: ; preds = %51, %11
%57 = load i64, i64* %6
ret i64 %57
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [19 x i8] c"read() failed: %s\0A\00"
@errno = global i32 0
@.str.1 = [20 x i8] c"write() failed: %s\0A\00"
define i64 @sendfile_from_socket(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
br label %6
6: ; preds = %29, %3
%7 = phi i64 [ %2, %3 ], [ %31, %29 ]
%8 = icmp eq i64 %7, 0
br i1 %8, label %32, label %9
9: ; preds = %6
%10 = icmp ult i64 %7, 4096
%11 = select i1 %10, i64 %7, i64 4096
%12 = call i64 @read(i32 %1, i8* %5, i64 %11)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %9
%15 = load i32, i32* @stderr
%16 = load i32, i32* @errno
%17 = tail call i8* @strerror(i32 %16)
%18 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %9
%21 = call i64 @write(i32 %0, i8* %5, i64 %12)
%22 = icmp slt i64 %21, %12
br i1 %22, label %23, label %29
23: ; preds = %20
%24 = load i32, i32* @stderr
%25 = load i32, i32* @errno
%26 = tail call i8* @strerror(i32 %25)
%27 = tail call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %26)
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %20
%30 = icmp eq i64 %12, 0
%31 = sub i64 %7, %12
br i1 %30, label %32, label %6
32: ; preds = %29, %6
%33 = phi i64 [ %31, %29 ], [ 0, %6 ]
ret i64 %33
}