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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SIOCGIWNAME;
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);
int sysapi_is_wdev(char *ifname) {
  int ret;
  int fd;
  fd = socket(AF_INET, SOCK_DGRAM, 0);
  if (fd < 0)
    return 0;
  struct iwreq iwr;
  memset(&iwr, 0, sizeof(struct iwreq));
  strncpy(iwr.ifr_name, ifname, sizeof(iwr.ifr_name));
  ret = ioctl(fd, SIOCGIWNAME, &iwr);
  if (ret < 0) {
    close(fd);
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<sysapi_is_wdev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%edi
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    118e <sysapi_is_wdev+0x3e>
movl   $0x0,-0x4(%rbp)
jmp    11ec <sysapi_is_wdev+0x9c>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%edi
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11e5 <sysapi_is_wdev+0x95>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    11ec <sysapi_is_wdev+0x9c>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysapi_is_wdev>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e86(%rip),%rax
mov    (%rax),%edi
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
xor    %ebp,%ebp
xor    %edx,%edx
call   1070 <socket@plt>
test   %eax,%eax
js     11c6 <sysapi_is_wdev+0x76>
mov    %eax,%ebx
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    0x8(%rsp),%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %rbp,%rdx
call   1050 <ioctl@plt>
mov    $0x1,%ebp
test   %eax,%eax
jns    11c6 <sysapi_is_wdev+0x76>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.iwreq = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIWNAME = global i32 0
define i32 @sysapi_is_wdev(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.iwreq
store i8* %0, i8** %3
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_DGRAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %1
store i32 0, i32* %2
br label %28
13: ; preds = %1
%14 = call i32 @memset(%struct.iwreq* %6, i32 0, i32 4)
%15 = getelementptr inbounds %struct.iwreq, %struct.iwreq* %6, i32 0, i32 0
%16 = load i32, i32* %15
%17 = load i8*, i8** %3
%18 = call i32 @strncpy(i32 %16, i8* %17, i32 4)
%19 = load i32, i32* %5
%20 = load i32, i32* @SIOCGIWNAME
%21 = call i32 @ioctl(i32 %19, i32 %20, %struct.iwreq* %6)
store i32 %21, i32* %4
%22 = load i32, i32* %4
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %13
%25 = load i32, i32* %5
%26 = call i32 @close(i32 %25)
store i32 0, i32* %2
br label %28
27: ; preds = %13
store i32 1, i32* %2
br label %28
28: ; preds = %27, %24, %12
%29 = load i32, i32* %2
ret i32 %29
}

LLVM IR (-O2):
%struct.iwreq = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIWNAME = global i32 0
define i32 @sysapi_is_wdev(i8* %0) {
%2 = alloca %struct.iwreq
%3 = load i32, i32* @AF_INET
%4 = load i32, i32* @SOCK_DGRAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %20, label %7
7: ; preds = %1
%8 = bitcast %struct.iwreq* %2 to i8*
%9 = call i32 @memset(%struct.iwreq* %2, i32 0, i32 4)
%10 = getelementptr inbounds %struct.iwreq, %struct.iwreq* %2, i64 0, i32 0
%11 = load i32, i32* %10
%12 = call i32 @strncpy(i32 %11, i8* %0, i32 4)
%13 = load i32, i32* @SIOCGIWNAME
%14 = call i32 @ioctl(i32 %5, i32 %13, %struct.iwreq* %2)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %7
%17 = call i32 @close(i32 %5)
br label %18
18: ; preds = %7, %16
%19 = phi i32 [ 0, %16 ], [ 1, %7 ]
br label %20
20: ; preds = %1, %18
%21 = phi i32 [ %19, %18 ], [ 0, %1 ]
ret i32 %21
}