#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_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  char const *name;
};
struct iwreq {
  char *ifr_name;
  TYPE_1__ u;
};
typedef int /*<<< orphan*/ pwrq;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ SIOCGIWNAME;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, struct iwreq *);
int /*<<< orphan*/ memset(struct iwreq *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(char *, char const *, int /*<<< orphan*/);
int check_wireless(const char *ifname, char *protocol) {
  int sock = -1;
  struct iwreq pwrq;
  memset(&pwrq, 0, sizeof(pwrq));
  strncpy(pwrq.ifr_name, ifname, IFNAMSIZ);
  if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror("socket");
    return 0;
  }
  if (ioctl(sock, SIOCGIWNAME, &pwrq) != -1) {
    if (protocol)
      strncpy(protocol, pwrq.u.name, IFNAMSIZ);
    close(sock);
    return 1;
  }
  close(sock);
  return 0;
}


x86 assembly (clang, -O0):
<check_wireless>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2e42(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    11da <check_wireless+0x7a>
lea    0xe37(%rip),%rdi
call   1070 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    123c <check_wireless+0xdc>
mov    -0x1c(%rbp),%edi
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
je     122d <check_wireless+0xcd>
cmpq   $0x0,-0x18(%rbp)
je     1219 <check_wireless+0xb9>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    0x2dc6(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    123c <check_wireless+0xdc>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_wireless>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x8(%rsp),%rdi
xor    %ebp,%ebp
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x8(%rsp),%rdi
mov    0x2e49(%rip),%r15
mov    (%r15),%edx
mov    %rbx,%rsi
call   1030 <strncpy@plt>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
cmp    $0xffffffff,%eax
je     120d <check_wireless+0xad>
mov    %eax,%ebx
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
je     11f7 <check_wireless+0x97>
test   %r14,%r14
je     11e9 <check_wireless+0x89>
mov    0x10(%rsp),%rsi
mov    (%r15),%edx
mov    %r14,%rdi
call   1030 <strncpy@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0x1,%ebp
jmp    1200 <check_wireless+0xa0>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdec(%rip),%rdi
call   1070 <perror@plt>
jmp    1200 <check_wireless+0xa0>

LLVM IR (-O0):
%struct.iwreq = type { i8*, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i8* }
@IFNAMSIZ = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@SIOCGIWNAME = global i32 0
define i32 @check_wireless(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.iwreq
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 -1, i32* %6
%8 = call i32 @memset(%struct.iwreq* %7, i32 0, i32 16)
%9 = getelementptr inbounds %struct.iwreq, %struct.iwreq* %7, i32 0, i32 0
%10 = load i8*, i8** %9
%11 = load i8*, i8** %4
%12 = load i32, i32* @IFNAMSIZ
%13 = call i32 @strncpy(i8* %10, i8* %11, i32 %12)
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
store i32 %16, i32* %6
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %41
20: ; preds = %2
%21 = load i32, i32* %6
%22 = load i32, i32* @SIOCGIWNAME
%23 = call i32 @ioctl(i32 %21, i32 %22, %struct.iwreq* %7)
%24 = icmp ne i32 %23, -1
br i1 %24, label %25, label %38
25: ; preds = %20
%26 = load i8*, i8** %5
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %35
28: ; preds = %25
%29 = load i8*, i8** %5
%30 = getelementptr inbounds %struct.iwreq, %struct.iwreq* %7, i32 0, i32 1
%31 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %30, i32 0, i32 0
%32 = load i8*, i8** %31
%33 = load i32, i32* @IFNAMSIZ
%34 = call i32 @strncpy(i8* %29, i8* %32, i32 %33)
br label %35
35: ; preds = %28, %25
%36 = load i32, i32* %6
%37 = call i32 @close(i32 %36)
store i32 1, i32* %3
br label %41
38: ; preds = %20
%39 = load i32, i32* %6
%40 = call i32 @close(i32 %39)
store i32 0, i32* %3
br label %41
41: ; preds = %38, %35, %18
%42 = load i32, i32* %3
ret i32 %42
}

LLVM IR (-O2):
%struct.iwreq = type { i8*, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i8* }
@IFNAMSIZ = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@SIOCGIWNAME = global i32 0
define i32 @check_wireless(i8* %0, i8* %1) {
%3 = alloca %struct.iwreq
%4 = bitcast %struct.iwreq* %3 to i8*
%5 = call i32 @memset(%struct.iwreq* %3, i32 0, i32 16)
%6 = getelementptr inbounds %struct.iwreq, %struct.iwreq* %3, i64 0, i32 0
%7 = load i8*, i8** %6
%8 = load i32, i32* @IFNAMSIZ
%9 = call i32 @strncpy(i8* %7, i8* %0, i32 %8)
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %31
16: ; preds = %2
%17 = load i32, i32* @SIOCGIWNAME
%18 = call i32 @ioctl(i32 %12, i32 %17, %struct.iwreq* %3)
%19 = icmp eq i32 %18, -1
br i1 %19, label %29, label %20
20: ; preds = %16
%21 = icmp eq i8* %1, null
br i1 %21, label %27, label %22
22: ; preds = %20
%23 = getelementptr inbounds %struct.iwreq, %struct.iwreq* %3, i64 0, i32 1, i32 0
%24 = load i8*, i8** %23
%25 = load i32, i32* @IFNAMSIZ
%26 = call i32 @strncpy(i8* %1, i8* %24, i32 %25)
br label %27
27: ; preds = %22, %20
%28 = call i32 @close(i32 %12)
br label %31
29: ; preds = %16
%30 = call i32 @close(i32 %12)
br label %31
31: ; preds = %29, %27, %14
%32 = phi i32 [ 0, %14 ], [ 1, %27 ], [ 0, %29 ]
ret i32 %32
}