#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*/ rhost;
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__ 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,
                                      char *, int, unsigned 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("testiptcrdr", LOG_PERROR | LOG_CONS, LOG_LOCAL0);
  eport = (unsigned short)atoi(argv[1]);
  iaddr = argv[2];
  iport = (unsigned short)atoi(argv[3]);
  {
    unsigned short p1, p2;
    char addr[16];
    int proto2;
    char desc[256];
    char rhost[256];
    unsigned int timestamp;
    u_int64_t packets, bytes;
    desc[0] = '\0';
    if (get_redirect_rule_by_index(0, "", &p1, addr, sizeof(addr), &p2, &proto2,
                                   desc, sizeof(desc), rhost, sizeof(rhost),
                                   &timestamp, &packets, &bytes) < 0) {
      printf("rule not found\n");
    } else {
      printf("redirected port %hu to %s:%hu proto %d   packets=%"
             "llu"
             " bytes=%"
             "llu"
             "\n",
             p1, addr, p2, proto2, packets, bytes);
    }
  }
  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   %r15
push   %r14
push   %rbx
sub    $0x298,%rsp
movl   $0x0,-0x1c(%rbp)
mov    %edi,-0x20(%rbp)
mov    %rsi,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rsi
lea    0xe74(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x4,-0x20(%rbp)
jge    11a9 <main+0x49>
movl   $0xffffffff,-0x1c(%rbp)
jmp    1309 <main+0x1a9>
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
mov    0x2e0f(%rip),%rax
or     (%rax),%esi
mov    0x2e26(%rip),%rax
mov    (%rax),%edx
lea    0xe68(%rip),%rdi
call   1060 <openlog@plt>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <atoi@plt>
mov    %ax,-0x2a(%rbp)
mov    -0x28(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
mov    0x18(%rax),%rdi
call   1050 <atoi@plt>
mov    %ax,-0x2c(%rbp)
movb   $0x0,-0x160(%rbp)
lea    -0x50(%rbp),%rcx
lea    -0x160(%rbp),%r14
lea    -0x260(%rbp),%rbx
xor    %edi,%edi
lea    0xe8c(%rip),%rsi
lea    -0x3a(%rbp),%rdx
mov    $0x10,%r8d
lea    -0x3c(%rbp),%r9
lea    -0x54(%rbp),%r15
lea    -0x264(%rbp),%r11
lea    -0x268(%rbp),%r10
lea    -0x26c(%rbp),%rax
mov    %r15,(%rsp)
mov    %r14,0x8(%rsp)
movl   $0x100,0x10(%rsp)
mov    %rbx,0x18(%rsp)
movl   $0x100,0x20(%rsp)
mov    %r11,0x28(%rsp)
mov    %r10,0x30(%rsp)
mov    %rax,0x38(%rsp)
call   1070 <get_redirect_rule_by_index@plt>
cmp    $0x0,%rax
jge    1296 <main+0x136>
lea    0xdb5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12c4 <main+0x164>
movzwl -0x3a(%rbp),%esi
lea    -0x50(%rbp),%rdx
movzwl -0x3c(%rbp),%ecx
mov    -0x54(%rbp),%r8d
mov    -0x268(%rbp),%r9d
mov    -0x26c(%rbp),%eax
lea    0xd95(%rip),%rdi
mov    %eax,(%rsp)
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xdc6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <list_redirect_rule@plt>
lea    0xdc7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x2a(%rbp),%ax
mov    0x2ce8(%rip),%rcx
mov    (%rcx),%esi
movzwl %ax,%edi
call   1040 <delete_redirect_and_filter_rules@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x298,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x230,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
mov    (%rsi),%rsi
lea    0xe72(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%eax
cmp    $0x4,%ebp
jl     12b3 <main+0x143>
mov    0x2e2e(%rip),%rax
mov    0x2e17(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e2c(%rip),%rax
mov    (%rax),%edx
lea    0xe6e(%rip),%rdi
call   1070 <openlog@plt>
mov    0x8(%rbx),%rdi
call   1060 <atoi@plt>
mov    %rax,%r14
movb   $0x0,0x30(%rsp)
lea    0x10(%rsp),%rax
lea    0x14(%rsp),%rbp
lea    0x1c(%rsp),%r10
lea    0x130(%rsp),%r11
lea    0x30(%rsp),%r15
lea    0x18(%rsp),%r12
lea    0xe2f(%rip),%rsi
lea    0xe(%rsp),%rdx
lea    0x20(%rsp),%rcx
lea    0xc(%rsp),%r9
xor    %edi,%edi
mov    $0x10,%r8d
push   %rax
push   %rbp
push   %r10
push   $0x100
push   %r11
push   $0x100
push   %r15
push   %r12
call   1080 <get_redirect_rule_by_index@plt>
add    $0x40,%rsp
test   %rax,%rax
js     1272 <main+0x102>
movzwl 0xe(%rsp),%esi
movzwl 0xc(%rsp),%ecx
mov    0x18(%rsp),%r8d
mov    0x14(%rsp),%r9d
mov    0x10(%rsp),%ebp
sub    $0x8,%rsp
lea    0xde0(%rip),%rdi
lea    0x28(%rsp),%rdx
xor    %eax,%eax
push   %rbp
call   1040 <printf@plt>
add    $0x10,%rsp
jmp    127e <main+0x10e>
lea    0xe2c(%rip),%rdi
call   1030 <puts@plt>
lea    0xdfc(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%rbx),%rdi
call   1090 <list_redirect_rule@plt>
lea    0xe02(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d3a(%rip),%rax
mov    (%rax),%esi
movzwl %r14w,%edi
call   1050 <delete_redirect_and_filter_rules@plt>
xor    %eax,%eax
add    $0x230,%rsp
pop    %rbx
pop    %r12
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"testiptcrdr\00"
@LOG_PERROR = global i32 0
@LOG_CONS = global i32 0
@LOG_LOCAL0 = global i32 0
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [16 x i8] c"rule not found\0A\00"
@.str.4 = [66 x i8] c"redirected port %hu to %s:%hu proto %d packets=%llu bytes=%llu\0A\00"
@.str.5 = [28 x i8] c"trying to list nat rules :\0A\00"
@.str.6 = [10 x i8] c"deleting\0A\00"
@IPPROTO_TCP = global i32 0
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 [256 x i8]
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = load i32, i32* %4
%23 = icmp slt i32 %22, 4
br i1 %23, label %24, label %25
24: ; preds = %2
store i32 -1, i32* %3
br label %72
25: ; preds = %2
%26 = load i32, i32* @LOG_PERROR
%27 = load i32, i32* @LOG_CONS
%28 = or i32 %26, %27
%29 = load i32, i32* @LOG_LOCAL0
%30 = call i32 @openlog(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %28, i32 %29)
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = call i64 @atoi(i8* %33)
%35 = trunc i64 %34 to i16
store i16 %35, i16* %6
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 2
%38 = load i8*, i8** %37
store i8* %38, i8** %8
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 3
%41 = load i8*, i8** %40
%42 = call i64 @atoi(i8* %41)
%43 = trunc i64 %42 to i16
store i16 %43, i16* %7
%44 = getelementptr inbounds [256 x i8], [256 x i8]* %13, i64 0, i64 0
store i8 0, i8* %44
%45 = getelementptr inbounds [16 x i8], [16 x i8]* %11, i64 0, i64 0
%46 = getelementptr inbounds [256 x i8], [256 x i8]* %13, i64 0, i64 0
%47 = getelementptr inbounds [256 x i8], [256 x i8]* %14, i64 0, i64 0
%48 = call i64 @get_redirect_rule_by_index(i32 0, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0), i16* %9, i8* %45, i32 16, i16* %10, i32* %12, i8* %46, i32 256, i8* %47, i32 256, i32* %15, i32* %16, i32* %17)
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %25
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %62
52: ; preds = %25
%53 = load i16, i16* %9
%54 = zext i16 %53 to i32
%55 = getelementptr inbounds [16 x i8], [16 x i8]* %11, i64 0, i64 0
%56 = load i16, i16* %10
%57 = zext i16 %56 to i32
%58 = load i32, i32* %12
%59 = load i32, i32* %16
%60 = load i32, i32* %17
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.4, i64 0, i64 0), i32 %54, i8* %55, i32 %57, i32 %58, i32 %59, i32 %60)
br label %62
62: ; preds = %52, %50
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0))
%64 = load i8**, i8*** %5
%65 = getelementptr inbounds i8*, i8** %64, i64 1
%66 = load i8*, i8** %65
%67 = call i32 @list_redirect_rule(i8* %66)
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0))
%69 = load i16, i16* %6
%70 = load i32, i32* @IPPROTO_TCP
%71 = call i32 @delete_redirect_and_filter_rules(i16 zeroext %69, i32 %70)
store i32 0, i32* %3
br label %72
72: ; preds = %62, %24
%73 = load i32, i32* %3
ret i32 %73
}

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