#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;
  if ((fd = open("/dev/net/tun", 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)
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
lea    0xe79(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11b7 <tun_alloc+0x47>
lea    0xe59(%rip),%rdi
call   1090 <perror@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1241 <tun_alloc+0xd1>
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     11ef <tun_alloc+0x7f>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2df0(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x24(%rbp),%edi
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    122f <tun_alloc+0xbf>
lea    0xdfe(%rip),%rdi
call   1090 <perror@plt>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1241 <tun_alloc+0xd1>
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
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 %15
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%14 = load i32, i32* %7
store i32 %14, i32* %3
br label %45
15: ; preds = %2
%16 = call i32 @memset(%struct.ifreq* %6, i32 0, i32 8)
%17 = load i32, i32* %5
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
store i32 %17, i32* %18
%19 = load i8*, i8** %4
%20 = load i8, i8* %19
%21 = icmp ne i8 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %15
%23 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%24 = load i32, i32* %23
%25 = load i8*, i8** %4
%26 = load i32, i32* @IFNAMSIZ
%27 = call i32 @strncpy(i32 %24, i8* %25, i32 %26)
br label %28
28: ; preds = %22, %15
%29 = load i32, i32* %7
%30 = load i32, i32* @TUNSETIFF
%31 = bitcast %struct.ifreq* %6 to i8*
%32 = call i32 @ioctl(i32 %29, i32 %30, i8* %31)
store i32 %32, i32* %8
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %39
34: ; preds = %28
%35 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%36 = load i32, i32* %7
%37 = call i32 @close(i32 %36)
%38 = load i32, i32* %8
store i32 %38, i32* %3
br label %45
39: ; preds = %28
%40 = load i8*, i8** %4
%41 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%42 = load i32, i32* %41
%43 = call i32 @strcpy(i8* %40, i32 %42)
%44 = load i32, i32* %7
store i32 %44, i32* %3
br label %45
45: ; preds = %39, %34, %12
%46 = load i32, i32* %3
ret i32 %46
}

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
}