#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 */
typedef struct TYPE_6__ TYPE_3__;
typedef struct TYPE_5__ TYPE_2__;
typedef struct TYPE_4__ TYPE_1__;

/* Type definitions */
struct TYPE_6__ {
  int level;
};
struct TYPE_4__ {
  int length;
  int flags;
  struct iwreq *pointer;
};
struct TYPE_5__ {
  TYPE_1__ data;
};
struct iwreq {
  TYPE_3__ qual;
  int /*<<< orphan*/ ifr_name;
  TYPE_2__ u;
};
struct iw_statistics {
  TYPE_3__ qual;
  int /*<<< orphan*/ ifr_name;
  TYPE_2__ u;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ SIOCGIWSTATS;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, struct iwreq *);
int /*<<< orphan*/ memset(struct iwreq *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int sysapi_get_wlan_rssi(char *ifname, int *rss) {
  int ret;
  int fd;
  fd = socket(AF_INET, SOCK_DGRAM, 0);
  if (fd < 0)
    return -1;
  struct iwreq iwr;
  struct iw_statistics stats;
  memset(&iwr, 0, sizeof(struct iwreq));
  memset(&stats, 0, sizeof(struct iw_statistics));
  iwr.u.data.pointer = &stats;
  iwr.u.data.length = sizeof(struct iw_statistics);
  iwr.u.data.flags = 1;
  strncpy(iwr.ifr_name, ifname, IFNAMSIZ);
  ret = ioctl(fd, SIOCGIWSTATS, &iwr);
  if (ret < 0) {
    close(fd);
    return -1;
  }
  *rss = stats.qual.level;
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<sysapi_get_wlan_rssi>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e81(%rip),%rax
mov    (%rax),%edi
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1192 <sysapi_get_wlan_rssi+0x42>
movl   $0xffffffff,-0x4(%rbp)
jmp    122b <sysapi_get_wlan_rssi+0xdb>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
lea    -0x50(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x18,-0x30(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x34(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2dfa(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%edi
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1213 <sysapi_get_wlan_rssi+0xc3>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122b <sysapi_get_wlan_rssi+0xdb>
mov    -0x50(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysapi_get_wlan_rssi>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e7d(%rip),%rax
mov    (%rax),%edi
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
test   %eax,%eax
js     11f7 <sysapi_get_wlan_rssi+0xa7>
mov    %eax,%ebx
lea    0x8(%rsp),%r12
xor    %r13d,%r13d
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
lea    0x20(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    %rbp,0x18(%rsp)
movabs $0x100000018,%rax
mov    %rax,0x10(%rsp)
mov    0xc(%rsp),%edi
mov    0x2e03(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rsi
call   1030 <strncpy@plt>
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r12,%rdx
call   1050 <ioctl@plt>
test   %eax,%eax
js     11ff <sysapi_get_wlan_rssi+0xaf>
mov    0x20(%rsp),%eax
mov    %eax,(%r14)
jmp    1205 <sysapi_get_wlan_rssi+0xb5>
mov    $0xffffffff,%r13d
jmp    120c <sysapi_get_wlan_rssi+0xbc>
mov    $0xffffffff,%r13d
mov    %ebx,%edi
call   1060 <close@plt>
mov    %r13d,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.iwreq = type { %struct.TYPE_6__, i32, %struct.TYPE_5__ }
%struct.TYPE_6__ = type { i32 }
%struct.TYPE_5__ = type { %struct.TYPE_4__ }
%struct.TYPE_4__ = type { i32, i32, %struct.iwreq* }
%struct.iw_statistics = type { %struct.TYPE_6__, i32, %struct.TYPE_5__ }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IFNAMSIZ = global i32 0
@SIOCGIWSTATS = global i32 0
define i32 @sysapi_get_wlan_rssi(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.iwreq
%9 = alloca %struct.iw_statistics
store i8* %0, i8** %4
store i32* %1, i32** %5
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_DGRAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %7
%13 = load i32, i32* %7
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 -1, i32* %3
br label %50
16: ; preds = %2
%17 = call i32 @memset(%struct.iwreq* %8, i32 0, i32 24)
%18 = bitcast %struct.iw_statistics* %9 to %struct.iwreq*
%19 = call i32 @memset(%struct.iwreq* %18, i32 0, i32 24)
%20 = bitcast %struct.iw_statistics* %9 to %struct.iwreq*
%21 = getelementptr inbounds %struct.iwreq, %struct.iwreq* %8, i32 0, i32 2
%22 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %21, i32 0, i32 0
%23 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %22, i32 0, i32 2
store %struct.iwreq* %20, %struct.iwreq** %23
%24 = getelementptr inbounds %struct.iwreq, %struct.iwreq* %8, i32 0, i32 2
%25 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %24, i32 0, i32 0
%26 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %25, i32 0, i32 0
store i32 24, i32* %26
%27 = getelementptr inbounds %struct.iwreq, %struct.iwreq* %8, i32 0, i32 2
%28 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %27, i32 0, i32 0
%29 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %28, i32 0, i32 1
store i32 1, i32* %29
%30 = getelementptr inbounds %struct.iwreq, %struct.iwreq* %8, i32 0, i32 1
%31 = load i32, i32* %30
%32 = load i8*, i8** %4
%33 = load i32, i32* @IFNAMSIZ
%34 = call i32 @strncpy(i32 %31, i8* %32, i32 %33)
%35 = load i32, i32* %7
%36 = load i32, i32* @SIOCGIWSTATS
%37 = call i32 @ioctl(i32 %35, i32 %36, %struct.iwreq* %8)
store i32 %37, i32* %6
%38 = load i32, i32* %6
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %16
%41 = load i32, i32* %7
%42 = call i32 @close(i32 %41)
store i32 -1, i32* %3
br label %50
43: ; preds = %16
%44 = getelementptr inbounds %struct.iw_statistics, %struct.iw_statistics* %9, i32 0, i32 0
%45 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %44, i32 0, i32 0
%46 = load i32, i32* %45
%47 = load i32*, i32** %5
store i32 %46, i32* %47
%48 = load i32, i32* %7
%49 = call i32 @close(i32 %48)
store i32 0, i32* %3
br label %50
50: ; preds = %43, %40, %15
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
%struct.iwreq = type { %struct.TYPE_6__, i32, %struct.TYPE_5__ }
%struct.TYPE_6__ = type { i32 }
%struct.TYPE_5__ = type { %struct.TYPE_4__ }
%struct.TYPE_4__ = type { i32, i32, %struct.iwreq* }
%struct.iw_statistics = type { %struct.TYPE_6__, i32, %struct.TYPE_5__ }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IFNAMSIZ = global i32 0
@SIOCGIWSTATS = global i32 0
define i32 @sysapi_get_wlan_rssi(i8* %0, i32* nocapture writeonly %1) {
%3 = alloca %struct.iwreq
%4 = alloca %struct.iw_statistics
%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 %32, label %9
9: ; preds = %2
%10 = bitcast %struct.iwreq* %3 to i8*
%11 = bitcast %struct.iw_statistics* %4 to i8*
%12 = call i32 @memset(%struct.iwreq* %3, i32 0, i32 24)
%13 = bitcast %struct.iw_statistics* %4 to %struct.iwreq*
%14 = call i32 @memset(%struct.iwreq* %13, i32 0, i32 24)
%15 = getelementptr inbounds %struct.iwreq, %struct.iwreq* %3, i64 0, i32 2, i32 0, i32 2
%16 = bitcast %struct.iwreq** %15 to %struct.iw_statistics**
store %struct.iw_statistics* %4, %struct.iw_statistics** %16
%17 = getelementptr inbounds %struct.iwreq, %struct.iwreq* %3, i64 0, i32 2, i32 0, i32 0
store i32 24, i32* %17
%18 = getelementptr inbounds %struct.iwreq, %struct.iwreq* %3, i64 0, i32 2, i32 0, i32 1
store i32 1, i32* %18
%19 = getelementptr inbounds %struct.iwreq, %struct.iwreq* %3, i64 0, i32 1
%20 = load i32, i32* %19
%21 = load i32, i32* @IFNAMSIZ
%22 = call i32 @strncpy(i32 %20, i8* %0, i32 %21)
%23 = load i32, i32* @SIOCGIWSTATS
%24 = call i32 @ioctl(i32 %7, i32 %23, %struct.iwreq* %3)
%25 = icmp slt i32 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %9
%27 = getelementptr inbounds %struct.iw_statistics, %struct.iw_statistics* %4, i64 0, i32 0, i32 0
%28 = load i32, i32* %27
store i32 %28, i32* %1
br label %29
29: ; preds = %9, %26
%30 = phi i32 [ 0, %26 ], [ -1, %9 ]
%31 = call i32 @close(i32 %7)
br label %32
32: ; preds = %2, %29
%33 = phi i32 [ %30, %29 ], [ -1, %2 ]
ret i32 %33
}