#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_TUN;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TUNSETIFF;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int ioctl(int, int /*<<< orphan*/, void *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int tun_open(char *devname) {
  struct ifreq ifr;
  int fd, err;
  if ((fd = open("/dev/net/tun", O_RDWR)) == -1) {
    perror("open /dev/net/tun");
    exit(1);
  }
  memset(&ifr, 0, sizeof(ifr));
  ifr.ifr_flags = (IFF_TUN | IFF_NO_PI);
  strncpy(ifr.ifr_name, devname, IFNAMSIZ);
  if ((err = ioctl(fd, TUNSETIFF, (void *)&ifr)) == -1) {
    perror("ioctl TUNSETIFF");
    close(fd);
    exit(1);
  }
  return fd;
}


x86 assembly (clang, -O0):
<tun_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
lea    0xe79(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11b3 <tun_open+0x43>
lea    0xe5c(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%eax
mov    0x2de5(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    0x2df2(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x14(%rbp),%edi
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    122c <tun_open+0xbc>
lea    0xdfd(%rip),%rdi
call   1080 <perror@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tun_open>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
lea    0xe79(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11fb <tun_open+0x8b>
mov    %eax,%ebx
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e15(%rip),%rax
mov    0x2dfe(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,0x8(%rsp)
mov    0xc(%rsp),%edi
mov    0x2e0b(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r15,%rdx
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
je     1211 <tun_open+0xa1>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xdfe(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xdfa(%rip),%rdi
call   1080 <perror@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

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

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