#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 /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TUNSETIFF;
int /*<<< orphan*/ assert(int /*<<< orphan*/);
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*/ strcpy(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int tap_creat(char *dev, int flags) {
  struct ifreq ifr;
  int fd, err;
  assert(dev != NULL);
  if ((fd = open("/dev/net/tun", O_RDWR)) < 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;
  }
  strcpy(dev, ifr.ifr_name);
  return fd;
}


x86 assembly (clang, -O0):
<tap_creat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
lea    0xe5f(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11bd <tap_creat+0x4d>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1241 <tap_creat+0xd1>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    -0x14(%rbp),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11fb <tap_creat+0x8b>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2de4(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x24(%rbp),%edi
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    122f <tap_creat+0xbf>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1241 <tap_creat+0xd1>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1040 <strcpy@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tap_creat>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r15
xor    %edi,%edi
test   %r15,%r15
setne  %dil
call   1080 <assert@plt>
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
lea    0xe65(%rip),%rdi
call   1090 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1204 <tap_creat+0x94>
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
or     %r14d,(%rsp)
cmpb   $0x0,(%r15)
je     11d4 <tap_creat+0x64>
mov    0x4(%rsp),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rsi
call   1030 <strncpy@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1060 <ioctl@plt>
test   %eax,%eax
js     11f9 <tap_creat+0x89>
mov    0x4(%rsp),%esi
mov    %r15,%rdi
call   1040 <strcpy@plt>
jmp    1204 <tap_creat+0x94>
mov    %eax,%ebx
mov    %ebp,%edi
call   1070 <close@plt>
mov    %ebx,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
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
@IFNAMSIZ = global i32 0
@TUNSETIFF = global i32 0
define i32 @tap_creat(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 i8*, i8** %4
%10 = icmp ne i8* %9, null
%11 = zext i1 %10 to i32
%12 = call i32 @assert(i32 %11)
%13 = load i32, i32* @O_RDWR
%14 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %13)
store i32 %14, i32* %7
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = load i32, i32* %7
store i32 %17, i32* %3
br label %50
18: ; preds = %2
%19 = call i32 @memset(%struct.ifreq* %6, i32 0, i32 8)
%20 = load i32, i32* %5
%21 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%22 = load i32, i32* %21
%23 = or i32 %22, %20
store i32 %23, i32* %21
%24 = load i8*, i8** %4
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %18
%29 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%30 = load i32, i32* %29
%31 = load i8*, i8** %4
%32 = load i32, i32* @IFNAMSIZ
%33 = call i32 @strncpy(i32 %30, i8* %31, i32 %32)
br label %34
34: ; preds = %28, %18
%35 = load i32, i32* %7
%36 = load i32, i32* @TUNSETIFF
%37 = bitcast %struct.ifreq* %6 to i8*
%38 = call i32 @ioctl(i32 %35, i32 %36, i8* %37)
store i32 %38, i32* %8
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %34
%41 = load i32, i32* %7
%42 = call i32 @close(i32 %41)
%43 = load i32, i32* %8
store i32 %43, i32* %3
br label %50
44: ; preds = %34
%45 = load i8*, i8** %4
%46 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%47 = load i32, i32* %46
%48 = call i32 @strcpy(i8* %45, i32 %47)
%49 = load i32, i32* %7
store i32 %49, i32* %3
br label %50
50: ; preds = %44, %40, %16
%51 = load i32, i32* %3
ret i32 %51
}

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