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

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
scalar_t__ strncmp(char *, char *, int);
int open_tun(char *name) {
  char path[48];
  if (strncmp(name, "tun", 3) != 0) {
    fprintf(stderr, "open_tun: Device name must start with 'tun'!\n");
    return -1;
  }
  snprintf(path, sizeof(path) - 1, "/dev/%s", name);
  int fd = open(path, O_RDWR);
  if (fd < 0) {
    fprintf(stderr, "open_tun: Error opening %s: %s\n", name, strerror(errno));
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<open_tun>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe99(%rip),%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     119e <open_tun+0x4e>
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
lea    0xe79(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1222 <open_tun+0xd2>
lea    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x2f,%esi
lea    0xe80(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x40(%rbp),%rdi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    121c <open_tun+0xcc>
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rdx
mov    %rax,%rcx
lea    0xe31(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1222 <open_tun+0xd2>
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_tun>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %rdi,%rbx
lea    0xe9e(%rip),%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     118f <open_tun+0x3f>
mov    0x2e60(%rip),%rax
mov    (%rax),%edi
lea    0xe83(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    11be <open_tun+0x6e>
lea    0xe9c(%rip),%rdx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x2f,%esi
mov    %rbx,%rcx
call   1040 <snprintf@plt>
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11c7 <open_tun+0x77>
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2e0a(%rip),%rax
mov    (%rax),%ebp
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe55(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1188 <open_tun+0x38>

LLVM IR (-O0):
@.str = [4 x i8] c"tun\00"
@stderr = global i32 0
@.str.1 = [46 x i8] c"open_tun: Device name must start with 'tun'!\0A\00"
@.str.2 = [8 x i8] c"/dev/%s\00"
@O_RDWR = global i32 0
@.str.3 = [32 x i8] c"open_tun: Error opening %s: %s\0A\00"
@errno = global i32 0
define i32 @open_tun(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [48 x i8]
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i64 @strncmp(i8* %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 3)
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %29
12: ; preds = %1
%13 = getelementptr inbounds [48 x i8], [48 x i8]* %4, i64 0, i64 0
%14 = load i8*, i8** %3
%15 = call i32 @snprintf(i8* %13, i32 47, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %14)
%16 = getelementptr inbounds [48 x i8], [48 x i8]* %4, i64 0, i64 0
%17 = load i32, i32* @O_RDWR
%18 = call i32 @open(i8* %16, i32 %17)
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %12
%22 = load i32, i32* @stderr
%23 = load i8*, i8** %3
%24 = load i32, i32* @errno
%25 = call i8* @strerror(i32 %24)
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %23, i8* %25)
store i32 -1, i32* %2
br label %29
27: ; preds = %12
%28 = load i32, i32* %5
store i32 %28, i32* %2
br label %29
29: ; preds = %27, %21, %9
%30 = load i32, i32* %2
ret i32 %30
}

LLVM IR (-O2):
@.str = [4 x i8] c"tun\00"
@stderr = global i32 0
@.str.1 = [46 x i8] c"open_tun: Device name must start with 'tun'!\0A\00"
@.str.2 = [8 x i8] c"/dev/%s\00"
@O_RDWR = global i32 0
@.str.3 = [32 x i8] c"open_tun: Error opening %s: %s\0A\00"
@errno = global i32 0
define i32 @open_tun(i8* %0) {
%2 = alloca [48 x i8]
%3 = getelementptr inbounds [48 x i8], [48 x i8]* %2, i64 0, i64 0
%4 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 3)
%5 = icmp eq i64 %4, 0
br i1 %5, label %9, label %6
6: ; preds = %1
%7 = load i32, i32* @stderr
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0))
br label %19
9: ; preds = %1
%10 = call i32 @snprintf(i8* %3, i32 47, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%11 = load i32, i32* @O_RDWR
%12 = call i32 @open(i8* %3, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %9
%15 = load i32, i32* @stderr
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %17)
br label %19
19: ; preds = %14, %9, %6
%20 = phi i32 [ -1, %6 ], [ -1, %14 ], [ %12, %9 ]
ret i32 %20
}