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

/* Variables and functions */
int atoi(char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int strlen(char *);
char *strstr(char *, char *);
int get_wlan0_channel() {
  int wlan0_channel = 0;
  char channel0[16] = "\0";
  char buffer[128];
  FILE *read_fp;
  int chars_read;
  int ret = 0;
  char *tmp;
  char i;
  char ch[10];
  memset(buffer, 0, sizeof(buffer));
  read_fp = popen("iw wlan0 info | grep \"channel\"", "r");
  if (read_fp != NULL) {
    chars_read = fread(buffer, sizeof(char), sizeof(buffer) - 1, read_fp);
    if (chars_read > 0) {
      tmp = strstr(buffer, "channel");
      tmp = tmp + strlen("channel ");
      printf("%s", buffer);
      i = 0;
      while (1) {
        if (tmp[i] == ' ')
          break;
        else
          ch[i] = tmp[i];
      }
      ch[i] = 0;
      printf("ch %s\n", ch);
      wlan0_channel = atoi(ch);
      printf("%d\n", wlan0_channel);
      ret = wlan0_channel;
    } else {
      ret = 0;
    }
    pclose(read_fp);
  }
  return ret;
}


x86 assembly (clang, -O0):
<get_wlan0_channel>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <memset@plt>
movl   $0x0,-0xb0(%rbp)
lea    -0xa0(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1070 <memset@plt>
lea    0xe3a(%rip),%rdi
lea    0xe52(%rip),%rsi
call   1080 <popen@plt>
mov    %rax,-0xa8(%rbp)
cmpq   $0x0,-0xa8(%rbp)
je     132d <get_wlan0_channel+0x1ad>
lea    -0xa0(%rbp),%rdi
mov    -0xa8(%rbp),%rcx
mov    $0x1,%esi
mov    $0x7f,%edx
call   1030 <fread@plt>
mov    %eax,-0xac(%rbp)
cmpl   $0x0,-0xac(%rbp)
jle    1317 <get_wlan0_channel+0x197>
lea    -0xa0(%rbp),%rdi
lea    0xdfc(%rip),%rsi
call   10a0 <strstr@plt>
mov    %rax,-0xb8(%rbp)
mov    -0xb8(%rbp),%rax
mov    %rax,-0xd0(%rbp)
lea    0xde3(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0xd0(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0xb8(%rbp)
lea    -0xa0(%rbp),%rsi
lea    0xdc3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movb   $0x0,-0xb9(%rbp)
mov    -0xb8(%rbp),%rax
movsbq -0xb9(%rbp),%rcx
ff
movsbl (%rax,%rcx,1),%eax
cmp    $0x20,%eax
jne    129e <get_wlan0_channel+0x11e>
jmp    12c4 <get_wlan0_channel+0x144>
mov    -0xb8(%rbp),%rax
movsbq -0xb9(%rbp),%rcx
ff
mov    (%rax,%rcx,1),%cl
movsbq -0xb9(%rbp),%rax
ff
mov    %cl,-0xc3(%rbp,%rax,1)
jmp    127d <get_wlan0_channel+0xfd>
movsbq -0xb9(%rbp),%rax
ff
movb   $0x0,-0xc3(%rbp,%rax,1)
lea    -0xc3(%rbp),%rsi
lea    0xd53(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0xc3(%rbp),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%esi
lea    0xd3a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0xb0(%rbp)
jmp    1321 <get_wlan0_channel+0x1a1>
movl   $0x0,-0xb0(%rbp)
mov    -0xa8(%rbp),%rdi
call   1060 <pclose@plt>
mov    -0xb0(%rbp),%eax
add    $0xd0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_wlan0_channel>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x98,%rsp
lea    0x10(%rsp),%rdi
xor    %ebp,%ebp
xor    %esi,%esi
mov    $0x80,%edx
call   1070 <memset@plt>
lea    0xe59(%rip),%rdi
lea    0xe71(%rip),%rsi
call   1080 <popen@plt>
test   %rax,%rax
je     1265 <get_wlan0_channel+0xe5>
mov    %rax,%rbx
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
mov    $0x7f,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
test   %eax,%eax
jle    125b <get_wlan0_channel+0xdb>
lea    0xe3c(%rip),%rsi
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   10a0 <strstr@plt>
mov    %rax,%r15
lea    0xe2d(%rip),%rdi
call   1040 <strlen@plt>
movslq %eax,%rbp
lea    0xe27(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
cmpb   $0x20,(%r15,%rbp,1)
je     1224 <get_wlan0_channel+0xa4>
mov    (%r15,%rbp,1),%al
cmp    $0x20,%al
jne    1220 <get_wlan0_channel+0xa0>
movb   $0x0,0x6(%rsp)
lea    0xe05(%rip),%rdi
lea    0x6(%rsp),%rbp
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbp,%rdi
call   1090 <atoi@plt>
mov    %eax,%ebp
lea    0xdec(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    125d <get_wlan0_channel+0xdd>
xor    %ebp,%ebp
mov    %rbx,%rdi
call   1060 <pclose@plt>
mov    %ebp,%eax
add    $0x98,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [31 x i8] c"iw wlan0 info | grep \22channel\22\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [8 x i8] c"channel\00"
@.str.3 = [9 x i8] c"channel \00"
@.str.4 = [3 x i8] c"%s\00"
@.str.5 = [7 x i8] c"ch %s\0A\00"
@.str.6 = [4 x i8] c"%d\0A\00"
define i32 @get_wlan0_channel() {
%1 = alloca i32
%2 = alloca [16 x i8]
%3 = alloca [128 x i8]
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8
%9 = alloca [10 x i8]
store i32 0, i32* %1
%10 = bitcast [16 x i8]* %2 to i8*
store i32 0, i32* %6
%11 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%12 = call i32 @memset(i8* %11, i32 0, i32 128)
%13 = call i32* @popen(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** %4
%14 = load i32*, i32** %4
%15 = icmp ne i32* %14, null
br i1 %15, label %16, label %65
16: ; preds = %0
%17 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%18 = load i32*, i32** %4
%19 = call i32 @fread(i8* %17, i32 1, i32 127, i32* %18)
store i32 %19, i32* %5
%20 = load i32, i32* %5
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %61
22: ; preds = %16
%23 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%24 = call i8* @strstr(i8* %23, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i8* %24, i8** %7
%25 = load i8*, i8** %7
%26 = call i32 @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
store i8* %28, i8** %7
%29 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %29)
store i8 0, i8* %8
br label %31
31: ; preds = %22, %49
%32 = load i8*, i8** %7
%33 = load i8, i8* %8
%34 = sext i8 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp eq i32 %37, 32
br i1 %38, label %39, label %40
39: ; preds = %31
br label %50
40: ; preds = %31
%41 = load i8*, i8** %7
%42 = load i8, i8* %8
%43 = sext i8 %42 to i64
%44 = getelementptr inbounds i8, i8* %41, i64 %43
%45 = load i8, i8* %44
%46 = load i8, i8* %8
%47 = sext i8 %46 to i64
%48 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 %47
store i8 %45, i8* %48
br label %49
49: ; preds = %40
br label %31
50: ; preds = %39
%51 = load i8, i8* %8
%52 = sext i8 %51 to i64
%53 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 %52
store i8 0, i8* %53
%54 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 0
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i8* %54)
%56 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 0
%57 = call i32 @atoi(i8* %56)
store i32 %57, i32* %1
%58 = load i32, i32* %1
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 %58)
%60 = load i32, i32* %1
store i32 %60, i32* %6
br label %62
61: ; preds = %16
store i32 0, i32* %6
br label %62
62: ; preds = %61, %50
%63 = load i32*, i32** %4
%64 = call i32 @pclose(i32* %63)
br label %65
65: ; preds = %62, %0
%66 = load i32, i32* %6
ret i32 %66
}

LLVM IR (-O2):
@.str = [31 x i8] c"iw wlan0 info | grep \22channel\22\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [8 x i8] c"channel\00"
@.str.3 = [9 x i8] c"channel \00"
@.str.4 = [3 x i8] c"%s\00"
@.str.5 = [7 x i8] c"ch %s\0A\00"
@.str.6 = [4 x i8] c"%d\0A\00"
define i32 @get_wlan0_channel() {
%1 = alloca [128 x i8]
%2 = alloca [10 x i8]
%3 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%5 = call i32 @memset(i8* %3, i32 0, i32 128)
%6 = call i32* @popen(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %30, label %8
8: ; preds = %0
%9 = call i32 @fread(i8* %3, i32 1, i32 127, i32* %6)
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %27
11: ; preds = %8
%12 = call i8* @strstr(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%13 = call i32 @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %12, i64 %14
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %3)
%17 = load i8, i8* %15
%18 = icmp eq i8 %17, 32
br i1 %18, label %23, label %19
19: ; preds = %11
%20 = load i8, i8* %15
%21 = icmp eq i8 %20, 32
br label %22
22: ; preds = %19, %22
br i1 %21, label %23, label %22
23: ; preds = %22, %11
store i8 0, i8* %4
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i8* %4)
%25 = call i32 @atoi(i8* %4)
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 %25)
br label %27
27: ; preds = %8, %23
%28 = phi i32 [ %25, %23 ], [ 0, %8 ]
%29 = call i32 @pclose(i32* %6)
br label %30
30: ; preds = %27, %0
%31 = phi i32 [ %28, %27 ], [ 0, %0 ]
ret i32 %31
}