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

/* Variables and functions */
int IFF_NO_PI;
int IFF_TAP;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TUNGETIFF;
int /*<<< orphan*/ TUNSETIFF;
int ioctl(int, int /*<<< orphan*/, void *);
int /*<<< orphan*/ jlog(int, char *, char *, int);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int /*<<< orphan*/, char *, char *);
extern int tun_create(char *devname, int *fd) {
  struct ifreq ifr;
  int ret;
  int dev;
  *fd = open("/dev/net/tun", O_RDWR);
  if (*fd < 0) {
    jlog(0x04, "tun_ifreq]> open tun failed :: %s:%i",
         "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
         "2022-01-22/02-19-57/repos/aboudreault/dnds-debian/refs/heads/master/"
         "libdnds/src/tun_ifreq.c",
         60);
    return -1;
  }
  memset(&ifr, 0, sizeof(ifr));
  ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
  ifr.ifr_name[0] = '\0';
  ret = ioctl(*fd, TUNSETIFF, (void *)&ifr);
  if (ret < 0) {
    jlog(0x04, "tun_ifreq]> ioctl TUNSETIFF :: %s:%i",
         "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
         "2022-01-22/02-19-57/repos/aboudreault/dnds-debian/refs/heads/master/"
         "libdnds/src/tun_ifreq.c",
         70);
    return -1;
  }
  ret = ioctl(*fd, TUNGETIFF, (void *)&ifr);
  if (ret < 0) {
    jlog(0x04, "tun_ifreq]> ioctl TUNGETIFF :: %s:%i",
         "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
         "2022-01-22/02-19-57/repos/aboudreault/dnds-debian/refs/heads/master/"
         "libdnds/src/tun_ifreq.c",
         76);
    return -1;
  }
  snprintf(devname, IFNAMSIZ, "%s", ifr.ifr_name);
  return ret;
}


