#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;
};
typedef int /*<<< orphan*/ ifr;

/* Variables and functions */
int IFF_NO_PI;
int IFF_TAP;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TUNSETIFF;
int /*<<< orphan*/ exit(int);
int ioctl(int, int /*<<< orphan*/, void *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int);
int tun_alloc() {
  struct ifreq ifr;
  int fd, err;
  memset(&(ifr), 0, sizeof(ifr));
  int fd_open_code = fd = open("/dev/net/tun", O_RDWR);
  if (fd_open_code < 0) {
    printf("Error opening device, code %d", fd_open_code);
    exit(1);
  }
  ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
  int ioctl_code = ioctl(fd, TUNSETIFF, (void *)&ifr);
  if (ioctl_code < 0) {
    printf("Error setting ioctl flags, code %d", ioctl_code);
    exit(1);
  }
  return fd;
}


x86 assembly (clang, -O0):
<tun_alloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    -0x8(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
lea    0xe88(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0xc(%rbp)
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11a6 <tun_alloc+0x56>
mov    -0x14(%rbp),%esi
lea    0xe76(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
mov    0x2e0a(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x8(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11f6 <tun_alloc+0xa6>
mov    -0x18(%rbp),%esi
lea    0xe44(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tun_alloc>:
push   %rbx
sub    $0x10,%rsp
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
lea    0xe8a(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11b8 <tun_alloc+0x68>
mov    0x2e50(%rip),%rax
mov    0x2e31(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,0x8(%rsp)
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1050 <ioctl@plt>
test   %eax,%eax
js     11c3 <tun_alloc+0x73>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe4e(%rip),%rdi
mov    %ebx,%esi
jmp    11cc <tun_alloc+0x7c>
lea    0xe61(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.ifreq = type { i32 }
@.str = [13 x i8] c"/dev/net/tun\00"
@O_RDWR = global i32 0
@.str.1 = [30 x i8] c"Error opening device, code %d\00"
@IFF_TAP = global i32 0
@IFF_NO_PI = global i32 0
@TUNSETIFF = global i32 0
@.str.2 = [35 x i8] c"Error setting ioctl flags, code %d\00"
define i32 @tun_alloc() {
%1 = alloca %struct.ifreq
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = call i32 @memset(%struct.ifreq* %1, i32 0, i32 4)
%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* %2
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %0
%12 = load i32, i32* %4
%13 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %0
%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* %1, i32 0, i32 0
store i32 %18, i32* %19
%20 = load i32, i32* %2
%21 = load i32, i32* @TUNSETIFF
%22 = bitcast %struct.ifreq* %1 to i8*
%23 = call i32 @ioctl(i32 %20, i32 %21, i8* %22)
store i32 %23, i32* %5
%24 = load i32, i32* %5
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %15
%27 = load i32, i32* %5
%28 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 %27)
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %15
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
%struct.ifreq = type { i32 }
@.str = [13 x i8] c"/dev/net/tun\00"
@O_RDWR = global i32 0
@.str.1 = [30 x i8] c"Error opening device, code %d\00"
@IFF_TAP = global i32 0
@IFF_NO_PI = global i32 0
@TUNSETIFF = global i32 0
@.str.2 = [35 x i8] c"Error setting ioctl flags, code %d\00"
define i32 @tun_alloc() {
%1 = alloca %struct.ifreq
%2 = bitcast %struct.ifreq* %1 to i8*
%3 = call i32 @memset(%struct.ifreq* %1, i32 0, i32 4)
%4 = load i32, i32* @O_RDWR
%5 = 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 %10
7: ; preds = %0
%8 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%9 = call i32 @exit(i32 1)
unreachable
10: ; preds = %0
%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* %1, i64 0, i32 0
store i32 %13, i32* %14
%15 = load i32, i32* @TUNSETIFF
%16 = call i32 @ioctl(i32 %5, i32 %15, i8* %2)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %10
%19 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 %16)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %10
ret i32 %5
}