#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 /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int ioctl(int, int /*<<< orphan*/, void *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
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;
  }
  fprintf(stderr, "********TUNIP opened /dev/net/tun\n");
  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    0x2e62(%rip),%rax
mov    (%rax),%esi
lea    0xe71(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11ac <tun_alloc+0x3c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1272 <tun_alloc+0x102>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
lea    0xe51(%rip),%rsi
call   1080 <fprintf@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
cmpl   $0x0,-0x14(%rbp)
je     11ec <tun_alloc+0x7c>
mov    0x2df6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    11f8 <tun_alloc+0x88>
mov    0x2dc5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    0x2da6(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     122c <tun_alloc+0xbc>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2da3(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x24(%rbp),%edi
mov    0x2d8a(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    1260 <tun_alloc+0xf0>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1272 <tun_alloc+0x102>
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    0x2e64(%rip),%rax
mov    (%rax),%esi
lea    0xe73(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     11cb <tun_alloc+0x5b>
mov    %eax,%ebx
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
lea    0xe65(%rip),%rsi
call   1080 <fprintf@plt>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
test   %ebp,%ebp
jne    11d2 <tun_alloc+0x62>
mov    0x2def(%rip),%rax
jmp    11d9 <tun_alloc+0x69>
mov    $0xffffffff,%ebx
jmp    1235 <tun_alloc+0xc5>
mov    0x2dff(%rip),%rax
mov    (%rax),%eax
mov    0x2dc6(%rip),%rcx
or     (%rcx),%eax
mov    %eax,0x8(%rsp)
cmpb   $0x0,(%r14)
je     1203 <tun_alloc+0x93>
mov    0xc(%rsp),%edi
mov    0x2dcf(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    0x2db6(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1060 <ioctl@plt>
test   %eax,%eax
js     122a <tun_alloc+0xba>
mov    0xc(%rsp),%esi
mov    %r14,%rdi
call   1040 <strcpy@plt>
jmp    1235 <tun_alloc+0xc5>
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
@stderr = global i32 0
@.str.1 = [35 x i8] c"********TUNIP opened /dev/net/tun\0A\00"
@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 %54
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @memset(%struct.ifreq* %6, i32 0, i32 8)
%17 = load i32, i32* %5
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = load i32, i32* @IFF_TAP
br label %23
21: ; preds = %13
%22 = load i32, i32* @IFF_TUN
br label %23
23: ; preds = %21, %19
%24 = phi i32 [ %20, %19 ], [ %22, %21 ]
%25 = load i32, i32* @IFF_NO_PI
%26 = or i32 %24, %25
%27 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
store i32 %26, i32* %27
%28 = load i8*, i8** %4
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %38
32: ; preds = %23
%33 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%34 = load i32, i32* %33
%35 = load i8*, i8** %4
%36 = load i32, i32* @IFNAMSIZ
%37 = call i32 @strncpy(i32 %34, i8* %35, i32 %36)
br label %38
38: ; preds = %32, %23
%39 = load i32, i32* %7
%40 = load i32, i32* @TUNSETIFF
%41 = bitcast %struct.ifreq* %6 to i8*
%42 = call i32 @ioctl(i32 %39, i32 %40, i8* %41)
store i32 %42, i32* %8
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %38
%45 = load i32, i32* %7
%46 = call i32 @close(i32 %45)
%47 = load i32, i32* %8
store i32 %47, i32* %3
br label %54
48: ; preds = %38
%49 = load i8*, i8** %4
%50 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%51 = load i32, i32* %50
%52 = call i32 @strcpy(i8* %49, i32 %51)
%53 = load i32, i32* %7
store i32 %53, i32* %3
br label %54
54: ; preds = %48, %44, %12
%55 = load i32, i32* %3
ret i32 %55
}

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