#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 ifreq {
  int ifr_flags;
  int /*<<< orphan*/ ifr_name;
};
typedef int /*<<< orphan*/ req;

/* Variables and functions */
int IFF_NO_PI;
int IFF_TUN;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TUNSETIFF;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int tun_open(char *tunname) {
  struct ifreq req;
  int dev;
  dev = open("/dev/net/tun", O_RDWR);
  if (dev == -1) {
    perror("dsptunnel: tun_open: open");
    return -1;
  }
  memset(&req, 0, sizeof(req));
  req.ifr_flags = IFF_TUN | IFF_NO_PI;
  strncpy(req.ifr_name, tunname, IFNAMSIZ);
  if (ioctl(dev, TUNSETIFF, &req) < 0) {
    perror("dsptunnel: tun_open: ioctl");
    close(dev);
    return -1;
  }
  return dev;
}


x86 assembly (clang, -O0):
<tun_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
lea    0xe84(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11a6 <tun_open+0x46>
lea    0xe78(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1225 <tun_open+0xc5>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
mov    0x2df2(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2dff(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x1c(%rbp),%edi
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    121f <tun_open+0xbf>
lea    0xe21(%rip),%rdi
call   1080 <perror@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1225 <tun_open+0xc5>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tun_open>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
lea    0xe84(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11eb <tun_open+0x8b>
mov    %eax,%ebx
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e25(%rip),%rax
mov    0x2e0e(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,0x8(%rsp)
mov    0xc(%rsp),%edi
mov    0x2e1b(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r15,%rdx
call   1050 <ioctl@plt>
test   %rax,%rax
js     11f9 <tun_open+0x99>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe1b(%rip),%rdi
call   1080 <perror@plt>
jmp    120c <tun_open+0xac>
lea    0xe27(%rip),%rdi
call   1080 <perror@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebx
jmp    11df <tun_open+0x7f>

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@.str = [13 x i8] c"/dev/net/tun\00"
@O_RDWR = global i32 0
@.str.1 = [26 x i8] c"dsptunnel: tun_open: open\00"
@IFF_TUN = global i32 0
@IFF_NO_PI = global i32 0
@IFNAMSIZ = global i32 0
@TUNSETIFF = global i32 0
@.str.2 = [27 x i8] c"dsptunnel: tun_open: ioctl\00"
define i32 @tun_open(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.ifreq
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i32, i32* @O_RDWR
%7 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %5
%8 = load i32, i32* %5
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %33
12: ; preds = %1
%13 = call i32 @memset(%struct.ifreq* %4, i32 0, i32 8)
%14 = load i32, i32* @IFF_TUN
%15 = load i32, i32* @IFF_NO_PI
%16 = or i32 %14, %15
%17 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 0
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 1
%19 = load i32, i32* %18
%20 = load i8*, i8** %3
%21 = load i32, i32* @IFNAMSIZ
%22 = call i32 @strncpy(i32 %19, i8* %20, i32 %21)
%23 = load i32, i32* %5
%24 = load i32, i32* @TUNSETIFF
%25 = call i64 @ioctl(i32 %23, i32 %24, %struct.ifreq* %4)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %12
%28 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%29 = load i32, i32* %5
%30 = call i32 @close(i32 %29)
store i32 -1, i32* %2
br label %33
31: ; preds = %12
%32 = load i32, i32* %5
store i32 %32, i32* %2
br label %33
33: ; preds = %31, %27, %10
%34 = load i32, i32* %2
ret i32 %34
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@.str = [13 x i8] c"/dev/net/tun\00"
@O_RDWR = global i32 0
@.str.1 = [26 x i8] c"dsptunnel: tun_open: open\00"
@IFF_TUN = global i32 0
@IFF_NO_PI = global i32 0
@IFNAMSIZ = global i32 0
@TUNSETIFF = global i32 0
@.str.2 = [27 x i8] c"dsptunnel: tun_open: ioctl\00"
define i32 @tun_open(i8* %0) {
%2 = alloca %struct.ifreq
%3 = bitcast %struct.ifreq* %2 to i8*
%4 = load i32, i32* @O_RDWR
%5 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %25
9: ; preds = %1
%10 = call i32 @memset(%struct.ifreq* %2, i32 0, i32 8)
%11 = load i32, i32* @IFF_TUN
%12 = load i32, i32* @IFF_NO_PI
%13 = or i32 %12, %11
%14 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 1
%16 = load i32, i32* %15
%17 = load i32, i32* @IFNAMSIZ
%18 = call i32 @strncpy(i32 %16, i8* %0, i32 %17)
%19 = load i32, i32* @TUNSETIFF
%20 = call i64 @ioctl(i32 %5, i32 %19, %struct.ifreq* %2)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %9
%23 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 @close(i32 %5)
br label %25
25: ; preds = %9, %22, %7
%26 = phi i32 [ -1, %7 ], [ -1, %22 ], [ %5, %9 ]
ret i32 %26
}