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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int sendfile(int, int, int *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, int);
int strlen(char *);
int serveFile(int socketfd, char *fileDir, int fileNumber) {
  int length, result;
  off_t written = 0;
  length = strlen(fileDir) + 21 * sizeof(char);
  char *name = malloc(length);
  if (name == NULL) {
    printf("Failed to malloc memory\n");
    return -1;
  }
  snprintf(name, 255, "%s/%d", "/usr/src/webserve", fileNumber);
  length = strlen(name);
  printf("Serving file %s\n", name);
  int in_fd = open(name, O_RDONLY);
  if (in_fd == -1) {
    perror("Failed to open file");
    return -1;
  }
  while (written < length) {
    result = sendfile(socketfd, in_fd, &written, (10 * 1048576) - written);
    if (result == -1) {
      perror("Error occured while calling sendfile\n");
      close(socketfd);
      return result;
    }
  }
  close(socketfd);
  close(in_fd);
  free(name);
  return 0;
}


x86 assembly (clang, -O0):
<serveFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
cltq
add    $0x15,%rax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11ec <serveFile+0x5c>
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12da <serveFile+0x14a>
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%r8d
mov    $0xff,%esi
lea    0xe19(%rip),%rdx
lea    0xe18(%rip),%rcx
call   1060 <snprintf@plt>
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x28(%rbp),%rsi
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
mov    0x2db3(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1261 <serveFile+0xd1>
lea    0xdf2(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12da <serveFile+0x14a>
jmp    1266 <serveFile+0xd6>
mov    -0x20(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    12ba <serveFile+0x12a>
mov    -0x8(%rbp),%edi
mov    -0x2c(%rbp),%esi
mov    $0xa00000,%ecx
sub    -0x20(%rbp),%ecx
lea    -0x20(%rbp),%rdx
call   1090 <sendfile@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    12b5 <serveFile+0x125>
lea    0xdb9(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12da <serveFile+0x14a>
jmp    1266 <serveFile+0xd6>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
mov    -0x2c(%rbp),%edi
call   1070 <close@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serveFile>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edx,%ebx
mov    %edi,%r15d
movl   $0x0,0xc(%rsp)
mov    %rsi,%rdi
call   1050 <strlen@plt>
lea    0x15(%rax),%edi
call   1090 <malloc@plt>
test   %rax,%rax
je     127c <serveFile+0xdc>
mov    %rax,%r14
lea    0xe24(%rip),%rdx
lea    0xe23(%rip),%rcx
mov    %rax,%rdi
mov    $0xff,%esi
mov    %ebx,%r8d
call   1070 <snprintf@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebp
lea    0xe14(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   10b0 <open@plt>
cmp    $0xffffffff,%eax
je     129a <serveFile+0xfa>
mov    %eax,%ebx
test   %ebp,%ebp
jle    1261 <serveFile+0xc1>
xor    %eax,%eax
lea    0xc(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    $0xa00000,%ecx
sub    %eax,%ecx
mov    %r15d,%edi
mov    %ebx,%esi
mov    %r12,%rdx
call   10a0 <sendfile@plt>
cmp    $0xffffffff,%eax
je     12a8 <serveFile+0x108>
mov    0xc(%rsp),%eax
cmp    %ebp,%eax
jl     1240 <serveFile+0xa0>
mov    %r15d,%edi
call   1080 <close@plt>
mov    %ebx,%edi
call   1080 <close@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    128d <serveFile+0xed>
lea    0xde0(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd88(%rip),%rdi
call   10c0 <perror@plt>
jmp    1288 <serveFile+0xe8>
lea    0xd8e(%rip),%rdi
call   10c0 <perror@plt>
mov    %r15d,%edi
call   1080 <close@plt>
jmp    1288 <serveFile+0xe8>

LLVM IR (-O0):
@.str = [25 x i8] c"Failed to malloc memory\0A\00"
@.str.1 = [6 x i8] c"%s/%d\00"
@.str.2 = [18 x i8] c"/usr/src/webserve\00"
@.str.3 = [17 x i8] c"Serving file %s\0A\00"
@O_RDONLY = global i32 0
@.str.4 = [20 x i8] c"Failed to open file\00"
@.str.5 = [38 x i8] c"Error occured while calling sendfile\0A\00"
define i32 @serveFile(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %10
%13 = load i8*, i8** %6
%14 = call i32 @strlen(i8* %13)
%15 = sext i32 %14 to i64
%16 = add i64 %15, 21
%17 = trunc i64 %16 to i32
store i32 %17, i32* %8
%18 = load i32, i32* %8
%19 = call i8* @malloc(i32 %18)
store i8* %19, i8** %11
%20 = load i8*, i8** %11
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %24
22: ; preds = %3
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %65
24: ; preds = %3
%25 = load i8*, i8** %11
%26 = load i32, i32* %7
%27 = call i32 @snprintf(i8* %25, i32 255, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %26)
%28 = load i8*, i8** %11
%29 = call i32 @strlen(i8* %28)
store i32 %29, i32* %8
%30 = load i8*, i8** %11
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %30)
%32 = load i8*, i8** %11
%33 = load i32, i32* @O_RDONLY
%34 = call i32 @open(i8* %32, i32 %33)
store i32 %34, i32* %12
%35 = load i32, i32* %12
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %39
37: ; preds = %24
%38 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %4
br label %65
39: ; preds = %24
br label %40
40: ; preds = %57, %39
%41 = load i32, i32* %10
%42 = load i32, i32* %8
%43 = icmp slt i32 %41, %42
br i1 %43, label %44, label %58
44: ; preds = %40
%45 = load i32, i32* %5
%46 = load i32, i32* %12
%47 = load i32, i32* %10
%48 = sub nsw i32 10485760, %47
%49 = call i32 @sendfile(i32 %45, i32 %46, i32* %10, i32 %48)
store i32 %49, i32* %9
%50 = load i32, i32* %9
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %57
52: ; preds = %44
%53 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0))
%54 = load i32, i32* %5
%55 = call i32 @close(i32 %54)
%56 = load i32, i32* %9
store i32 %56, i32* %4
br label %65
57: ; preds = %44
br label %40
58: ; preds = %40
%59 = load i32, i32* %5
%60 = call i32 @close(i32 %59)
%61 = load i32, i32* %12
%62 = call i32 @close(i32 %61)
%63 = load i8*, i8** %11
%64 = call i32 @free(i8* %63)
store i32 0, i32* %4
br label %65
65: ; preds = %58, %52, %37, %22
%66 = load i32, i32* %4
ret i32 %66
}

LLVM IR (-O2):
@.str.1 = [6 x i8] c"%s/%d\00"
@.str.2 = [18 x i8] c"/usr/src/webserve\00"
@.str.3 = [17 x i8] c"Serving file %s\0A\00"
@O_RDONLY = global i32 0
@.str.4 = [20 x i8] c"Failed to open file\00"
@.str.5 = [38 x i8] c"Error occured while calling sendfile\0A\00"
@str = [24 x i8] c"Failed to malloc memory\00"
define i32 @serveFile(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%6 = tail call i32 @strlen(i8* %1)
%7 = add i32 %6, 21
%8 = tail call i8* @malloc(i32 %7)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %12
10: ; preds = %3
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
br label %38
12: ; preds = %3
%13 = tail call i32 @snprintf(i8* %8, i32 255, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %2)
%14 = tail call i32 @strlen(i8* %8)
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %8)
%16 = load i32, i32* @O_RDONLY
%17 = tail call i32 @open(i8* %8, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %21, label %19
19: ; preds = %12
%20 = icmp sgt i32 %14, 0
br i1 %20, label %23, label %34
21: ; preds = %12
%22 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %38
23: ; preds = %19, %28
%24 = phi i32 [ %29, %28 ], [ 0, %19 ]
%25 = sub nsw i32 10485760, %24
%26 = call i32 @sendfile(i32 %0, i32 %17, i32* %4, i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %31, label %28
28: ; preds = %23
%29 = load i32, i32* %4
%30 = icmp slt i32 %29, %14
br i1 %30, label %23, label %34
31: ; preds = %23
%32 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0))
%33 = call i32 @close(i32 %0)
br label %38
34: ; preds = %28, %19
%35 = call i32 @close(i32 %0)
%36 = call i32 @close(i32 %17)
%37 = call i32 @free(i8* %8)
br label %38
38: ; preds = %21, %31, %34, %10
%39 = phi i32 [ -1, %10 ], [ -1, %21 ], [ -1, %31 ], [ 0, %34 ]
ret i32 %39
}