#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*/ u_int64_t;
typedef int /*<<< orphan*/ desc;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ IPPROTO_TCP;
int LOG_CONS;
int /*<<< orphan*/ LOG_LOCAL0;
int LOG_PERROR;
scalar_t__ add_filter_rule(int /*<<< orphan*/, char const *, unsigned short);
scalar_t__ addnatrule(int /*<<< orphan*/, unsigned short, char const *,
                      unsigned short);
scalar_t__ atoi(char *);
int /*<<< orphan*/ delete_redirect_and_filter_rules(unsigned short,
                                                    int /*<<< orphan*/);
scalar_t__ get_redirect_rule_by_index(int /*<<< orphan*/, char *,
                                      unsigned short *, char *, int,
                                      unsigned short *, int *, char *, int,
                                      int /*<<< orphan*/ *,
                                      int /*<<< orphan*/ *);
int /*<<< orphan*/ list_redirect_rule(char *);
int /*<<< orphan*/ openlog(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char **argv) {
  unsigned short eport, iport;
  const char *iaddr;
  printf("Usage %s <ext_port> <internal_ip> <internal_port>\n", argv[0]);
  if (argc < 4)
    return -1;
  openlog("testuptcrdr", LOG_PERROR | LOG_CONS, LOG_LOCAL0);
  eport = (unsigned short)atoi(argv[1]);
  iaddr = argv[2];
  iport = (unsigned short)atoi(argv[3]);
  printf("trying to redirect port %hu to %s:%hu\n", eport, iaddr, iport);
  if (addnatrule(IPPROTO_TCP, eport, iaddr, iport) < 0)
    return -1;
  if (add_filter_rule(IPPROTO_TCP, iaddr, iport) < 0)
    return -1;
  {
    unsigned short p1, p2;
    char addr[16];
    int proto2;
    char desc[256];
    u_int64_t packets, bytes;
    desc[0] = '\0';
    if (get_redirect_rule_by_index(0, "", &p1, addr, sizeof(addr), &p2, &proto2,
                                   desc, sizeof(desc), &packets, &bytes) < 0) {
      printf(
          "redirected port %hu to %s:%hu proto %d   packets=%llu bytes=%llu\n",
          p1, addr, p2, proto2, packets, bytes);
    } else {
      printf("rule not found\n");
    }
  }
  printf("trying to list nat rules :\n");
  list_redirect_rule(argv[1]);
  printf("deleting\n");
  delete_redirect_and_filter_rules(eport, IPPROTO_TCP);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %rbx
sub    $0x178,%rsp
movl   $0x0,-0xc(%rbp)
mov    %edi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
lea    0xe58(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x4,-0x10(%rbp)
jge    11c5 <main+0x45>
movl   $0xffffffff,-0xc(%rbp)
jmp    137e <main+0x1fe>
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
mov    0x2df3(%rip),%rax
or     (%rax),%esi
mov    0x2e0a(%rip),%rax
mov    (%rax),%edx
lea    0xe4c(%rip),%rdi
call   1080 <openlog@plt>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <atoi@plt>
mov    %ax,-0x1a(%rbp)
mov    -0x18(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    0x18(%rax),%rdi
call   1070 <atoi@plt>
mov    %ax,-0x1c(%rbp)
movzwl -0x1a(%rbp),%esi
mov    -0x28(%rbp),%rdx
movzwl -0x1c(%rbp),%ecx
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
mov    -0x1a(%rbp),%ax
mov    -0x28(%rbp),%rdx
movzwl %ax,%esi
movzwl -0x1c(%rbp),%ecx
call   1030 <addnatrule@plt>
cmp    $0x0,%rax
jge    1267 <main+0xe7>
movl   $0xffffffff,-0xc(%rbp)
jmp    137e <main+0x1fe>
mov    0x2d72(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rsi
movzwl -0x1c(%rbp),%edx
call   1060 <add_filter_rule@plt>
cmp    $0x0,%rax
jge    1293 <main+0x113>
movl   $0xffffffff,-0xc(%rbp)
jmp    137e <main+0x1fe>
movb   $0x0,-0x150(%rbp)
lea    -0x40(%rbp),%rcx
lea    -0x150(%rbp),%r11
xor    %edi,%edi
lea    0xe25(%rip),%rsi
lea    -0x2a(%rbp),%rdx
mov    $0x10,%r8d
lea    -0x2c(%rbp),%r9
lea    -0x44(%rbp),%rbx
lea    -0x154(%rbp),%r10
lea    -0x158(%rbp),%rax
mov    %rbx,(%rsp)
mov    %r11,0x8(%rsp)
movl   $0x100,0x10(%rsp)
mov    %r10,0x18(%rsp)
mov    %rax,0x20(%rsp)
call   1090 <get_redirect_rule_by_index@plt>
cmp    $0x0,%rax
jge    132b <main+0x1ab>
movzwl -0x2a(%rbp),%esi
lea    -0x40(%rbp),%rdx
movzwl -0x2c(%rbp),%ecx
mov    -0x44(%rbp),%r8d
mov    -0x154(%rbp),%r9d
mov    -0x158(%rbp),%eax
lea    0xd4a(%rip),%rdi
mov    %eax,(%rsp)
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1339 <main+0x1b9>
lea    0xd76(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xd78(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rdi
call   10a0 <list_redirect_rule@plt>
lea    0xd79(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1a(%rbp),%ax
mov    0x2c73(%rip),%rcx
mov    (%rcx),%esi
movzwl %ax,%edi
call   1050 <delete_redirect_and_filter_rules@plt>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0x178,%rsp
pop    %rbx
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x138,%rsp
mov    %rsi,%r12
mov    %edi,%ebx
mov    (%rsi),%rsi
lea    0xe50(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%r15d
cmp    $0x4,%ebx
jl     1333 <main+0x1a3>
mov    0x2e0b(%rip),%rax
mov    0x2df4(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e09(%rip),%rax
mov    (%rax),%edx
lea    0xe4b(%rip),%rdi
call   1090 <openlog@plt>
mov    0x8(%r12),%rdi
call   1080 <atoi@plt>
mov    %rax,%rbx
mov    0x10(%r12),%rbp
mov    0x18(%r12),%rdi
call   1080 <atoi@plt>
mov    %rax,%r13
mov    %rbx,0x18(%rsp)
movzwl %bx,%ebx
movzwl %r13w,%r14d
lea    0xe20(%rip),%rdi
mov    %ebx,%esi
mov    %rbp,%rdx
mov    %r14d,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
mov    %ebx,%esi
mov    %rbp,%rdx
mov    %r14d,%ecx
call   1040 <addnatrule@plt>
test   %rax,%rax
js     1333 <main+0x1a3>
mov    0x2d8c(%rip),%r14
mov    (%r14),%edi
movzwl %r13w,%edx
mov    %rbp,%rsi
call   1070 <add_filter_rule@plt>
test   %rax,%rax
js     1333 <main+0x1a3>
movb   $0x0,0x30(%rsp)
sub    $0x8,%rsp
lea    0x14(%rsp),%rax
lea    0x18(%rsp),%rbp
lea    0x38(%rsp),%rbx
lea    0x1c(%rsp),%r10
lea    0xda2(%rip),%rsi
lea    0x12(%rsp),%rdx
lea    0x28(%rsp),%rcx
lea    0x10(%rsp),%r9
xor    %edi,%edi
mov    $0x10,%r8d
push   %rax
push   %rbp
push   $0x100
push   %rbx
push   %r10
call   10a0 <get_redirect_rule_by_index@plt>
add    $0x30,%rsp
test   %rax,%rax
js     12cd <main+0x13d>
lea    0xde2(%rip),%rdi
call   1030 <puts@plt>
jmp    1301 <main+0x171>
movzwl 0xa(%rsp),%esi
movzwl 0x8(%rsp),%ecx
mov    0x14(%rsp),%r8d
mov    0x10(%rsp),%r9d
mov    0xc(%rsp),%ebp
sub    $0x8,%rsp
lea    0xd76(%rip),%rdi
lea    0x28(%rsp),%rdx
xor    %eax,%eax
push   %rbp
call   1050 <printf@plt>
add    $0x10,%rsp
lea    0xdaf(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%r12),%rdi
call   10b0 <list_redirect_rule@plt>
lea    0xdb4(%rip),%rdi
call   1030 <puts@plt>
mov    (%r14),%esi
movzwl 0x18(%rsp),%edi
call   1060 <delete_redirect_and_filter_rules@plt>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x138,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [51 x i8] c"Usage %s <ext_port> <internal_ip> <internal_port>\0A\00"
@.str.1 = [12 x i8] c"testuptcrdr\00"
@LOG_PERROR = global i32 0
@LOG_CONS = global i32 0
@LOG_LOCAL0 = global i32 0
@.str.2 = [39 x i8] c"trying to redirect port %hu to %s:%hu\0A\00"
@IPPROTO_TCP = global i32 0
@.str.3 = [1 x i8] zeroinitializer
@.str.4 = [66 x i8] c"redirected port %hu to %s:%hu proto %d packets=%llu bytes=%llu\0A\00"
@.str.5 = [16 x i8] c"rule not found\0A\00"
@.str.6 = [28 x i8] c"trying to list nat rules :\0A\00"
@.str.7 = [10 x i8] c"deleting\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i16
%7 = alloca i16
%8 = alloca i8*
%9 = alloca i16
%10 = alloca i16
%11 = alloca [16 x i8]
%12 = alloca i32
%13 = alloca [256 x i8]
%14 = alloca i32
%15 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = load i32, i32* %4
%21 = icmp slt i32 %20, 4
br i1 %21, label %22, label %23
22: ; preds = %2
store i32 -1, i32* %3
br label %90
23: ; preds = %2
%24 = load i32, i32* @LOG_PERROR
%25 = load i32, i32* @LOG_CONS
%26 = or i32 %24, %25
%27 = load i32, i32* @LOG_LOCAL0
%28 = call i32 @openlog(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %26, i32 %27)
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = call i64 @atoi(i8* %31)
%33 = trunc i64 %32 to i16
store i16 %33, i16* %6
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 2
%36 = load i8*, i8** %35
store i8* %36, i8** %8
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 3
%39 = load i8*, i8** %38
%40 = call i64 @atoi(i8* %39)
%41 = trunc i64 %40 to i16
store i16 %41, i16* %7
%42 = load i16, i16* %6
%43 = zext i16 %42 to i32
%44 = load i8*, i8** %8
%45 = load i16, i16* %7
%46 = zext i16 %45 to i32
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 %43, i8* %44, i32 %46)
%48 = load i32, i32* @IPPROTO_TCP
%49 = load i16, i16* %6
%50 = load i8*, i8** %8
%51 = load i16, i16* %7
%52 = call i64 @addnatrule(i32 %48, i16 zeroext %49, i8* %50, i16 zeroext %51)
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %23
store i32 -1, i32* %3
br label %90
55: ; preds = %23
%56 = load i32, i32* @IPPROTO_TCP
%57 = load i8*, i8** %8
%58 = load i16, i16* %7
%59 = call i64 @add_filter_rule(i32 %56, i8* %57, i16 zeroext %58)
%60 = icmp slt i64 %59, 0
br i1 %60, label %61, label %62
61: ; preds = %55
store i32 -1, i32* %3
br label %90
62: ; preds = %55
%63 = getelementptr inbounds [256 x i8], [256 x i8]* %13, i64 0, i64 0
store i8 0, i8* %63
%64 = getelementptr inbounds [16 x i8], [16 x i8]* %11, i64 0, i64 0
%65 = getelementptr inbounds [256 x i8], [256 x i8]* %13, i64 0, i64 0
%66 = call i64 @get_redirect_rule_by_index(i32 0, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0), i16* %9, i8* %64, i32 16, i16* %10, i32* %12, i8* %65, i32 256, i32* %14, i32* %15)
%67 = icmp slt i64 %66, 0
br i1 %67, label %68, label %78
68: ; preds = %62
%69 = load i16, i16* %9
%70 = zext i16 %69 to i32
%71 = getelementptr inbounds [16 x i8], [16 x i8]* %11, i64 0, i64 0
%72 = load i16, i16* %10
%73 = zext i16 %72 to i32
%74 = load i32, i32* %12
%75 = load i32, i32* %14
%76 = load i32, i32* %15
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.4, i64 0, i64 0), i32 %70, i8* %71, i32 %73, i32 %74, i32 %75, i32 %76)
br label %80
78: ; preds = %62
%79 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
br label %80
80: ; preds = %78, %68
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0))
%82 = load i8**, i8*** %5
%83 = getelementptr inbounds i8*, i8** %82, i64 1
%84 = load i8*, i8** %83
%85 = call i32 @list_redirect_rule(i8* %84)
%86 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0))
%87 = load i16, i16* %6
%88 = load i32, i32* @IPPROTO_TCP
%89 = call i32 @delete_redirect_and_filter_rules(i16 zeroext %87, i32 %88)
store i32 0, i32* %3
br label %90
90: ; preds = %80, %61, %54, %22
%91 = load i32, i32* %3
ret i32 %91
}

