#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*/ perror(char *);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
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) {
    perror("Opening /dev/net/tun");
    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) {
    perror("ioctl(TUNSETIFF)");
    close(fd);
    return err;
  }
  strcpy(dev, ifr.ifr_name);
  return fd;
}


x86 assembly (clang, -O0):
<tun_alloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    0xe82(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11bf <tun_alloc+0x4f>
lea    0xe51(%rip),%rdi
call   1090 <perror@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1249 <tun_alloc+0xd9>
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     11f7 <tun_alloc+0x87>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2de8(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x24(%rbp),%edi
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    1237 <tun_alloc+0xc7>
lea    0xdf6(%rip),%rdi
call   1090 <perror@plt>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1249 <tun_alloc+0xd9>
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):
<tun_alloc>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%rbx
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
lea    0xe7b(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%r15d
test   %eax,%eax
js     11f8 <tun_alloc+0x88>
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    %r14d,(%rsp)
cmpb   $0x0,(%rbx)
je     11c6 <tun_alloc+0x56>
mov    0x4(%rsp),%edi
mov    0x2e1c(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rsi
call   1030 <strncpy@plt>
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %r15d,%edi
call   1060 <ioctl@plt>
test   %eax,%eax
js     1206 <tun_alloc+0x96>
mov    0x4(%rsp),%esi
mov    %rbx,%rdi
call   1040 <strcpy@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe01(%rip),%rdi
call   1090 <perror@plt>
jmp    11ea <tun_alloc+0x7a>
mov    %eax,%ebp
lea    0xe06(%rip),%rdi
call   1090 <perror@plt>
mov    %r15d,%edi
call   1070 <close@plt>
mov    %ebp,%r15d
jmp    11ea <tun_alloc+0x7a>

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

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