#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*/ 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*/ 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_test_flag(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 testing interface flags: %s\n", dev,
            strerror(errno));
    return -1;
  }
  close(fd);
  return (ifr.ifr_flags & flags);
}


x86 assembly (clang, -O0):
<tap_test_flag>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %si,%ax
mov    %rdi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e2b(%rip),%rax
mov    (%rax),%edx
call   1050 <safe_strncpy@plt>
mov    -0x24(%rbp),%edi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    1207 <tap_test_flag+0xa7>
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xe0a(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121c <tap_test_flag+0xbc>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movswl -0x20(%rbp),%eax
movswl -0x12(%rbp),%ecx
and    %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tap_test_flag>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebx
mov    0x2e39(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rsi
call   1050 <safe_strncpy@plt>
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
js     11c4 <tap_test_flag+0x64>
mov    %ebx,%edi
call   1040 <close@plt>
and    0x8(%rsp),%bp
movswl %bp,%eax
jmp    11f4 <tap_test_flag+0x94>
mov    0x2e05(%rip),%rax
mov    (%rax),%ebx
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe1e(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1060 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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 testing interface flags: %s\0A\00"
@errno = global i32 0
define i32 @tap_test_flag(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 %35
26: ; preds = %2
%27 = load i32, i32* %7
%28 = call i32 @close(i32 %27)
%29 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%30 = load i16, i16* %29
%31 = sext i16 %30 to i32
%32 = load i16, i16* %5
%33 = sext i16 %32 to i32
%34 = and i32 %31, %33
store i32 %34, i32* %3
br label %35
35: ; preds = %26, %20
%36 = load i32, i32* %3
ret i32 %36
}

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 testing interface flags: %s\0A\00"
@errno = global i32 0
define i32 @tap_test_flag(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 %24
18: ; preds = %2
%19 = call i32 @close(i32 %7)
%20 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
%21 = load i16, i16* %20
%22 = and i16 %21, %1
%23 = sext i16 %22 to i32
br label %24
24: ; preds = %18, %13
%25 = phi i32 [ -1, %13 ], [ %23, %18 ]
ret i32 %25
}