#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 */
typedef int /*<<< orphan*/ name;

/* Variables and functions */
int /*<<< orphan*/ gt_na(char *);
scalar_t__ str_empty(char *);
char *strdup(char *);
int tun_create_by_id(char *, int, unsigned int, int);
int tun_create_by_name(char *, int, char *, int);
int tun_create(char *dev_name, char **ret_name, int mq) {
  char name[64] = {0};
  int fd = -1;
  if (mq)
    gt_na("IFF_MULTI_QUEUE");
  if (str_empty(dev_name)) {
    for (unsigned id = 0; id < 32 && fd == -1; id++)
      fd = tun_create_by_id(name, sizeof(name), id, mq);
  } else {
    fd = tun_create_by_name(name, sizeof(name), dev_name, mq);
  }
  if (fd != -1 && ret_name)
    *ret_name = strdup(name);
  return fd;
}


x86 assembly (clang, -O0):
<tun_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1040 <memset@plt>
movl   $0xffffffff,-0x64(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11a0 <tun_create+0x40>
lea    0xe65(%rip),%rdi
call   1030 <gt_na@plt>
mov    -0x8(%rbp),%rdi
call   1050 <str_empty@plt>
cmp    $0x0,%rax
je     120d <tun_create+0xad>
movl   $0x0,-0x68(%rbp)
xor    %eax,%eax
cmpl   $0x20,-0x68(%rbp)
mov    %al,-0x69(%rbp)
jae    11d3 <tun_create+0x73>
cmpl   $0xffffffff,-0x64(%rbp)
sete   %al
mov    %al,-0x69(%rbp)
mov    -0x69(%rbp),%al
test   $0x1,%al
jne    11e3 <tun_create+0x83>
jmp    1208 <tun_create+0xa8>
lea    -0x60(%rbp),%rdi
mov    -0x68(%rbp),%edx
mov    -0x14(%rbp),%ecx
mov    $0x40,%esi
call   1060 <tun_create_by_id@plt>
mov    %eax,-0x64(%rbp)
mov    -0x68(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x68(%rbp)
jmp    11ba <tun_create+0x5a>
jmp    1225 <tun_create+0xc5>
lea    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x14(%rbp),%ecx
mov    $0x40,%esi
call   1070 <tun_create_by_name@plt>
mov    %eax,-0x64(%rbp)
cmpl   $0xffffffff,-0x64(%rbp)
je     124d <tun_create+0xed>
cmpq   $0x0,-0x10(%rbp)
je     124d <tun_create+0xed>
lea    -0x60(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x64(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tun_create>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x40,%rsp
mov    %edx,%r15d
mov    %rsi,%r14
mov    %rdi,%rbx
xorps  %xmm0,%xmm0
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
test   %edx,%edx
je     118b <tun_create+0x3b>
lea    0xe7a(%rip),%rdi
call   1030 <gt_na@plt>
mov    %rbx,%rdi
call   1040 <str_empty@plt>
test   %rax,%rax
je     11c3 <tun_create+0x73>
xor    %ebp,%ebp
mov    %rsp,%r12
nopl   (%rax)
mov    %r12,%rdi
mov    $0x40,%esi
mov    %ebp,%edx
mov    %r15d,%ecx
call   1050 <tun_create_by_id@plt>
mov    %eax,%ebx
cmp    $0x1e,%ebp
ja     11d8 <tun_create+0x88>
add    $0x1,%ebp
cmp    $0xffffffff,%ebx
je     11a0 <tun_create+0x50>
jmp    11d8 <tun_create+0x88>
mov    %rsp,%rdi
mov    $0x40,%esi
mov    %rbx,%rdx
mov    %r15d,%ecx
call   1060 <tun_create_by_name@plt>
mov    %eax,%ebx
test   %r14,%r14
je     11ed <tun_create+0x9d>
cmp    $0xffffffff,%ebx
je     11ed <tun_create+0x9d>
mov    %rsp,%rdi
call   1070 <strdup@plt>
mov    %rax,(%r14)
mov    %ebx,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"IFF_MULTI_QUEUE\00"
define i32 @tun_create(i8* %0, i8** %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [64 x i8]
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i32 %2, i32* %6
%10 = bitcast [64 x i8]* %7 to i8*
store i32 -1, i32* %8
%11 = load i32, i32* %6
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = call i32 @gt_na(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %3
%16 = load i8*, i8** %4
%17 = call i64 @str_empty(i8* %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %37
19: ; preds = %15
store i32 0, i32* %9
br label %20
20: ; preds = %33, %19
%21 = load i32, i32* %9
%22 = icmp ult i32 %21, 32
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = load i32, i32* %8
%25 = icmp eq i32 %24, -1
br label %26
26: ; preds = %23, %20
%27 = phi i1 [ false, %20 ], [ %25, %23 ]
br i1 %27, label %28, label %36
28: ; preds = %26
%29 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%30 = load i32, i32* %9
%31 = load i32, i32* %6
%32 = call i32 @tun_create_by_id(i8* %29, i32 64, i32 %30, i32 %31)
store i32 %32, i32* %8
br label %33
33: ; preds = %28
%34 = load i32, i32* %9
%35 = add i32 %34, 1
store i32 %35, i32* %9
br label %20
36: ; preds = %26
br label %42
37: ; preds = %15
%38 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%39 = load i8*, i8** %4
%40 = load i32, i32* %6
%41 = call i32 @tun_create_by_name(i8* %38, i32 64, i8* %39, i32 %40)
store i32 %41, i32* %8
br label %42
42: ; preds = %37, %36
%43 = load i32, i32* %8
%44 = icmp ne i32 %43, -1
br i1 %44, label %45, label %52
45: ; preds = %42
%46 = load i8**, i8*** %5
%47 = icmp ne i8** %46, null
br i1 %47, label %48, label %52
48: ; preds = %45
%49 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%50 = call i8* @strdup(i8* %49)
%51 = load i8**, i8*** %5
store i8* %50, i8** %51
br label %52
52: ; preds = %48, %45, %42
%53 = load i32, i32* %8
ret i32 %53
}

LLVM IR (-O2):
@.str = [16 x i8] c"IFF_MULTI_QUEUE\00"
define i32 @tun_create(i8* %0, i8** writeonly %1, i32 %2) {
%4 = alloca [64 x i8]
%5 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%6 = icmp eq i32 %2, 0
br i1 %6, label %9, label %7
7: ; preds = %3
%8 = tail call i32 @gt_na(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %3
%10 = tail call i64 @str_empty(i8* %0)
%11 = icmp eq i64 %10, 0
br i1 %11, label %19, label %12
12: ; preds = %9, %12
%13 = phi i32 [ %15, %12 ], [ 0, %9 ]
%14 = call i32 @tun_create_by_id(i8* %5, i32 64, i32 %13, i32 %2)
%15 = add nuw nsw i32 %13, 1
%16 = icmp ult i32 %13, 31
%17 = icmp eq i32 %14, -1
%18 = select i1 %16, i1 %17, i1 false
br i1 %18, label %12, label %21
19: ; preds = %9
%20 = call i32 @tun_create_by_name(i8* %5, i32 64, i8* %0, i32 %2)
br label %21
21: ; preds = %12, %19
%22 = phi i32 [ %20, %19 ], [ %14, %12 ]
%23 = icmp ne i32 %22, -1
%24 = icmp ne i8** %1, null
%25 = and i1 %24, %23
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = call i8* @strdup(i8* %5)
store i8* %27, i8** %1
br label %28
28: ; preds = %26, %21
ret i32 %22
}