#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 /*<<< orphan*/ LOG_INFO;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TUNSETIFF;
int /*<<< orphan*/ TUNSETPERSIST;
int /*<<< orphan*/ fatal(char *);
scalar_t__ ioctl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *,
                           int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char const *);
int alloc_tap(const char *dev) {
  const char *clonedev = "/dev/net/tun";
  struct ifreq ifr;
  int persist = 1;
  int fd;
  fd = open(clonedev, O_RDWR);
  if (fd == -1)
    fatal("Failed to open /dev/net/tun");
  memset(&ifr, 0, sizeof(ifr));
  ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
  strncpy(ifr.ifr_name, dev, IFNAMSIZ);
  if (ioctl(fd, TUNSETIFF, (void *)&ifr) < 0)
    fatal("TUNSETIFF failed");
  if (ioctl(fd, TUNSETPERSIST, persist) < 0)
    fatal("TUNSETPERSIST failed");
  syslog(LOG_INFO, "successfully created TAP device %s", dev);
  return fd;
}


x86 assembly (clang, -O0):
<alloc_tap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe9c(%rip),%rax
mov    %rax,-0x10(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11a9 <alloc_tap+0x49>
lea    0xe5c(%rip),%rdi
call   1080 <fatal@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
mov    0x2ddf(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    0x2df4(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%edi
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rax
mov    %eax,%edx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    1210 <alloc_tap+0xb0>
lea    0xe11(%rip),%rdi
call   1080 <fatal@plt>
mov    -0x20(%rbp),%edi
mov    0x2da6(%rip),%rax
mov    (%rax),%esi
mov    -0x1c(%rbp),%edx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    123a <alloc_tap+0xda>
lea    0xdf8(%rip),%rdi
call   1080 <fatal@plt>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdf4(%rip),%rsi
call   1060 <syslog@plt>
mov    -0x20(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<alloc_tap>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
lea    0xe94(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1193 <alloc_tap+0x33>
lea    0xe72(%rip),%rdi
call   1080 <fatal@plt>
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e2a(%rip),%rax
mov    0x2df3(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,0x8(%rsp)
mov    0xc(%rsp),%edi
mov    0x2e08(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %ebx,%edx
call   1050 <ioctl@plt>
test   %rax,%rax
jns    11f5 <alloc_tap+0x95>
lea    0xe2c(%rip),%rdi
call   1080 <fatal@plt>
mov    0x2dc4(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    $0x1,%edx
call   1050 <ioctl@plt>
test   %rax,%rax
jns    121b <alloc_tap+0xbb>
lea    0xe17(%rip),%rdi
call   1080 <fatal@plt>
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
lea    0xe17(%rip),%rsi
mov    %r14,%rdx
call   1060 <syslog@plt>
mov    %ebp,%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
@.str.1 = [28 x i8] c"Failed to 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 = [17 x i8] c"TUNSETIFF failed\00"
@TUNSETPERSIST = global i32 0
@.str.3 = [21 x i8] c"TUNSETPERSIST failed\00"
@LOG_INFO = global i32 0
@.str.4 = [35 x i8] c"successfully created TAP device %s\00"
define i32 @alloc_tap(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca %struct.ifreq
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %2
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8** %3
store i32 1, i32* %5
%7 = load i8*, i8** %3
%8 = load i32, i32* @O_RDWR
%9 = call i32 @open(i8* %7, i32 %8)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = call i32 @fatal(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %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 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 1
%21 = load i32, i32* %20
%22 = load i8*, i8** %2
%23 = load i32, i32* @IFNAMSIZ
%24 = call i32 @strncpy(i32 %21, i8* %22, i32 %23)
%25 = load i32, i32* %6
%26 = load i32, i32* @TUNSETIFF
%27 = bitcast %struct.ifreq* %4 to i8*
%28 = ptrtoint i8* %27 to i32
%29 = call i64 @ioctl(i32 %25, i32 %26, i32 %28)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %14
%32 = call i32 @fatal(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %33
33: ; preds = %31, %14
%34 = load i32, i32* %6
%35 = load i32, i32* @TUNSETPERSIST
%36 = load i32, i32* %5
%37 = call i64 @ioctl(i32 %34, i32 %35, i32 %36)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %33
%40 = call i32 @fatal(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %41
41: ; preds = %39, %33
%42 = load i32, i32* @LOG_INFO
%43 = load i8*, i8** %2
%44 = call i32 @syslog(i32 %42, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i8* %43)
%45 = load i32, i32* %6
ret i32 %45
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@.str = [13 x i8] c"/dev/net/tun\00"
@O_RDWR = global i32 0
@.str.1 = [28 x i8] c"Failed to 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 = [17 x i8] c"TUNSETIFF failed\00"
@TUNSETPERSIST = global i32 0
@.str.3 = [21 x i8] c"TUNSETPERSIST failed\00"
@LOG_INFO = global i32 0
@.str.4 = [35 x i8] c"successfully created TAP device %s\00"
define i32 @alloc_tap(i8* %0) {
%2 = alloca %struct.ifreq
%3 = bitcast %struct.ifreq* %2 to i8*
%4 = load i32, i32* @O_RDWR
%5 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 @fatal(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %9
9: ; preds = %7, %1
%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 = ptrtoint %struct.ifreq* %2 to i64
%21 = trunc i64 %20 to i32
%22 = call i64 @ioctl(i32 %5, i32 %19, i32 %21)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %9
%25 = call i32 @fatal(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %26
26: ; preds = %24, %9
%27 = load i32, i32* @TUNSETPERSIST
%28 = call i64 @ioctl(i32 %5, i32 %27, i32 1)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = call i32 @fatal(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %32
32: ; preds = %30, %26
%33 = load i32, i32* @LOG_INFO
%34 = call i32 @syslog(i32 %33, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i8* %0)
ret i32 %5
}