#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;
};

/* Variables and functions */
int IFF_NO_PI;
int IFF_TAP;
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, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int init_tap_device(char *name) {
  int fd, ret;
  struct ifreq ifr;
  if (name == NULL) {
    return -1;
  }
  fd = open("/dev/net/tun", O_RDWR);
  if (fd < 0) {
    return -2;
  }
  memset(&ifr, 0x0, sizeof(struct ifreq));
  ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
  strncpy(ifr.ifr_name, name, IFNAMSIZ);
  ret = ioctl(fd, TUNSETIFF, (void *)&ifr);
  if (ret != 0) {
    close(fd);
    return -3;
  }
  return fd;
}


x86 assembly (clang, -O0):
<init_tap_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1173 <init_tap_device+0x23>
movl   $0xffffffff,-0x4(%rbp)
jmp    1217 <init_tap_device+0xc7>
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
lea    0xe7d(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11a1 <init_tap_device+0x51>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1217 <init_tap_device+0xc7>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e20(%rip),%rax
mov    (%rax),%eax
mov    0x2df7(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2dfc(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x14(%rbp),%edi
mov    0x2de3(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     1211 <init_tap_device+0xc1>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1217 <init_tap_device+0xc7>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_tap_device>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
test   %rdi,%rdi
je     11e4 <init_tap_device+0x94>
mov    %rdi,%r14
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
lea    0xe8b(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11eb <init_tap_device+0x9b>
mov    %eax,%ebx
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e3d(%rip),%rax
mov    0x2e16(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,0x8(%rsp)
mov    0xc(%rsp),%edi
mov    0x2e1b(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r15,%rdx
call   1050 <ioctl@plt>
test   %eax,%eax
je     11f0 <init_tap_device+0xa0>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xfffffffd,%ebx
jmp    11f0 <init_tap_device+0xa0>
mov    $0xffffffff,%ebx
jmp    11f0 <init_tap_device+0xa0>
mov    $0xfffffffe,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
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_NO_PI = global i32 0
@IFNAMSIZ = global i32 0
@TUNSETIFF = global i32 0
define i32 @init_tap_device(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.ifreq
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %10
9: ; preds = %1
store i32 -1, i32* %2
br label %38
10: ; preds = %1
%11 = load i32, i32* @O_RDWR
%12 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %10
store i32 -2, i32* %2
br label %38
16: ; preds = %10
%17 = call i32 @memset(%struct.ifreq* %6, i32 0, i32 8)
%18 = load i32, i32* @IFF_TAP
%19 = load i32, i32* @IFF_NO_PI
%20 = or i32 %18, %19
%21 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%23 = load i32, i32* %22
%24 = load i8*, i8** %3
%25 = load i32, i32* @IFNAMSIZ
%26 = call i32 @strncpy(i32 %23, i8* %24, i32 %25)
%27 = load i32, i32* %4
%28 = load i32, i32* @TUNSETIFF
%29 = bitcast %struct.ifreq* %6 to i8*
%30 = call i32 @ioctl(i32 %27, i32 %28, i8* %29)
store i32 %30, i32* %5
%31 = load i32, i32* %5
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %16
%34 = load i32, i32* %4
%35 = call i32 @close(i32 %34)
store i32 -3, i32* %2
br label %38
36: ; preds = %16
%37 = load i32, i32* %4
store i32 %37, i32* %2
br label %38
38: ; preds = %36, %33, %15, %9
%39 = load i32, i32* %2
ret i32 %39
}

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_NO_PI = global i32 0
@IFNAMSIZ = global i32 0
@TUNSETIFF = global i32 0
define i32 @init_tap_device(i8* %0) {
%2 = alloca %struct.ifreq
%3 = bitcast %struct.ifreq* %2 to i8*
%4 = icmp eq i8* %0, null
br i1 %4, label %24, label %5
5: ; preds = %1
%6 = load i32, i32* @O_RDWR
%7 = tail 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 %24, label %9
9: ; preds = %5
%10 = call i32 @memset(%struct.ifreq* %2, i32 0, i32 8)
%11 = load i32, i32* @IFF_TAP
%12 = load i32, i32* @IFF_NO_PI
%13 = or i32 %12, %11
%14 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
store i32 %13, i32* %14
%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)
%19 = load i32, i32* @TUNSETIFF
%20 = call i32 @ioctl(i32 %7, i32 %19, i8* %3)
%21 = icmp eq i32 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %9
%23 = call i32 @close(i32 %7)
br label %24
24: ; preds = %9, %5, %1, %22
%25 = phi i32 [ -3, %22 ], [ -1, %1 ], [ -2, %5 ], [ %7, %9 ]
ret i32 %25
}