#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_VNET_HDR;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TUNSETIFF;
int /*<<< orphan*/ TUNSETNOCSUM;
int /*<<< orphan*/ TUNSETOFFLOAD;
int TUN_F_CSUM;
int TUN_F_TSO4;
int TUN_F_TSO6;
int TUN_F_TSO_ECN;
int /*<<< orphan*/ err(int, char *);
scalar_t__ ioctl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ memcpy(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int open_or_die(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int get_tun_device(char tapif[IFNAMSIZ]) {
  struct ifreq ifr;
  int netfd;
  memset(&ifr, 0, sizeof(ifr));
  netfd = open_or_die("/dev/net/tun", O_RDWR);
  ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_VNET_HDR;
  strcpy(ifr.ifr_name, "tap%d");
  if (ioctl(netfd, TUNSETIFF, &ifr) != 0)
    err(1, "configuring /dev/net/tun");
  if (ioctl(netfd, TUNSETOFFLOAD,
            TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6 | TUN_F_TSO_ECN) != 0)
    err(1, "Could not set features for tun device");
  ioctl(netfd, TUNSETNOCSUM, 1);
  memcpy(tapif, ifr.ifr_name, IFNAMSIZ);
  return netfd;
}


x86 assembly (clang, -O0):
<get_tun_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
lea    0xe86(%rip),%rdi
call   1060 <open_or_die@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e25(%rip),%rax
mov    (%rax),%eax
mov    0x2ddc(%rip),%rcx
or     (%rcx),%eax
mov    0x2ddb(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%edi
lea    0xe44(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x14(%rbp),%edi
mov    0x2de5(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
mov    $0x0,%al
call   1050 <ioctl@plt>
cmp    $0x0,%rax
je     11f3 <get_tun_device+0x93>
mov    $0x1,%edi
lea    0xe18(%rip),%rsi
call   1080 <err@plt>
mov    -0x14(%rbp),%edi
mov    0x2da3(%rip),%rax
mov    (%rax),%esi
mov    0x2da2(%rip),%rax
mov    (%rax),%edx
mov    0x2dd9(%rip),%rax
or     (%rax),%edx
mov    0x2de0(%rip),%rax
or     (%rax),%edx
mov    0x2d77(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1050 <ioctl@plt>
cmp    $0x0,%rax
je     1245 <get_tun_device+0xe5>
mov    $0x1,%edi
lea    0xddf(%rip),%rsi
call   1080 <err@plt>
mov    -0x14(%rbp),%edi
mov    0x2d81(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edx
mov    $0x0,%al
call   1050 <ioctl@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    0x2d4d(%rip),%rax
mov    (%rax),%edx
call   1070 <memcpy@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_tun_device>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
lea    0xe83(%rip),%rdi
call   1060 <open_or_die@plt>
mov    %eax,%ebp
mov    0x2e23(%rip),%rax
mov    0x2ddc(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2dd9(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x8(%rsp)
mov    0xc(%rsp),%edi
lea    0xe40(%rip),%rsi
call   1030 <strcpy@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <ioctl@plt>
test   %rax,%rax
je     11f0 <get_tun_device+0x90>
lea    0xe20(%rip),%rsi
mov    $0x1,%edi
call   1080 <err@plt>
mov    0x2db1(%rip),%rax
mov    0x2dea(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d97(%rip),%rax
mov    0x2de8(%rip),%rcx
or     (%rcx),%edx
mov    (%rax),%esi
mov    0x2d7d(%rip),%rax
or     (%rax),%edx
mov    %ebp,%edi
xor    %eax,%eax
call   1050 <ioctl@plt>
test   %rax,%rax
je     123c <get_tun_device+0xdc>
lea    0xded(%rip),%rsi
mov    $0x1,%edi
call   1080 <err@plt>
mov    0x2d8d(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    $0x1,%edx
xor    %eax,%eax
call   1050 <ioctl@plt>
mov    0xc(%rsp),%esi
mov    0x2d5a(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
call   1070 <memcpy@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@IFNAMSIZ = global i32 0
@.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
@IFF_VNET_HDR = global i32 0
@.str.1 = [6 x i8] c"tap%d\00"
@TUNSETIFF = global i32 0
@.str.2 = [25 x i8] c"configuring /dev/net/tun\00"
@TUNSETOFFLOAD = global i32 0
@TUN_F_CSUM = global i32 0
@TUN_F_TSO4 = global i32 0
@TUN_F_TSO6 = global i32 0
@TUN_F_TSO_ECN = global i32 0
@.str.3 = [38 x i8] c"Could not set features for tun device\00"
@TUNSETNOCSUM = global i32 0
define i32 @get_tun_device(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.ifreq
%4 = alloca i32
store i8* %0, i8** %2
%5 = load i32, i32* @IFNAMSIZ
%6 = zext i32 %5 to i64
%7 = call i32 @memset(%struct.ifreq* %3, i32 0, i32 8)
%8 = load i32, i32* @O_RDWR
%9 = call i32 @open_or_die(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %4
%10 = load i32, i32* @IFF_TAP
%11 = load i32, i32* @IFF_NO_PI
%12 = or i32 %10, %11
%13 = load i32, i32* @IFF_VNET_HDR
%14 = or i32 %12, %13
%15 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i32 0, i32 0
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i32 0, i32 1
%17 = load i32, i32* %16
%18 = call i32 @strcpy(i32 %17, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* %4
%20 = load i32, i32* @TUNSETIFF
%21 = call i64 (i32, i32, ...) @ioctl(i32 %19, i32 %20, %struct.ifreq* %3)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %1
%24 = call i32 @err(i32 1, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %25
25: ; preds = %23, %1
%26 = load i32, i32* %4
%27 = load i32, i32* @TUNSETOFFLOAD
%28 = load i32, i32* @TUN_F_CSUM
%29 = load i32, i32* @TUN_F_TSO4
%30 = or i32 %28, %29
%31 = load i32, i32* @TUN_F_TSO6
%32 = or i32 %30, %31
%33 = load i32, i32* @TUN_F_TSO_ECN
%34 = or i32 %32, %33
%35 = call i64 (i32, i32, ...) @ioctl(i32 %26, i32 %27, i32 %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %25
%38 = call i32 @err(i32 1, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
br label %39
39: ; preds = %37, %25
%40 = load i32, i32* %4
%41 = load i32, i32* @TUNSETNOCSUM
%42 = call i64 (i32, i32, ...) @ioctl(i32 %40, i32 %41, i32 1)
%43 = load i8*, i8** %2
%44 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i32 0, i32 1
%45 = load i32, i32* %44
%46 = load i32, i32* @IFNAMSIZ
%47 = call i32 @memcpy(i8* %43, i32 %45, i32 %46)
%48 = load i32, i32* %4
ret i32 %48
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@IFNAMSIZ = global i32 0
@.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
@IFF_VNET_HDR = global i32 0
@.str.1 = [6 x i8] c"tap%d\00"
@TUNSETIFF = global i32 0
@.str.2 = [25 x i8] c"configuring /dev/net/tun\00"
@TUNSETOFFLOAD = global i32 0
@TUN_F_CSUM = global i32 0
@TUN_F_TSO4 = global i32 0
@TUN_F_TSO6 = global i32 0
@TUN_F_TSO_ECN = global i32 0
@.str.3 = [38 x i8] c"Could not set features for tun device\00"
@TUNSETNOCSUM = global i32 0
define i32 @get_tun_device(i8* %0) {
%2 = alloca %struct.ifreq
%3 = bitcast %struct.ifreq* %2 to i8*
%4 = call i32 @memset(%struct.ifreq* %2, i32 0, i32 8)
%5 = load i32, i32* @O_RDWR
%6 = call i32 @open_or_die(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = load i32, i32* @IFF_TAP
%8 = load i32, i32* @IFF_NO_PI
%9 = or i32 %8, %7
%10 = load i32, i32* @IFF_VNET_HDR
%11 = or i32 %9, %10
%12 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 1
%14 = load i32, i32* %13
%15 = call i32 @strcpy(i32 %14, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%16 = load i32, i32* @TUNSETIFF
%17 = call i64 (i32, i32, ...) @ioctl(i32 %6, i32 %16, %struct.ifreq* %2)
%18 = icmp eq i64 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %1
%20 = call i32 @err(i32 1, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %19, %1
%22 = load i32, i32* @TUNSETOFFLOAD
%23 = load i32, i32* @TUN_F_CSUM
%24 = load i32, i32* @TUN_F_TSO4
%25 = or i32 %24, %23
%26 = load i32, i32* @TUN_F_TSO6
%27 = or i32 %25, %26
%28 = load i32, i32* @TUN_F_TSO_ECN
%29 = or i32 %27, %28
%30 = call i64 (i32, i32, ...) @ioctl(i32 %6, i32 %22, i32 %29)
%31 = icmp eq i64 %30, 0
br i1 %31, label %34, label %32
32: ; preds = %21
%33 = call i32 @err(i32 1, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
br label %34
34: ; preds = %32, %21
%35 = load i32, i32* @TUNSETNOCSUM
%36 = call i64 (i32, i32, ...) @ioctl(i32 %6, i32 %35, i32 1)
%37 = load i32, i32* %13
%38 = load i32, i32* @IFNAMSIZ
%39 = call i32 @memcpy(i8* %0, i32 %37, i32 %38)
ret i32 %6
}