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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
scalar_t__ ferror(int /*<<< orphan*/ *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int send(int, char *, int, int /*<<< orphan*/);
void sendfile(FILE *fp, int sockfd) {
  int n;
  char sendline[4096] = {0};
  while ((n = fread(sendline, sizeof(char), 4096, fp)) > 0) {
    if (n != 4096 && ferror(fp)) {
      perror("Read File Error");
      exit(1);
    }
    if (send(sockfd, sendline, n, 0) == -1) {
      perror("Can't send file");
      exit(1);
    }
    memset(sendline, 0, 4096);
  }
}


x86 assembly (clang, -O0):
<sendfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1010,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x1010(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1060 <memset@plt>
lea    -0x1010(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1000,%edx
call   1040 <fread@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jle    122c <sendfile+0xcc>
cmpl   $0x1000,-0x10(%rbp)
je     11e1 <sendfile+0x81>
mov    -0x8(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     11e1 <sendfile+0x81>
lea    0xe2e(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0xc(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    -0x10(%rbp),%edx
xor    %ecx,%ecx
call   1050 <send@plt>
cmp    $0xffffffff,%eax
jne    1214 <sendfile+0xb4>
lea    0xe0b(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    -0x1010(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1060 <memset@plt>
jmp    1185 <sendfile+0x25>
add    $0x1010,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sendfile>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    %esi,%r14d
mov    %rdi,%r15
mov    %rsp,%rbx
mov    $0x1000,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
test   %eax,%eax
jle    1203 <sendfile+0xa3>
mov    %eax,%ebx
mov    %rsp,%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmp    $0x1000,%ebx
je     11c5 <sendfile+0x65>
mov    %r15,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
jne    121a <sendfile+0xba>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
cmp    $0xffffffff,%eax
je     1211 <sendfile+0xb1>
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1060 <memset@plt>
mov    %rbp,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
mov    %eax,%ebx
test   %eax,%eax
jg     11b0 <sendfile+0x50>
add    $0x1008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdf8(%rip),%rdi
jmp    1221 <sendfile+0xc1>
lea    0xddf(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"Read File Error\00"
@.str.1 = [16 x i8] c"Can't send file\00"
define void @sendfile(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [4096 x i8]
store i32* %0, i32** %3
store i32 %1, i32* %4
%7 = bitcast [4096 x i8]* %6 to i8*
br label %8
8: ; preds = %32, %2
%9 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%10 = load i32*, i32** %3
%11 = call i32 @fread(i8* %9, i32 1, i32 4096, i32* %10)
store i32 %11, i32* %5
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %35
13: ; preds = %8
%14 = load i32, i32* %5
%15 = icmp ne i32 %14, 4096
br i1 %15, label %16, label %23
16: ; preds = %13
%17 = load i32*, i32** %3
%18 = call i64 @ferror(i32* %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %16, %13
%24 = load i32, i32* %4
%25 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%26 = load i32, i32* %5
%27 = call i32 @send(i32 %24, i8* %25, i32 %26, i32 0)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %23
%30 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %23
%33 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%34 = call i32 @memset(i8* %33, i32 0, i32 4096)
br label %8
35: ; preds = %8
ret void
}

LLVM IR (-O2):
@.str = [16 x i8] c"Read File Error\00"
@.str.1 = [16 x i8] c"Can't send file\00"
define void @sendfile(i32* nocapture %0, i32 %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = call i32 @fread(i8* %4, i32 1, i32 4096, i32* %0)
%6 = icmp sgt i32 %5, 0
br i1 %6, label %7, label %26
7: ; preds = %2, %22
%8 = phi i32 [ %24, %22 ], [ %5, %2 ]
%9 = icmp eq i32 %8, 4096
br i1 %9, label %16, label %10
10: ; preds = %7
%11 = call i64 @ferror(i32* %0)
%12 = icmp eq i64 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %10
%14 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %10, %7
%17 = call i32 @send(i32 %1, i8* %4, i32 %8, i32 0)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %16
%23 = call i32 @memset(i8* %4, i32 0, i32 4096)
%24 = call i32 @fread(i8* %4, i32 1, i32 4096, i32* %0)
%25 = icmp sgt i32 %24, 0
br i1 %25, label %7, label %26
26: ; preds = %22, %2
ret void
}