#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*/ tmp_dev;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int S_IFCHR;
int getpid();
int /*<<< orphan*/ makedev(int, int);
scalar_t__ mknod(char *, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ system(char *);
int /*<<< orphan*/ unlink(char *);
int open_tap_dev() {
  int fd;
  char tmp_dev[32];
  system("modprobe tun");
  fd = open("/dev/net/tun", O_RDWR);
  if (fd >= 0)
    return fd;
  snprintf(tmp_dev, sizeof(tmp_dev), "/tmp/__tmp_tap_dev_%d", getpid());
  if (mknod(tmp_dev, 0644 | S_IFCHR, makedev(10, 200))) {
    perror("mknod()");
    return -1;
  }
  fd = open(tmp_dev, O_RDWR);
  unlink(tmp_dev);
  return fd;
}


x86 assembly (clang, -O0):
<open_tap_dev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
lea    0xe71(%rip),%rdi
call   1050 <system@plt>
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
lea    0xe69(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     11c1 <open_tap_dev+0x41>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1263 <open_tap_dev+0xe3>
lea    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x48(%rbp),%rdi
mov    %eax,%ecx
mov    $0x20,%esi
lea    0xe38(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2de2(%rip),%rax
mov    (%rax),%eax
or     $0x1a4,%eax
mov    %eax,-0x34(%rbp)
mov    $0xa,%edi
mov    $0xc8,%esi
call   10a0 <makedev@plt>
mov    -0x40(%rbp),%rdi
mov    -0x34(%rbp),%esi
mov    %eax,%edx
call   1070 <mknod@plt>
cmp    $0x0,%rax
je     123f <open_tap_dev+0xbf>
lea    0xe02(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1263 <open_tap_dev+0xe3>
lea    -0x30(%rbp),%rdi
mov    0x2d9e(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
lea    -0x30(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_tap_dev>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
lea    0xe71(%rip),%rdi
call   1050 <system@plt>
mov    0x2e4d(%rip),%r14
mov    (%r14),%esi
lea    0xe68(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1216 <open_tap_dev+0x96>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe5c(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x20,%esi
mov    %eax,%ecx
call   1060 <snprintf@plt>
mov    0x2e01(%rip),%rax
mov    $0x1a4,%ebp
or     (%rax),%ebp
mov    $0xa,%edi
mov    $0xc8,%esi
call   10a0 <makedev@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1070 <mknod@plt>
test   %rax,%rax
jne    1221 <open_tap_dev+0xa1>
mov    (%r14),%esi
mov    %rsp,%rbp
mov    %rbp,%rdi
call   1080 <open@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1030 <unlink@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe08(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%ebx
jmp    1216 <open_tap_dev+0x96>

LLVM IR (-O0):
@.str = [13 x i8] c"modprobe tun\00"
@.str.1 = [13 x i8] c"/dev/net/tun\00"
@O_RDWR = global i32 0
@.str.2 = [22 x i8] c"/tmp/__tmp_tap_dev_%d\00"
@S_IFCHR = global i32 0
@.str.3 = [8 x i8] c"mknod()\00"
define i32 @open_tap_dev() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [32 x i8]
%4 = call i32 @system(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%5 = load i32, i32* @O_RDWR
%6 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %5)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = icmp sge i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = load i32, i32* %2
store i32 %10, i32* %1
br label %30
11: ; preds = %0
%12 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%13 = call i32 (...) @getpid()
%14 = call i32 @snprintf(i8* %12, i32 32, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %13)
%15 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%16 = load i32, i32* @S_IFCHR
%17 = or i32 420, %16
%18 = call i32 @makedev(i32 10, i32 200)
%19 = call i64 @mknod(i8* %15, i32 %17, i32 %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %11
%22 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %1
br label %30
23: ; preds = %11
%24 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%25 = load i32, i32* @O_RDWR
%26 = call i32 @open(i8* %24, i32 %25)
store i32 %26, i32* %2
%27 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%28 = call i32 @unlink(i8* %27)
%29 = load i32, i32* %2
store i32 %29, i32* %1
br label %30
30: ; preds = %23, %21, %9
%31 = load i32, i32* %1
ret i32 %31
}

LLVM IR (-O2):
@.str = [13 x i8] c"modprobe tun\00"
@.str.1 = [13 x i8] c"/dev/net/tun\00"
@O_RDWR = global i32 0
@.str.2 = [22 x i8] c"/tmp/__tmp_tap_dev_%d\00"
@S_IFCHR = global i32 0
@.str.3 = [8 x i8] c"mknod()\00"
define i32 @open_tap_dev() {
%1 = alloca [32 x i8]
%2 = getelementptr inbounds [32 x i8], [32 x i8]* %1, i64 0, i64 0
%3 = tail call i32 @system(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%4 = load i32, i32* @O_RDWR
%5 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %4)
%6 = icmp sgt i32 %5, -1
br i1 %6, label %21, label %7
7: ; preds = %0
%8 = tail call i32 (...) @getpid()
%9 = call i32 @snprintf(i8* %2, i32 32, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %8)
%10 = load i32, i32* @S_IFCHR
%11 = or i32 %10, 420
%12 = call i32 @makedev(i32 10, i32 200)
%13 = call i64 @mknod(i8* %2, i32 %11, i32 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %7
%16 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %21
17: ; preds = %7
%18 = load i32, i32* @O_RDWR
%19 = call i32 @open(i8* %2, i32 %18)
%20 = call i32 @unlink(i8* %2)
br label %21
21: ; preds = %0, %17, %15
%22 = phi i32 [ -1, %15 ], [ %19, %17 ], [ %5, %0 ]
ret i32 %22
}