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

/* Variables and functions */
int IFF_NO_PI;
int IFF_TAP;
int IFF_VNET_HDR;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int, int /*<<< orphan*/, char *);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *, int);
int /*<<< orphan*/ tap_set_flags(int, int, char *);
int /*<<< orphan*/ tap_set_offload(int);
int tap_open(char *devname) {
  const char tun_ctrl_dev[] = "/dev/net/tun";
  int fd;
  fd = open(tun_ctrl_dev, O_RDWR);
  if (fd == -1)
    error(1, errno, "open tun ctrl");
  tap_set_flags(fd, IFF_TAP | IFF_VNET_HDR | IFF_NO_PI, devname);
  tap_set_offload(fd);
  printf("opened tap dev %s fd %d\n", devname, fd);
  return fd;
}


x86 assembly (clang, -O0):
<tap_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0xe9d(%rip),%rax
mov    %rax,-0x15(%rbp)
mov    0xe9b(%rip),%eax
mov    %eax,-0xd(%rbp)
mov    0xe96(%rip),%al
mov    %al,-0x9(%rbp)
lea    -0x15(%rbp),%rdi
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11b2 <tap_open+0x62>
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xe60(%rip),%rdx
call   1050 <error@plt>
mov    -0x1c(%rbp),%edi
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
mov    0x2dfb(%rip),%rax
or     (%rax),%esi
mov    0x2dea(%rip),%rax
or     (%rax),%esi
mov    -0x8(%rbp),%rdx
call   1070 <tap_set_flags@plt>
mov    -0x1c(%rbp),%edi
call   1030 <tap_set_offload@plt>
mov    -0x8(%rbp),%rsi
mov    -0x1c(%rbp),%edx
lea    0xe2c(%rip),%rdi
call   1040 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tap_open>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
movabs $0x6e75742f74656e,%rax
mov    %rax,0xd(%rsp)
movabs $0x74656e2f7665642f,%rax
6e 65 74
mov    %rax,0x8(%rsp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdi
call   1060 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11ab <tap_open+0x5b>
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
lea    0xe6c(%rip),%rdx
mov    $0x1,%edi
call   1050 <error@plt>
mov    0x2e26(%rip),%rax
mov    0x2e07(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2df4(%rip),%rax
or     (%rax),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   1070 <tap_set_flags@plt>
mov    %ebp,%edi
call   1030 <tap_set_offload@plt>
lea    0xe3d(%rip),%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <printf@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@__const.tap_open.tun_ctrl_dev = [13 x i8] c"/dev/net/tun\00"
@O_RDWR = global i32 0
@errno = global i32 0
@.str = [14 x i8] c"open tun ctrl\00"
@IFF_TAP = global i32 0
@IFF_VNET_HDR = global i32 0
@IFF_NO_PI = global i32 0
@.str.1 = [25 x i8] c"opened tap dev %s fd %d\0A\00"
define i32 @tap_open(i8* %0) {
%2 = alloca i8*
%3 = alloca [13 x i8]
%4 = alloca i32
store i8* %0, i8** %2
%5 = bitcast [13 x i8]* %3 to i8*
%6 = getelementptr inbounds [13 x i8], [13 x i8]* %3, i64 0, i64 0
%7 = load i32, i32* @O_RDWR
%8 = call i32 @open(i8* %6, i32 %7)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i32, i32* @errno
%13 = call i32 @error(i32 1, i32 %12, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %11, %1
%15 = load i32, i32* %4
%16 = load i32, i32* @IFF_TAP
%17 = load i32, i32* @IFF_VNET_HDR
%18 = or i32 %16, %17
%19 = load i32, i32* @IFF_NO_PI
%20 = or i32 %18, %19
%21 = load i8*, i8** %2
%22 = call i32 @tap_set_flags(i32 %15, i32 %20, i8* %21)
%23 = load i32, i32* %4
%24 = call i32 @tap_set_offload(i32 %23)
%25 = load i8*, i8** %2
%26 = load i32, i32* %4
%27 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %25, i32 %26)
%28 = load i32, i32* %4
ret i32 %28
}

LLVM IR (-O2):
@__const.tap_open.tun_ctrl_dev = [13 x i8] c"/dev/net/tun\00"
@O_RDWR = global i32 0
@errno = global i32 0
@.str = [14 x i8] c"open tun ctrl\00"
@IFF_TAP = global i32 0
@IFF_VNET_HDR = global i32 0
@IFF_NO_PI = global i32 0
@.str.1 = [25 x i8] c"opened tap dev %s fd %d\0A\00"
define i32 @tap_open(i8* %0) {
%2 = alloca [13 x i8]
%3 = getelementptr inbounds [13 x i8], [13 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @O_RDWR
%5 = call i32 @open(i8* %3, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = load i32, i32* @errno
%9 = tail call i32 @error(i32 1, i32 %8, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %7, %1
%11 = load i32, i32* @IFF_TAP
%12 = load i32, i32* @IFF_VNET_HDR
%13 = or i32 %12, %11
%14 = load i32, i32* @IFF_NO_PI
%15 = or i32 %13, %14
%16 = tail call i32 @tap_set_flags(i32 %5, i32 %15, i8* %0)
%17 = tail call i32 @tap_set_offload(i32 %5)
%18 = tail call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %5)
ret i32 %5
}