x86 assembly (clang, -O0):
<tun_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
lea    0xe90(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
cmpl   $0x0,(%rax)
jge    11b3 <tun_create+0x63>
mov    $0x4,%edi
lea    0xe77(%rip),%rsi
lea    0xe95(%rip),%rdx
mov    $0x3c,%ecx
call   1040 <jlog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129e <tun_create+0x14e>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    0x2ddd(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    122d <tun_create+0xdd>
mov    $0x4,%edi
lea    0xebe(%rip),%rsi
lea    0xe1b(%rip),%rdx
mov    $0x46,%ecx
call   1040 <jlog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129e <tun_create+0x14e>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    0x2d8e(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    127b <tun_create+0x12b>
mov    $0x4,%edi
lea    0xe95(%rip),%rsi
lea    0xdcd(%rip),%rdx
mov    $0x4c,%ecx
call   1040 <jlog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129e <tun_create+0x14e>
mov    -0x10(%rbp),%rdi
mov    0x2d4a(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%rcx
lea    0xe85(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tun_create>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
lea    0xe91(%rip),%rdi
call   1070 <open@plt>
mov    %eax,(%rbx)
test   %eax,%eax
js     11f8 <tun_create+0xa8>
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2e45(%rip),%rax
mov    0x2e16(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,(%rsp)
mov    0x8(%rsp),%rax
movb   $0x0,(%rax)
mov    (%rbx),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdx
call   1060 <ioctl@plt>
test   %eax,%eax
js     1212 <tun_create+0xc2>
mov    (%rbx),%edi
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
call   1060 <ioctl@plt>
test   %eax,%eax
js     122c <tun_create+0xdc>
mov    %eax,%ebx
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
mov    0x8(%rsp),%rcx
lea    0xf2a(%rip),%rdx
mov    %r14,%rdi
call   1030 <snprintf@plt>
jmp    124e <tun_create+0xfe>
lea    0xe0e(%rip),%rsi
lea    0xe2c(%rip),%rdx
mov    $0x4,%edi
mov    $0x3c,%ecx
jmp    1244 <tun_create+0xf4>
lea    0xeb5(%rip),%rsi
lea    0xe12(%rip),%rdx
mov    $0x4,%edi
mov    $0x46,%ecx
jmp    1244 <tun_create+0xf4>
lea    0xec0(%rip),%rsi
lea    0xdf8(%rip),%rdx
mov    $0x4,%edi
mov    $0x4c,%ecx
call   1040 <jlog@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.ifreq = type { i32, i8* }
@.str = [13 x i8] c"/dev/net/tun\00"
@O_RDWR = global i32 0
@.str.1 = [37 x i8] c"tun_ifreq]> open tun failed :: %s:%i\00"
@.str.2 = [156 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/aboudreault/dnds-debian/refs/heads/master/libdnds/src/tun_ifreq.c\00"
@IFF_TAP = global i32 0
@IFF_NO_PI = global i32 0
@TUNSETIFF = global i32 0
@.str.3 = [37 x i8] c"tun_ifreq]> ioctl TUNSETIFF :: %s:%i\00"
@TUNGETIFF = global i32 0
@.str.4 = [37 x i8] c"tun_ifreq]> ioctl TUNGETIFF :: %s:%i\00"
@IFNAMSIZ = global i32 0
@.str.5 = [3 x i8] c"%s\00"
define i32 @tun_create(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)
%11 = load i32*, i32** %5
store i32 %10, i32* %11
%12 = load i32*, i32** %5
%13 = load i32, i32* %12
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @jlog(i32 4, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([156 x i8], [156 x i8]* @.str.2, i64 0, i64 0), i32 60)
store i32 -1, i32* %3
br label %52
17: ; preds = %2
%18 = call i32 @memset(%struct.ifreq* %6, i32 0, i32 16)
%19 = load i32, i32* @IFF_TAP
%20 = load i32, i32* @IFF_NO_PI
%21 = or i32 %19, %20
%22 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
store i32 %21, i32* %22
%23 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%24 = load i8*, i8** %23
%25 = getelementptr inbounds i8, i8* %24, i64 0
store i8 0, i8* %25
%26 = load i32*, i32** %5
%27 = load i32, i32* %26
%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 = load i32, i32* %7
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %17
%34 = call i32 @jlog(i32 4, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([156 x i8], [156 x i8]* @.str.2, i64 0, i64 0), i32 70)
store i32 -1, i32* %3
br label %52
35: ; preds = %17
%36 = load i32*, i32** %5
%37 = load i32, i32* %36
%38 = load i32, i32* @TUNGETIFF
%39 = bitcast %struct.ifreq* %6 to i8*
%40 = call i32 @ioctl(i32 %37, i32 %38, i8* %39)
store i32 %40, i32* %7
%41 = load i32, i32* %7
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %35
%44 = call i32 @jlog(i32 4, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([156 x i8], [156 x i8]* @.str.2, i64 0, i64 0), i32 76)
store i32 -1, i32* %3
br label %52
45: ; preds = %35
%46 = load i8*, i8** %4
%47 = load i32, i32* @IFNAMSIZ
%48 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%49 = load i8*, i8** %48
%50 = call i32 @snprintf(i8* %46, i32 %47, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %49)
%51 = load i32, i32* %7
store i32 %51, i32* %3
br label %52
52: ; preds = %45, %43, %33, %15
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i8* }
@.str = [13 x i8] c"/dev/net/tun\00"
@O_RDWR = global i32 0
@.str.1 = [37 x i8] c"tun_ifreq]> open tun failed :: %s:%i\00"
@.str.2 = [156 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/aboudreault/dnds-debian/refs/heads/master/libdnds/src/tun_ifreq.c\00"
@IFF_TAP = global i32 0
@IFF_NO_PI = global i32 0
@TUNSETIFF = global i32 0
@.str.3 = [37 x i8] c"tun_ifreq]> ioctl TUNSETIFF :: %s:%i\00"
@TUNGETIFF = global i32 0
@.str.4 = [37 x i8] c"tun_ifreq]> ioctl TUNGETIFF :: %s:%i\00"
@IFNAMSIZ = global i32 0
@.str.5 = [3 x i8] c"%s\00"
define i32 @tun_create(i8* %0, i32* nocapture %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)
store i32 %6, i32* %1
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @jlog(i32 4, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([156 x i8], [156 x i8]* @.str.2, i64 0, i64 0), i32 60)
br label %35
10: ; preds = %2
%11 = call i32 @memset(%struct.ifreq* %3, i32 0, i32 16)
%12 = load i32, i32* @IFF_TAP
%13 = load i32, i32* @IFF_NO_PI
%14 = or i32 %13, %12
%15 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%17 = load i8*, i8** %16
store i8 0, i8* %17
%18 = load i32, i32* %1
%19 = load i32, i32* @TUNSETIFF
%20 = call i32 @ioctl(i32 %18, i32 %19, i8* %4)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %10
%23 = call i32 @jlog(i32 4, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([156 x i8], [156 x i8]* @.str.2, i64 0, i64 0), i32 70)
br label %35
24: ; preds = %10
%25 = load i32, i32* %1
%26 = load i32, i32* @TUNGETIFF
%27 = call i32 @ioctl(i32 %25, i32 %26, i8* %4)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = call i32 @jlog(i32 4, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([156 x i8], [156 x i8]* @.str.2, i64 0, i64 0), i32 76)
br label %35
31: ; preds = %24
%32 = load i32, i32* @IFNAMSIZ
%33 = load i8*, i8** %16
%34 = call i32 @snprintf(i8* %0, i32 %32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %33)
br label %35
35: ; preds = %31, %29, %22, %8
%36 = phi i32 [ -1, %8 ], [ -1, %22 ], [ -1, %29 ], [ %27, %31 ]
ret i32 %36
}