#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 {
  char *ifr_name;
  int /*<<< orphan*/ ifr_flags;
};

/* Variables and functions */
int /*<<< orphan*/ IFF_TAP;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TUNSETIFF;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int /*<<< orphan*/ ifconfig(char *, char *);
int ioctl(int, int /*<<< orphan*/, void *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char *);
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(char *, char *, int /*<<< orphan*/);
int tunopen(char *gotdev, char *wantdev, char *addr) {
  struct ifreq ifr;
  int tunfd;
  tunfd = -1;
  memset(&ifr, 0, sizeof ifr);
  ifr.ifr_flags = IFF_TAP;
  if (wantdev != NULL)
    strncpy(ifr.ifr_name, wantdev, IFNAMSIZ);
  if ((tunfd = open("/dev/net/tun", O_RDWR)) == -1) {
    fprintf(stderr, "open %s: %s\n", ifr.ifr_name, strerror(errno));
    goto error_out;
  }
  if (ioctl(tunfd, TUNSETIFF, (void *)&ifr) == -1) {
    fprintf(stderr, "ioctl TUNSETIFF %s: %s\n", ifr.ifr_name, strerror(errno));
    goto error_out;
  }
  ifconfig(ifr.ifr_name, addr);
  if (gotdev != NULL)
    strcpy(gotdev, ifr.ifr_name);
  return tunfd;
error_out:
  if (tunfd != -1)
    close(tunfd);
  return -1;
}


x86 assembly (clang, -O0):
<tunopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0xffffffff,-0x34(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11e8 <tunopen+0x58>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2de7(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
lea    0xe08(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x34(%rbp)
cmp    $0xffffffff,%eax
jne    1246 <tunopen+0xb6>
mov    0x2dc0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xdd1(%rip),%rsi
call   1080 <fprintf@plt>
jmp    12d1 <tunopen+0x141>
mov    -0x34(%rbp),%edi
mov    0x2d70(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    12a1 <tunopen+0x111>
mov    0x2d65(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2d39(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rdx
mov    %rax,%rcx
lea    0xd83(%rip),%rsi
call   1080 <fprintf@plt>
jmp    12d1 <tunopen+0x141>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1090 <ifconfig@plt>
cmpq   $0x0,-0x10(%rbp)
je     12c6 <tunopen+0x136>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12ea <tunopen+0x15a>
cmpl   $0xffffffff,-0x34(%rbp)
je     12e3 <tunopen+0x153>
mov    -0x34(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tunopen>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
test   %rbx,%rbx
je     11dc <tunopen+0x4c>
mov    0x8(%rsp),%rdi
mov    0x2df6(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rsi
call   1030 <strncpy@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
lea    0xe14(%rip),%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     1233 <tunopen+0xa3>
mov    %eax,%ebx
mov    0x2dc1(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1060 <ioctl@plt>
cmp    $0xffffffff,%eax
je     1265 <tunopen+0xd5>
mov    0x8(%rsp),%rdi
mov    %r15,%rsi
call   1090 <ifconfig@plt>
test   %r14,%r14
je     12a1 <tunopen+0x111>
mov    0x8(%rsp),%rsi
mov    %r14,%rdi
call   1040 <strcpy@plt>
jmp    12a1 <tunopen+0x111>
mov    0x2d96(%rip),%rax
mov    (%rax),%ebx
mov    0x8(%rsp),%r14
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdb7(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1080 <fprintf@plt>
jmp    129c <tunopen+0x10c>
mov    0x2d64(%rip),%rax
mov    (%rax),%ebp
mov    0x8(%rsp),%r14
mov    0x2d3e(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd92(%rip),%rsi
mov    %ebp,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1080 <fprintf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i8*, i32 }
@IFF_TAP = global i32 0
@IFNAMSIZ = global i32 0
@.str = [13 x i8] c"/dev/net/tun\00"
@O_RDWR = global i32 0
@stderr = global i32 0
@.str.1 = [13 x i8] c"open %s: %s\0A\00"
@errno = global i32 0
@TUNSETIFF = global i32 0
@.str.2 = [24 x i8] c"ioctl TUNSETIFF %s: %s\0A\00"
define i32 @tunopen(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca %struct.ifreq
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 -1, i32* %9
%10 = call i32 @memset(%struct.ifreq* %8, i32 0, i32 16)
%11 = load i32, i32* @IFF_TAP
%12 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 1
store i32 %11, i32* %12
%13 = load i8*, i8** %6
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %21
15: ; preds = %3
%16 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 0
%17 = load i8*, i8** %16
%18 = load i8*, i8** %6
%19 = load i32, i32* @IFNAMSIZ
%20 = call i32 @strncpy(i8* %17, i8* %18, i32 %19)
br label %21
21: ; preds = %15, %3
%22 = load i32, i32* @O_RDWR
%23 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %22)
store i32 %23, i32* %9
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %32
25: ; preds = %21
%26 = load i32, i32* @stderr
%27 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 0
%28 = load i8*, i8** %27
%29 = load i32, i32* @errno
%30 = call i8* @strerror(i32 %29)
%31 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %28, i8* %30)
br label %59
32: ; preds = %21
%33 = load i32, i32* %9
%34 = load i32, i32* @TUNSETIFF
%35 = bitcast %struct.ifreq* %8 to i8*
%36 = call i32 @ioctl(i32 %33, i32 %34, i8* %35)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %45
38: ; preds = %32
%39 = load i32, i32* @stderr
%40 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 0
%41 = load i8*, i8** %40
%42 = load i32, i32* @errno
%43 = call i8* @strerror(i32 %42)
%44 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %41, i8* %43)
br label %59
45: ; preds = %32
%46 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 0
%47 = load i8*, i8** %46
%48 = load i8*, i8** %7
%49 = call i32 @ifconfig(i8* %47, i8* %48)
%50 = load i8*, i8** %5
%51 = icmp ne i8* %50, null
br i1 %51, label %52, label %57
52: ; preds = %45
%53 = load i8*, i8** %5
%54 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 0
%55 = load i8*, i8** %54
%56 = call i32 @strcpy(i8* %53, i8* %55)
br label %57
57: ; preds = %52, %45
%58 = load i32, i32* %9
store i32 %58, i32* %4
br label %66
59: ; preds = %38, %25
%60 = load i32, i32* %9
%61 = icmp ne i32 %60, -1
br i1 %61, label %62, label %65
62: ; preds = %59
%63 = load i32, i32* %9
%64 = call i32 @close(i32 %63)
br label %65
65: ; preds = %62, %59
store i32 -1, i32* %4
br label %66
66: ; preds = %65, %57
%67 = load i32, i32* %4
ret i32 %67
}

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