#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 */
struct stat {
  scalar_t__ st_size;
};
typedef scalar_t__ off_t;

/* Variables and functions */
int /*<<< orphan*/ BUFSIZ;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ error_msg_and_exit(char *);
scalar_t__ fstat(int, struct stat *);
int open(char *, int /*<<< orphan*/);
scalar_t__ send(int, void *, int, int /*<<< orphan*/);
scalar_t__ sendfile(int, int, scalar_t__ *, int /*<<< orphan*/);
void send_file(int client_socket) {
  int fd;
  struct stat file_stat;
  off_t remain_data = 0ul;
  off_t sent_bytes = 0ul;
  off_t offset = 0ul;
  fd = open("file", O_RDONLY);
  if (fd < 0) {
    error_msg_and_exit("Error openfing file");
  }
  if (fstat(fd, &file_stat) < 0) {
    error_msg_and_exit("Error fstat");
  }
  if (send(client_socket, (void *)&file_stat.st_size, sizeof(file_stat.st_size),
           0) < 0) {
    error_msg_and_exit("Error on sending file size");
  }
  remain_data = file_stat.st_size;
  while (((sent_bytes = sendfile(client_socket, fd, (off_t *)&offset, BUFSIZ)) >
          0) &&
         (remain_data > 0)) {
  }
  close(fd);
}


x86 assembly (clang, -O0):
<send_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
lea    0xe7c(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11b1 <send_file+0x51>
lea    0xe54(%rip),%rdi
call   1070 <error_msg_and_exit@plt>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1080 <fstat@plt>
cmp    $0x0,%rax
jge    11d3 <send_file+0x73>
lea    0xe46(%rip),%rdi
call   1070 <error_msg_and_exit@plt>
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
xor    %ecx,%ecx
call   1030 <send@plt>
cmp    $0x0,%rax
jge    11fc <send_file+0x9c>
lea    0xe29(%rip),%rdi
call   1070 <error_msg_and_exit@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    0x2dc7(%rip),%rax
mov    (%rax),%ecx
lea    -0x28(%rbp),%rdx
call   1050 <sendfile@plt>
mov    %rax,%rcx
mov    %rcx,-0x20(%rbp)
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x29(%rbp)
jle    123d <send_file+0xdd>
cmpq   $0x0,-0x18(%rbp)
setg   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    124d <send_file+0xed>
jmp    1252 <send_file+0xf2>
jmp    1204 <send_file+0xa4>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r14d
movq   $0x0,0x8(%rsp)
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
lea    0xe87(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%r15d
test   %eax,%eax
jns    11a0 <send_file+0x40>
lea    0xe65(%rip),%rdi
call   1070 <error_msg_and_exit@plt>
mov    %rsp,%rsi
mov    %r15d,%edi
call   1080 <fstat@plt>
test   %rax,%rax
jns    11bc <send_file+0x5c>
lea    0xe5d(%rip),%rdi
call   1070 <error_msg_and_exit@plt>
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0x8,%edx
xor    %ecx,%ecx
call   1030 <send@plt>
test   %rax,%rax
jns    11df <send_file+0x7f>
lea    0xe46(%rip),%rdi
call   1070 <error_msg_and_exit@plt>
mov    (%rsp),%rbp
mov    0x2dee(%rip),%r12
lea    0x8(%rsp),%rbx
nop
mov    (%r12),%ecx
mov    %r14d,%edi
mov    %r15d,%esi
mov    %rbx,%rdx
call   1050 <sendfile@plt>
test   %rax,%rax
jle    120c <send_file+0xac>
test   %rbp,%rbp
jg     11f0 <send_file+0x90>
mov    %r15d,%edi
call   1040 <close@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@.str = [5 x i8] c"file\00"
@O_RDONLY = global i32 0
@.str.1 = [20 x i8] c"Error openfing file\00"
@.str.2 = [12 x i8] c"Error fstat\00"
@.str.3 = [27 x i8] c"Error on sending file size\00"
@BUFSIZ = global i32 0
define void @send_file(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.stat
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
store i32 %0, i32* %2
store i64 0, i64* %5
store i64 0, i64* %6
store i64 0, i64* %7
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = call i32 @error_msg_and_exit(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %1
%15 = load i32, i32* %3
%16 = call i64 @fstat(i32 %15, %struct.stat* %4)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = call i32 @error_msg_and_exit(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %18, %14
%21 = load i32, i32* %2
%22 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%23 = bitcast i64* %22 to i8*
%24 = call i64 @send(i32 %21, i8* %23, i32 8, i32 0)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %20
%27 = call i32 @error_msg_and_exit(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
br label %28
28: ; preds = %26, %20
%29 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%30 = load i64, i64* %29
store i64 %30, i64* %5
br label %31
31: ; preds = %42, %28
%32 = load i32, i32* %2
%33 = load i32, i32* %3
%34 = load i32, i32* @BUFSIZ
%35 = call i64 @sendfile(i32 %32, i32 %33, i64* %7, i32 %34)
store i64 %35, i64* %6
%36 = icmp sgt i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %31
%38 = load i64, i64* %5
%39 = icmp sgt i64 %38, 0
br label %40
40: ; preds = %37, %31
%41 = phi i1 [ false, %31 ], [ %39, %37 ]
br i1 %41, label %42, label %43
42: ; preds = %40
br label %31
43: ; preds = %40
%44 = load i32, i32* %3
%45 = call i32 @close(i32 %44)
ret void
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str = [5 x i8] c"file\00"
@O_RDONLY = global i32 0
@.str.1 = [20 x i8] c"Error openfing file\00"
@.str.2 = [12 x i8] c"Error fstat\00"
@.str.3 = [27 x i8] c"Error on sending file size\00"
@BUFSIZ = global i32 0
define void @send_file(i32 %0) {
%2 = alloca %struct.stat
%3 = alloca i64
%4 = bitcast %struct.stat* %2 to i8*
%5 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = tail call i32 @error_msg_and_exit(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %11
11: ; preds = %9, %1
%12 = call i64 @fstat(i32 %7, %struct.stat* %2)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = tail call i32 @error_msg_and_exit(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %16
16: ; preds = %14, %11
%17 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%18 = call i64 @send(i32 %0, i8* %4, i32 8, i32 0)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @error_msg_and_exit(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
br label %22
22: ; preds = %20, %16
%23 = load i64, i64* %17
%24 = icmp sgt i64 %23, 0
br label %25
25: ; preds = %25, %22
%26 = load i32, i32* @BUFSIZ
%27 = call i64 @sendfile(i32 %0, i32 %7, i64* %3, i32 %26)
%28 = icmp sgt i64 %27, 0
%29 = select i1 %28, i1 %24, i1 false
br i1 %29, label %25, label %30
30: ; preds = %25
%31 = call i32 @close(i32 %7)
ret void
}