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

/* Variables and functions */
int /*<<< orphan*/ IFNAMSIZ;
int O_NONBLOCK;
int 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);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int TapCreate(char *dev, int flags) {
  int fd, err;
  struct ifreq ifr;
  if ((fd = open("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
    return fd;
  memset(&ifr, 0, sizeof(ifr));
  ifr.ifr_flags |= flags;
  if (*dev != '\0')
    strncpy(ifr.ifr_name, dev, IFNAMSIZ);
  if ((err = ioctl(fd, TUNSETIFF, (void *)&ifr)) < 0) {
    close(fd);
    return err;
  }
  return fd;
}


x86 assembly (clang, -O0):
<TapCreate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
mov    0x2e71(%rip),%rax
or     (%rax),%esi
lea    0xe88(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    1194 <TapCreate+0x44>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    120c <TapCreate+0xbc>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    -0x14(%rbp),%eax
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11d2 <TapCreate+0x82>
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e05(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%edi
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1206 <TapCreate+0xb6>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    120c <TapCreate+0xbc>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<TapCreate>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2e84(%rip),%rax
mov    0x2e75(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe8a(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11d5 <TapCreate+0x85>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
or     %ebp,0x8(%rsp)
cmpb   $0x0,(%r14)
je     11b1 <TapCreate+0x61>
mov    0xc(%rsp),%edi
mov    0x2e29(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1050 <ioctl@plt>
test   %eax,%eax
jns    11d5 <TapCreate+0x85>
mov    %eax,%ebp
mov    %ebx,%edi
call   1060 <close@plt>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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