#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 */
struct output {
  int dummy;
};
struct ifreq {
  char *ifr_name;
  int ifr_flags;
};

/* Variables and functions */
int IFF_NO_PI;
int IFF_TAP;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TUNSETIFF;
scalar_t__ do_exec(char **, int, struct output *);
int /*<<< orphan*/ forward_ip(struct output *);
scalar_t__ ioctl(int, int /*<<< orphan*/, void *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int /*<<< orphan*/ mk_node(char *, int, int);
int /*<<< orphan*/ mkdir(char *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ output_errno(struct output *, char *);
scalar_t__ setreuid(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int do_tuntap_up(char *gate_addr, struct output *output, struct ifreq *ifr) {
  int tap_fd;
  char *ifconfig_argv[] = {"ifconfig", ifr->ifr_name,     gate_addr,
                           "netmask",  "255.255.255.255", "up",
                           NULL};
  char *insmod_argv[] = {"modprobe", "tun", NULL};
  if (setreuid(0, 0) < 0) {
    output_errno(output, "setreuid to root failed : ");
    return (-1);
  }
  umask(0);
  mkdir("/dev/net", 0755);
  mk_node("/dev/net/tun", 10, 200);
  do_exec(insmod_argv, 0, output);
  if ((tap_fd = open("/dev/net/tun", O_RDWR)) < 0) {
    output_errno(output, "Opening /dev/net/tun failed : ");
    return (-1);
  }
  memset(ifr, 0, sizeof(*ifr));
  ifr->ifr_flags = IFF_TAP | IFF_NO_PI;
  ifr->ifr_name[0] = '\0';
  if (ioctl(tap_fd, TUNSETIFF, (void *)ifr) < 0) {
    output_errno(output, "TUNSETIFF : ");
    return (-1);
  }
  if ((*gate_addr != '\0') && do_exec(ifconfig_argv, 1, output))
    return (-1);
  forward_ip(output);
  return (tap_fd);
}


x86 assembly (clang, -O0):
<do_tuntap_up>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    0xe42(%rip),%rax
mov    %rax,-0x60(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x58(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
lea    0xe2d(%rip),%rax
mov    %rax,-0x48(%rbp)
lea    0xe2a(%rip),%rax
mov    %rax,-0x40(%rbp)
lea    0xe2f(%rip),%rax
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x30(%rbp)
mov    0x2bdb(%rip),%rax
mov    %rax,-0x80(%rbp)
mov    0x2bd8(%rip),%rax
mov    %rax,-0x78(%rbp)
mov    0x2bd5(%rip),%rax
mov    %rax,-0x70(%rbp)
xor    %esi,%esi
mov    %esi,%edi
call   1090 <setreuid@plt>
cmp    $0x0,%rax
jge    124e <do_tuntap_up+0xae>
mov    -0x18(%rbp),%rdi
lea    0xdf0(%rip),%rsi
call   1030 <output_errno@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    137d <do_tuntap_up+0x1dd>
xor    %edi,%edi
call   1080 <umask@plt>
lea    0xdec(%rip),%rdi
mov    $0x1ed,%esi
call   1040 <mkdir@plt>
lea    0xde4(%rip),%rdi
mov    $0xa,%esi
mov    $0xc8,%edx
call   10c0 <mk_node@plt>
lea    -0x80(%rbp),%rdi
mov    -0x18(%rbp),%rdx
xor    %esi,%esi
call   1050 <do_exec@plt>
mov    0x2d4e(%rip),%rax
mov    (%rax),%esi
lea    0xdb6(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    12c8 <do_tuntap_up+0x128>
mov    -0x18(%rbp),%rdi
lea    0xda7(%rip),%rsi
call   1030 <output_errno@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    137d <do_tuntap_up+0x1dd>
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    0x2cf1(%rip),%rax
mov    (%rax),%ecx
mov    0x2cd0(%rip),%rax
or     (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
movb   $0x0,(%rax)
mov    -0x24(%rbp),%edi
mov    0x2cc3(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%rdx
call   1070 <ioctl@plt>
cmp    $0x0,%rax
jge    1336 <do_tuntap_up+0x196>
mov    -0x18(%rbp),%rdi
lea    0xd58(%rip),%rsi
call   1030 <output_errno@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    137d <do_tuntap_up+0x1dd>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     136e <do_tuntap_up+0x1ce>
lea    -0x60(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x1,%esi
call   1050 <do_exec@plt>
cmp    $0x0,%rax
je     136e <do_tuntap_up+0x1ce>
movl   $0xffffffff,-0x4(%rbp)
jmp    137d <do_tuntap_up+0x1dd>
mov    -0x18(%rbp),%rdi
call   10b0 <forward_ip@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_tuntap_up>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x58,%rsp
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe46(%rip),%rax
mov    %rax,(%rsp)
mov    (%rdx),%rax
mov    %rax,0x8(%rsp)
mov    %rdi,0x10(%rsp)
lea    0xe37(%rip),%rax
mov    %rax,0x18(%rsp)
lea    0xe33(%rip),%rax
mov    %rax,0x20(%rsp)
lea    0xe37(%rip),%rax
mov    %rax,0x28(%rsp)
movq   $0x0,0x30(%rsp)
mov    0x2bf1(%rip),%rax
mov    %rax,0x50(%rsp)
movaps 0x2bd5(%rip),%xmm0
movaps %xmm0,0x40(%rsp)
xor    %edi,%edi
xor    %esi,%esi
call   1090 <setreuid@plt>
test   %rax,%rax
js     12ea <do_tuntap_up+0x14a>
xor    %edi,%edi
call   1080 <umask@plt>
lea    0xe18(%rip),%rdi
mov    $0x1ed,%esi
call   1040 <mkdir@plt>
lea    0xe10(%rip),%rbp
mov    %rbp,%rdi
mov    $0xa,%esi
mov    $0xc8,%edx
call   10c0 <mk_node@plt>
lea    0x40(%rsp),%rdi
xor    %esi,%esi
mov    %r14,%rdx
call   1050 <do_exec@plt>
mov    0x2d77(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     12f3 <do_tuntap_up+0x153>
mov    %eax,%ebp
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    0x2d41(%rip),%rax
mov    0x2d22(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,0x8(%rbx)
mov    (%rbx),%rax
movb   $0x0,(%rax)
mov    0x2d1e(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   1070 <ioctl@plt>
test   %rax,%rax
js     12fc <do_tuntap_up+0x15c>
cmpb   $0x0,(%r15)
je     12de <do_tuntap_up+0x13e>
mov    %rsp,%rdi
mov    $0x1,%esi
mov    %r14,%rdx
call   1050 <do_exec@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
jne    1310 <do_tuntap_up+0x170>
mov    %r14,%rdi
call   10b0 <forward_ip@plt>
mov    %ebp,%eax
jmp    1310 <do_tuntap_up+0x170>
lea    0xd3c(%rip),%rsi
jmp    1303 <do_tuntap_up+0x163>
lea    0xd64(%rip),%rsi
jmp    1303 <do_tuntap_up+0x163>
lea    0xd7a(%rip),%rsi
mov    %r14,%rdi
call   1030 <output_errno@plt>
mov    $0xffffffff,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.output = type { i32 }
%struct.ifreq = type { i8*, i32 }
@.str = [9 x i8] c"ifconfig\00"
@.str.1 = [8 x i8] c"netmask\00"
@.str.2 = [16 x i8] c"255.255.255.255\00"
@.str.3 = [3 x i8] c"up\00"
@.str.4 = [9 x i8] c"modprobe\00"
@.str.5 = [4 x i8] c"tun\00"
@__const.do_tuntap_up.insmod_argv = [3 x i8*] [i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i32 0, i32 0), i8* null]
@.str.6 = [27 x i8] c"setreuid to root failed : \00"
@.str.7 = [9 x i8] c"/dev/net\00"
@.str.8 = [13 x i8] c"/dev/net/tun\00"
@O_RDWR = global i32 0
@.str.9 = [31 x i8] c"Opening /dev/net/tun failed : \00"
@IFF_TAP = global i32 0
@IFF_NO_PI = global i32 0
@TUNSETIFF = global i32 0
@.str.10 = [13 x i8] c"TUNSETIFF : \00"
define i32 @do_tuntap_up(i8* %0, %struct.output* %1, %struct.ifreq* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca %struct.output*
%7 = alloca %struct.ifreq*
%8 = alloca i32
%9 = alloca [7 x i8*]
%10 = alloca [3 x i8*]
store i8* %0, i8** %5
store %struct.output* %1, %struct.output** %6
store %struct.ifreq* %2, %struct.ifreq** %7
%11 = getelementptr inbounds [7 x i8*], [7 x i8*]* %9, i64 0, i64 0
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8** %11
%12 = getelementptr inbounds i8*, i8** %11, i64 1
%13 = load %struct.ifreq*, %struct.ifreq** %7
%14 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %13, i32 0, i32 0
%15 = load i8*, i8** %14
store i8* %15, i8** %12
%16 = getelementptr inbounds i8*, i8** %12, i64 1
%17 = load i8*, i8** %5
store i8* %17, i8** %16
%18 = getelementptr inbounds i8*, i8** %16, i64 1
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8** %18
%19 = getelementptr inbounds i8*, i8** %18, i64 1
store i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8** %19
%20 = getelementptr inbounds i8*, i8** %19, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8** %20
%21 = getelementptr inbounds i8*, i8** %20, i64 1
store i8* null, i8** %21
%22 = bitcast [3 x i8*]* %10 to i8*
%23 = call i64 @setreuid(i32 0, i32 0)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %3
%26 = load %struct.output*, %struct.output** %6
%27 = call i32 @output_errno(%struct.output* %26, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0))
store i32 -1, i32* %4
br label %77
28: ; preds = %3
%29 = call i32 @umask(i32 0)
%30 = call i32 @mkdir(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i32 493)
%31 = call i32 @mk_node(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0), i32 10, i32 200)
%32 = getelementptr inbounds [3 x i8*], [3 x i8*]* %10, i64 0, i64 0
%33 = load %struct.output*, %struct.output** %6
%34 = call i64 @do_exec(i8** %32, i32 0, %struct.output* %33)
%35 = load i32, i32* @O_RDWR
%36 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0), i32 %35)
store i32 %36, i32* %8
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %28
%39 = load %struct.output*, %struct.output** %6
%40 = call i32 @output_errno(%struct.output* %39, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.9, i64 0, i64 0))
store i32 -1, i32* %4
br label %77
41: ; preds = %28
%42 = load %struct.ifreq*, %struct.ifreq** %7
%43 = call i32 @memset(%struct.ifreq* %42, i32 0, i32 16)
%44 = load i32, i32* @IFF_TAP
%45 = load i32, i32* @IFF_NO_PI
%46 = or i32 %44, %45
%47 = load %struct.ifreq*, %struct.ifreq** %7
%48 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %47, i32 0, i32 1
store i32 %46, i32* %48
%49 = load %struct.ifreq*, %struct.ifreq** %7
%50 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %49, i32 0, i32 0
%51 = load i8*, i8** %50
%52 = getelementptr inbounds i8, i8* %51, i64 0
store i8 0, i8* %52
%53 = load i32, i32* %8
%54 = load i32, i32* @TUNSETIFF
%55 = load %struct.ifreq*, %struct.ifreq** %7
%56 = bitcast %struct.ifreq* %55 to i8*
%57 = call i64 @ioctl(i32 %53, i32 %54, i8* %56)
%58 = icmp slt i64 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %41
%60 = load %struct.output*, %struct.output** %6
%61 = call i32 @output_errno(%struct.output* %60, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.10, i64 0, i64 0))
store i32 -1, i32* %4
br label %77
62: ; preds = %41
%63 = load i8*, i8** %5
%64 = load i8, i8* %63
%65 = sext i8 %64 to i32
%66 = icmp ne i32 %65, 0
br i1 %66, label %67, label %73
67: ; preds = %62
%68 = getelementptr inbounds [7 x i8*], [7 x i8*]* %9, i64 0, i64 0
%69 = load %struct.output*, %struct.output** %6
%70 = call i64 @do_exec(i8** %68, i32 1, %struct.output* %69)
%71 = icmp ne i64 %70, 0
br i1 %71, label %72, label %73
72: ; preds = %67
store i32 -1, i32* %4
br label %77
73: ; preds = %67, %62
%74 = load %struct.output*, %struct.output** %6
%75 = call i32 @forward_ip(%struct.output* %74)
%76 = load i32, i32* %8
store i32 %76, i32* %4
br label %77
77: ; preds = %73, %72, %59, %38, %25
%78 = load i32, i32* %4
ret i32 %78
}

LLVM IR (-O2):
%struct.output = type { i32 }
%struct.ifreq = type { i8*, i32 }
@.str = [9 x i8] c"ifconfig\00"
@.str.1 = [8 x i8] c"netmask\00"
@.str.2 = [16 x i8] c"255.255.255.255\00"
@.str.3 = [3 x i8] c"up\00"
@.str.4 = [9 x i8] c"modprobe\00"
@.str.5 = [4 x i8] c"tun\00"
@__const.do_tuntap_up.insmod_argv = [3 x i8*] [i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i32 0, i32 0), i8* null]
@.str.6 = [27 x i8] c"setreuid to root failed : \00"
@.str.7 = [9 x i8] c"/dev/net\00"
@.str.8 = [13 x i8] c"/dev/net/tun\00"
@O_RDWR = global i32 0
@.str.9 = [31 x i8] c"Opening /dev/net/tun failed : \00"
@IFF_TAP = global i32 0
@IFF_NO_PI = global i32 0
@TUNSETIFF = global i32 0
@.str.10 = [13 x i8] c"TUNSETIFF : \00"
define i32 @do_tuntap_up(i8* %0, %struct.output* %1, %struct.ifreq* %2) {
%4 = alloca [7 x i8*]
%5 = alloca [3 x i8*]
%6 = bitcast [7 x i8*]* %4 to i8*
%7 = getelementptr inbounds [7 x i8*], [7 x i8*]* %4, i64 0, i64 0
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8** %7
%8 = getelementptr inbounds [7 x i8*], [7 x i8*]* %4, i64 0, i64 1
%9 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
%10 = load i8*, i8** %9
store i8* %10, i8** %8
%11 = getelementptr inbounds [7 x i8*], [7 x i8*]* %4, i64 0, i64 2
store i8* %0, i8** %11
%12 = getelementptr inbounds [7 x i8*], [7 x i8*]* %4, i64 0, i64 3
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8** %12
%13 = getelementptr inbounds [7 x i8*], [7 x i8*]* %4, i64 0, i64 4
store i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8** %13
%14 = getelementptr inbounds [7 x i8*], [7 x i8*]* %4, i64 0, i64 5
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8** %14
%15 = getelementptr inbounds [7 x i8*], [7 x i8*]* %4, i64 0, i64 6
store i8* null, i8** %15
%16 = bitcast [3 x i8*]* %5 to i8*
%17 = tail call i64 @setreuid(i32 0, i32 0)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %3
%20 = tail call i32 @output_errno(%struct.output* %1, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0))
br label %53
21: ; preds = %3
%22 = tail call i32 @umask(i32 0)
%23 = tail call i32 @mkdir(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i32 493)
%24 = tail call i32 @mk_node(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0), i32 10, i32 200)
%25 = getelementptr inbounds [3 x i8*], [3 x i8*]* %5, i64 0, i64 0
%26 = call i64 @do_exec(i8** %25, i32 0, %struct.output* %1)
%27 = load i32, i32* @O_RDWR
%28 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0), i32 %27)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %21
%31 = call i32 @output_errno(%struct.output* %1, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.9, i64 0, i64 0))
br label %53
32: ; preds = %21
%33 = call i32 @memset(%struct.ifreq* %2, i32 0, i32 16)
%34 = load i32, i32* @IFF_TAP
%35 = load i32, i32* @IFF_NO_PI
%36 = or i32 %35, %34
%37 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 1
store i32 %36, i32* %37
%38 = load i8*, i8** %9
store i8 0, i8* %38
%39 = load i32, i32* @TUNSETIFF
%40 = bitcast %struct.ifreq* %2 to i8*
%41 = call i64 @ioctl(i32 %28, i32 %39, i8* %40)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %32
%44 = call i32 @output_errno(%struct.output* %1, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.10, i64 0, i64 0))
br label %53
45: ; preds = %32
%46 = load i8, i8* %0
%47 = icmp eq i8 %46, 0
br i1 %47, label %51, label %48
48: ; preds = %45
%49 = call i64 @do_exec(i8** %7, i32 1, %struct.output* %1)
%50 = icmp eq i64 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %48, %45
%52 = call i32 @forward_ip(%struct.output* %1)
br label %53
53: ; preds = %48, %51, %43, %30, %19
%54 = phi i32 [ -1, %19 ], [ -1, %30 ], [ -1, %43 ], [ %28, %51 ], [ -1, %48 ]
ret i32 %54
}