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

/* Variables and functions */
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SIOCGIFFLAGS;
int /*<<< orphan*/ SIOCSIFFLAGS;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ safe_strncpy(int /*<<< orphan*/, char const *,
                                int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int tap_set_flags(const char *dev, short flags) {
  struct ifreq ifr;
  int fd;
  fd = socket(PF_INET, SOCK_DGRAM, 0);
  safe_strncpy(ifr.ifr_name, dev, IFNAMSIZ);
  if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
    fprintf(stderr, "%s: ERROR while getting interface flags: %s\n", dev,
            strerror(errno));
    return (-1);
  }
  safe_strncpy(ifr.ifr_name, dev, IFNAMSIZ);
  ifr.ifr_flags |= flags;
  if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0) {
    perror("SIOCSIFFLAGS");
    return -1;
  }
  close(fd);
  return (0);
}


x86 assembly (clang, -O0):
<tap_set_flags>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %si,%ax
mov    %rdi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e13(%rip),%rax
mov    (%rax),%edx
call   1050 <safe_strncpy@plt>
mov    -0x24(%rbp),%edi
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    1217 <tap_set_flags+0xa7>
mov    0x2dee(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xdfa(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1280 <tap_set_flags+0x110>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2d9b(%rip),%rax
mov    (%rax),%edx
call   1050 <safe_strncpy@plt>
movswl -0x12(%rbp),%ecx
movswl -0x20(%rbp),%eax
or     %ecx,%eax
mov    %ax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2db4(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    1271 <tap_set_flags+0x101>
lea    0xdcd(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1280 <tap_set_flags+0x110>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tap_set_flags>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%rbx
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ebp
mov    0x2e21(%rip),%r15
mov    (%r15),%edx
mov    %rbx,%rsi
call   1050 <safe_strncpy@plt>
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
js     11f9 <tap_set_flags+0x89>
mov    0x4(%rsp),%edi
mov    (%r15),%edx
mov    %rbx,%rsi
call   1050 <safe_strncpy@plt>
or     %r14w,(%rsp)
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
js     1234 <tap_set_flags+0xc4>
mov    %ebp,%edi
call   1040 <close@plt>
xor    %eax,%eax
jmp    1229 <tap_set_flags+0xb9>
mov    0x2dc8(%rip),%rax
mov    (%rax),%ebp
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xde9(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1060 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdf2(%rip),%rdi
call   1070 <perror@plt>
jmp    1224 <tap_set_flags+0xb4>

LLVM IR (-O0):
%struct.ifreq = type { i16, i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IFNAMSIZ = global i32 0
@SIOCGIFFLAGS = global i32 0
@stderr = global i32 0
@.str = [45 x i8] c"%s: ERROR while getting interface flags: %s\0A\00"
@errno = global i32 0
@SIOCSIFFLAGS = global i32 0
@.str.1 = [13 x i8] c"SIOCSIFFLAGS\00"
define i32 @tap_set_flags(i8* %0, i16 signext %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i16
%6 = alloca %struct.ifreq
%7 = alloca i32
store i8* %0, i8** %4
store i16 %1, i16* %5
%8 = load i32, i32* @PF_INET
%9 = load i32, i32* @SOCK_DGRAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %7
%11 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%12 = load i32, i32* %11
%13 = load i8*, i8** %4
%14 = load i32, i32* @IFNAMSIZ
%15 = call i32 @safe_strncpy(i32 %12, i8* %13, i32 %14)
%16 = load i32, i32* %7
%17 = load i32, i32* @SIOCGIFFLAGS
%18 = call i64 @ioctl(i32 %16, i32 %17, %struct.ifreq* %6)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %2
%21 = load i32, i32* @stderr
%22 = load i8*, i8** %4
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i8* %22, i8* %24)
store i32 -1, i32* %3
br label %48
26: ; preds = %2
%27 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%28 = load i32, i32* %27
%29 = load i8*, i8** %4
%30 = load i32, i32* @IFNAMSIZ
%31 = call i32 @safe_strncpy(i32 %28, i8* %29, i32 %30)
%32 = load i16, i16* %5
%33 = sext i16 %32 to i32
%34 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%35 = load i16, i16* %34
%36 = sext i16 %35 to i32
%37 = or i32 %36, %33
%38 = trunc i32 %37 to i16
store i16 %38, i16* %34
%39 = load i32, i32* %7
%40 = load i32, i32* @SIOCSIFFLAGS
%41 = call i64 @ioctl(i32 %39, i32 %40, %struct.ifreq* %6)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %26
%44 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %48
45: ; preds = %26
%46 = load i32, i32* %7
%47 = call i32 @close(i32 %46)
store i32 0, i32* %3
br label %48
48: ; preds = %45, %43, %20
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
%struct.ifreq = type { i16, i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IFNAMSIZ = global i32 0
@SIOCGIFFLAGS = global i32 0
@stderr = global i32 0
@.str = [45 x i8] c"%s: ERROR while getting interface flags: %s\0A\00"
@errno = global i32 0
@SIOCSIFFLAGS = global i32 0
@.str.1 = [13 x i8] c"SIOCSIFFLAGS\00"
define i32 @tap_set_flags(i8* %0, i16 signext %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = load i32, i32* @PF_INET
%6 = load i32, i32* @SOCK_DGRAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = load i32, i32* @IFNAMSIZ
%9 = tail call i32 @safe_strncpy(i32 undef, i8* %0, i32 %8)
%10 = load i32, i32* @SIOCGIFFLAGS
%11 = call i64 @ioctl(i32 %7, i32 %10, %struct.ifreq* %3)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i32, i32* @errno
%16 = call i8* @strerror(i32 %15)
%17 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %16)
br label %33
18: ; preds = %2
%19 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%20 = load i32, i32* %19
%21 = load i32, i32* @IFNAMSIZ
%22 = call i32 @safe_strncpy(i32 %20, i8* %0, i32 %21)
%23 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
%24 = load i16, i16* %23
%25 = or i16 %24, %1
store i16 %25, i16* %23
%26 = load i32, i32* @SIOCSIFFLAGS
%27 = call i64 @ioctl(i32 %7, i32 %26, %struct.ifreq* %3)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %18
%30 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %33
31: ; preds = %18
%32 = call i32 @close(i32 %7)
br label %33
33: ; preds = %31, %29, %13
%34 = phi i32 [ -1, %13 ], [ -1, %29 ], [ 0, %31 ]
ret i32 %34
}