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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFFLAGS;
int af_socket(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int /*<<< orphan*/ strlcpy(int /*<<< orphan*/, char const *,
                           int /*<<< orphan*/);
short device_get_flags(const char *ifname) {
  short flags;
  int ret, sock;
  struct ifreq ifr;
  sock = af_socket(AF_INET);
  memset(&ifr, 0, sizeof(ifr));
  strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
  ret = ioctl(sock, SIOCGIFFLAGS, &ifr);
  if (!ret)
    flags = ifr.ifr_flags;
  else
    flags = 0;
  close(sock);
  return flags;
}


x86 assembly (clang, -O0):
<device_get_flags>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%edi
call   1030 <af_socket@plt>
mov    %eax,-0x14(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    -0x1c(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    0x2e45(%rip),%rax
mov    (%rax),%edx
call   1070 <strlcpy@plt>
mov    -0x14(%rbp),%edi
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jne    11c1 <device_get_flags+0x71>
mov    -0x20(%rbp),%ax
mov    %ax,-0xa(%rbp)
jmp    11c7 <device_get_flags+0x77>
movw   $0x0,-0xa(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movswl -0xa(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<device_get_flags>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbp
mov    0x2e87(%rip),%rax
mov    (%rax),%edi
call   1030 <af_socket@plt>
mov    %eax,%ebx
mov    %rsp,%r15
xor    %r14d,%r14d
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x4(%rsp),%edi
mov    0x2e46(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rsi
call   1070 <strlcpy@plt>
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r15,%rdx
call   1050 <ioctl@plt>
test   %eax,%eax
jne    11b0 <device_get_flags+0x60>
movzwl (%rsp),%r14d
mov    %ebx,%edi
call   1060 <close@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i16, i32 }
@AF_INET = global i32 0
@IFNAMSIZ = global i32 0
@SIOCGIFFLAGS = global i32 0
define signext i16 @device_get_flags(i8* %0) {
%2 = alloca i8*
%3 = alloca i16
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.ifreq
store i8* %0, i8** %2
%7 = load i32, i32* @AF_INET
%8 = call i32 @af_socket(i32 %7)
store i32 %8, i32* %5
%9 = call i32 @memset(%struct.ifreq* %6, i32 0, i32 8)
%10 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%11 = load i32, i32* %10
%12 = load i8*, i8** %2
%13 = load i32, i32* @IFNAMSIZ
%14 = call i32 @strlcpy(i32 %11, i8* %12, i32 %13)
%15 = load i32, i32* %5
%16 = load i32, i32* @SIOCGIFFLAGS
%17 = call i32 @ioctl(i32 %15, i32 %16, %struct.ifreq* %6)
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = icmp ne i32 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %1
%21 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%22 = load i16, i16* %21
store i16 %22, i16* %3
br label %24
23: ; preds = %1
store i16 0, i16* %3
br label %24
24: ; preds = %23, %20
%25 = load i32, i32* %5
%26 = call i32 @close(i32 %25)
%27 = load i16, i16* %3
ret i16 %27
}

LLVM IR (-O2):
%struct.ifreq = type { i16, i32 }
@AF_INET = global i32 0
@IFNAMSIZ = global i32 0
@SIOCGIFFLAGS = global i32 0
define signext i16 @device_get_flags(i8* %0) {
%2 = alloca %struct.ifreq
%3 = bitcast %struct.ifreq* %2 to i8*
%4 = load i32, i32* @AF_INET
%5 = tail call i32 @af_socket(i32 %4)
%6 = call i32 @memset(%struct.ifreq* %2, i32 0, i32 8)
%7 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 1
%8 = load i32, i32* %7
%9 = load i32, i32* @IFNAMSIZ
%10 = call i32 @strlcpy(i32 %8, i8* %0, i32 %9)
%11 = load i32, i32* @SIOCGIFFLAGS
%12 = call i32 @ioctl(i32 %5, i32 %11, %struct.ifreq* %2)
%13 = icmp eq i32 %12, 0
%14 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
%15 = load i16, i16* %14
%16 = select i1 %13, i16 %15, i16 0
%17 = call i32 @close(i32 %5)
ret i16 %16
}