#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 O_NONBLOCK;
int O_RDWR;
int /*<<< orphan*/ TUNSETIFF;
scalar_t__ ioctl(int, int /*<<< orphan*/, void *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, int);
int open_tap(const char *name) {
  struct ifreq ifr;
  int fd;
  if ((fd = open("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0) {
    perror("open /dev/net/tun");
    return -1;
  }
  memset(&ifr, 0, sizeof(ifr));
  ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
  strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name) - 1);
  if (ioctl(fd, TUNSETIFF, (void *)&ifr) < 0) {
    perror("TUNSETIFF");
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<open_tap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
mov    0x2e74(%rip),%rax
or     (%rax),%esi
lea    0xe90(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    119e <open_tap+0x4e>
lea    0xe73(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1211 <open_tap+0xc1>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e1b(%rip),%rax
mov    (%rax),%eax
mov    0x2dfa(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    -0x1c(%rbp),%edi
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    120b <open_tap+0xbb>
lea    0xe18(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1211 <open_tap+0xc1>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_tap>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e85(%rip),%rax
mov    0x2e76(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe90(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11df <open_tap+0x8f>
mov    %eax,%ebx
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e35(%rip),%rax
mov    0x2e16(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,0x8(%rsp)
mov    0xc(%rsp),%edi
mov    %r14,%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r15,%rdx
call   1050 <ioctl@plt>
test   %rax,%rax
js     11e8 <open_tap+0x98>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe1a(%rip),%rdi
jmp    11ef <open_tap+0x9f>
lea    0xe23(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11d3 <open_tap+0x83>

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@.str = [13 x i8] c"/dev/net/tun\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [18 x i8] c"open /dev/net/tun\00"
@IFF_TAP = global i32 0
@IFF_NO_PI = global i32 0
@TUNSETIFF = global i32 0
@.str.2 = [10 x i8] c"TUNSETIFF\00"
define i32 @open_tap(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.ifreq
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_NONBLOCK
%8 = or i32 %6, %7
%9 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %5
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %32
13: ; preds = %1
%14 = call i32 @memset(%struct.ifreq* %4, i32 0, i32 8)
%15 = load i32, i32* @IFF_TAP
%16 = load i32, i32* @IFF_NO_PI
%17 = or i32 %15, %16
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 0
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 1
%20 = load i32, i32* %19
%21 = load i8*, i8** %3
%22 = call i32 @strncpy(i32 %20, i8* %21, i32 3)
%23 = load i32, i32* %5
%24 = load i32, i32* @TUNSETIFF
%25 = bitcast %struct.ifreq* %4 to i8*
%26 = call i64 @ioctl(i32 %23, i32 %24, i8* %25)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %13
%29 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %32
30: ; preds = %13
%31 = load i32, i32* %5
store i32 %31, i32* %2
br label %32
32: ; preds = %30, %28, %11
%33 = load i32, i32* %2
ret i32 %33
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@.str = [13 x i8] c"/dev/net/tun\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [18 x i8] c"open /dev/net/tun\00"
@IFF_TAP = global i32 0
@IFF_NO_PI = global i32 0
@TUNSETIFF = global i32 0
@.str.2 = [10 x i8] c"TUNSETIFF\00"
define i32 @open_tap(i8* %0) {
%2 = alloca %struct.ifreq
%3 = bitcast %struct.ifreq* %2 to i8*
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_NONBLOCK
%6 = or i32 %5, %4
%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 %9, label %11
9: ; preds = %1
%10 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %25
11: ; preds = %1
%12 = call i32 @memset(%struct.ifreq* %2, i32 0, i32 8)
%13 = load i32, i32* @IFF_TAP
%14 = load i32, i32* @IFF_NO_PI
%15 = or i32 %14, %13
%16 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 1
%18 = load i32, i32* %17
%19 = call i32 @strncpy(i32 %18, i8* %0, i32 3)
%20 = load i32, i32* @TUNSETIFF
%21 = call i64 @ioctl(i32 %7, i32 %20, i8* %3)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %11
%24 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %25
25: ; preds = %11, %23, %9
%26 = phi i32 [ -1, %9 ], [ -1, %23 ], [ %7, %11 ]
ret i32 %26
}