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

/* Variables and functions */
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ strlen(char *);
char *strstr(char *, char *);
int cc_iw_get_mac_by_dev(char (*mac)[17 + 1], int mac_max_size, char *wlan) {
  FILE *fp;
  char *pos;
  char line[256];
  int i = 0;
  char command[128];
  snprintf(command, 128, "iw dev %s station dump", wlan);
start_:
  fp = popen(command, "r");
  if (!fp) {
    sleep(10);
    goto start_;
  }
  while (fgets(line, 256, fp)) {
    pos = strstr(line, "Station ");
    if (pos != NULL) {
      pos += strlen("Station ");
      memcpy(mac[i], pos, 17);
      if (++i >= mac_max_size)
        break;
    }
  }
  pclose(fp);
  return i;
}


x86 assembly (clang, -O0):
<cc_iw_get_mac_by_dev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1c0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x134(%rbp)
lea    -0x1c0(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x80,%esi
lea    0xe49(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x1c0(%rbp),%rdi
lea    0xe4d(%rip),%rsi
call   1080 <popen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ed <cc_iw_get_mac_by_dev+0x6d>
mov    $0xa,%edi
call   1090 <sleep@plt>
jmp    11bc <cc_iw_get_mac_by_dev+0x3c>
jmp    11f2 <cc_iw_get_mac_by_dev+0x72>
lea    -0x130(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x100,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     1296 <cc_iw_get_mac_by_dev+0x116>
lea    -0x130(%rbp),%rdi
lea    0xdfa(%rip),%rsi
call   10a0 <strstr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1291 <cc_iw_get_mac_by_dev+0x111>
lea    0xddf(%rip),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
movslq -0x134(%rbp),%rax
imul   $0x12,%rax,%rax
add    %rax,%rdi
mov    -0x28(%rbp),%rsi
mov    $0x11,%edx
call   1070 <memcpy@plt>
mov    -0x134(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x134(%rbp)
cmp    -0xc(%rbp),%eax
jl     128c <cc_iw_get_mac_by_dev+0x10c>
jmp    1296 <cc_iw_get_mac_by_dev+0x116>
jmp    1291 <cc_iw_get_mac_by_dev+0x111>
jmp    11f2 <cc_iw_get_mac_by_dev+0x72>
mov    -0x20(%rbp),%rdi
call   1040 <pclose@plt>
mov    -0x134(%rbp),%eax
add    $0x1c0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cc_iw_get_mac_by_dev>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x198,%rsp
mov    %rdx,%rcx
mov    %esi,0xc(%rsp)
mov    %rdi,%r15
lea    0xe5e(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x80,%esi
call   1050 <snprintf@plt>
lea    0xe5c(%rip),%rsi
mov    %rbx,%rdi
call   1080 <popen@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    11fd <cc_iw_get_mac_by_dev+0x7d>
lea    0xe45(%rip),%r14
lea    0x10(%rsp),%rbx
nopw   0x0(%rax,%rax,1)
mov    $0xa,%edi
call   1090 <sleep@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <popen@plt>
test   %rax,%rax
je     11e0 <cc_iw_get_mac_by_dev+0x60>
mov    %rax,%rbp
lea    0x90(%rsp),%rdi
mov    $0x100,%esi
mov    %rbp,%rdx
call   1060 <fgets@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     1286 <cc_iw_get_mac_by_dev+0x106>
lea    0xdf9(%rip),%r12
lea    0x90(%rsp),%r13
jmp    1245 <cc_iw_get_mac_by_dev+0xc5>
nopw   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    $0x100,%esi
mov    %rbp,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     1286 <cc_iw_get_mac_by_dev+0x106>
mov    %r13,%rdi
mov    %r12,%rsi
call   10a0 <strstr@plt>
test   %rax,%rax
je     1230 <cc_iw_get_mac_by_dev+0xb0>
mov    %rax,%r14
mov    %r12,%rdi
call   1030 <strlen@plt>
cltq
add    %rax,%r14
movslq %ebx,%rbx
lea    (%rbx,%rbx,8),%rax
lea    (%r15,%rax,2),%rdi
mov    %r14,%rsi
mov    $0x11,%edx
call   1070 <memcpy@plt>
add    $0x1,%ebx
cmp    0xc(%rsp),%ebx
jl     1230 <cc_iw_get_mac_by_dev+0xb0>
mov    %rbp,%rdi
call   1040 <pclose@plt>
mov    %ebx,%eax
add    $0x198,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [23 x i8] c"iw dev %s station dump\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [9 x i8] c"Station \00"
define i32 @cc_iw_get_mac_by_dev([18 x i8]* %0, i32 %1, i8* %2) {
%4 = alloca [18 x i8]*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca [256 x i8]
%10 = alloca i32
%11 = alloca [128 x i8]
store [18 x i8]* %0, [18 x i8]** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
store i32 0, i32* %10
%12 = getelementptr inbounds [128 x i8], [128 x i8]* %11, i64 0, i64 0
%13 = load i8*, i8** %6
%14 = call i32 @snprintf(i8* %12, i32 128, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %13)
br label %15
15: ; preds = %20, %3
%16 = getelementptr inbounds [128 x i8], [128 x i8]* %11, i64 0, i64 0
%17 = call i32* @popen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %17, i32** %7
%18 = load i32*, i32** %7
%19 = icmp ne i32* %18, null
br i1 %19, label %22, label %20
20: ; preds = %15
%21 = call i32 @sleep(i32 10)
br label %15
22: ; preds = %15
br label %23
23: ; preds = %51, %22
%24 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%25 = load i32*, i32** %7
%26 = call i64 @fgets(i8* %24, i32 256, i32* %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %52
28: ; preds = %23
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%30 = call i8* @strstr(i8* %29, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
store i8* %30, i8** %8
%31 = load i8*, i8** %8
%32 = icmp ne i8* %31, null
br i1 %32, label %33, label %51
33: ; preds = %28
%34 = call i32 @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%35 = load i8*, i8** %8
%36 = sext i32 %34 to i64
%37 = getelementptr inbounds i8, i8* %35, i64 %36
store i8* %37, i8** %8
%38 = load [18 x i8]*, [18 x i8]** %4
%39 = load i32, i32* %10
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds [18 x i8], [18 x i8]* %38, i64 %40
%42 = getelementptr inbounds [18 x i8], [18 x i8]* %41, i64 0, i64 0
%43 = load i8*, i8** %8
%44 = call i32 @memcpy(i8* %42, i8* %43, i32 17)
%45 = load i32, i32* %10
%46 = add nsw i32 %45, 1
store i32 %46, i32* %10
%47 = load i32, i32* %5
%48 = icmp sge i32 %46, %47
br i1 %48, label %49, label %50
49: ; preds = %33
br label %52
50: ; preds = %33
br label %51
51: ; preds = %50, %28
br label %23
52: ; preds = %49, %23
%53 = load i32*, i32** %7
%54 = call i32 @pclose(i32* %53)
%55 = load i32, i32* %10
ret i32 %55
}

LLVM IR (-O2):
@.str = [23 x i8] c"iw dev %s station dump\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [9 x i8] c"Station \00"
define i32 @cc_iw_get_mac_by_dev([18 x i8]* %0, i32 %1, i8* %2) {
%4 = alloca [256 x i8]
%5 = alloca [128 x i8]
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%8 = call i32 @snprintf(i8* %7, i32 128, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %2)
%9 = call i32* @popen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %15, label %11
11: ; preds = %15, %3
%12 = phi i32* [ %9, %3 ], [ %17, %15 ]
%13 = call i64 @fgets(i8* %6, i32 256, i32* %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %36, label %19
15: ; preds = %3, %15
%16 = call i32 @sleep(i32 10)
%17 = call i32* @popen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp eq i32* %17, null
br i1 %18, label %15, label %11
19: ; preds = %11, %32
%20 = phi i32 [ %33, %32 ], [ 0, %11 ]
%21 = call i8* @strstr(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%22 = icmp eq i8* %21, null
br i1 %22, label %32, label %23
23: ; preds = %19
%24 = call i32 @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %21, i64 %25
%27 = sext i32 %20 to i64
%28 = getelementptr inbounds [18 x i8], [18 x i8]* %0, i64 %27, i64 0
%29 = call i32 @memcpy(i8* %28, i8* %26, i32 17)
%30 = add nsw i32 %20, 1
%31 = icmp slt i32 %30, %1
br i1 %31, label %32, label %36
32: ; preds = %23, %19
%33 = phi i32 [ %30, %23 ], [ %20, %19 ]
%34 = call i64 @fgets(i8* %6, i32 256, i32* %12)
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %19
36: ; preds = %32, %23, %11
%37 = phi i32 [ 0, %11 ], [ %30, %23 ], [ %33, %32 ]
%38 = call i32 @pclose(i32* %12)
ret i32 %37
}