#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;
  char *ifr_name;
};

/* Variables and functions */
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 *, char *);
int mktun(char *dev, int flags, struct ifreq *ifr) {
  int fd, stat;
  char *clonedev = "/dev/net/tun";
  if ((fd = open(clonedev, O_RDWR)) < 0)
    return fd;
  memset(ifr, 0, sizeof(*ifr));
  ifr->ifr_flags = flags;
  if (*dev)
    strcpy(ifr->ifr_name, dev);
  if ((stat = ioctl(fd, TUNSETIFF, (void *)ifr)) < 0) {
    perror("ioctl()");
    close(fd);
    return stat;
  }
  strcpy(dev, ifr->ifr_name);
  return fd;
}


x86 assembly (clang, -O0):
<mktun>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
lea    0xe86(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11a7 <mktun+0x47>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1235 <mktun+0xd5>
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    -0x14(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     11de <mktun+0x7e>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x24(%rbp),%edi
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    121e <mktun+0xbe>
lea    0xe07(%rip),%rdi
call   1080 <perror@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1235 <mktun+0xd5>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rsi
call   1030 <strcpy@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mktun>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %esi,%r15d
mov    %rdi,%r14
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
lea    0xe80(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11d2 <mktun+0x72>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    %r15d,(%rbx)
cmpb   $0x0,(%r14)
je     11af <mktun+0x4f>
mov    0x8(%rbx),%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   1050 <ioctl@plt>
test   %eax,%eax
js     11df <mktun+0x7f>
mov    0x8(%rbx),%rsi
mov    %r14,%rdi
call   1030 <strcpy@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%r15d
lea    0xe24(%rip),%rdi
call   1080 <perror@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %r15d,%ebp
jmp    11d2 <mktun+0x72>

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

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