#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 errno;
int ioctl(int, int /*<<< orphan*/, void *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int, char *);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
char *strerror(int);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int tun_alloc(char *dev, int flags) {
  struct ifreq ifr;
  int fd, err;
  char *clonedev = "/dev/net/tun";
  if ((fd = open(clonedev, O_RDWR)) < 0) {
    printf("Error: %d %s\n", errno, strerror(errno));
    return fd;
  }
  memset(&ifr, 0, sizeof(ifr));
  ifr.ifr_flags = flags;
  if (*dev) {
    strncpy(ifr.ifr_name, dev, IFNAMSIZ);
  }
  if ((err = ioctl(fd, TUNSETIFF, (void *)&ifr)) < 0) {
    printf("Error: %d %s\n", errno, strerror(errno));
    close(fd);
    return err;
  }
  strcpy(dev, ifr.ifr_name);
  return fd;
}


x86 assembly (clang, -O0):
<tun_alloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    0xe6a(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11ef <tun_alloc+0x6f>
mov    0x2e09(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x34(%rbp),%esi
mov    %rax,%rdx
lea    0xe2e(%rip),%rdi
call   1050 <printf@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1299 <tun_alloc+0x119>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     1227 <tun_alloc+0xa7>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2db8(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x24(%rbp),%edi
mov    0x2d9f(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1070 <ioctl@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    1287 <tun_alloc+0x107>
mov    0x2d79(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d6d(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x38(%rbp),%esi
mov    %rax,%rdx
lea    0xd9e(%rip),%rdi
call   1050 <printf@plt>
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1299 <tun_alloc+0x119>
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    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tun_alloc>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
lea    0xe63(%rip),%rdi
call   1090 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11ff <tun_alloc+0x7f>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    %ebp,0x8(%rsp)
cmpb   $0x0,(%r14)
je     11d8 <tun_alloc+0x58>
mov    0xc(%rsp),%edi
mov    0x2e0a(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1070 <ioctl@plt>
test   %eax,%eax
js     1222 <tun_alloc+0xa2>
mov    0xc(%rsp),%esi
mov    %r14,%rdi
call   1040 <strcpy@plt>
jmp    1251 <tun_alloc+0xd1>
mov    0x2dc2(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   10a0 <strerror@plt>
lea    0xdf7(%rip),%rdi
mov    %ebp,%esi
mov    %rax,%rdx
call   1050 <printf@plt>
jmp    1251 <tun_alloc+0xd1>
mov    %eax,%ebp
mov    0x2d9d(%rip),%rax
mov    (%rax),%r14d
mov    %r14d,%edi
call   10a0 <strerror@plt>
lea    0xdd0(%rip),%rdi
mov    %r14d,%esi
mov    %rax,%rdx
call   1050 <printf@plt>
mov    %ebx,%edi
call   1080 <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
@.str.1 = [14 x i8] c"Error: %d %s\0A\00"
@errno = global i32 0
@IFNAMSIZ = global i32 0
@TUNSETIFF = global i32 0
define i32 @tun_alloc(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.ifreq
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8** %9
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_RDWR
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %7
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %2
%15 = load i32, i32* @errno
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %15, i8* %17)
%19 = load i32, i32* %7
store i32 %19, i32* %3
br label %53
20: ; preds = %2
%21 = call i32 @memset(%struct.ifreq* %6, i32 0, i32 8)
%22 = load i32, i32* %5
%23 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
store i32 %22, i32* %23
%24 = load i8*, i8** %4
%25 = load i8, i8* %24
%26 = icmp ne i8 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %20
%28 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%29 = load i32, i32* %28
%30 = load i8*, i8** %4
%31 = load i32, i32* @IFNAMSIZ
%32 = call i32 @strncpy(i32 %29, i8* %30, i32 %31)
br label %33
33: ; preds = %27, %20
%34 = load i32, i32* %7
%35 = load i32, i32* @TUNSETIFF
%36 = bitcast %struct.ifreq* %6 to i8*
%37 = call i32 @ioctl(i32 %34, i32 %35, i8* %36)
store i32 %37, i32* %8
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %47
39: ; preds = %33
%40 = load i32, i32* @errno
%41 = load i32, i32* @errno
%42 = call i8* @strerror(i32 %41)
%43 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %40, i8* %42)
%44 = load i32, i32* %7
%45 = call i32 @close(i32 %44)
%46 = load i32, i32* %8
store i32 %46, i32* %3
br label %53
47: ; preds = %33
%48 = load i8*, i8** %4
%49 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%50 = load i32, i32* %49
%51 = call i32 @strcpy(i8* %48, i32 %50)
%52 = load i32, i32* %7
store i32 %52, i32* %3
br label %53
53: ; preds = %47, %39, %14
%54 = load i32, i32* %3
ret i32 %54
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@.str = [13 x i8] c"/dev/net/tun\00"
@O_RDWR = global i32 0
@.str.1 = [14 x i8] c"Error: %d %s\0A\00"
@errno = global i32 0
@IFNAMSIZ = global i32 0
@TUNSETIFF = global i32 0
define i32 @tun_alloc(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 %8, label %12
8: ; preds = %2
%9 = load i32, i32* @errno
%10 = tail call i8* @strerror(i32 %9)
%11 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %9, i8* %10)
br label %35
12: ; preds = %2
%13 = call i32 @memset(%struct.ifreq* %3, i32 0, i32 8)
%14 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
store i32 %1, i32* %14
%15 = load i8, i8* %0
%16 = icmp eq i8 %15, 0
br i1 %16, label %22, label %17
17: ; preds = %12
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%19 = load i32, i32* %18
%20 = load i32, i32* @IFNAMSIZ
%21 = call i32 @strncpy(i32 %19, i8* %0, i32 %20)
br label %22
22: ; preds = %17, %12
%23 = load i32, i32* @TUNSETIFF
%24 = call i32 @ioctl(i32 %6, i32 %23, i8* %4)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %22
%27 = load i32, i32* @errno
%28 = call i8* @strerror(i32 %27)
%29 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %27, i8* %28)
%30 = call i32 @close(i32 %6)
br label %35
31: ; preds = %22
%32 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%33 = load i32, i32* %32
%34 = call i32 @strcpy(i8* %0, i32 %33)
br label %35
35: ; preds = %31, %26, %8
%36 = phi i32 [ %6, %8 ], [ %24, %26 ], [ %6, %31 ]
ret i32 %36
}