#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*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
size_t fread(char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
scalar_t__ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, long, int, int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcat(char *, char *);
int sendFile(char *filepath) {
  FILE *fp = fopen(filepath, "r");
  long filesize;
  char *content = NULL;
  if (fp != NULL) {
    if (fseek(fp, 0L, SEEK_END) == 0) {
      filesize = ftell(fp);
      if (filesize != -1) {
        content = malloc(sizeof(char) * (filesize + 1));
      }
      if (fseek(fp, 0L, SEEK_SET) == 0) {
        size_t readlen = fread(content, sizeof(char), filesize, fp);
        if (readlen > 0) {
          content[++readlen] = '\0';
        }
      }
    }
  } else {
    fprintf(stderr, "ERROR: could not read file\n");
    fclose(fp);
    return -1;
  }
  char *msg = malloc(sizeof(char) * (filesize + 3));
  strcat(msg, "W ");
  strcat(msg, content);
  FILE *procfp;
  procfp = fopen("/proc/firewallExtension", "w");
  if (procfp == NULL) {
    fprintf(stderr, "ERROR: could not wrtie to /proc file\n");
    fclose(fp);
    exit(1);
  }
  fwrite(msg, (filesize + 3), sizeof(char), procfp);
  fclose(procfp);
  free(content);
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<sendFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe39(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     129e <sendFile+0xee>
mov    -0x18(%rbp),%rdi
mov    0x2dfa(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1090 <fseek@plt>
cmp    $0x0,%rax
jne    1299 <sendFile+0xe9>
mov    -0x18(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
je     1232 <sendFile+0x82>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2d93(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1090 <fseek@plt>
cmp    $0x0,%rax
jne    1294 <sendFile+0xe4>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jbe    128f <sendFile+0xdf>
mov    -0x28(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
movb   $0x0,0x1(%rax,%rcx,1)
jmp    1294 <sendFile+0xe4>
jmp    1299 <sendFile+0xe9>
jmp    12c8 <sendFile+0x118>
mov    0x2d33(%rip),%rax
mov    (%rax),%edi
lea    0xd54(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1382 <sendFile+0x1d2>
mov    -0x20(%rbp),%rax
add    $0x3,%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
lea    0xd34(%rip),%rsi
call   10b0 <strcat@plt>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   10b0 <strcat@plt>
lea    0xd1e(%rip),%rdi
lea    0xd2f(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1346 <sendFile+0x196>
mov    0x2cb3(%rip),%rax
mov    (%rax),%edi
lea    0xd0d(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0x3,%rsi
mov    -0x40(%rbp),%rcx
mov    $0x1,%edx
call   10d0 <fwrite@plt>
mov    -0x40(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sendFile>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
lea    0xe40(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1208 <sendFile+0x58>
mov    %rax,%r15
mov    0x2e14(%rip),%rax
mov    (%rax),%edx
xor    %r14d,%r14d
mov    %r15,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
test   %rax,%rax
jne    1267 <sendFile+0xb7>
mov    %r15,%rdi
call   1070 <ftell@plt>
mov    %rax,%r12
cmp    $0xffffffffffffffff,%rax
je     122e <sendFile+0x7e>
lea    0x1(%r12),%edi
call   1080 <malloc@plt>
mov    %rax,%r14
jmp    1231 <sendFile+0x81>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
lea    0xdea(%rip),%rsi
call   1060 <fprintf@plt>
xor    %edi,%edi
call   1050 <fclose@plt>
mov    $0xffffffff,%eax
jmp    12d8 <sendFile+0x128>
xor    %r14d,%r14d
mov    0x2d98(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
test   %rax,%rax
jne    1267 <sendFile+0xb7>
mov    %r14,%rdi
mov    $0x1,%esi
mov    %r12,%rdx
mov    %r15,%rcx
call   1040 <fread@plt>
test   %rax,%rax
je     1267 <sendFile+0xb7>
movb   $0x0,0x1(%rax,%r14,1)
add    $0x3,%r12
mov    %r12d,%edi
call   1080 <malloc@plt>
mov    %rax,%r13
lea    0xda1(%rip),%rsi
mov    %rax,%rdi
call   10b0 <strcat@plt>
mov    %r13,%rdi
mov    %r14,%rsi
call   10b0 <strcat@plt>
lea    0xd8a(%rip),%rdi
lea    0xd9b(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     12e2 <sendFile+0x132>
mov    %rax,%rbx
mov    %r13,%rdi
mov    %r12,%rsi
mov    $0x1,%edx
mov    %rax,%rcx
call   10d0 <fwrite@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    0x2cef(%rip),%rax
mov    (%rax),%edi
lea    0xd49(%rip),%rsi
call   1060 <fprintf@plt>
mov    %r15,%rdi
call   1050 <fclose@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@stderr = global i32 0
@.str.1 = [28 x i8] c"ERROR: could not read file\0A\00"
@.str.2 = [3 x i8] c"W \00"
@.str.3 = [24 x i8] c"/proc/firewallExtension\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [38 x i8] c"ERROR: could not wrtie to /proc file\0A\00"
define i32 @sendFile(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32*
store i8* %0, i8** %3
%10 = load i8*, i8** %3
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %4
store i8* null, i8** %6
%12 = load i32*, i32** %4
%13 = icmp ne i32* %12, null
br i1 %13, label %14, label %50
14: ; preds = %1
%15 = load i32*, i32** %4
%16 = load i32, i32* @SEEK_END
%17 = call i64 @fseek(i32* %15, i64 0, i32 %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %49
19: ; preds = %14
%20 = load i32*, i32** %4
%21 = call i64 @ftell(i32* %20)
store i64 %21, i64* %5
%22 = load i64, i64* %5
%23 = icmp ne i64 %22, -1
br i1 %23, label %24, label %30
24: ; preds = %19
%25 = load i64, i64* %5
%26 = add nsw i64 %25, 1
%27 = mul i64 1, %26
%28 = trunc i64 %27 to i32
%29 = call i8* @malloc(i32 %28)
store i8* %29, i8** %6
br label %30
30: ; preds = %24, %19
%31 = load i32*, i32** %4
%32 = load i32, i32* @SEEK_SET
%33 = call i64 @fseek(i32* %31, i64 0, i32 %32)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %48
35: ; preds = %30
%36 = load i8*, i8** %6
%37 = load i64, i64* %5
%38 = load i32*, i32** %4
%39 = call i64 @fread(i8* %36, i32 1, i64 %37, i32* %38)
store i64 %39, i64* %7
%40 = load i64, i64* %7
%41 = icmp ugt i64 %40, 0
br i1 %41, label %42, label %47
42: ; preds = %35
%43 = load i8*, i8** %6
%44 = load i64, i64* %7
%45 = add i64 %44, 1
store i64 %45, i64* %7
%46 = getelementptr inbounds i8, i8* %43, i64 %45
store i8 0, i8* %46
br label %47
47: ; preds = %42, %35
br label %48
48: ; preds = %47, %30
br label %49
49: ; preds = %48, %14
br label %55
50: ; preds = %1
%51 = load i32, i32* @stderr
%52 = call i32 @fprintf(i32 %51, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%53 = load i32*, i32** %4
%54 = call i32 @fclose(i32* %53)
store i32 -1, i32* %2
br label %87
55: ; preds = %49
%56 = load i64, i64* %5
%57 = add nsw i64 %56, 3
%58 = mul i64 1, %57
%59 = trunc i64 %58 to i32
%60 = call i8* @malloc(i32 %59)
store i8* %60, i8** %8
%61 = load i8*, i8** %8
%62 = call i32 @strcat(i8* %61, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%63 = load i8*, i8** %8
%64 = load i8*, i8** %6
%65 = call i32 @strcat(i8* %63, i8* %64)
%66 = call i32* @fopen(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %66, i32** %9
%67 = load i32*, i32** %9
%68 = icmp eq i32* %67, null
br i1 %68, label %69, label %75
69: ; preds = %55
%70 = load i32, i32* @stderr
%71 = call i32 @fprintf(i32 %70, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0))
%72 = load i32*, i32** %4
%73 = call i32 @fclose(i32* %72)
%74 = call i32 @exit(i32 1)
unreachable
75: ; preds = %55
%76 = load i8*, i8** %8
%77 = load i64, i64* %5
%78 = add nsw i64 %77, 3
%79 = load i32*, i32** %9
%80 = call i32 @fwrite(i8* %76, i64 %78, i32 1, i32* %79)
%81 = load i32*, i32** %9
%82 = call i32 @fclose(i32* %81)
%83 = load i8*, i8** %6
%84 = call i32 @free(i8* %83)
%85 = load i32*, i32** %4
%86 = call i32 @fclose(i32* %85)
store i32 0, i32* %2
br label %87
87: ; preds = %75, %50
%88 = load i32, i32* %2
ret i32 %88
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@stderr = global i32 0
@.str.1 = [28 x i8] c"ERROR: could not read file\0A\00"
@.str.2 = [3 x i8] c"W \00"
@.str.3 = [24 x i8] c"/proc/firewallExtension\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [38 x i8] c"ERROR: could not wrtie to /proc file\0A\00"
define i32 @sendFile(i8* nocapture %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %26, label %4
4: ; preds = %1
%5 = load i32, i32* @SEEK_END
%6 = tail call i64 @fseek(i32* %2, i64 0, i32 %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %30
8: ; preds = %4
%9 = tail call i64 @ftell(i32* %2)
%10 = icmp eq i64 %9, -1
br i1 %10, label %15, label %11
11: ; preds = %8
%12 = trunc i64 %9 to i32
%13 = add i32 %12, 1
%14 = tail call i8* @malloc(i32 %13)
br label %15
15: ; preds = %11, %8
%16 = phi i8* [ %14, %11 ], [ null, %8 ]
%17 = load i32, i32* @SEEK_SET
%18 = tail call i64 @fseek(i32* %2, i64 0, i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %30
20: ; preds = %15
%21 = tail call i64 @fread(i8* %16, i32 1, i64 %9, i32* %2)
%22 = icmp eq i64 %21, 0
br i1 %22, label %30, label %23
23: ; preds = %20
%24 = add i64 %21, 1
%25 = getelementptr inbounds i8, i8* %16, i64 %24
store i8 0, i8* %25
br label %30
26: ; preds = %1
%27 = load i32, i32* @stderr
%28 = tail call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%29 = tail call i32 @fclose(i32* null)
br label %50
30: ; preds = %20, %23, %4, %15
%31 = phi i8* [ %16, %15 ], [ null, %4 ], [ %16, %23 ], [ %16, %20 ]
%32 = phi i64 [ %9, %15 ], [ undef, %4 ], [ %9, %23 ], [ %9, %20 ]
%33 = add nsw i64 %32, 3
%34 = trunc i64 %33 to i32
%35 = tail call i8* @malloc(i32 %34)
%36 = tail call i32 @strcat(i8* %35, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%37 = tail call i32 @strcat(i8* %35, i8* %31)
%38 = tail call i32* @fopen(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%39 = icmp eq i32* %38, null
br i1 %39, label %40, label %45
40: ; preds = %30
%41 = load i32, i32* @stderr
%42 = tail call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0))
%43 = tail call i32 @fclose(i32* %2)
%44 = tail call i32 @exit(i32 1)
unreachable
45: ; preds = %30
%46 = tail call i32 @fwrite(i8* %35, i64 %33, i32 1, i32* %38)
%47 = tail call i32 @fclose(i32* %38)
%48 = tail call i32 @free(i8* %31)
%49 = tail call i32 @fclose(i32* %2)
br label %50
50: ; preds = %45, %26
%51 = phi i32 [ 0, %45 ], [ -1, %26 ]
ret i32 %51
}