#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;
};

/* Variables and functions */
int IFF_NO_PI;
int IFF_TUN;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TUNSETIFF;
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int open_tun(char *name) {
  int fd = open("/dev/net/tun", O_RDWR);
  if (fd < 0) {
    perror("open");
    return -1;
  }
  struct ifreq ifr;
  strncpy(ifr.ifr_name, name, IFNAMSIZ);
  ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
  if (ioctl(fd, TUNSETIFF, &ifr) < 0) {
    perror("ioctl");
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<open_tun>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e95(%rip),%rax
mov    (%rax),%esi
lea    0xea4(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1186 <open_tun+0x46>
lea    0xe98(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ed <open_tun+0xad>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e44(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    0x2e26(%rip),%rax
mov    (%rax),%eax
mov    0x2e0d(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    11e7 <open_tun+0xa7>
lea    0xe3c(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ed <open_tun+0xad>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_tun>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e9a(%rip),%rax
mov    (%rax),%esi
lea    0xea9(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
js     11aa <open_tun+0x6a>
mov    %eax,%ebx
mov    0x2e6f(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    0x2e4e(%rip),%rax
mov    0x2e37(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,(%rsp)
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
js     11b3 <open_tun+0x73>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe5c(%rip),%rdi
jmp    11ba <open_tun+0x7a>
lea    0xe58(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11a0 <open_tun+0x60>

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

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