LLVM IR (-O2):
@.str = [51 x i8] c"Usage %s <ext_port> <internal_ip> <internal_port>\0A\00"
@.str.1 = [12 x i8] c"testuptcrdr\00"
@LOG_PERROR = global i32 0
@LOG_CONS = global i32 0
@LOG_LOCAL0 = global i32 0
@.str.2 = [39 x i8] c"trying to redirect port %hu to %s:%hu\0A\00"
@IPPROTO_TCP = global i32 0
@.str.3 = [1 x i8] zeroinitializer
@.str.4 = [66 x i8] c"redirected port %hu to %s:%hu proto %d packets=%llu bytes=%llu\0A\00"
@str = [15 x i8] c"rule not found\00"
@str.8 = [27 x i8] c"trying to list nat rules :\00"
@str.9 = [9 x i8] c"deleting\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i16
%4 = alloca i16
%5 = alloca [16 x i8]
%6 = alloca i32
%7 = alloca [256 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = load i8*, i8** %1
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = icmp slt i32 %0, 4
br i1 %12, label %69, label %13
13: ; preds = %2
%14 = load i32, i32* @LOG_PERROR
%15 = load i32, i32* @LOG_CONS
%16 = or i32 %15, %14
%17 = load i32, i32* @LOG_LOCAL0
%18 = tail call i32 @openlog(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %16, i32 %17)
%19 = getelementptr inbounds i8*, i8** %1, i64 1
%20 = load i8*, i8** %19
%21 = tail call i64 @atoi(i8* %20)
%22 = trunc i64 %21 to i16
%23 = getelementptr inbounds i8*, i8** %1, i64 2
%24 = load i8*, i8** %23
%25 = getelementptr inbounds i8*, i8** %1, i64 3
%26 = load i8*, i8** %25
%27 = tail call i64 @atoi(i8* %26)
%28 = trunc i64 %27 to i16
%29 = trunc i64 %21 to i32
%30 = and i32 %29, 65535
%31 = trunc i64 %27 to i32
%32 = and i32 %31, 65535
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 %30, i8* %24, i32 %32)
%34 = load i32, i32* @IPPROTO_TCP
%35 = tail call i64 @addnatrule(i32 %34, i16 zeroext %22, i8* %24, i16 zeroext %28)
%36 = icmp slt i64 %35, 0
br i1 %36, label %69, label %37
37: ; preds = %13
%38 = load i32, i32* @IPPROTO_TCP
%39 = tail call i64 @add_filter_rule(i32 %38, i8* %24, i16 zeroext %28)
%40 = icmp slt i64 %39, 0
br i1 %40, label %69, label %41
41: ; preds = %37
%42 = bitcast i16* %3 to i8*
%43 = bitcast i16* %4 to i8*
%44 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%45 = bitcast i32* %6 to i8*
%46 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%47 = bitcast i32* %8 to i8*
%48 = bitcast i32* %9 to i8*
store i8 0, i8* %46
%49 = call i64 @get_redirect_rule_by_index(i32 0, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0), i16* %3, i8* %44, i32 16, i16* %4, i32* %6, i8* %46, i32 256, i32* %8, i32* %9)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %60
51: ; preds = %41
%52 = load i16, i16* %3
%53 = zext i16 %52 to i32
%54 = load i16, i16* %4
%55 = zext i16 %54 to i32
%56 = load i32, i32* %6
%57 = load i32, i32* %8
%58 = load i32, i32* %9
%59 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([66 x i8], [66 x i8]* @.str.4, i64 0, i64 0), i32 %53, i8* %44, i32 %55, i32 %56, i32 %57, i32 %58)
br label %62
60: ; preds = %41
%61 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %62
62: ; preds = %60, %51
%63 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.8, i64 0, i64 0))
%64 = load i8*, i8** %19
%65 = call i32 @list_redirect_rule(i8* %64)
%66 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.9, i64 0, i64 0))
%67 = load i32, i32* @IPPROTO_TCP
%68 = call i32 @delete_redirect_and_filter_rules(i16 zeroext %22, i32 %67)
br label %69
69: ; preds = %37, %13, %2, %62
%70 = phi i32 [ 0, %62 ], [ -1, %2 ], [ -1, %13 ], [ -1, %37 ]
ret i32 %70
}