#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*/ IFNAMSIZ;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int tun_alloc(char *dev, int flags) {
  struct ifreq ifr;
  int fd, err;
  char *clonedev = "/dev/net/tun";
  if ((fd = open(clonedev, O_RDWR)) < 0) {
    return fd;
  }
  memset(&ifr, 0, sizeof(ifr));
  ifr.ifr_flags = flags;
  if (*dev) {
    strncpy(ifr.ifr_name, dev, IFNAMSIZ);
  }
}


x86 assembly (clang, -O0):
<tun_alloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    0xeba(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    0x2e93(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    1173 <tun_alloc+0x43>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ab <tun_alloc+0x7b>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     11ab <tun_alloc+0x7b>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e34(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tun_alloc>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
mov    0x2ea3(%rip),%rax
mov    (%rax),%esi
lea    0xeb2(%rip),%rdi
call   1050 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1189 <tun_alloc+0x59>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    %r14d,0x8(%rsp)
cmpb   $0x0,(%rbx)
je     1189 <tun_alloc+0x59>
mov    0xc(%rsp),%edi
mov    0x2e59(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rsi
call   1030 <strncpy@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
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
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*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8** %9
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_RDWR
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %7
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = load i32, i32* %7
store i32 %15, i32* %3
br label %29
16: ; preds = %2
%17 = call i32 @memset(%struct.ifreq* %6, i32 0, i32 8)
%18 = load i32, i32* %5
%19 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
store i32 %18, i32* %19
%20 = load i8*, i8** %4
%21 = load i8, i8* %20
%22 = icmp ne i8 %21, 0
br i1 %22, label %23, label %29
23: ; preds = %16
%24 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%25 = load i32, i32* %24
%26 = load i8*, i8** %4
%27 = load i32, i32* @IFNAMSIZ
%28 = call i32 @strncpy(i32 %25, i8* %26, i32 %27)
br label %29
29: ; preds = %14, %23, %16
%30 = load i32, i32* %3
ret i32 %30
}

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
define i32 @tun_alloc(i8* %0, i32 %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = load i32, i32* @O_RDWR
%6 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %18, label %8
8: ; preds = %2
%9 = call i32 @memset(%struct.ifreq* %3, i32 0, i32 8)
%10 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
store i32 %1, i32* %10
%11 = load i8, i8* %0
%12 = icmp eq i8 %11, 0
br i1 %12, label %18, label %13
13: ; preds = %8
%14 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%15 = load i32, i32* %14
%16 = load i32, i32* @IFNAMSIZ
%17 = call i32 @strncpy(i32 %15, i8* %0, i32 %16)
br label %18
18: ; preds = %8, %13, %2
ret i32 %6
}