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


x86 assembly (clang, -O0):
<open_tun_dev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
lea    0xe74(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ecx
mov    %ecx,-0x1c(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    11ba <open_tun_dev+0x4a>
lea    0xe63(%rip),%rdi
call   1090 <perror@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1249 <open_tun_dev+0xd9>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2df7(%rip),%rax
mov    (%rax),%eax
mov    0x2dde(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2deb(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x1c(%rbp),%edi
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,%ecx
mov    %ecx,-0x20(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
jle    1237 <open_tun_dev+0xc7>
lea    0xe0d(%rip),%rdi
call   1090 <perror@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1249 <open_tun_dev+0xd9>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <strcpy@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_tun_dev>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e66(%rip),%rax
mov    (%rax),%esi
lea    0xe75(%rip),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1204 <open_tun_dev+0x94>
mov    %eax,%ebx
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2e16(%rip),%rax
mov    0x2dff(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,0x8(%rsp)
mov    0xc(%rsp),%edi
mov    0x2e0c(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %rbp,%rdx
call   1060 <ioctl@plt>
test   %eax,%eax
js     1217 <open_tun_dev+0xa7>
mov    0xc(%rsp),%esi
mov    %r14,%rdi
call   1040 <strcpy@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe02(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11f9 <open_tun_dev+0x89>
mov    %eax,%ebp
lea    0xe0c(%rip),%rdi
call   1090 <perror@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    %ebp,%ebx
jmp    11f9 <open_tun_dev+0x89>

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

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