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

/* Variables and functions */
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SIOCGIFFLAGS;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *,
                           int /*<<< orphan*/);
int wpa_driver_wired_get_ifflags(const char *ifname, int *flags) {
  struct ifreq ifr;
  int s;
  s = socket(PF_INET, SOCK_DGRAM, 0);
  if (s < 0) {
    perror("socket");
    return -1;
  }
  memset(&ifr, 0, sizeof(ifr));
  strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
  if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
    perror("ioctl[SIOCGIFFLAGS]");
    close(s);
    return -1;
  }
  close(s);
  *flags = ifr.ifr_flags & 0xffff;
  return 0;
}


x86 assembly (clang, -O0):
<wpa_driver_wired_get_ifflags>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11ae <wpa_driver_wired_get_ifflags+0x4e>
lea    0xe63(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1232 <wpa_driver_wired_get_ifflags+0xd2>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e04(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x24(%rbp),%edi
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rax
mov    %eax,%edx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    1214 <wpa_driver_wired_get_ifflags+0xb4>
lea    0xe0c(%rip),%rdi
call   1070 <perror@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1232 <wpa_driver_wired_get_ifflags+0xd2>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
mov    -0x20(%rbp),%ecx
and    $0xffff,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wpa_driver_wired_get_ifflags>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
mov    0x2e66(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
test   %eax,%eax
js     11f4 <wpa_driver_wired_get_ifflags+0x94>
mov    %eax,%ebp
lea    0x8(%rsp),%r12
xor    %r15d,%r15d
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0xc(%rsp),%edi
mov    0x2e1d(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rsi
call   1030 <strncpy@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r12d,%edx
call   1050 <ioctl@plt>
test   %rax,%rax
js     1208 <wpa_driver_wired_get_ifflags+0xa8>
mov    %ebp,%edi
call   1060 <close@plt>
movzwl 0x8(%rsp),%eax
mov    %eax,(%r14)
mov    %r15d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe05(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%r15d
jmp    11e4 <wpa_driver_wired_get_ifflags+0x84>
lea    0xdf8(%rip),%rdi
call   1070 <perror@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    $0xffffffff,%r15d
jmp    11e4 <wpa_driver_wired_get_ifflags+0x84>

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [7 x i8] c"socket\00"
@IFNAMSIZ = global i32 0
@SIOCGIFFLAGS = global i32 0
@.str.1 = [20 x i8] c"ioctl[SIOCGIFFLAGS]\00"
define i32 @wpa_driver_wired_get_ifflags(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca %struct.ifreq
%7 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %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 = load i32, i32* %7
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %38
15: ; preds = %2
%16 = call i32 @memset(%struct.ifreq* %6, i32 0, i32 8)
%17 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, 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 = ptrtoint %struct.ifreq* %6 to i32
%25 = call i64 @ioctl(i32 %22, i32 %23, i32 %24)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %15
%28 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%29 = load i32, i32* %7
%30 = call i32 @close(i32 %29)
store i32 -1, i32* %3
br label %38
31: ; preds = %15
%32 = load i32, i32* %7
%33 = call i32 @close(i32 %32)
%34 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%35 = load i32, i32* %34
%36 = and i32 %35, 65535
%37 = load i32*, i32** %5
store i32 %36, i32* %37
store i32 0, i32* %3
br label %38
38: ; preds = %31, %27, %13
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [7 x i8] c"socket\00"
@IFNAMSIZ = global i32 0
@SIOCGIFFLAGS = global i32 0
@.str.1 = [20 x i8] c"ioctl[SIOCGIFFLAGS]\00"
define i32 @wpa_driver_wired_get_ifflags(i8* %0, i32* nocapture writeonly %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 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %30
11: ; preds = %2
%12 = call i32 @memset(%struct.ifreq* %3, i32 0, i32 8)
%13 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%14 = load i32, i32* %13
%15 = load i32, i32* @IFNAMSIZ
%16 = call i32 @strncpy(i32 %14, i8* %0, i32 %15)
%17 = load i32, i32* @SIOCGIFFLAGS
%18 = ptrtoint %struct.ifreq* %3 to i64
%19 = trunc i64 %18 to i32
%20 = call i64 @ioctl(i32 %7, i32 %17, i32 %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %11
%23 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @close(i32 %7)
br label %30
25: ; preds = %11
%26 = call i32 @close(i32 %7)
%27 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
%28 = load i32, i32* %27
%29 = and i32 %28, 65535
store i32 %29, i32* %1
br label %30
30: ; preds = %25, %22, %9
%31 = phi i32 [ -1, %9 ], [ -1, %22 ], [ 0, %25 ]
ret i32 %31
}