#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*/ uint16_t;

/* Variables and functions */
int /*<<< orphan*/ cmd(char *, char const *, ...);
int /*<<< orphan*/ cndc(char *, ...);
int getpid();
int random();
scalar_t__ should_block_ipv6(char const *);
int /*<<< orphan*/ srandom(int);
int time(int /*<<< orphan*/ *);
void up_if(unsigned int *netid, const char *iface, uint16_t listen_port) {
  srandom(time(NULL) ^ getpid());
  while (*netid < 4096)
    *netid = random() & 0xfffe;
  cmd("wg set %s fwmark 0x20000", iface);
  cmd("iptables -I OUTPUT 1 -m mark --mark 0x20000 -j ACCEPT -m comment "
      "--comment \"wireguard rule %s\"",
      iface);
  cmd("ip6tables -I OUTPUT 1 -m mark --mark 0x20000 -j ACCEPT -m comment "
      "--comment \"wireguard rule %s\"",
      iface);
  if (listen_port) {
    cmd("iptables -I INPUT 1 -p udp --dport %u -j ACCEPT -m comment --comment "
        "\"wireguard rule %s\"",
        listen_port, iface);
    cmd("ip6tables -I INPUT 1 -p udp --dport %u -j %s -m comment --comment "
        "\"wireguard rule %s\"",
        listen_port, should_block_ipv6(iface) ? "DROP" : "ACCEPT", iface);
  }
  cndc("interface setcfg %s up", iface);
  cndc("network create %u vpn 1 1", *netid);
  cndc("network interface add %u %s", *netid, iface);
}


