#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_clr_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_clr_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_clr_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    1283 <tap_clr_flags+0x113>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2d9b(%rip),%rax
mov    (%rax),%edx
call   1050 <safe_strncpy@plt>
movswl -0x12(%rbp),%ecx
xor    $0xffffffff,%ecx
movswl -0x20(%rbp),%eax
and    %ecx,%eax
mov    %ax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2db1(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    1274 <tap_clr_flags+0x104>
lea    0xdca(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1283 <tap_clr_flags+0x113>
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_clr_flags>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %rdi,%r14
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ebp
mov    0x2e22(%rip),%r15
mov    (%r15),%edx
mov    %r14,%rsi
call   1050 <safe_strncpy@plt>
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
js     11f9 <tap_clr_flags+0x89>
mov    0x4(%rsp),%edi
mov    (%r15),%edx
mov    %r14,%rsi
call   1050 <safe_strncpy@plt>
not    %ebx
and    %bx,(%rsp)
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
js     1234 <tap_clr_flags+0xc4>
mov    %ebp,%edi
call   1040 <close@plt>
xor    %eax,%eax
jmp    1229 <tap_clr_flags+0xb9>
mov    0x2dc8(%rip),%rax
mov    (%rax),%ebx
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xde9(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%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_clr_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_clr_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 %49
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 = xor i32 %33, -1
%35 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%36 = load i16, i16* %35
%37 = sext i16 %36 to i32
%38 = and i32 %37, %34
%39 = trunc i32 %38 to i16
store i16 %39, i16* %35
%40 = load i32, i32* %7
%41 = load i32, i32* @SIOCSIFFLAGS
%42 = call i64 @ioctl(i32 %40, i32 %41, %struct.ifreq* %6)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %26
%45 = 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 %49
46: ; preds = %26
%47 = load i32, i32* %7
%48 = call i32 @close(i32 %47)
store i32 0, i32* %3
br label %49
49: ; preds = %46, %44, %20
%50 = load i32, i32* %3
ret i32 %50
}

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_clr_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 %34
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 = xor i16 %1, -1
%24 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
%25 = load i16, i16* %24
%26 = and i16 %25, %23
store i16 %26, i16* %24
%27 = load i32, i32* @SIOCSIFFLAGS
%28 = call i64 @ioctl(i32 %7, i32 %27, %struct.ifreq* %3)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %18
%31 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %34
32: ; preds = %18
%33 = call i32 @close(i32 %7)
br label %34
34: ; preds = %32, %30, %13
%35 = phi i32 [ -1, %13 ], [ -1, %30 ], [ 0, %32 ]
ret i32 %35
}