#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 O_NONBLOCK;
int O_RDWR;
int /*<<< orphan*/ TUNSETIFF;
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int);
int get_interface(char *name) {
  int interface = open("/dev/net/tun", O_RDWR | O_NONBLOCK);
  struct ifreq ifr;
  memset(&ifr, 0, sizeof(ifr));
  ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
  strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
  if (ioctl(interface, TUNSETIFF, &ifr)) {
    perror("Cannot get TUN interface");
    return -1;
  }
  return interface;
}


x86 assembly (clang, -O0):
<get_interface>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
mov    0x2e74(%rip),%rax
or     (%rax),%esi
lea    0xe8b(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e34(%rip),%rax
mov    (%rax),%eax
mov    0x2e1b(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    -0x14(%rbp),%edi
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
je     11ea <get_interface+0x9a>
lea    0xe34(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f0 <get_interface+0xa0>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_interface>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e85(%rip),%rax
mov    0x2e76(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe8b(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebx
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e31(%rip),%rax
mov    0x2e1a(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,0x8(%rsp)
mov    0xc(%rsp),%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r15,%rdx
call   1050 <ioctl@plt>
test   %rax,%rax
jne    11db <get_interface+0x8b>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe2b(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11cf <get_interface+0x7f>

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

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@.str = [13 x i8] c"/dev/net/tun\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@IFF_TUN = global i32 0
@IFF_NO_PI = global i32 0
@TUNSETIFF = global i32 0
@.str.1 = [25 x i8] c"Cannot get TUN interface\00"
define i32 @get_interface(i8* %0) {
%2 = alloca %struct.ifreq
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_NONBLOCK
%5 = or i32 %4, %3
%6 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = bitcast %struct.ifreq* %2 to i8*
%8 = call i32 @memset(%struct.ifreq* %2, i32 0, i32 8)
%9 = load i32, i32* @IFF_TUN
%10 = load i32, i32* @IFF_NO_PI
%11 = or i32 %10, %9
%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 @strncpy(i32 %14, i8* %0, i32 4)
%16 = load i32, i32* @TUNSETIFF
%17 = call i64 @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 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %1, %19
%22 = phi i32 [ -1, %19 ], [ %6, %1 ]
ret i32 %22
}