x86 assembly (clang, -O0):
<up_if>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1060 <time@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x18(%rbp),%edi
xor    %eax,%edi
call   1030 <srandom@plt>
mov    -0x8(%rbp),%rax
cmpl   $0x1000,(%rax)
jae    11ca <up_if+0x5a>
mov    $0x0,%al
call   1080 <random@plt>
mov    %eax,%ecx
and    $0xfffe,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
jmp    11a0 <up_if+0x30>
mov    -0x10(%rbp),%rsi
lea    0xe2b(%rip),%rdi
mov    $0x0,%al
call   1050 <cmd@plt>
mov    -0x10(%rbp),%rsi
lea    0xe32(%rip),%rdi
mov    $0x0,%al
call   1050 <cmd@plt>
mov    -0x10(%rbp),%rsi
lea    0xe7f(%rip),%rdi
mov    $0x0,%al
call   1050 <cmd@plt>
cmpl   $0x0,-0x14(%rbp)
je     1260 <up_if+0xf0>
movslq -0x14(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xebf(%rip),%rdi
mov    $0x0,%al
call   1050 <cmd@plt>
movslq -0x14(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <should_block_ipv6@plt>
mov    -0x20(%rbp),%rsi
mov    %rax,%rcx
lea    0xf4d(%rip),%rdx
lea    0xf41(%rip),%rax
cmp    $0x0,%rcx
cmovne %rax,%rdx
mov    -0x10(%rbp),%rcx
lea    0xed8(%rip),%rdi
mov    $0x0,%al
call   1050 <cmd@plt>
mov    -0x10(%rbp),%rsi
lea    0xf28(%rip),%rdi
mov    $0x0,%al
call   1090 <cndc@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%esi
lea    0xf2b(%rip),%rdi
mov    $0x0,%al
call   1090 <cndc@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rdx
lea    0xf2d(%rip),%rdi
mov    $0x0,%al
call   1090 <cndc@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<up_if>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %edi,%edi
call   1060 <time@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
xor    %eax,%ebp
mov    %ebp,%edi
call   1030 <srandom@plt>
cmpl   $0xfff,(%rbx)
ja     11c5 <up_if+0x55>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1080 <random@plt>
and    $0xfffe,%eax
mov    %eax,(%rbx)
cmp    $0x1000,%eax
jb     11b0 <up_if+0x40>
lea    0xe34(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <cmd@plt>
lea    0xe3c(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <cmd@plt>
lea    0xe8a(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <cmd@plt>
test   %r15d,%r15d
je     1245 <up_if+0xd5>
movslq %r15d,%rbp
lea    0xed1(%rip),%rdi
mov    %rbp,%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <cmd@plt>
mov    %r14,%rdi
call   1070 <should_block_ipv6@plt>
test   %rax,%rax
lea    0xf66(%rip),%rax
lea    0xf5a(%rip),%rdx
cmove  %rax,%rdx
lea    0xef9(%rip),%rdi
mov    %rbp,%rsi
mov    %r14,%rcx
xor    %eax,%eax
call   1050 <cmd@plt>
lea    0xf47(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1090 <cndc@plt>
mov    (%rbx),%esi
lea    0xf4b(%rip),%rdi
xor    %eax,%eax
call   1090 <cndc@plt>
mov    (%rbx),%esi
lea    0xf55(%rip),%rdi
mov    %r14,%rdx
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1090 <cndc@plt>

LLVM IR (-O0):
@.str = [25 x i8] c"wg set %s fwmark 0x20000\00"
@.str.1 = [95 x i8] c"iptables -I OUTPUT 1 -m mark --mark 0x20000 -j ACCEPT -m comment --comment \22wireguard rule %s\22\00"
@.str.2 = [96 x i8] c"ip6tables -I OUTPUT 1 -m mark --mark 0x20000 -j ACCEPT -m comment --comment \22wireguard rule %s\22\00"
@.str.3 = [89 x i8] c"iptables -I INPUT 1 -p udp --dport %u -j ACCEPT -m comment --comment \22wireguard rule %s\22\00"
@.str.4 = [86 x i8] c"ip6tables -I INPUT 1 -p udp --dport %u -j %s -m comment --comment \22wireguard rule %s\22\00"
@.str.5 = [5 x i8] c"DROP\00"
@.str.6 = [7 x i8] c"ACCEPT\00"
@.str.7 = [23 x i8] c"interface setcfg %s up\00"
@.str.8 = [26 x i8] c"network create %u vpn 1 1\00"
@.str.9 = [28 x i8] c"network interface add %u %s\00"
define void @up_if(i32* %0, i8* %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
store i32* %0, i32** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%7 = call i32 @time(i32* null)
%8 = call i32 (...) @getpid()
%9 = xor i32 %7, %8
%10 = call i32 @srandom(i32 %9)
br label %11
11: ; preds = %15, %3
%12 = load i32*, i32** %4
%13 = load i32, i32* %12
%14 = icmp ult i32 %13, 4096
br i1 %14, label %15, label %19
15: ; preds = %11
%16 = call i32 (...) @random()
%17 = and i32 %16, 65534
%18 = load i32*, i32** %4
store i32 %17, i32* %18
br label %11
19: ; preds = %11
%20 = load i8*, i8** %5
%21 = call i32 (i8*, i8*, ...) @cmd(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = load i8*, i8** %5
%23 = call i32 (i8*, i8*, ...) @cmd(i8* getelementptr inbounds ([95 x i8], [95 x i8]* @.str.1, i64 0, i64 0), i8* %22)
%24 = load i8*, i8** %5
%25 = call i32 (i8*, i8*, ...) @cmd(i8* getelementptr inbounds ([96 x i8], [96 x i8]* @.str.2, i64 0, i64 0), i8* %24)
%26 = load i32, i32* %6
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %44
28: ; preds = %19
%29 = load i32, i32* %6
%30 = sext i32 %29 to i64
%31 = inttoptr i64 %30 to i8*
%32 = load i8*, i8** %5
%33 = call i32 (i8*, i8*, ...) @cmd(i8* getelementptr inbounds ([89 x i8], [89 x i8]* @.str.3, i64 0, i64 0), i8* %31, i8* %32)
%34 = load i32, i32* %6
%35 = sext i32 %34 to i64
%36 = inttoptr i64 %35 to i8*
%37 = load i8*, i8** %5
%38 = call i64 @should_block_ipv6(i8* %37)
%39 = icmp ne i64 %38, 0
%40 = zext i1 %39 to i64
%41 = select i1 %39, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0)
%42 = load i8*, i8** %5
%43 = call i32 (i8*, i8*, ...) @cmd(i8* getelementptr inbounds ([86 x i8], [86 x i8]* @.str.4, i64 0, i64 0), i8* %36, i8* %41, i8* %42)
br label %44
44: ; preds = %28, %19
%45 = load i8*, i8** %5
%46 = call i32 (i8*, ...) @cndc(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.7, i64 0, i64 0), i8* %45)
%47 = load i32*, i32** %4
%48 = load i32, i32* %47
%49 = call i32 (i8*, ...) @cndc(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.8, i64 0, i64 0), i32 %48)
%50 = load i32*, i32** %4
%51 = load i32, i32* %50
%52 = load i8*, i8** %5
%53 = call i32 (i8*, ...) @cndc(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.9, i64 0, i64 0), i32 %51, i8* %52)
ret void
}

LLVM IR (-O2):
@.str = [25 x i8] c"wg set %s fwmark 0x20000\00"
@.str.1 = [95 x i8] c"iptables -I OUTPUT 1 -m mark --mark 0x20000 -j ACCEPT -m comment --comment \22wireguard rule %s\22\00"
@.str.2 = [96 x i8] c"ip6tables -I OUTPUT 1 -m mark --mark 0x20000 -j ACCEPT -m comment --comment \22wireguard rule %s\22\00"
@.str.3 = [89 x i8] c"iptables -I INPUT 1 -p udp --dport %u -j ACCEPT -m comment --comment \22wireguard rule %s\22\00"
@.str.4 = [86 x i8] c"ip6tables -I INPUT 1 -p udp --dport %u -j %s -m comment --comment \22wireguard rule %s\22\00"
@.str.5 = [5 x i8] c"DROP\00"
@.str.6 = [7 x i8] c"ACCEPT\00"
@.str.7 = [23 x i8] c"interface setcfg %s up\00"
@.str.8 = [26 x i8] c"network create %u vpn 1 1\00"
@.str.9 = [28 x i8] c"network interface add %u %s\00"
define void @up_if(i32* nocapture %0, i8* %1, i32 %2) {
%4 = tail call i32 @time(i32* null)
%5 = tail call i32 (...) @getpid()
%6 = xor i32 %5, %4
%7 = tail call i32 @srandom(i32 %6)
%8 = load i32, i32* %0
%9 = icmp ult i32 %8, 4096
br i1 %9, label %10, label %14
10: ; preds = %3, %10
%11 = tail call i32 (...) @random()
%12 = and i32 %11, 65534
store i32 %12, i32* %0
%13 = icmp ult i32 %12, 4096
br i1 %13, label %10, label %14
14: ; preds = %10, %3
%15 = tail call i32 (i8*, i8*, ...) @cmd(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %1)
%16 = tail call i32 (i8*, i8*, ...) @cmd(i8* getelementptr inbounds ([95 x i8], [95 x i8]* @.str.1, i64 0, i64 0), i8* %1)
%17 = tail call i32 (i8*, i8*, ...) @cmd(i8* getelementptr inbounds ([96 x i8], [96 x i8]* @.str.2, i64 0, i64 0), i8* %1)
%18 = icmp eq i32 %2, 0
br i1 %18, label %27, label %19
19: ; preds = %14
%20 = sext i32 %2 to i64
%21 = inttoptr i64 %20 to i8*
%22 = tail call i32 (i8*, i8*, ...) @cmd(i8* getelementptr inbounds ([89 x i8], [89 x i8]* @.str.3, i64 0, i64 0), i8* %21, i8* %1)
%23 = tail call i64 @should_block_ipv6(i8* %1)
%24 = icmp eq i64 %23, 0
%25 = select i1 %24, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0)
%26 = tail call i32 (i8*, i8*, ...) @cmd(i8* getelementptr inbounds ([86 x i8], [86 x i8]* @.str.4, i64 0, i64 0), i8* %21, i8* %25, i8* %1)
br label %27
27: ; preds = %19, %14
%28 = tail call i32 (i8*, ...) @cndc(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.7, i64 0, i64 0), i8* %1)
%29 = load i32, i32* %0
%30 = tail call i32 (i8*, ...) @cndc(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.8, i64 0, i64 0), i32 %29)
%31 = load i32, i32* %0
%32 = tail call i32 (i8*, ...) @cndc(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.9, i64 0, i64 0), i32 %31, i8* %1)
ret void
}