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


x86 assembly (clang, -O0):
<open_tun>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
lea    0xe81(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    119b <open_tun+0x3b>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1219 <open_tun+0xb9>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     11d3 <open_tun+0x73>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e0c(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x24(%rbp),%edi
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    1207 <open_tun+0xa7>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1219 <open_tun+0xb9>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1040 <strcpy@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_tun>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
lea    0xe83(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11ea <open_tun+0x8a>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    %ebp,0x8(%rsp)
cmpb   $0x0,(%r14)
je     11b8 <open_tun+0x58>
mov    0xc(%rsp),%edi
mov    0x2e2a(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1060 <ioctl@plt>
test   %eax,%eax
js     11df <open_tun+0x7f>
mov    0xc(%rsp),%esi
mov    %r14,%rdi
call   1040 <strcpy@plt>
jmp    11ea <open_tun+0x8a>
mov    %eax,%ebp
mov    %ebx,%edi
call   1070 <close@plt>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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