#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 IFF_NO_PI;
int IFF_TAP;
int /*<<< orphan*/ IFNAMSIZ;
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*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int tap_create(char *dev_name) {
  int fd;
  int rc;
  struct ifreq ifr;
  fd = open("/dev/net/tun", O_RDWR);
  if (fd < 0) {
    perror("Opening /dev/net/tun");
    return fd;
  }
  memset(&ifr, 0, sizeof(ifr));
  ifr.ifr_flags = (IFF_TAP | IFF_NO_PI);
  strncpy(ifr.ifr_name, dev_name, IFNAMSIZ);
  rc = ioctl(fd, TUNSETIFF, (void *)&ifr);
  if (rc < 0) {
    perror("ioctl(TUNSETIFF)");
    close(fd);
    return rc;
  }
  return fd;
}


x86 assembly (clang, -O0):
<tap_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
lea    0xe8c(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11a5 <tap_create+0x45>
lea    0xe6b(%rip),%rdi
call   1080 <perror@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1226 <tap_create+0xc6>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%eax
mov    0x2df3(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2df8(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x14(%rbp),%edi
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1220 <tap_create+0xc0>
lea    0xe0d(%rip),%rdi
call   1080 <perror@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1226 <tap_create+0xc6>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tap_create>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
lea    0xe8d(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11e7 <tap_create+0x87>
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e37(%rip),%rax
mov    0x2e10(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,0x8(%rsp)
mov    0xc(%rsp),%edi
mov    0x2e15(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %rbp,%rdx
call   1050 <ioctl@plt>
test   %eax,%eax
js     11f5 <tap_create+0x95>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe12(%rip),%rdi
call   1080 <perror@plt>
jmp    11dc <tap_create+0x7c>
mov    %eax,%ebp
lea    0xe17(%rip),%rdi
call   1080 <perror@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %ebp,%ebx
jmp    11dc <tap_create+0x7c>

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

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