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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ IFF_BROADCAST;
scalar_t__ IFF_LOOPBACK;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFFLAGS;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int _sysapi_ifflags(char *ifname, int flag) {
  int ret;
  int fd;
  struct ifreq ifr;
  fd = socket(AF_INET, SOCK_DGRAM, 0);
  if (fd < 0)
    return -1;
  memset(&ifr, 0, sizeof(struct ifreq));
  strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
  ret = ioctl(fd, SIOCGIFFLAGS, &ifr);
  if (ret < 0) {
    close(fd);
    return -1;
  }
  close(fd);
  if (flag == 1) {
    return ifr.ifr_flags && IFF_LOOPBACK;
  } else if (flag == 2) {
    return ifr.ifr_flags && IFF_BROADCAST;
  }
  return -1;
}


x86 assembly (clang, -O0):
<_sysapi_ifflags>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%edi
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1191 <_sysapi_ifflags+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    1276 <_sysapi_ifflags+0x126>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    -0x28(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e11(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x1c(%rbp),%edi
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11ec <_sysapi_ifflags+0x9c>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1276 <_sysapi_ifflags+0x126>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
cmpl   $0x1,-0x14(%rbp)
jne    122f <_sysapi_ifflags+0xdf>
xor    %eax,%eax
cmpq   $0x0,-0x30(%rbp)
mov    %al,-0x31(%rbp)
je     121f <_sysapi_ifflags+0xcf>
mov    0x2de3(%rip),%rax
cmpq   $0x0,(%rax)
setne  %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
jmp    1276 <_sysapi_ifflags+0x126>
cmpl   $0x2,-0x14(%rbp)
jne    126a <_sysapi_ifflags+0x11a>
xor    %eax,%eax
cmpq   $0x0,-0x30(%rbp)
mov    %al,-0x32(%rbp)
je     125a <_sysapi_ifflags+0x10a>
mov    0x2d68(%rip),%rax
cmpq   $0x0,(%rax)
setne  %al
mov    %al,-0x32(%rbp)
mov    -0x32(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
jmp    1276 <_sysapi_ifflags+0x126>
jmp    126f <_sysapi_ifflags+0x11f>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_sysapi_ifflags>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
mov    0x2e77(%rip),%rax
mov    (%rax),%edi
mov    0x2e66(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    $0xffffffff,%r15d
test   %eax,%eax
js     120b <_sysapi_ifflags+0xbb>
mov    %eax,%ebp
mov    %rsp,%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x8(%rsp),%edi
mov    0x2e18(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rsi
call   1030 <strncpy@plt>
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r12,%rdx
call   1050 <ioctl@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1060 <close@plt>
test   %ebx,%ebx
js     120b <_sysapi_ifflags+0xbb>
cmp    $0x2,%r14d
je     11ef <_sysapi_ifflags+0x9f>
cmp    $0x1,%r14d
jne    120b <_sysapi_ifflags+0xbb>
cmpq   $0x0,(%rsp)
setne  %al
mov    0x2e0b(%rip),%rcx
jmp    11fe <_sysapi_ifflags+0xae>
cmpq   $0x0,(%rsp)
setne  %al
mov    0x2dba(%rip),%rcx
cmpq   $0x0,(%rcx)
setne  %cl
and    %al,%cl
movzbl %cl,%r15d
mov    %r15d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i64, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IFNAMSIZ = global i32 0
@SIOCGIFFLAGS = global i32 0
@IFF_LOOPBACK = global i64 0
@IFF_BROADCAST = global i64 0
define i32 @_sysapi_ifflags(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.ifreq
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_DGRAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %2
store i32 -1, i32* %3
br label %60
15: ; preds = %2
%16 = call i32 @memset(%struct.ifreq* %8, i32 0, i32 16)
%17 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 1
%18 = load i32, i32* %17
%19 = load i8*, i8** %4
%20 = load i32, i32* @IFNAMSIZ
%21 = call i32 @strncpy(i32 %18, i8* %19, i32 %20)
%22 = load i32, i32* %7
%23 = load i32, i32* @SIOCGIFFLAGS
%24 = call i32 @ioctl(i32 %22, i32 %23, %struct.ifreq* %8)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %15
%28 = load i32, i32* %7
%29 = call i32 @close(i32 %28)
store i32 -1, i32* %3
br label %60
30: ; preds = %15
%31 = load i32, i32* %7
%32 = call i32 @close(i32 %31)
%33 = load i32, i32* %5
%34 = icmp eq i32 %33, 1
br i1 %34, label %35, label %45
35: ; preds = %30
%36 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 0
%37 = load i64, i64* %36
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %35
%40 = load i64, i64* @IFF_LOOPBACK
%41 = icmp ne i64 %40, 0
br label %42
42: ; preds = %39, %35
%43 = phi i1 [ false, %35 ], [ %41, %39 ]
%44 = zext i1 %43 to i32
store i32 %44, i32* %3
br label %60
45: ; preds = %30
%46 = load i32, i32* %5
%47 = icmp eq i32 %46, 2
br i1 %47, label %48, label %58
48: ; preds = %45
%49 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 0
%50 = load i64, i64* %49
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %55
52: ; preds = %48
%53 = load i64, i64* @IFF_BROADCAST
%54 = icmp ne i64 %53, 0
br label %55
55: ; preds = %52, %48
%56 = phi i1 [ false, %48 ], [ %54, %52 ]
%57 = zext i1 %56 to i32
store i32 %57, i32* %3
br label %60
58: ; preds = %45
br label %59
59: ; preds = %58
store i32 -1, i32* %3
br label %60
60: ; preds = %59, %55, %42, %27, %14
%61 = load i32, i32* %3
ret i32 %61
}

LLVM IR (-O2):
%struct.ifreq = type { i64, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IFNAMSIZ = global i32 0
@SIOCGIFFLAGS = global i32 0
@IFF_LOOPBACK = global i64 0
@IFF_BROADCAST = global i64 0
define i32 @_sysapi_ifflags(i8* %0, i32 %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_DGRAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %36, label %9
9: ; preds = %2
%10 = call i32 @memset(%struct.ifreq* %3, i32 0, i32 16)
%11 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%12 = load i32, i32* %11
%13 = load i32, i32* @IFNAMSIZ
%14 = call i32 @strncpy(i32 %12, i8* %0, i32 %13)
%15 = load i32, i32* @SIOCGIFFLAGS
%16 = call i32 @ioctl(i32 %7, i32 %15, %struct.ifreq* %3)
%17 = icmp slt i32 %16, 0
%18 = call i32 @close(i32 %7)
br i1 %17, label %36, label %19
19: ; preds = %9
switch i32 %1, label %36 [
i32 1, label %20
i32 2, label %28
]
20: ; preds = %19
%21 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
%22 = load i64, i64* %21
%23 = icmp ne i64 %22, 0
%24 = load i64, i64* @IFF_LOOPBACK
%25 = icmp ne i64 %24, 0
%26 = select i1 %23, i1 %25, i1 false
%27 = zext i1 %26 to i32
br label %36
28: ; preds = %19
%29 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
%30 = load i64, i64* %29
%31 = icmp ne i64 %30, 0
%32 = load i64, i64* @IFF_BROADCAST
%33 = icmp ne i64 %32, 0
%34 = select i1 %31, i1 %33, i1 false
%35 = zext i1 %34 to i32
br label %36
36: ; preds = %9, %19, %2, %28, %20
%37 = phi i32 [ %27, %20 ], [ %35, %28 ], [ -1, %2 ], [ -1, %19 ], [ -1, %9 ]
ret i32 %37
}