#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 */
int BUFSIZ;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int sscanf(char *, char *, int *);
int system_get_best_wifi(int *wifi) {
  FILE *f;
  char buf[BUFSIZ];
  int tmp, max = -1;
  f = fopen("/proc/net/wireless", "rt");
  if (f == NULL) {
    return 0;
  }
  while (fgets(buf, BUFSIZ, f)) {
    if (sscanf(buf, "%*s %*d %d", &tmp) == 1 && max < tmp) {
      max = tmp;
    }
  }
  fclose(f);
  if (max < 0) {
    return 0;
  }
  *wifi = max;
  return 1;
}


x86 assembly (clang, -O0):
<system_get_best_wifi>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e8d(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
movl   $0xffffffff,-0x30(%rbp)
lea    0xe7a(%rip),%rdi
lea    0xe86(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b4 <system_get_best_wifi+0x74>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    124a <system_get_best_wifi+0x10a>
jmp    11b9 <system_get_best_wifi+0x79>
mov    -0x40(%rbp),%rdi
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rdx
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     120d <system_get_best_wifi+0xcd>
mov    -0x40(%rbp),%rdi
lea    0xe32(%rip),%rsi
lea    -0x2c(%rbp),%rdx
call   1050 <sscanf@plt>
cmp    $0x1,%eax
jne    1208 <system_get_best_wifi+0xc8>
mov    -0x30(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    1208 <system_get_best_wifi+0xc8>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x30(%rbp)
jmp    11b9 <system_get_best_wifi+0x79>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
cmpl   $0x0,-0x30(%rbp)
jge    1233 <system_get_best_wifi+0xf3>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    124a <system_get_best_wifi+0x10a>
mov    -0x30(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<system_get_best_wifi>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
mov    0x2e85(%rip),%r15
mov    (%r15),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe8a(%rip),%rdi
lea    0xe96(%rip),%rsi
call   1060 <fopen@plt>
xor    %r13d,%r13d
test   %rax,%rax
je     122a <system_get_best_wifi+0xea>
mov    %rax,%r12
mov    (%r15),%esi
mov    %rbx,%rdi
mov    %rax,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     1222 <system_get_best_wifi+0xe2>
mov    %r14,-0x38(%rbp)
mov    $0xffffffff,%r15d
lea    -0x2c(%rbp),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15d,%r13d
mov    %rbx,%rdi
lea    0xe49(%rip),%rsi
mov    %r14,%rdx
call   1050 <sscanf@plt>
mov    -0x2c(%rbp),%r15d
cmp    %r15d,%r13d
cmovge %r13d,%r15d
cmp    $0x1,%eax
cmovne %r13d,%r15d
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
mov    %r12,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
jne    11c0 <system_get_best_wifi+0x80>
mov    %r12,%rdi
call   1030 <fclose@plt>
test   %r15d,%r15d
mov    $0x0,%r13d
js     122a <system_get_best_wifi+0xea>
mov    -0x38(%rbp),%rax
mov    %r15d,(%rax)
mov    $0x1,%r13d
jmp    122a <system_get_best_wifi+0xea>
mov    %r12,%rdi
call   1030 <fclose@plt>
mov    %r13d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@BUFSIZ = global i32 0
@.str = [19 x i8] c"/proc/net/wireless\00"
@.str.1 = [3 x i8] c"rt\00"
@.str.2 = [11 x i8] c"%*s %*d %d\00"
define i32 @system_get_best_wifi(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32* %0, i32** %3
%10 = load i32, i32* @BUFSIZ
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %5
%13 = alloca i8, i64 %11
store i64 %11, i64* %6
store i32 -1, i32* %8
%14 = call i32* @fopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %14, i32** %4
%15 = load i32*, i32** %4
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %18
17: ; preds = %1
store i32 0, i32* %2
store i32 1, i32* %9
br label %43
18: ; preds = %1
br label %19
19: ; preds = %33, %18
%20 = load i32, i32* @BUFSIZ
%21 = load i32*, i32** %4
%22 = call i64 @fgets(i8* %13, i32 %20, i32* %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %34
24: ; preds = %19
%25 = call i32 @sscanf(i8* %13, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32* %7)
%26 = icmp eq i32 %25, 1
br i1 %26, label %27, label %33
27: ; preds = %24
%28 = load i32, i32* %8
%29 = load i32, i32* %7
%30 = icmp slt i32 %28, %29
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = load i32, i32* %7
store i32 %32, i32* %8
br label %33
33: ; preds = %31, %27, %24
br label %19
34: ; preds = %19
%35 = load i32*, i32** %4
%36 = call i32 @fclose(i32* %35)
%37 = load i32, i32* %8
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %34
store i32 0, i32* %2
store i32 1, i32* %9
br label %43
40: ; preds = %34
%41 = load i32, i32* %8
%42 = load i32*, i32** %3
store i32 %41, i32* %42
store i32 1, i32* %2
store i32 1, i32* %9
br label %43
43: ; preds = %40, %39, %17
%44 = load i8*, i8** %5
%45 = load i32, i32* %2
ret i32 %45
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@.str = [19 x i8] c"/proc/net/wireless\00"
@.str.1 = [3 x i8] c"rt\00"
@.str.2 = [11 x i8] c"%*s %*d %d\00"
define i32 @system_get_best_wifi(i32* nocapture writeonly %0) {
%2 = alloca i32
%3 = load i32, i32* @BUFSIZ
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = bitcast i32* %2 to i8*
%7 = tail call i32* @fopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %30, label %9
9: ; preds = %1
%10 = load i32, i32* @BUFSIZ
%11 = call i64 @fgets(i8* %5, i32 %10, i32* %7)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = call i32 @fclose(i32* %7)
br label %30
15: ; preds = %9, %15
%16 = phi i32 [ %22, %15 ], [ -1, %9 ]
%17 = call i32 @sscanf(i8* %5, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32* %2)
%18 = icmp eq i32 %17, 1
%19 = load i32, i32* %2
%20 = icmp slt i32 %16, %19
%21 = select i1 %18, i1 %20, i1 false
%22 = select i1 %21, i32 %19, i32 %16
%23 = load i32, i32* @BUFSIZ
%24 = call i64 @fgets(i8* %5, i32 %23, i32* %7)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %15
26: ; preds = %15
%27 = call i32 @fclose(i32* %7)
%28 = icmp slt i32 %22, 0
br i1 %28, label %30, label %29
29: ; preds = %26
store i32 %22, i32* %0
br label %30
30: ; preds = %13, %26, %1, %29
%31 = phi i32 [ 1, %29 ], [ 0, %1 ], [ 0, %26 ], [ 0, %13 ]
ret i32 %31
}