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

/* Variables and functions */
int ARPHRD_PPP;
int /*<<< orphan*/ IFF_NO_PI;
int /*<<< orphan*/ IFF_TAP;
int /*<<< orphan*/ IFF_TUN;
int /*<<< orphan*/ IFF_UP;
int IFNAMSIZ;
int /*<<< orphan*/ TUNSETIFF;
int /*<<< orphan*/ TUNSETLINK;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int open_tap_dev();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ set_unset_dev_flags(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
int setup_tap_dev(int linktype) {
  int fd;
  struct ifreq ifr;
  char ifname[IFNAMSIZ];
  fd = open_tap_dev();
  if (fd < 0)
    return -1;
  memset(&ifr, 0, sizeof(ifr));
  if (linktype == ARPHRD_PPP)
    ifr.ifr_flags = IFF_TUN;
  else
    ifr.ifr_flags = IFF_TAP;
  ifr.ifr_flags |= IFF_NO_PI;
  if (ioctl(fd, TUNSETIFF, &ifr) < 0) {
    perror("ioctl(TUNSETIFF)");
    goto err;
  }
  printf("tap dev: %s opened\n", ifr.ifr_name);
  strncpy(ifname, ifr.ifr_name, IFNAMSIZ - 1);
  ifname[IFNAMSIZ - 1] = 0;
  if (set_unset_dev_flags(ifname, IFF_UP, 0)) {
    perror("set_unset_dev_flags(unset IFF_UP)");
    goto err;
  }
  if (ioctl(fd, TUNSETLINK, linktype)) {
    perror("ioctl(TUNSETLINK)");
    goto err;
  }
  if (set_unset_dev_flags(ifname, IFF_UP, 1)) {
    perror("set_unset_dev_flags(set IFF_UP)");
    goto err;
  }
  return fd;
err:
  close(fd);
  return -1;
}


x86 assembly (clang, -O0):
<setup_tap_dev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   1080 <open_tap_dev@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11de <setup_tap_dev+0x5e>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    1353 <setup_tap_dev+0x1d3>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    -0x8(%rbp),%eax
mov    0x2df8(%rip),%rcx
cmp    (%rcx),%eax
jne    1211 <setup_tap_dev+0x91>
mov    0x2da9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
jmp    121d <setup_tap_dev+0x9d>
mov    0x2db8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2d7c(%rip),%rax
mov    (%rax),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2d8a(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
mov    $0x0,%al
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    125e <setup_tap_dev+0xde>
lea    0xdac(%rip),%rdi
call   1090 <perror@plt>
jmp    133d <setup_tap_dev+0x1bd>
mov    -0x20(%rbp),%rsi
lea    0xda8(%rip),%rdi
call   1040 <printf@plt>
mov    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    0x2d4b(%rip),%rax
mov    (%rax),%edx
sub    $0x1,%edx
call   1030 <strncpy@plt>
mov    -0x40(%rbp),%rdi
mov    0x2d36(%rip),%rax
mov    (%rax),%eax
sub    $0x1,%eax
cltq
movb   $0x0,(%rdi,%rax,1)
mov    0x2d3c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <set_unset_dev_flags@plt>
cmp    $0x0,%rax
je     12c8 <setup_tap_dev+0x148>
lea    0xd67(%rip),%rdi
call   1090 <perror@plt>
jmp    133d <setup_tap_dev+0x1bd>
mov    -0xc(%rbp),%edi
mov    0x2ce6(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%edx
mov    $0x0,%al
call   1060 <ioctl@plt>
cmp    $0x0,%rax
je     12f9 <setup_tap_dev+0x179>
lea    0xd58(%rip),%rdi
call   1090 <perror@plt>
jmp    133d <setup_tap_dev+0x1bd>
mov    -0x40(%rbp),%rdi
mov    0x2cdc(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edx
call   10a0 <set_unset_dev_flags@plt>
cmp    $0x0,%rax
je     132b <setup_tap_dev+0x1ab>
lea    0xd38(%rip),%rdi
call   1090 <perror@plt>
jmp    133d <setup_tap_dev+0x1bd>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    1353 <setup_tap_dev+0x1d3>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_tap_dev>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r15d
mov    %rsp,%r13
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
xor    %eax,%eax
call   1080 <open_tap_dev@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
js     12e3 <setup_tap_dev+0x163>
mov    %eax,%r12d
mov    %r13,-0x30(%rbp)
lea    -0x40(%rbp),%r13
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2e09(%rip),%rax
cmp    %r15d,(%rax)
jne    11f5 <setup_tap_dev+0x75>
mov    0x2dbd(%rip),%rax
jmp    11fc <setup_tap_dev+0x7c>
mov    0x2dd4(%rip),%rax
mov    (%rax),%eax
mov    0x2d9b(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x38(%rbp)
mov    0x2daf(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
mov    %r13,%rdx
xor    %eax,%eax
call   1060 <ioctl@plt>
test   %rax,%rax
js     12b0 <setup_tap_dev+0x130>
mov    -0x40(%rbp),%rsi
lea    0xddd(%rip),%rdi
call   1040 <printf@plt>
mov    -0x40(%rbp),%rsi
mov    0x2d84(%rip),%r13
mov    0x0(%r13),%edx
add    $0xffffffff,%edx
mov    %rbx,%rdi
call   1030 <strncpy@plt>
movslq 0x0(%r13),%rax
movb   $0x0,-0x1(%rax,%rbx,1)
mov    0x2d7d(%rip),%r13
mov    0x0(%r13),%esi
mov    %rbx,%rdi
xor    %edx,%edx
call   10a0 <set_unset_dev_flags@plt>
test   %rax,%rax
jne    12b9 <setup_tap_dev+0x139>
mov    0x2d3b(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
mov    %r15d,%edx
xor    %eax,%eax
call   1060 <ioctl@plt>
test   %rax,%rax
jne    12c2 <setup_tap_dev+0x142>
mov    0x0(%r13),%esi
mov    %rbx,%rdi
mov    $0x1,%edx
call   10a0 <set_unset_dev_flags@plt>
test   %rax,%rax
jne    12cb <setup_tap_dev+0x14b>
mov    %r12d,%r14d
mov    -0x30(%rbp),%r13
jmp    12e3 <setup_tap_dev+0x163>
lea    0xd49(%rip),%rdi
jmp    12d2 <setup_tap_dev+0x152>
lea    0xd65(%rip),%rdi
jmp    12d2 <setup_tap_dev+0x152>
lea    0xd7e(%rip),%rdi
jmp    12d2 <setup_tap_dev+0x152>
lea    0xd87(%rip),%rdi
mov    -0x30(%rbp),%r13
call   1090 <perror@plt>
mov    %r12d,%edi
call   1070 <close@plt>
mov    %r13,%rsp
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i8*, i32 }
@IFNAMSIZ = global i32 0
@ARPHRD_PPP = global i32 0
@IFF_TUN = global i32 0
@IFF_TAP = global i32 0
@IFF_NO_PI = global i32 0
@TUNSETIFF = global i32 0
@.str = [17 x i8] c"ioctl(TUNSETIFF)\00"
@.str.1 = [20 x i8] c"tap dev: %s opened\0A\00"
@IFF_UP = global i32 0
@.str.2 = [34 x i8] c"set_unset_dev_flags(unset IFF_UP)\00"
@TUNSETLINK = global i32 0
@.str.3 = [18 x i8] c"ioctl(TUNSETLINK)\00"
@.str.4 = [32 x i8] c"set_unset_dev_flags(set IFF_UP)\00"
define i32 @setup_tap_dev(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.ifreq
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
store i32 %0, i32* %3
%9 = load i32, i32* @IFNAMSIZ
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %6
%12 = alloca i8, i64 %10
store i64 %10, i64* %7
%13 = call i32 (...) @open_tap_dev()
store i32 %13, i32* %4
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %1
store i32 -1, i32* %2
store i32 1, i32* %8
br label %76
17: ; preds = %1
%18 = call i32 @memset(%struct.ifreq* %5, i32 0, i32 16)
%19 = load i32, i32* %3
%20 = load i32, i32* @ARPHRD_PPP
%21 = icmp eq i32 %19, %20
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = load i32, i32* @IFF_TUN
%24 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 1
store i32 %23, i32* %24
br label %28
25: ; preds = %17
%26 = load i32, i32* @IFF_TAP
%27 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 1
store i32 %26, i32* %27
br label %28
28: ; preds = %25, %22
%29 = load i32, i32* @IFF_NO_PI
%30 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 1
%31 = load i32, i32* %30
%32 = or i32 %31, %29
store i32 %32, i32* %30
%33 = load i32, i32* %4
%34 = load i32, i32* @TUNSETIFF
%35 = call i64 (i32, i32, ...) @ioctl(i32 %33, i32 %34, %struct.ifreq* %5)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %28
%38 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %73
39: ; preds = %28
%40 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 0
%41 = load i8*, i8** %40
%42 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %41)
%43 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 0
%44 = load i8*, i8** %43
%45 = load i32, i32* @IFNAMSIZ
%46 = sub nsw i32 %45, 1
%47 = call i32 @strncpy(i8* %12, i8* %44, i32 %46)
%48 = load i32, i32* @IFNAMSIZ
%49 = sub nsw i32 %48, 1
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %12, i64 %50
store i8 0, i8* %51
%52 = load i32, i32* @IFF_UP
%53 = call i64 @set_unset_dev_flags(i8* %12, i32 %52, i32 0)
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %39
%56 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
br label %73
57: ; preds = %39
%58 = load i32, i32* %4
%59 = load i32, i32* @TUNSETLINK
%60 = load i32, i32* %3
%61 = call i64 (i32, i32, ...) @ioctl(i32 %58, i32 %59, i32 %60)
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %65
63: ; preds = %57
%64 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
br label %73
65: ; preds = %57
%66 = load i32, i32* @IFF_UP
%67 = call i64 @set_unset_dev_flags(i8* %12, i32 %66, i32 1)
%68 = icmp ne i64 %67, 0
br i1 %68, label %69, label %71
69: ; preds = %65
%70 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0))
br label %73
71: ; preds = %65
%72 = load i32, i32* %4
store i32 %72, i32* %2
store i32 1, i32* %8
br label %76
73: ; preds = %69, %63, %55, %37
%74 = load i32, i32* %4
%75 = call i32 @close(i32 %74)
store i32 -1, i32* %2
store i32 1, i32* %8
br label %76
76: ; preds = %73, %71, %16
%77 = load i8*, i8** %6
%78 = load i32, i32* %2
ret i32 %78
}

LLVM IR (-O2):
%struct.ifreq = type { i8*, i32 }
@IFNAMSIZ = global i32 0
@ARPHRD_PPP = global i32 0
@IFF_TUN = global i32 0
@IFF_TAP = global i32 0
@IFF_NO_PI = global i32 0
@TUNSETIFF = global i32 0
@.str = [17 x i8] c"ioctl(TUNSETIFF)\00"
@.str.1 = [20 x i8] c"tap dev: %s opened\0A\00"
@IFF_UP = global i32 0
@.str.2 = [34 x i8] c"set_unset_dev_flags(unset IFF_UP)\00"
@TUNSETLINK = global i32 0
@.str.3 = [18 x i8] c"ioctl(TUNSETLINK)\00"
@.str.4 = [32 x i8] c"set_unset_dev_flags(set IFF_UP)\00"
define i32 @setup_tap_dev(i32 %0) {
%2 = alloca %struct.ifreq
%3 = bitcast %struct.ifreq* %2 to i8*
%4 = load i32, i32* @IFNAMSIZ
%5 = zext i32 %4 to i64
%6 = tail call i8* @llvm.stacksave()
%7 = alloca i8, i64 %5
%8 = tail call i32 (...) @open_tap_dev()
%9 = icmp slt i32 %8, 0
br i1 %9, label %50, label %10
10: ; preds = %1
%11 = call i32 @memset(%struct.ifreq* %2, i32 0, i32 16)
%12 = load i32, i32* @ARPHRD_PPP
%13 = icmp eq i32 %12, %0
%14 = load i32, i32* @IFF_TUN
%15 = load i32, i32* @IFF_TAP
%16 = select i1 %13, i32 %14, i32 %15
%17 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 1
%18 = load i32, i32* @IFF_NO_PI
%19 = or i32 %16, %18
store i32 %19, i32* %17
%20 = load i32, i32* @TUNSETIFF
%21 = call i64 (i32, i32, ...) @ioctl(i32 %8, i32 %20, %struct.ifreq* %2)
%22 = icmp slt i64 %21, 0
br i1 %22, label %46, label %23
23: ; preds = %10
%24 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
%25 = load i8*, i8** %24
%26 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %25)
%27 = load i8*, i8** %24
%28 = load i32, i32* @IFNAMSIZ
%29 = add nsw i32 %28, -1
%30 = call i32 @strncpy(i8* %7, i8* %27, i32 %29)
%31 = load i32, i32* @IFNAMSIZ
%32 = add nsw i32 %31, -1
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %7, i64 %33
store i8 0, i8* %34
%35 = load i32, i32* @IFF_UP
%36 = call i64 @set_unset_dev_flags(i8* %7, i32 %35, i32 0)
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %46
38: ; preds = %23
%39 = load i32, i32* @TUNSETLINK
%40 = call i64 (i32, i32, ...) @ioctl(i32 %8, i32 %39, i32 %0)
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %38
%43 = load i32, i32* @IFF_UP
%44 = call i64 @set_unset_dev_flags(i8* %7, i32 %43, i32 1)
%45 = icmp eq i64 %44, 0
br i1 %45, label %50, label %46
46: ; preds = %42, %38, %23, %10
%47 = phi i8* [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), %10 ], [ getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), %23 ], [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), %38 ], [ getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), %42 ]
%48 = call i32 @perror(i8* %47)
%49 = call i32 @close(i32 %8)
br label %50
50: ; preds = %42, %1, %46
%51 = phi i32 [ -1, %46 ], [ -1, %1 ], [ %8, %42 ]
ret i32 %51
}