#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 /*<<< orphan*/ IFNAMSIZ;
int O_CLOEXEC;
int 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);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *,
                           int /*<<< orphan*/);
int tap_alloc(const char *dev) {
  struct ifreq ifr;
  int fd, err;
  if ((fd = open("/dev/net/tun", O_RDWR | O_CLOEXEC)) < 0) {
    perror("could not open /dev/net/tun");
    return -1;
  }
  memset(&ifr, 0, sizeof(ifr));
  ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
  if (*dev)
    strncpy(ifr.ifr_name, dev, IFNAMSIZ);
  if ((err = ioctl(fd, TUNSETIFF, (void *)&ifr)) < 0) {
    perror("could not get tap device");
    close(fd);
    return err;
  }
  return fd;
}


x86 assembly (clang, -O0):
<tap_alloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
mov    0x2e34(%rip),%rax
or     (%rax),%esi
lea    0xe8a(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11ae <tap_alloc+0x4e>
lea    0xe63(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123b <tap_alloc+0xdb>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e13(%rip),%rax
mov    (%rax),%eax
mov    0x2dea(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     11f5 <tap_alloc+0x95>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2de2(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x1c(%rbp),%edi
mov    0x2dc9(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    1235 <tap_alloc+0xd5>
lea    0xdff(%rip),%rdi
call   1080 <perror@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    123b <tap_alloc+0xdb>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tap_alloc>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e76(%rip),%rax
mov    0x2e37(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe8b(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11f5 <tap_alloc+0x95>
mov    %eax,%ebx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e31(%rip),%rax
mov    0x2e0a(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,0x8(%rsp)
cmpb   $0x0,(%r14)
je     11d1 <tap_alloc+0x71>
mov    0xc(%rsp),%edi
mov    0x2e09(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1050 <ioctl@plt>
test   %eax,%eax
js     1208 <tap_alloc+0xa8>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe04(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11ea <tap_alloc+0x8a>
mov    %eax,%ebp
lea    0xe0b(%rip),%rdi
call   1080 <perror@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %ebp,%ebx
jmp    11ea <tap_alloc+0x8a>

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

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@.str = [13 x i8] c"/dev/net/tun\00"
@O_RDWR = global i32 0
@O_CLOEXEC = global i32 0
@.str.1 = [28 x i8] c"could not open /dev/net/tun\00"
@IFF_TAP = global i32 0
@IFF_NO_PI = global i32 0
@IFNAMSIZ = global i32 0
@TUNSETIFF = global i32 0
@.str.2 = [25 x i8] c"could not get tap device\00"
define i32 @tap_alloc(i8* %0) {
%2 = alloca %struct.ifreq
%3 = bitcast %struct.ifreq* %2 to i8*
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_CLOEXEC
%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 ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %31
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 = load i8, i8* %0
%18 = icmp eq i8 %17, 0
br i1 %18, label %24, label %19
19: ; preds = %11
%20 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, 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, %11
%25 = load i32, i32* @TUNSETIFF
%26 = call i32 @ioctl(i32 %7, i32 %25, i8* %3)
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%30 = call i32 @close(i32 %7)
br label %31
31: ; preds = %24, %28, %9
%32 = phi i32 [ -1, %9 ], [ %26, %28 ], [ %7, %24 ]
ret i32 %32
}