#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_TAP;
int IFF_TUN;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TUNSETIFF;
int /*<<< orphan*/ close(int);
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 tap) {
  struct ifreq ifr;
  int fd, err;
  if ((fd = open("/dev/net/tun", O_RDWR)) < 0) {
    return -1;
  }
  memset(&ifr, 0, sizeof(ifr));
  ifr.ifr_flags = (tap ? IFF_TAP : IFF_TUN) | IFF_NO_PI;
  if (*dev != 0)
    strncpy(ifr.ifr_name, dev, IFNAMSIZ);
  if ((err = ioctl(fd, TUNSETIFF, (void *)&ifr)) < 0) {
    close(fd);
    return err;
  }
  strcpy(dev, ifr.ifr_name);
  return fd;
}


x86 assembly (clang, -O0):
<tun_alloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
lea    0xe81(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    119c <tun_alloc+0x3c>
movl   $0xffffffff,-0x4(%rbp)
jmp    124d <tun_alloc+0xed>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
cmpl   $0x0,-0x14(%rbp)
je     11c7 <tun_alloc+0x67>
mov    0x2e1b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    11d3 <tun_alloc+0x73>
mov    0x2df2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    0x2dd3(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1207 <tun_alloc+0xa7>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2dd0(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x24(%rbp),%edi
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    123b <tun_alloc+0xdb>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    124d <tun_alloc+0xed>
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    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tun_alloc>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
lea    0xe83(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11a6 <tun_alloc+0x46>
mov    %eax,%ebx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
test   %ebp,%ebp
jne    11ad <tun_alloc+0x4d>
mov    0x2e1c(%rip),%rax
jmp    11b4 <tun_alloc+0x54>
mov    $0xffffffff,%ebx
jmp    1210 <tun_alloc+0xb0>
mov    0x2e24(%rip),%rax
mov    (%rax),%eax
mov    0x2df3(%rip),%rcx
or     (%rcx),%eax
mov    %eax,0x8(%rsp)
cmpb   $0x0,(%r14)
je     11de <tun_alloc+0x7e>
mov    0xc(%rsp),%edi
mov    0x2dfc(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    0x2de3(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1060 <ioctl@plt>
test   %eax,%eax
js     1205 <tun_alloc+0xa5>
mov    0xc(%rsp),%esi
mov    %r14,%rdi
call   1040 <strcpy@plt>
jmp    1210 <tun_alloc+0xb0>
mov    %eax,%ebp
mov    %ebx,%edi
call   1070 <close@plt>
mov    %ebp,%ebx
mov    %ebx,%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
@IFF_TAP = global i32 0
@IFF_TUN = global i32 0
@IFF_NO_PI = global i32 0
@IFNAMSIZ = global i32 0
@TUNSETIFF = 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
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i32, i32* @O_RDWR
%10 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %7
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 -1, i32* %3
br label %52
13: ; preds = %2
%14 = call i32 @memset(%struct.ifreq* %6, i32 0, i32 8)
%15 = load i32, i32* %5
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = load i32, i32* @IFF_TAP
br label %21
19: ; preds = %13
%20 = load i32, i32* @IFF_TUN
br label %21
21: ; preds = %19, %17
%22 = phi i32 [ %18, %17 ], [ %20, %19 ]
%23 = load i32, i32* @IFF_NO_PI
%24 = or i32 %22, %23
%25 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
store i32 %24, i32* %25
%26 = load i8*, i8** %4
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %36
30: ; preds = %21
%31 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%32 = load i32, i32* %31
%33 = load i8*, i8** %4
%34 = load i32, i32* @IFNAMSIZ
%35 = call i32 @strncpy(i32 %32, i8* %33, i32 %34)
br label %36
36: ; preds = %30, %21
%37 = load i32, i32* %7
%38 = load i32, i32* @TUNSETIFF
%39 = bitcast %struct.ifreq* %6 to i8*
%40 = call i32 @ioctl(i32 %37, i32 %38, i8* %39)
store i32 %40, i32* %8
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %36
%43 = load i32, i32* %7
%44 = call i32 @close(i32 %43)
%45 = load i32, i32* %8
store i32 %45, i32* %3
br label %52
46: ; preds = %36
%47 = load i8*, i8** %4
%48 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%49 = load i32, i32* %48
%50 = call i32 @strcpy(i8* %47, i32 %49)
%51 = load i32, i32* %7
store i32 %51, i32* %3
br label %52
52: ; preds = %46, %42, %12
%53 = load i32, i32* %3
ret i32 %53
}

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