#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*/ exists(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ readFile(char *);
int /*<<< orphan*/ sendFile(char *);
int /*<<< orphan*/ stderr;
scalar_t__ strncmp(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
int main(int argc, char **argv) {
  char filepath[1024];
  FILE *procfp;
  if (argc == 2 && strncmp(argv[1], "L", 2) == 0) {
    procfp = fopen("/proc/firewallExtension", "w");
    if (procfp == NULL) {
      fprintf(stderr, "ERROR: could not write to /proc file\n");
      exit(1);
    }
    fwrite("L ", 3, sizeof(char), procfp);
    fclose(procfp);
  } else if (argc == 3 && strncmp(argv[1], "W", 2) == 0) {
    strncpy(filepath, argv[2], 1024 - 1);
    filepath[1024 - 1] = '\0';
    if (!exists(filepath)) {
      fprintf(stderr, "ERROR: \n     file '%s' does not exist\n", filepath);
      exit(1);
    }
    readFile(filepath);
    sendFile(filepath);
  } else {
    fprintf(stderr, "Usage:\n firewallSetup L \n firewallSetup W <filepath>\n");
    fprintf(stderr,
            "E.g.: ./firewallSetup W /home/Documents/firewallrulles.txt\n");
    exit(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    125d <main+0xbd>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe2e(%rip),%rsi
mov    $0x2,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    125d <main+0xbd>
lea    0xe15(%rip),%rdi
lea    0xe26(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x418(%rbp)
cmpq   $0x0,-0x418(%rbp)
jne    122f <main+0x8f>
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
lea    0xdfe(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x418(%rbp),%rcx
lea    0xe05(%rip),%rdi
mov    $0x3,%esi
mov    $0x1,%edx
call   10b0 <fwrite@plt>
mov    -0x418(%rbp),%rdi
call   1050 <fclose@plt>
jmp    133e <main+0x19e>
cmpl   $0x3,-0x8(%rbp)
jne    1301 <main+0x161>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdcf(%rip),%rsi
mov    $0x2,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    1301 <main+0x161>
lea    -0x410(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
mov    $0x3ff,%edx
call   1030 <strncpy@plt>
movb   $0x0,-0x11(%rbp)
lea    -0x410(%rbp),%rdi
call   1080 <exists@plt>
cmp    $0x0,%eax
jne    12e4 <main+0x144>
mov    0x2d1d(%rip),%rax
mov    (%rax),%edi
lea    -0x410(%rbp),%rdx
lea    0xd74(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    -0x410(%rbp),%rdi
call   10c0 <readFile@plt>
lea    -0x410(%rbp),%rdi
call   1070 <sendFile@plt>
jmp    1339 <main+0x199>
mov    0x2cd8(%rip),%rax
mov    (%rax),%edi
lea    0xd5d(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2cc1(%rip),%rax
mov    (%rax),%edi
lea    0xd7c(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    133e <main+0x19e>
xor    %eax,%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x400,%rsp
mov    %rsi,%rbx
cmp    $0x3,%edi
je     1219 <main+0x79>
cmp    $0x2,%edi
jne    1279 <main+0xd9>
mov    0x8(%rbx),%rdi
lea    0xe3c(%rip),%rsi
mov    $0x2,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
jne    1279 <main+0xd9>
lea    0xe24(%rip),%rdi
lea    0xe35(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     129b <main+0xfb>
mov    %rax,%rbx
lea    0xe45(%rip),%rdi
mov    $0x3,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10b0 <fwrite@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    126e <main+0xce>
mov    0x8(%rbx),%rdi
lea    0xe21(%rip),%rsi
mov    $0x2,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
jne    1279 <main+0xd9>
mov    0x10(%rbx),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x3ff,%edx
call   1030 <strncpy@plt>
movb   $0x0,0x3ff(%rsp)
mov    %rbx,%rdi
call   1080 <exists@plt>
test   %eax,%eax
je     12bc <main+0x11c>
mov    %rsp,%rbx
mov    %rbx,%rdi
call   10c0 <readFile@plt>
mov    %rbx,%rdi
call   1070 <sendFile@plt>
xor    %eax,%eax
add    $0x400,%rsp
pop    %rbx
ret
mov    0x2d60(%rip),%rbx
mov    (%rbx),%edi
lea    0xde5(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    (%rbx),%edi
lea    0xe0b(%rip),%rsi
jmp    12ab <main+0x10b>
mov    0x2d3e(%rip),%rax
mov    (%rax),%edi
lea    0xd71(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2d1d(%rip),%rax
mov    (%rax),%edi
lea    0xd7b(%rip),%rsi
mov    %rsp,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"L\00"
@.str.1 = [24 x i8] c"/proc/firewallExtension\00"
@.str.2 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.3 = [38 x i8] c"ERROR: could not write to /proc file\0A\00"
@.str.4 = [3 x i8] c"L \00"
@.str.5 = [2 x i8] c"W\00"
@.str.6 = [39 x i8] c"ERROR: \0A file '%s' does not exist\0A\00"
@.str.7 = [54 x i8] c"Usage:\0A firewallSetup L \0A firewallSetup W <filepath>\0A\00"
@.str.8 = [60 x i8] c"E.g.: ./firewallSetup W /home/Documents/firewallrulles.txt\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [1024 x i8]
%7 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp eq i32 %8, 2
br i1 %9, label %10, label %29
10: ; preds = %2
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 1
%13 = load i8*, i8** %12
%14 = call i64 @strncmp(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 2)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %29
16: ; preds = %10
%17 = call i32* @fopen(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %17, i32** %7
%18 = load i32*, i32** %7
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %24
20: ; preds = %16
%21 = load i32, i32* @stderr
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %16
%25 = load i32*, i32** %7
%26 = call i32 @fwrite(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 3, i32 1, i32* %25)
%27 = load i32*, i32** %7
%28 = call i32 @fclose(i32* %27)
br label %65
29: ; preds = %10, %2
%30 = load i32, i32* %4
%31 = icmp eq i32 %30, 3
br i1 %31, label %32, label %58
32: ; preds = %29
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 1
%35 = load i8*, i8** %34
%36 = call i64 @strncmp(i8* %35, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 2)
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %58
38: ; preds = %32
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 2
%42 = load i8*, i8** %41
%43 = call i32 @strncpy(i8* %39, i8* %42, i32 1023)
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 1023
store i8 0, i8* %44
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%46 = call i32 @exists(i8* %45)
%47 = icmp ne i32 %46, 0
br i1 %47, label %53, label %48
48: ; preds = %38
%49 = load i32, i32* @stderr
%50 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0), i8* %50)
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %38
%54 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%55 = call i32 @readFile(i8* %54)
%56 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%57 = call i32 @sendFile(i8* %56)
br label %64
58: ; preds = %32, %29
%59 = load i32, i32* @stderr
%60 = call i32 (i32, i8*, ...) @fprintf(i32 %59, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.7, i64 0, i64 0))
%61 = load i32, i32* @stderr
%62 = call i32 (i32, i8*, ...) @fprintf(i32 %61, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.8, i64 0, i64 0))
%63 = call i32 @exit(i32 1)
unreachable
64: ; preds = %53
br label %65
65: ; preds = %64, %24
ret i32 0
}

LLVM IR (-O2):
@.str = [2 x i8] c"L\00"
@.str.1 = [24 x i8] c"/proc/firewallExtension\00"
@.str.2 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.3 = [38 x i8] c"ERROR: could not write to /proc file\0A\00"
@.str.4 = [3 x i8] c"L \00"
@.str.5 = [2 x i8] c"W\00"
@.str.6 = [39 x i8] c"ERROR: \0A file '%s' does not exist\0A\00"
@.str.7 = [54 x i8] c"Usage:\0A firewallSetup L \0A firewallSetup W <filepath>\0A\00"
@.str.8 = [60 x i8] c"E.g.: ./firewallSetup W /home/Documents/firewallrulles.txt\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
switch i32 %0, label %39 [
i32 2, label %5
i32 3, label %20
]
5: ; preds = %2
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
%8 = tail call i64 @strncmp(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 2)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %39
10: ; preds = %5
%11 = tail call i32* @fopen(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = load i32, i32* @stderr
%15 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %10
%18 = tail call i32 @fwrite(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 3, i32 1, i32* %11)
%19 = tail call i32 @fclose(i32* %11)
br label %45
20: ; preds = %2
%21 = getelementptr inbounds i8*, i8** %1, i64 1
%22 = load i8*, i8** %21
%23 = tail call i64 @strncmp(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 2)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %39
25: ; preds = %20
%26 = getelementptr inbounds i8*, i8** %1, i64 2
%27 = load i8*, i8** %26
%28 = call i32 @strncpy(i8* %4, i8* %27, i32 1023)
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 1023
store i8 0, i8* %29
%30 = call i32 @exists(i8* %4)
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %25
%33 = load i32, i32* @stderr
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0), i8* %4)
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %25
%37 = call i32 @readFile(i8* %4)
%38 = call i32 @sendFile(i8* %4)
br label %45
39: ; preds = %5, %2, %20
%40 = load i32, i32* @stderr
%41 = tail call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.7, i64 0, i64 0))
%42 = load i32, i32* @stderr
%43 = tail call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.8, i64 0, i64 0))
%44 = tail call i32 @exit(i32 1)
unreachable
45: ; preds = %36, %17
ret i32 0
}