#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;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
scalar_t__ IFNAMSIZ;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TUNSETIFF;
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, void *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, scalar_t__);
int tun_alloc(char *dev, int flags) {
  struct ifreq ifr = {0};
  int fd, err;
  if ((fd = open("/dev/net/tun", O_RDWR)) < 0) {
    perror("Opening /dev/net/tun");
    return fd;
  }
  ifr.ifr_flags = flags;
  if (*dev) {
    strncpy(ifr.ifr_name, dev, IFNAMSIZ - 1);
  }
  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    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
lea    0xe69(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11c7 <tun_alloc+0x57>
lea    0xe49(%rip),%rdi
call   1090 <perror@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1246 <tun_alloc+0xd6>
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     11f4 <tun_alloc+0x84>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2df0(%rip),%rax
mov    (%rax),%rdx
sub    $0x1,%rdx
call   1030 <strncpy@plt>
mov    -0x24(%rbp),%edi
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    1234 <tun_alloc+0xc4>
lea    0xdf9(%rip),%rdi
call   1090 <perror@plt>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1246 <tun_alloc+0xd6>
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   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
movq   $0x0,0x4(%rsp)
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
lea    0xe82(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%r14d
test   %eax,%eax
js     11e2 <tun_alloc+0x82>
mov    %ebp,(%rsp)
cmpb   $0x0,(%rbx)
je     11b2 <tun_alloc+0x52>
mov    0x2e37(%rip),%rax
mov    (%rax),%rdx
add    $0xffffffffffffffff,%rdx
xor    %edi,%edi
mov    %rbx,%rsi
call   1030 <strncpy@plt>
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %r14d,%edi
call   1050 <ioctl@plt>
test   %eax,%eax
js     11f0 <tun_alloc+0x90>
mov    0x4(%rsp),%esi
mov    %rbx,%rdi
call   1040 <strcpy@plt>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe17(%rip),%rdi
call   1080 <perror@plt>
jmp    11d6 <tun_alloc+0x76>
mov    %eax,%ebp
lea    0xe1c(%rip),%rdi
call   1080 <perror@plt>
mov    %r14d,%edi
call   1060 <close@plt>
mov    %ebp,%r14d
jmp    11d6 <tun_alloc+0x76>

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

LLVM IR (-O2):
%struct.ifreq = type { i32, 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 i64 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 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%6 = bitcast i32* %5 to i64*
store i64 0, i64* %6
%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 %12
10: ; preds = %2
%11 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %31
12: ; preds = %2
%13 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
store i32 %1, i32* %13
%14 = load i8, i8* %0
%15 = icmp eq i8 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %12
%17 = load i64, i64* @IFNAMSIZ
%18 = add nsw i64 %17, -1
%19 = tail call i32 @strncpy(i32 0, i8* %0, i64 %18)
br label %20
20: ; preds = %16, %12
%21 = load i32, i32* @TUNSETIFF
%22 = call i32 @ioctl(i32 %8, 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 %8)
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, %10
%32 = phi i32 [ %8, %10 ], [ %22, %24 ], [ %8, %27 ]
ret i32 %32
}