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

/* Variables and functions */
char *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ SEEK_END;
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
size_t lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
char *mmap(int /*<<< orphan*/ *, size_t, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, size_t);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
char *strerror(int);
int write(int, char *, size_t);
int file_send2(int conn_fd, const char *file) {
  int iFd = open(file, O_RDONLY);
  if (iFd < 0) {
    printf("open %s failed: errno:%d, errMsg:%s\n", file, errno,
           strerror(errno));
    return -1;
  }
  size_t iFileSize = lseek(iFd, 0, SEEK_END);
  printf("iFileSize=%lu\n", iFileSize);
  char *pFile = mmap(NULL, iFileSize, PROT_READ, MAP_PRIVATE, iFd, 0);
  if (pFile == MAP_FAILED) {
    fprintf(stderr, "mmap failed:%s\n", strerror(errno));
    return -1;
  }
  int iret = 0;
  iret = write(conn_fd, pFile, iFileSize);
  if (iret < 0) {
    perror("write");
    return -1;
  }
  munmap(pFile, iFileSize);
  return 0;
}


x86 assembly (clang, -O0):
<file_send2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1204 <file_send2+0x74>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2df3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%edx
mov    %rax,%rcx
lea    0xe0f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f3 <file_send2+0x163>
mov    -0x14(%rbp),%edi
mov    0x2dd2(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <lseek@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rsi
mov    0x2dc0(%rip),%rax
mov    (%rax),%edx
mov    0x2d77(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2d4e(%rip),%rcx
cmp    (%rcx),%rax
jne    12a3 <file_send2+0x113>
mov    0x2d56(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2d42(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    %rax,%rdx
lea    0xda2(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f3 <file_send2+0x163>
movl   $0x0,-0x2c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    12df <file_send2+0x14f>
lea    0xd76(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f3 <file_send2+0x163>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1080 <munmap@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_send2>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %edi,%r14d
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <open@plt>
test   %eax,%eax
js     1229 <file_send2+0x99>
mov    %eax,%ebp
mov    0x2e28(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1060 <lseek@plt>
mov    %rax,%rbx
lea    0xe58(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edx
mov    0x2dd1(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbp
mov    0x2dad(%rip),%rax
cmp    (%rax),%rbp
je     1251 <file_send2+0xc1>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
test   %eax,%eax
js     1283 <file_send2+0xf3>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1080 <munmap@plt>
xor    %eax,%eax
jmp    127e <file_send2+0xee>
mov    0x2d90(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   10b0 <strerror@plt>
lea    0xdc0(%rip),%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1279 <file_send2+0xe9>
mov    0x2d70(%rip),%rax
mov    (%rax),%ebx
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdc5(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdba(%rip),%rdi
call   10a0 <perror@plt>
jmp    1279 <file_send2+0xe9>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [37 x i8] c"open %s failed: errno:%d, errMsg:%s\0A\00"
@errno = global i32 0
@SEEK_END = global i32 0
@.str.1 = [15 x i8] c"iFileSize=%lu\0A\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@stderr = global i32 0
@.str.2 = [16 x i8] c"mmap failed:%s\0A\00"
@.str.3 = [6 x i8] c"write\00"
define i32 @file_send2(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%10 = load i8*, i8** %5
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %2
%16 = load i8*, i8** %5
%17 = load i32, i32* @errno
%18 = load i32, i32* @errno
%19 = call i8* @strerror(i32 %18)
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %16, i32 %17, i8* %19)
store i32 -1, i32* %3
br label %53
21: ; preds = %2
%22 = load i32, i32* %6
%23 = load i32, i32* @SEEK_END
%24 = call i64 @lseek(i32 %22, i32 0, i32 %23)
store i64 %24, i64* %7
%25 = load i64, i64* %7
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i64 %25)
%27 = load i64, i64* %7
%28 = load i32, i32* @PROT_READ
%29 = load i32, i32* @MAP_PRIVATE
%30 = load i32, i32* %6
%31 = call i8* @mmap(i32* null, i64 %27, i32 %28, i32 %29, i32 %30, i32 0)
store i8* %31, i8** %8
%32 = load i8*, i8** %8
%33 = load i8*, i8** @MAP_FAILED
%34 = icmp eq i8* %32, %33
br i1 %34, label %35, label %40
35: ; preds = %21
%36 = load i32, i32* @stderr
%37 = load i32, i32* @errno
%38 = call i8* @strerror(i32 %37)
%39 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %38)
store i32 -1, i32* %3
br label %53
40: ; preds = %21
store i32 0, i32* %9
%41 = load i32, i32* %4
%42 = load i8*, i8** %8
%43 = load i64, i64* %7
%44 = call i32 @write(i32 %41, i8* %42, i64 %43)
store i32 %44, i32* %9
%45 = load i32, i32* %9
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %40
%48 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %53
49: ; preds = %40
%50 = load i8*, i8** %8
%51 = load i64, i64* %7
%52 = call i32 @munmap(i8* %50, i64 %51)
store i32 0, i32* %3
br label %53
53: ; preds = %49, %47, %35, %15
%54 = load i32, i32* %3
ret i32 %54
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [37 x i8] c"open %s failed: errno:%d, errMsg:%s\0A\00"
@errno = global i32 0
@SEEK_END = global i32 0
@.str.1 = [15 x i8] c"iFileSize=%lu\0A\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@stderr = global i32 0
@.str.2 = [16 x i8] c"mmap failed:%s\0A\00"
@.str.3 = [6 x i8] c"write\00"
define i32 @file_send2(i32 %0, i8* %1) {
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* %1, i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = load i32, i32* @errno
%8 = tail call i8* @strerror(i32 %7)
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %1, i32 %7, i8* %8)
br label %31
10: ; preds = %2
%11 = load i32, i32* @SEEK_END
%12 = tail call i64 @lseek(i32 %4, i32 0, i32 %11)
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i64 %12)
%14 = load i32, i32* @PROT_READ
%15 = load i32, i32* @MAP_PRIVATE
%16 = tail call i8* @mmap(i32* null, i64 %12, i32 %14, i32 %15, i32 %4, i32 0)
%17 = load i8*, i8** @MAP_FAILED
%18 = icmp eq i8* %16, %17
br i1 %18, label %19, label %24
19: ; preds = %10
%20 = load i32, i32* @stderr
%21 = load i32, i32* @errno
%22 = tail call i8* @strerror(i32 %21)
%23 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %22)
br label %31
24: ; preds = %10
%25 = tail call i32 @write(i32 %0, i8* %16, i64 %12)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %31
29: ; preds = %24
%30 = tail call i32 @munmap(i8* %16, i64 %12)
br label %31
31: ; preds = %19, %29, %27, %6
%32 = phi i32 [ -1, %6 ], [ -1, %19 ], [ -1, %27 ], [ 0, %29 ]
ret i32 %32
}