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

/* Variables and functions */
int IFF_NO_PI;
int IFF_TUN;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TUNSETIFF;
scalar_t__ ioctl(int, int /*<<< orphan*/, void *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int tap_open(char *name) {
  int fd;
  struct ifreq ifr;
  fd = open("/dev/net/tun", O_RDWR);
  if (fd < 0) {
    return -1;
  }
  memset(&ifr, 0, sizeof(ifr));
  ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
  if (name) {
    strncpy(ifr.ifr_name, name, IFNAMSIZ);
  }
  if (ioctl(fd, TUNSETIFF, (void *)&ifr) < 0) {
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<tap_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e95(%rip),%rax
mov    (%rax),%esi
lea    0xea4(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    117a <tap_open+0x3a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f0 <tap_open+0xb0>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e37(%rip),%rax
mov    (%rax),%eax
mov    0x2e1e(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11bf <tap_open+0x7f>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e20(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x14(%rbp),%edi
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    11ea <tap_open+0xaa>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f0 <tap_open+0xb0>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tap_open>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e9a(%rip),%rax
mov    (%rax),%esi
lea    0xea9(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11c0 <tap_open+0x80>
mov    %eax,%ebx
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e50(%rip),%rax
mov    0x2e39(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,(%rsp)
test   %r14,%r14
je     11a0 <tap_open+0x60>
mov    0x4(%rsp),%edi
mov    0x2e42(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1050 <ioctl@plt>
test   %rax,%rax
mov    $0xffffffff,%eax
cmovs  %eax,%ebx
jmp    11c5 <tap_open+0x85>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@.str = [13 x i8] c"/dev/net/tun\00"
@O_RDWR = global i32 0
@IFF_TUN = global i32 0
@IFF_NO_PI = global i32 0
@IFNAMSIZ = global i32 0
@TUNSETIFF = global i32 0
define i32 @tap_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 slt i32 %5, 0
br i1 %6, label %24, label %7
7: ; preds = %1
%8 = call i32 @memset(%struct.ifreq* %2, i32 0, i32 8)
%9 = load i32, i32* @IFF_TUN
%10 = load i32, i32* @IFF_NO_PI
%11 = or i32 %10, %9
%12 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
store i32 %11, i32* %12
%13 = icmp eq i8* %0, null
br i1 %13, label %19, label %14
14: ; preds = %7
%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)
br label %19
19: ; preds = %14, %7
%20 = load i32, i32* @TUNSETIFF
%21 = call i64 @ioctl(i32 %5, i32 %20, i8* %3)
%22 = icmp slt i64 %21, 0
%23 = select i1 %22, i32 -1, i32 %5
br label %24
24: ; preds = %19, %1
%25 = phi i32 [ -1, %1 ], [ %23, %19 ]
ret i32 %25
}