#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*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ IFNAMSIZ;
int O_NONBLOCK;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TUNSETIFF;
int fcntl(int, int /*<<< orphan*/, ...);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int open_or_die(char *, int /*<<< orphan*/);
int /*<<< orphan*/ panic(char *);
int /*<<< orphan*/ strlcpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int tun_open_or_die(char *name, int type) {
  int fd, ret;
  struct ifreq ifr;
  fd = open_or_die("/dev/net/tun", O_RDWR);
  memset(&ifr, 0, sizeof(ifr));
  ifr.ifr_flags = type;
  if (name)
    strlcpy(ifr.ifr_name, name, IFNAMSIZ);
  ret = ioctl(fd, TUNSETIFF, &ifr);
  if (ret < 0)
    panic("ioctl screwed up!\n");
  ret = fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
  if (ret < 0)
    panic("fctnl screwed up!\n");
  return fd;
}


x86 assembly (clang, -O0):
<tun_open_or_die>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
lea    0xe81(%rip),%rdi
call   1070 <open_or_die@plt>
mov    %eax,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x20(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11bd <tun_open_or_die+0x5d>
mov    -0x1c(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    0x2e1a(%rip),%rax
mov    (%rax),%edx
call   1080 <strlcpy@plt>
mov    -0x10(%rbp),%edi
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11eb <tun_open_or_die+0x8b>
lea    0xe27(%rip),%rdi
call   1040 <panic@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2dc8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%edi
mov    0x2db1(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    -0x28(%rbp),%edi
mov    -0x24(%rbp),%esi
mov    %eax,%edx
mov    0x2dc1(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1241 <tun_open_or_die+0xe1>
lea    0xde4(%rip),%rdi
call   1040 <panic@plt>
mov    -0x10(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tun_open_or_die>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
lea    0xe83(%rip),%rdi
call   1070 <open_or_die@plt>
mov    %eax,%ebx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    %ebp,0x8(%rsp)
test   %r14,%r14
je     11b3 <tun_open_or_die+0x53>
mov    0xc(%rsp),%edi
mov    0x2e27(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rsi
call   1080 <strlcpy@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1060 <ioctl@plt>
test   %eax,%eax
jns    11d8 <tun_open_or_die+0x78>
lea    0xe3a(%rip),%rdi
call   1040 <panic@plt>
mov    0x2de1(%rip),%rax
mov    (%rax),%ebp
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    0x2de6(%rip),%rcx
or     (%rcx),%eax
mov    %ebx,%edi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %eax,%eax
jns    1219 <tun_open_or_die+0xb9>
lea    0xe0c(%rip),%rdi
call   1040 <panic@plt>
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
@IFNAMSIZ = global i32 0
@TUNSETIFF = global i32 0
@.str.1 = [19 x i8] c"ioctl screwed up!\0A\00"
@F_SETFL = global i32 0
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.2 = [19 x i8] c"fctnl screwed up!\0A\00"
define i32 @tun_open_or_die(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.ifreq
store i8* %0, i8** %3
store i32 %1, i32* %4
%8 = load i32, i32* @O_RDWR
%9 = call i32 @open_or_die(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %5
%10 = call i32 @memset(%struct.ifreq* %7, i32 0, i32 8)
%11 = load i32, i32* %4
%12 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 0
store i32 %11, i32* %12
%13 = load i8*, i8** %3
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %21
15: ; preds = %2
%16 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 1
%17 = load i32, i32* %16
%18 = load i8*, i8** %3
%19 = load i32, i32* @IFNAMSIZ
%20 = call i32 @strlcpy(i32 %17, i8* %18, i32 %19)
br label %21
21: ; preds = %15, %2
%22 = load i32, i32* %5
%23 = load i32, i32* @TUNSETIFF
%24 = call i32 @ioctl(i32 %22, i32 %23, %struct.ifreq* %7)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %21
%28 = call i32 @panic(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %29
29: ; preds = %27, %21
%30 = load i32, i32* %5
%31 = load i32, i32* @F_SETFL
%32 = load i32, i32* %5
%33 = load i32, i32* @F_GETFL
%34 = call i32 (i32, i32, ...) @fcntl(i32 %32, i32 %33)
%35 = load i32, i32* @O_NONBLOCK
%36 = or i32 %34, %35
%37 = call i32 (i32, i32, ...) @fcntl(i32 %30, i32 %31, i32 %36)
store i32 %37, i32* %6
%38 = load i32, i32* %6
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %29
%41 = call i32 @panic(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %42
42: ; preds = %40, %29
%43 = load i32, i32* %5
ret i32 %43
}

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
@.str.1 = [19 x i8] c"ioctl screwed up!\0A\00"
@F_SETFL = global i32 0
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.2 = [19 x i8] c"fctnl screwed up!\0A\00"
define i32 @tun_open_or_die(i8* %0, i32 %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = load i32, i32* @O_RDWR
%6 = tail call i32 @open_or_die(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = call i32 @memset(%struct.ifreq* %3, i32 0, i32 8)
%8 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
store i32 %1, i32* %8
%9 = icmp eq i8* %0, null
br i1 %9, label %15, label %10
10: ; preds = %2
%11 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%12 = load i32, i32* %11
%13 = load i32, i32* @IFNAMSIZ
%14 = call i32 @strlcpy(i32 %12, i8* %0, i32 %13)
br label %15
15: ; preds = %10, %2
%16 = load i32, i32* @TUNSETIFF
%17 = call i32 @ioctl(i32 %6, i32 %16, %struct.ifreq* %3)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = call i32 @panic(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %19, %15
%22 = load i32, i32* @F_SETFL
%23 = load i32, i32* @F_GETFL
%24 = call i32 (i32, i32, ...) @fcntl(i32 %6, i32 %23)
%25 = load i32, i32* @O_NONBLOCK
%26 = or i32 %25, %24
%27 = call i32 (i32, i32, ...) @fcntl(i32 %6, i32 %22, i32 %26)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %21
%30 = call i32 @panic(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %31
31: ; preds = %29, %21
ret i32 %6
}