#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*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ send(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int sendFile(char *fileName, int socket) {
  char *buffer;
  int bufferSize = 65535;
  buffer = malloc(bufferSize * sizeof(char));
  int returnValue = 0;
  FILE *outFile = fopen(fileName, "r");
  if (outFile == NULL) {
    memset(buffer, 0, bufferSize);
    sprintf(buffer, "File \"%s\" not found.\n", fileName);
    printf("%s", buffer);
    send(socket, "File not Found!", 16, 0);
    recv(socket, buffer, bufferSize, 0);
    returnValue = 1;
  } else {
    send(socket, "@OK", 4, 0);
    recv(socket, buffer, bufferSize, 0);
    while (!feof(outFile)) {
      memset(buffer, 0, bufferSize);
      fgets(buffer, bufferSize, outFile);
      send(socket, buffer, strlen(buffer), 0);
    }
    memset(buffer, 0, bufferSize);
    strcpy(buffer + 1, "@EOF");
    send(socket, buffer, 5, 0);
  }
  free(buffer);
  return returnValue;
}


x86 assembly (clang, -O0):
<sendFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0xffff,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   10c0 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe05(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1274 <sendFile+0xb4>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
xor    %esi,%esi
call   1090 <memset@plt>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xdd6(%rip),%rsi
call   10e0 <sprintf@plt>
mov    -0x18(%rbp),%rsi
lea    0xddc(%rip),%rdi
call   1080 <printf@plt>
mov    -0xc(%rbp),%edi
lea    0xdd0(%rip),%rsi
mov    $0x10,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
movl   $0x1,-0x20(%rbp)
jmp    1338 <sendFile+0x178>
mov    -0xc(%rbp),%edi
lea    0xdad(%rip),%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
mov    -0x28(%rbp),%rdi
call   10b0 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    12b9 <sendFile+0xf9>
jmp    1303 <sendFile+0x143>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
xor    %esi,%esi
call   1090 <memset@plt>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x28(%rbp),%rdx
call   10a0 <fgets@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
jmp    129b <sendFile+0xdb>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
xor    %esi,%esi
call   1090 <memset@plt>
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
lea    0xd0f(%rip),%rsi
call   1050 <strcpy@plt>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x5,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sendFile>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%r14
mov    $0xffff,%edi
call   10c0 <malloc@plt>
mov    %rax,%rbx
lea    0xe1f(%rip),%rsi
mov    %r14,%rdi
call   10d0 <fopen@plt>
test   %rax,%rax
je     1290 <sendFile+0xd0>
mov    %rax,%rbp
lea    0xe2f(%rip),%rsi
xor    %r14d,%r14d
mov    %r15d,%edi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0xffff,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
mov    %rbp,%rdi
call   10b0 <feof@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xffff,%edx
test   %eax,%eax
jne    1264 <sendFile+0xa4>
call   1090 <memset@plt>
mov    %rbx,%rdi
mov    $0xffff,%esi
mov    %rbp,%rdx
call   10a0 <fgets@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
jmp    1220 <sendFile+0x60>
call   1090 <memset@plt>
mov    %rbx,%rdi
add    $0x1,%rdi
lea    0xdb8(%rip),%rsi
call   1050 <strcpy@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x5,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
jmp    12ee <sendFile+0x12e>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xffff,%edx
call   1090 <memset@plt>
lea    0xd5c(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
call   10e0 <sprintf@plt>
lea    0xd60(%rip),%rdi
mov    %rbx,%rsi
call   1080 <printf@plt>
lea    0xd54(%rip),%rsi
mov    %r15d,%edi
mov    $0x10,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0xffff,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
mov    $0x1,%r14d
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [22 x i8] c"File \22%s\22 not found.\0A\00"
@.str.2 = [3 x i8] c"%s\00"
@.str.3 = [16 x i8] c"File not Found!\00"
@.str.4 = [4 x i8] c"@OK\00"
@.str.5 = [5 x i8] c"@EOF\00"
define i32 @sendFile(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 65535, i32* %6
%9 = load i32, i32* %6
%10 = sext i32 %9 to i64
%11 = mul i64 %10, 1
%12 = trunc i64 %11 to i32
%13 = call i8* @malloc(i32 %12)
store i8* %13, i8** %5
store i32 0, i32* %7
%14 = load i8*, i8** %3
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %8
%16 = load i32*, i32** %8
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %33
18: ; preds = %2
%19 = load i8*, i8** %5
%20 = load i32, i32* %6
%21 = call i32 @memset(i8* %19, i32 0, i32 %20)
%22 = load i8*, i8** %5
%23 = load i8*, i8** %3
%24 = call i32 @sprintf(i8* %22, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %23)
%25 = load i8*, i8** %5
%26 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %25)
%27 = load i32, i32* %4
%28 = call i32 @send(i32 %27, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 16, i32 0)
%29 = load i32, i32* %4
%30 = load i8*, i8** %5
%31 = load i32, i32* %6
%32 = call i32 @recv(i32 %29, i8* %30, i32 %31, i32 0)
store i32 1, i32* %7
br label %68
33: ; preds = %2
%34 = load i32, i32* %4
%35 = call i32 @send(i32 %34, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 4, i32 0)
%36 = load i32, i32* %4
%37 = load i8*, i8** %5
%38 = load i32, i32* %6
%39 = call i32 @recv(i32 %36, i8* %37, i32 %38, i32 0)
br label %40
40: ; preds = %45, %33
%41 = load i32*, i32** %8
%42 = call i32 @feof(i32* %41)
%43 = icmp ne i32 %42, 0
%44 = xor i1 %43, true
br i1 %44, label %45, label %58
45: ; preds = %40
%46 = load i8*, i8** %5
%47 = load i32, i32* %6
%48 = call i32 @memset(i8* %46, i32 0, i32 %47)
%49 = load i8*, i8** %5
%50 = load i32, i32* %6
%51 = load i32*, i32** %8
%52 = call i32 @fgets(i8* %49, i32 %50, i32* %51)
%53 = load i32, i32* %4
%54 = load i8*, i8** %5
%55 = load i8*, i8** %5
%56 = call i32 @strlen(i8* %55)
%57 = call i32 @send(i32 %53, i8* %54, i32 %56, i32 0)
br label %40
58: ; preds = %40
%59 = load i8*, i8** %5
%60 = load i32, i32* %6
%61 = call i32 @memset(i8* %59, i32 0, i32 %60)
%62 = load i8*, i8** %5
%63 = getelementptr inbounds i8, i8* %62, i64 1
%64 = call i32 @strcpy(i8* %63, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%65 = load i32, i32* %4
%66 = load i8*, i8** %5
%67 = call i32 @send(i32 %65, i8* %66, i32 5, i32 0)
br label %68
68: ; preds = %58, %18
%69 = load i8*, i8** %5
%70 = call i32 @free(i8* %69)
%71 = load i32, i32* %7
ret i32 %71
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [22 x i8] c"File \22%s\22 not found.\0A\00"
@.str.2 = [3 x i8] c"%s\00"
@.str.3 = [16 x i8] c"File not Found!\00"
@.str.4 = [4 x i8] c"@OK\00"
@.str.5 = [5 x i8] c"@EOF\00"
define i32 @sendFile(i8* %0, i32 %1) {
%3 = tail call dereferenceable_or_null(65535) i8* @malloc(i32 65535)
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %12
6: ; preds = %2
%7 = tail call i32 @memset(i8* %3, i32 0, i32 65535)
%8 = tail call i32 @sprintf(i8* dereferenceable(1) %3, i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%9 = tail call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %3)
%10 = tail call i32 @send(i32 %1, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 16, i32 0)
%11 = tail call i32 @recv(i32 %1, i8* %3, i32 65535, i32 0)
br label %29
12: ; preds = %2
%13 = tail call i32 @send(i32 %1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 4, i32 0)
%14 = tail call i32 @recv(i32 %1, i8* %3, i32 65535, i32 0)
%15 = tail call i32 @feof(i32* %4)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %24
17: ; preds = %12, %17
%18 = tail call i32 @memset(i8* %3, i32 0, i32 65535)
%19 = tail call i32 @fgets(i8* %3, i32 65535, i32* %4)
%20 = tail call i32 @strlen(i8* %3)
%21 = tail call i32 @send(i32 %1, i8* %3, i32 %20, i32 0)
%22 = tail call i32 @feof(i32* %4)
%23 = icmp eq i32 %22, 0
br i1 %23, label %17, label %24
24: ; preds = %17, %12
%25 = tail call i32 @memset(i8* %3, i32 0, i32 65535)
%26 = getelementptr inbounds i8, i8* %3, i64 1
%27 = tail call i32 @strcpy(i8* %26, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%28 = tail call i32 @send(i32 %1, i8* %3, i32 5, i32 0)
br label %29
29: ; preds = %24, %6
%30 = phi i32 [ 1, %6 ], [ 0, %24 ]
%31 = tail call i32 @free(i8* %3)
ret i32 %30
}