#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 */
scalar_t__ atoi(char const *);
int /*<<< orphan*/ create_proxy(char const *, scalar_t__);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ remove_proxy(char const *);
int /*<<< orphan*/ set_enabled(char const *, int);
scalar_t__ setuid(int /*<<< orphan*/);
scalar_t__ strcmp(char const *, char *);
int main(int argc, char *argv[]) {
  const char *mode = NULL;
  const char *host = NULL;
  const char *port = NULL;
  if (argc < 3 || argc > 4) {
    printf("LocalghostHelper (--enable|--disable) host [port]\n");
    return 1;
  }
  mode = argv[1];
  host = argv[2];
  port = argc == 4 ? argv[3] : NULL;
  if (setuid(0) != 0) {
    printf("Not running as root.\n");
    return 2;
  }
  if (strcmp(mode, "--enable") == 0 && (!port || atoi(port) > 0)) {
    set_enabled(host, 1);
    if (port) {
      create_proxy(host, atoi(port));
    }
  } else if (strcmp(mode, "--disable") == 0) {
    set_enabled(host, 0);
    remove_proxy(host);
  } else {
    printf("LocalghostHelper (--enable|--disable) host [port]\n");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpl   $0x3,-0x8(%rbp)
jl     11b2 <main+0x42>
cmpl   $0x4,-0x8(%rbp)
jle    11ca <main+0x5a>
lea    0xe47(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1303 <main+0x193>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x4,-0x8(%rbp)
jne    11fd <main+0x8d>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x30(%rbp)
jmp    1208 <main+0x98>
xor    %eax,%eax
mov    %rax,-0x30(%rbp)
jmp    1208 <main+0x98>
mov    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
xor    %edi,%edi
call   1090 <setuid@plt>
cmp    $0x0,%rax
je     1239 <main+0xc9>
lea    0xe0b(%rip),%rdi
call   1030 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1303 <main+0x193>
mov    -0x18(%rbp),%rdi
lea    0xe05(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    12ac <main+0x13c>
cmpq   $0x0,-0x28(%rbp)
je     1271 <main+0x101>
mov    -0x28(%rbp),%rdi
call   1080 <atoi@plt>
cmp    $0x0,%rax
jle    12ac <main+0x13c>
mov    -0x20(%rbp),%rdi
mov    $0x1,%esi
call   1060 <set_enabled@plt>
cmpq   $0x0,-0x28(%rbp)
je     12a7 <main+0x137>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   1080 <atoi@plt>
mov    -0x38(%rbp),%rdi
mov    %rax,%rsi
call   1040 <create_proxy@plt>
jmp    12fc <main+0x18c>
mov    -0x18(%rbp),%rdi
lea    0xd9b(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    12df <main+0x16f>
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
call   1060 <set_enabled@plt>
mov    -0x20(%rbp),%rdi
call   1070 <remove_proxy@plt>
jmp    12f7 <main+0x187>
lea    0xd1a(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1303 <main+0x193>
jmp    12fc <main+0x18c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
lea    -0x5(%rdi),%eax
cmp    $0xfffffffd,%eax
ja     1190 <main+0x20>
lea    0xea4(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
jmp    11c3 <main+0x53>
cmp    $0x4,%edi
jne    119b <main+0x2b>
mov    0x18(%rsi),%r15
jmp    119e <main+0x2e>
xor    %r15d,%r15d
mov    0x8(%rsi),%rbx
mov    0x10(%rsi),%r14
xor    %edi,%edi
call   1090 <setuid@plt>
test   %rax,%rax
je     11cb <main+0x5b>
lea    0xe5a(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe2e(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strcmp@plt>
test   %rax,%rax
je     1209 <main+0x99>
lea    0xe23(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strcmp@plt>
test   %rax,%rax
jne    117d <main+0xd>
xor    %ebx,%ebx
mov    %r14,%rdi
xor    %esi,%esi
call   1060 <set_enabled@plt>
mov    %r14,%rdi
call   1070 <remove_proxy@plt>
jmp    11c3 <main+0x53>
test   %r15,%r15
je     123d <main+0xcd>
mov    %r15,%rdi
call   1080 <atoi@plt>
test   %rax,%rax
jle    11df <main+0x6f>
mov    %r14,%rdi
mov    $0x1,%esi
call   1060 <set_enabled@plt>
mov    %r15,%rdi
call   1080 <atoi@plt>
mov    %r14,%rdi
mov    %rax,%rsi
call   1040 <create_proxy@plt>
jmp    124a <main+0xda>
mov    %r14,%rdi
mov    $0x1,%esi
call   1060 <set_enabled@plt>
xor    %ebx,%ebx
jmp    11c3 <main+0x53>

LLVM IR (-O0):
@.str = [51 x i8] c"LocalghostHelper (--enable|--disable) host [port]\0A\00"
@.str.1 = [22 x i8] c"Not running as root.\0A\00"
@.str.2 = [9 x i8] c"--enable\00"
@.str.3 = [10 x i8] c"--disable\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %6
store i8* null, i8** %7
store i8* null, i8** %8
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 3
br i1 %10, label %14, label %11
11: ; preds = %2
%12 = load i32, i32* %4
%13 = icmp sgt i32 %12, 4
br i1 %13, label %14, label %16
14: ; preds = %11, %2
%15 = call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %71
16: ; preds = %11
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
store i8* %19, i8** %6
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 2
%22 = load i8*, i8** %21
store i8* %22, i8** %7
%23 = load i32, i32* %4
%24 = icmp eq i32 %23, 4
br i1 %24, label %25, label %29
25: ; preds = %16
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 3
%28 = load i8*, i8** %27
br label %30
29: ; preds = %16
br label %30
30: ; preds = %29, %25
%31 = phi i8* [ %28, %25 ], [ null, %29 ]
store i8* %31, i8** %8
%32 = call i64 @setuid(i32 0)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %30
%35 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %3
br label %71
36: ; preds = %30
%37 = load i8*, i8** %6
%38 = call i64 @strcmp(i8* %37, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %58
40: ; preds = %36
%41 = load i8*, i8** %8
%42 = icmp ne i8* %41, null
br i1 %42, label %43, label %47
43: ; preds = %40
%44 = load i8*, i8** %8
%45 = call i64 @atoi(i8* %44)
%46 = icmp sgt i64 %45, 0
br i1 %46, label %47, label %58
47: ; preds = %43, %40
%48 = load i8*, i8** %7
%49 = call i32 @set_enabled(i8* %48, i32 1)
%50 = load i8*, i8** %8
%51 = icmp ne i8* %50, null
br i1 %51, label %52, label %57
52: ; preds = %47
%53 = load i8*, i8** %7
%54 = load i8*, i8** %8
%55 = call i64 @atoi(i8* %54)
%56 = call i32 @create_proxy(i8* %53, i64 %55)
br label %57
57: ; preds = %52, %47
br label %70
58: ; preds = %43, %36
%59 = load i8*, i8** %6
%60 = call i64 @strcmp(i8* %59, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%61 = icmp eq i64 %60, 0
br i1 %61, label %62, label %67
62: ; preds = %58
%63 = load i8*, i8** %7
%64 = call i32 @set_enabled(i8* %63, i32 0)
%65 = load i8*, i8** %7
%66 = call i32 @remove_proxy(i8* %65)
br label %69
67: ; preds = %58
%68 = call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %71
69: ; preds = %62
br label %70
70: ; preds = %69, %57
store i32 0, i32* %3
br label %71
71: ; preds = %70, %67, %34, %14
%72 = load i32, i32* %3
ret i32 %72
}

LLVM IR (-O2):
@.str.2 = [9 x i8] c"--enable\00"
@.str.3 = [10 x i8] c"--disable\00"
@str.4 = [21 x i8] c"Not running as root.\00"
@str.5 = [50 x i8] c"LocalghostHelper (--enable|--disable) host [port]\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = add i32 %0, -5
%4 = icmp ult i32 %3, -2
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @str.5, i64 0, i64 0))
br label %44
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = getelementptr inbounds i8*, i8** %1, i64 2
%11 = load i8*, i8** %10
%12 = icmp eq i32 %0, 4
br i1 %12, label %13, label %16
13: ; preds = %7
%14 = getelementptr inbounds i8*, i8** %1, i64 3
%15 = load i8*, i8** %14
br label %16
16: ; preds = %7, %13
%17 = phi i8* [ %15, %13 ], [ null, %7 ]
%18 = tail call i64 @setuid(i32 0)
%19 = icmp eq i64 %18, 0
br i1 %19, label %22, label %20
20: ; preds = %16
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.4, i64 0, i64 0))
br label %44
22: ; preds = %16
%23 = tail call i64 @strcmp(i8* %9, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %34
25: ; preds = %22
%26 = icmp eq i8* %17, null
br i1 %26, label %42, label %27
27: ; preds = %25
%28 = tail call i64 @atoi(i8* %17)
%29 = icmp sgt i64 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %27
%31 = tail call i32 @set_enabled(i8* %11, i32 1)
%32 = tail call i64 @atoi(i8* %17)
%33 = tail call i32 @create_proxy(i8* %11, i64 %32)
br label %44
34: ; preds = %27, %22
%35 = tail call i64 @strcmp(i8* %9, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %34
%38 = tail call i32 @set_enabled(i8* %11, i32 0)
%39 = tail call i32 @remove_proxy(i8* %11)
br label %44
40: ; preds = %34
%41 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @str.5, i64 0, i64 0))
br label %44
42: ; preds = %25
%43 = tail call i32 @set_enabled(i8* %11, i32 1)
br label %44
44: ; preds = %37, %30, %42, %40, %20, %5
%45 = phi i32 [ 1, %5 ], [ 2, %20 ], [ 1, %40 ], [ 0, %42 ], [ 0, %30 ], [ 0, %37 ]
ret i32 %45
}