#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 {
  char *ifr_name;
  int /*<<< orphan*/ ifr_flags;
};
typedef int /*<<< orphan*/ ifr;

/* Variables and functions */
int /*<<< orphan*/ IFF_TUN;
int IFNAMSIZ;
int /*<<< orphan*/ TUNSETIFF;
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, void *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int tun_alloc(char *dev, int fd) {
  struct ifreq ifr;
  int err;
  memset(&ifr, 0, sizeof(ifr));
  ifr.ifr_flags = IFF_TUN;
  if (*dev) {
    strncpy(ifr.ifr_name, dev, IFNAMSIZ);
    ifr.ifr_name[IFNAMSIZ - 1] = '\0';
  }
  if ((err = ioctl(fd, TUNSETIFF, (void *)&ifr)) < 0) {
    close(fd);
    return err;
  }
  strcpy(dev, ifr.ifr_name);
  return 0;
}


x86 assembly (clang, -O0):
<tun_alloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2e5a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     11b5 <tun_alloc+0x65>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2e49(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rax
mov    0x2e37(%rip),%rcx
mov    (%rcx),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x14(%rbp),%edi
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    11e9 <tun_alloc+0x99>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11fd <tun_alloc+0xad>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1040 <strcpy@plt>
movl   $0x0,-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,%r14d
mov    %rdi,%rbx
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2e5c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
cmpb   $0x0,(%rbx)
je     11a2 <tun_alloc+0x52>
mov    (%rsp),%rdi
mov    0x2e56(%rip),%rbp
mov    0x0(%rbp),%edx
mov    %rbx,%rsi
call   1030 <strncpy@plt>
mov    (%rsp),%rax
movslq 0x0(%rbp),%rcx
movb   $0x0,-0x1(%rcx,%rax,1)
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %r14d,%edi
call   1060 <ioctl@plt>
test   %eax,%eax
js     11ca <tun_alloc+0x7a>
mov    (%rsp),%rsi
mov    %rbx,%rdi
call   1040 <strcpy@plt>
xor    %ebp,%ebp
jmp    11d4 <tun_alloc+0x84>
mov    %eax,%ebp
mov    %r14d,%edi
call   1070 <close@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.ifreq = type { i8*, i32 }
@IFF_TUN = 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 = call i32 @memset(%struct.ifreq* %3, i32 0, i32 16)
%6 = load i32, i32* @IFF_TUN
%7 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
store i32 %6, i32* %7
%8 = load i8, i8* %0
%9 = icmp eq i8 %8, 0
br i1 %9, label %20, label %10
10: ; preds = %2
%11 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
%12 = load i8*, i8** %11
%13 = load i32, i32* @IFNAMSIZ
%14 = call i32 @strncpy(i8* %12, i8* %0, i32 %13)
%15 = load i8*, i8** %11
%16 = load i32, i32* @IFNAMSIZ
%17 = add nsw i32 %16, -1
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %15, i64 %18
store i8 0, i8* %19
br label %20
20: ; preds = %10, %2
%21 = load i32, i32* @TUNSETIFF
%22 = call i32 @ioctl(i32 %1, i32 %21, i8* %4)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 @close(i32 %1)
br label %30
26: ; preds = %20
%27 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
%28 = load i8*, i8** %27
%29 = call i32 @strcpy(i8* %0, i8* %28)
br label %30
30: ; preds = %26, %24
%31 = phi i32 [ %22, %24 ], [ 0, %26 ]
ret i32 %31
}