#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*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
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";
  char error_msg[50];
  if ((fd = open(clonedev, O_RDWR)) < 0) {
    snprintf(error_msg, 50, "Error opening /dev/net/tun, errorno: %d", errno);
    printf("%s\n", error_msg);
    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    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    0xe5a(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    1201 <tun_alloc+0x71>
lea    -0x70(%rbp),%rdi
mov    0x2df5(%rip),%rax
mov    (%rax),%ecx
mov    $0x32,%esi
lea    0xe2c(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x70(%rbp),%rsi
lea    0xe44(%rip),%rdi
call   1050 <printf@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    128b <tun_alloc+0xfb>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1070 <memset@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     1239 <tun_alloc+0xa9>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2da6(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x24(%rbp),%edi
mov    0x2d8d(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1080 <ioctl@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    1279 <tun_alloc+0xe9>
lea    0xdd8(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x24(%rbp),%edi
call   1090 <close@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    128b <tun_alloc+0xfb>
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    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tun_alloc>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
lea    0xe53(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     120a <tun_alloc+0x7a>
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1070 <memset@plt>
mov    %ebp,(%rsp)
cmpb   $0x0,(%r14)
je     11e5 <tun_alloc+0x55>
mov    0x4(%rsp),%edi
mov    0x2dfd(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1080 <ioctl@plt>
test   %eax,%eax
js     1244 <tun_alloc+0xb4>
mov    0x4(%rsp),%esi
mov    %r14,%rdi
call   1040 <strcpy@plt>
jmp    1239 <tun_alloc+0xa9>
mov    0x2db7(%rip),%rax
mov    (%rax),%ecx
lea    0xdf3(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x32,%esi
call   1060 <snprintf@plt>
lea    0xe04(%rip),%rdi
mov    %rbp,%rsi
call   1050 <printf@plt>
mov    %ebx,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %eax,%ebp
lea    0xdec(%rip),%rdi
call   10b0 <perror@plt>
mov    %ebx,%edi
call   1090 <close@plt>
mov    %ebp,%ebx
jmp    1239 <tun_alloc+0xa9>

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

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