#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 /*<<< orphan*/ F_SETFL;
int IFF_NO_PI;
int IFF_TAP;
int IFF_TUN;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ O_NONBLOCK;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TUNSETIFF;
int /*<<< orphan*/ close(int);
int errno;
int fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int ioctl(int, int /*<<< orphan*/, void *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int tun_alloc(char *dev, int *flags) {
  struct ifreq ifr;
  memset(&ifr, 0, sizeof(ifr));
  int fd, err;
  char *clonedev = "/dev/net/tun";
  if ((fd = open(clonedev, O_RDWR)) < 0) {
    return fd;
  }
  if (*dev) {
    strncpy(ifr.ifr_name, dev, IFNAMSIZ);
  }
  ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
  if ((err = ioctl(fd, TUNSETIFF, (void *)&ifr)) < 0) {
    ifr.ifr_flags = IFF_TAP;
    if ((err = ioctl(fd, TUNSETIFF, (void *)&ifr)) < 0) {
      close(fd);
      return err;
    }
  }
  if ((err = fcntl(fd, F_SETFL, O_NONBLOCK)) < 0) {
    int saved_errno = errno;
    close(fd);
    errno = saved_errno;
    return err;
  }
  strcpy(dev, ifr.ifr_name);
  return fd;
}


x86 assembly (clang, -O0):
<tun_alloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
lea    0xe69(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11c4 <tun_alloc+0x54>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12c4 <tun_alloc+0x154>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     11e6 <tun_alloc+0x76>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2de9(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    0x2dbb(%rip),%rax
mov    (%rax),%eax
mov    0x2da2(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1070 <ioctl@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    1261 <tun_alloc+0xf1>
mov    0x2dad(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2d8e(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1070 <ioctl@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    125c <tun_alloc+0xec>
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12c4 <tun_alloc+0x154>
jmp    1261 <tun_alloc+0xf1>
mov    -0x24(%rbp),%edi
mov    0x2d4d(%rip),%rax
mov    (%rax),%esi
mov    0x2d6c(%rip),%rax
mov    (%rax),%edx
call   1050 <fcntl@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    12b2 <tun_alloc+0x142>
mov    0x2d22(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
mov    -0x34(%rbp),%ecx
mov    0x2d0b(%rip),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12c4 <tun_alloc+0x154>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1040 <strcpy@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tun_alloc>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
lea    0xe67(%rip),%rdi
call   1090 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     125b <tun_alloc+0xeb>
cmpb   $0x0,(%r14)
je     11c3 <tun_alloc+0x53>
mov    0x4(%rsp),%edi
mov    0x2e0f(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    0x2dde(%rip),%rax
mov    0x2dc7(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,(%rsp)
mov    0x2de1(%rip),%rbp
mov    0x0(%rbp),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1070 <ioctl@plt>
test   %eax,%eax
jns    120d <tun_alloc+0x9d>
mov    0x2dd9(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    0x0(%rbp),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1070 <ioctl@plt>
test   %eax,%eax
js     1250 <tun_alloc+0xe0>
mov    0x2da4(%rip),%rax
mov    (%rax),%esi
mov    0x2dc3(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
call   1050 <fcntl@plt>
test   %eax,%eax
js     1238 <tun_alloc+0xc8>
mov    0x4(%rsp),%esi
mov    %r14,%rdi
call   1040 <strcpy@plt>
jmp    125b <tun_alloc+0xeb>
mov    %eax,%ebp
mov    0x2d6f(%rip),%r14
mov    (%r14),%r15d
mov    %ebx,%edi
call   1080 <close@plt>
mov    %r15d,(%r14)
jmp    1259 <tun_alloc+0xe9>
mov    %eax,%ebp
mov    %ebx,%edi
call   1080 <close@plt>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@.str = [13 x i8] c"/dev/net/tun\00"
@O_RDWR = global i32 0
@IFNAMSIZ = global i32 0
@IFF_TUN = global i32 0
@IFF_NO_PI = global i32 0
@TUNSETIFF = global i32 0
@IFF_TAP = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@errno = global i32 0
define i32 @tun_alloc(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca %struct.ifreq
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
%11 = call i32 @memset(%struct.ifreq* %6, i32 0, i32 8)
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8** %9
%12 = load i8*, i8** %9
%13 = load i32, i32* @O_RDWR
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %7
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = load i32, i32* %7
store i32 %17, i32* %3
br label %69
18: ; preds = %2
%19 = load i8*, i8** %4
%20 = load i8, i8* %19
%21 = icmp ne i8 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %18
%23 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%24 = load i32, i32* %23
%25 = load i8*, i8** %4
%26 = load i32, i32* @IFNAMSIZ
%27 = call i32 @strncpy(i32 %24, i8* %25, i32 %26)
br label %28
28: ; preds = %22, %18
%29 = load i32, i32* @IFF_TUN
%30 = load i32, i32* @IFF_NO_PI
%31 = or i32 %29, %30
%32 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
store i32 %31, i32* %32
%33 = load i32, i32* %7
%34 = load i32, i32* @TUNSETIFF
%35 = bitcast %struct.ifreq* %6 to i8*
%36 = call i32 @ioctl(i32 %33, i32 %34, i8* %35)
store i32 %36, i32* %8
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %51
38: ; preds = %28
%39 = load i32, i32* @IFF_TAP
%40 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
store i32 %39, i32* %40
%41 = load i32, i32* %7
%42 = load i32, i32* @TUNSETIFF
%43 = bitcast %struct.ifreq* %6 to i8*
%44 = call i32 @ioctl(i32 %41, i32 %42, i8* %43)
store i32 %44, i32* %8
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %50
46: ; preds = %38
%47 = load i32, i32* %7
%48 = call i32 @close(i32 %47)
%49 = load i32, i32* %8
store i32 %49, i32* %3
br label %69
50: ; preds = %38
br label %51
51: ; preds = %50, %28
%52 = load i32, i32* %7
%53 = load i32, i32* @F_SETFL
%54 = load i32, i32* @O_NONBLOCK
%55 = call i32 @fcntl(i32 %52, i32 %53, i32 %54)
store i32 %55, i32* %8
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %63
57: ; preds = %51
%58 = load i32, i32* @errno
store i32 %58, i32* %10
%59 = load i32, i32* %7
%60 = call i32 @close(i32 %59)
%61 = load i32, i32* %10
store i32 %61, i32* @errno
%62 = load i32, i32* %8
store i32 %62, i32* %3
br label %69
63: ; preds = %51
%64 = load i8*, i8** %4
%65 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%66 = load i32, i32* %65
%67 = call i32 @strcpy(i8* %64, i32 %66)
%68 = load i32, i32* %7
store i32 %68, i32* %3
br label %69
69: ; preds = %63, %57, %46, %16
%70 = load i32, i32* %3
ret i32 %70
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@.str = [13 x i8] c"/dev/net/tun\00"
@O_RDWR = global i32 0
@IFNAMSIZ = global i32 0
@IFF_TUN = global i32 0
@IFF_NO_PI = global i32 0
@TUNSETIFF = global i32 0
@IFF_TAP = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@errno = global i32 0
define i32 @tun_alloc(i8* %0, i32* nocapture readnone %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = call i32 @memset(%struct.ifreq* %3, i32 0, i32 8)
%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)
%8 = icmp slt i32 %7, 0
br i1 %8, label %44, label %9
9: ; preds = %2
%10 = load i8, i8* %0
%11 = icmp eq i8 %10, 0
br i1 %11, label %17, label %12
12: ; preds = %9
%13 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%14 = load i32, i32* %13
%15 = load i32, i32* @IFNAMSIZ
%16 = call i32 @strncpy(i32 %14, i8* %0, i32 %15)
br label %17
17: ; preds = %12, %9
%18 = load i32, i32* @IFF_TUN
%19 = load i32, i32* @IFF_NO_PI
%20 = or i32 %19, %18
%21 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
store i32 %20, i32* %21
%22 = load i32, i32* @TUNSETIFF
%23 = call i32 @ioctl(i32 %7, i32 %22, i8* %4)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %32
25: ; preds = %17
%26 = load i32, i32* @IFF_TAP
store i32 %26, i32* %21
%27 = load i32, i32* @TUNSETIFF
%28 = call i32 @ioctl(i32 %7, i32 %27, i8* %4)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = call i32 @close(i32 %7)
br label %44
32: ; preds = %25, %17
%33 = load i32, i32* @F_SETFL
%34 = load i32, i32* @O_NONBLOCK
%35 = call i32 @fcntl(i32 %7, i32 %33, i32 %34)
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %32
%38 = load i32, i32* @errno
%39 = call i32 @close(i32 %7)
store i32 %38, i32* @errno
br label %44
40: ; preds = %32
%41 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%42 = load i32, i32* %41
%43 = call i32 @strcpy(i8* %0, i32 %42)
br label %44
44: ; preds = %2, %40, %37, %30
%45 = phi i32 [ %28, %30 ], [ %35, %37 ], [ %7, %40 ], [ %7, %2 ]
ret i32 %45
}