#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 */
int acceptLoop(int);
int atoi(char *);
int /*<<< orphan*/ close(int);
scalar_t__ createProxyAddr(char *, int);
int createServerSocket(char *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int main(int argc, char *argv[]) {
  int err;
  int fd;
  char *listen_addr;
  int listen_port;
  if (argc == 4) {
    listen_addr = "0.0.0.0";
    listen_port = atoi(argv[3]);
  } else if (argc == 5) {
    listen_addr = argv[3];
    listen_port = atoi(argv[4]);
  } else {
    printf("Usage: %s <proxy host> <proxy port> "
           "[<listen address>] <listen port>\n"
           "Tunnels all TCP connections redirected "
           "by the local linux iptables firewall\n"
           "to <listen port> via the http proxy "
           "<proxy host>, which has to support the\n"
           "HTTPS/SSL CONNECT command.\n"
           "Use\n# iptables -t nat -A OUTPUT -p tcp "
           "-d ! <proxy host> \\\n"
           "\t-j REDIRECT --to-port <listen port>\n"
           "or a similar command to configure "
           "the firewall.\n",
           argv[0]);
    exit(1);
  }
  fd = createServerSocket(listen_addr, listen_port);
  if (fd < 0) {
    perror("Error opening server socket");
    return fd;
  }
  if (createProxyAddr(argv[1], atoi(argv[2])) != 0) {
    perror("Error resolving proxy address");
    close(fd);
    return -1;
  }
  err = acceptLoop(fd);
  if (err < 0) {
    perror("Error in accept loop");
    close(fd);
    return err;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
jne    11c0 <main+0x40>
lea    0xe59(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x24(%rbp)
jmp    120d <main+0x8d>
cmpl   $0x5,-0x8(%rbp)
jne    11eb <main+0x6b>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x24(%rbp)
jmp    1208 <main+0x88>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe0f(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    120d <main+0x8d>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1030 <createServerSocket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    123d <main+0xbd>
lea    0xf63(%rip),%rdi
call   1080 <perror@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12ce <main+0x14e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1090 <atoi@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%esi
call   1050 <createProxyAddr@plt>
cmp    $0x0,%rax
je     128b <main+0x10b>
lea    0xf3a(%rip),%rdi
call   1080 <perror@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ce <main+0x14e>
mov    -0x18(%rbp),%edi
call   1070 <acceptLoop@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    12bf <main+0x13f>
lea    0xf23(%rip),%rdi
call   1080 <perror@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12ce <main+0x14e>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
cmp    $0x4,%edi
je     11a0 <main+0x20>
cmp    $0x5,%edi
jne    123d <main+0xbd>
mov    0x18(%rbx),%rbp
mov    $0x4,%eax
jmp    11ac <main+0x2c>
lea    0xe59(%rip),%rbp
mov    $0x3,%eax
mov    (%rbx,%rax,8),%rdi
call   1090 <atoi@plt>
mov    %rbp,%rdi
mov    %eax,%esi
call   1030 <createServerSocket@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11fc <main+0x7c>
mov    0x8(%rbx),%r14
mov    0x10(%rbx),%rdi
call   1090 <atoi@plt>
mov    %r14,%rdi
mov    %eax,%esi
call   1050 <createProxyAddr@plt>
test   %rax,%rax
jne    120a <main+0x8a>
mov    %ebp,%edi
call   1070 <acceptLoop@plt>
test   %eax,%eax
js     1224 <main+0xa4>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xf8d(%rip),%rdi
call   1080 <perror@plt>
jmp    11f5 <main+0x75>
lea    0xf9b(%rip),%rdi
call   1080 <perror@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebp
jmp    11f5 <main+0x75>
mov    %eax,%ebx
lea    0xf9d(%rip),%rdi
call   1080 <perror@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %ebx,%ebp
jmp    11f5 <main+0x75>
mov    (%rbx),%rsi
lea    0xdc1(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"0.0.0.0\00"
@.str.1 = [392 x i8] c"Usage: %s <proxy host> <proxy port> [<listen address>] <listen port>\0ATunnels all TCP connections redirected by the local linux iptables firewall\0Ato <listen port> via the http proxy <proxy host>, which has to support the\0AHTTPS/SSL CONNECT command.\0AUse\0A# iptables -t nat -A OUTPUT -p tcp -d ! <proxy host> \\\0A\09-j REDIRECT --to-port <listen port>\0Aor a similar command to configure the firewall.\0A\00"
@.str.2 = [28 x i8] c"Error opening server socket\00"
@.str.3 = [30 x i8] c"Error resolving proxy address\00"
@.str.4 = [21 x i8] c"Error in accept loop\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp eq i32 %10, 4
br i1 %11, label %12, label %17
12: ; preds = %2
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8** %8
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 3
%15 = load i8*, i8** %14
%16 = call i32 @atoi(i8* %15)
store i32 %16, i32* %9
br label %35
17: ; preds = %2
%18 = load i32, i32* %4
%19 = icmp eq i32 %18, 5
br i1 %19, label %20, label %28
20: ; preds = %17
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 3
%23 = load i8*, i8** %22
store i8* %23, i8** %8
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 4
%26 = load i8*, i8** %25
%27 = call i32 @atoi(i8* %26)
store i32 %27, i32* %9
br label %34
28: ; preds = %17
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 0
%31 = load i8*, i8** %30
%32 = call i32 @printf(i8* getelementptr inbounds ([392 x i8], [392 x i8]* @.str.1, i64 0, i64 0), i8* %31)
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %20
br label %35
35: ; preds = %34, %12
%36 = load i8*, i8** %8
%37 = load i32, i32* %9
%38 = call i32 @createServerSocket(i8* %36, i32 %37)
store i32 %38, i32* %7
%39 = load i32, i32* %7
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %35
%42 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%43 = load i32, i32* %7
store i32 %43, i32* %3
br label %71
44: ; preds = %35
%45 = load i8**, i8*** %5
%46 = getelementptr inbounds i8*, i8** %45, i64 1
%47 = load i8*, i8** %46
%48 = load i8**, i8*** %5
%49 = getelementptr inbounds i8*, i8** %48, i64 2
%50 = load i8*, i8** %49
%51 = call i32 @atoi(i8* %50)
%52 = call i64 @createProxyAddr(i8* %47, i32 %51)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %58
54: ; preds = %44
%55 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
%56 = load i32, i32* %7
%57 = call i32 @close(i32 %56)
store i32 -1, i32* %3
br label %71
58: ; preds = %44
%59 = load i32, i32* %7
%60 = call i32 @acceptLoop(i32 %59)
store i32 %60, i32* %6
%61 = load i32, i32* %6
%62 = icmp slt i32 %61, 0
br i1 %62, label %63, label %68
63: ; preds = %58
%64 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
%65 = load i32, i32* %7
%66 = call i32 @close(i32 %65)
%67 = load i32, i32* %6
store i32 %67, i32* %3
br label %71
68: ; preds = %58
%69 = load i32, i32* %7
%70 = call i32 @close(i32 %69)
store i32 0, i32* %3
br label %71
71: ; preds = %68, %63, %54, %41
%72 = load i32, i32* %3
ret i32 %72
}

LLVM IR (-O2):
@.str = [8 x i8] c"0.0.0.0\00"
@.str.1 = [392 x i8] c"Usage: %s <proxy host> <proxy port> [<listen address>] <listen port>\0ATunnels all TCP connections redirected by the local linux iptables firewall\0Ato <listen port> via the http proxy <proxy host>, which has to support the\0AHTTPS/SSL CONNECT command.\0AUse\0A# iptables -t nat -A OUTPUT -p tcp -d ! <proxy host> \\\0A\09-j REDIRECT --to-port <listen port>\0Aor a similar command to configure the firewall.\0A\00"
@.str.2 = [28 x i8] c"Error opening server socket\00"
@.str.3 = [30 x i8] c"Error resolving proxy address\00"
@.str.4 = [21 x i8] c"Error in accept loop\00"
define i32 @main(i32 %0, i8** nocapture %1) {
switch i32 %0, label %6 [
i32 4, label %10
i32 5, label %3
]
3: ; preds = %2
%4 = getelementptr inbounds i8*, i8** %1, i64 3
%5 = load i8*, i8** %4
br label %10
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 @printf(i8* getelementptr inbounds ([392 x i8], [392 x i8]* @.str.1, i64 0, i64 0), i8* %7)
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2, %3
%11 = phi i64 [ 4, %3 ], [ 3, %2 ]
%12 = phi i8* [ %5, %3 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), %2 ]
%13 = getelementptr inbounds i8*, i8** %1, i64 %11
%14 = load i8*, i8** %13
%15 = tail call i32 @atoi(i8* %14)
%16 = tail call i32 @createServerSocket(i8* %12, i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %10
%19 = tail call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %39
20: ; preds = %10
%21 = getelementptr inbounds i8*, i8** %1, i64 1
%22 = load i8*, i8** %21
%23 = getelementptr inbounds i8*, i8** %1, i64 2
%24 = load i8*, i8** %23
%25 = tail call i32 @atoi(i8* %24)
%26 = tail call i64 @createProxyAddr(i8* %22, i32 %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %20
%29 = tail call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
%30 = tail call i32 @close(i32 %16)
br label %39
31: ; preds = %20
%32 = tail call i32 @acceptLoop(i32 %16)
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
%36 = tail call i32 @close(i32 %16)
br label %39
37: ; preds = %31
%38 = tail call i32 @close(i32 %16)
br label %39
39: ; preds = %37, %34, %28, %18
%40 = phi i32 [ %16, %18 ], [ -1, %28 ], [ %32, %34 ], [ 0, %37 ]
ret i32 %40
}