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

/* Variables and functions */
int MAX_LINKS;
int atoi(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
scalar_t__ strncmp(char *, char *, int);
int get_netlink_num(int i) {
  FILE *fp;
  int netlink_num = (MAX_LINKS - 1);
  char str[64];
  char *srch = "netlink_num";
  char filename[64];
  if (i == 0) {
    strcpy(filename, "/proc/mwlan/config");
  } else if (i > 0) {
    sprintf(filename, "/proc/mwlan/config%d", i);
  }
  fp = fopen(filename, "r");
  if (fp) {
    while (!feof(fp)) {
      fgets(str, sizeof(str), fp);
      if (strncmp(str, srch, strlen(srch)) == 0) {
        netlink_num = atoi(str + strlen(srch) + 1);
        break;
      }
    }
    fclose(fp);
  } else {
    return -1;
  }
  printf("Netlink number = %d\n", netlink_num);
  return netlink_num;
}


x86 assembly (clang, -O0):
<get_netlink_num>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e23(%rip),%rax
mov    (%rax),%eax
sub    $0x1,%eax
mov    %eax,-0x14(%rbp)
lea    0xe3c(%rip),%rax
mov    %rax,-0x68(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    11ea <get_netlink_num+0x4a>
lea    -0xb0(%rbp),%rdi
lea    0xe2c(%rip),%rsi
call   1040 <strcpy@plt>
jmp    120f <get_netlink_num+0x6f>
cmpl   $0x0,-0x8(%rbp)
jle    120a <get_netlink_num+0x6a>
lea    -0xb0(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe1a(%rip),%rsi
call   10c0 <sprintf@plt>
jmp    120f <get_netlink_num+0x6f>
lea    -0xb0(%rbp),%rdi
lea    0xe17(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     12e8 <get_netlink_num+0x148>
jmp    1236 <get_netlink_num+0x96>
mov    -0x10(%rbp),%rdi
call   1090 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1254 <get_netlink_num+0xb4>
jmp    12da <get_netlink_num+0x13a>
lea    -0x60(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x40,%esi
call   1080 <fgets@plt>
lea    -0x60(%rbp),%rax
mov    %rax,-0xc0(%rbp)
mov    -0x68(%rbp),%rax
mov    %rax,-0xb8(%rbp)
mov    -0x68(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0xc0(%rbp),%rdi
mov    -0xb8(%rbp),%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    12d5 <get_netlink_num+0x135>
lea    -0x60(%rbp),%rax
mov    %rax,-0xc8(%rbp)
mov    -0x68(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0xc8(%rbp),%rdi
cltq
add    %rax,%rdi
add    $0x1,%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x14(%rbp)
jmp    12da <get_netlink_num+0x13a>
jmp    1236 <get_netlink_num+0x96>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
jmp    12f4 <get_netlink_num+0x154>
movl   $0xffffffff,-0x4(%rbp)
jmp    1309 <get_netlink_num+0x169>
mov    -0x14(%rbp),%esi
lea    0xd38(%rip),%rdi
call   1070 <printf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xd0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_netlink_num>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x88,%rsp
mov    0x2e24(%rip),%rax
mov    (%rax),%r14d
test   %edi,%edi
je     11d0 <get_netlink_num+0x30>
jle    11df <get_netlink_num+0x3f>
mov    %edi,%edx
lea    0xe59(%rip),%rsi
mov    %rsp,%rdi
call   10c0 <sprintf@plt>
jmp    11df <get_netlink_num+0x3f>
lea    0xe35(%rip),%rsi
mov    %rsp,%rdi
call   1040 <strcpy@plt>
lea    0xe4e(%rip),%rsi
mov    %rsp,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     127d <get_netlink_num+0xdd>
mov    %rax,%rbx
add    $0xffffffff,%r14d
lea    0x40(%rsp),%r15
lea    0xdf6(%rip),%rbp
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1090 <feof@plt>
test   %eax,%eax
jne    1264 <get_netlink_num+0xc4>
mov    %r15,%rdi
mov    $0x40,%esi
mov    %rbx,%rdx
call   1080 <fgets@plt>
mov    %rbp,%rdi
call   1060 <strlen@plt>
mov    %r15,%rdi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    1210 <get_netlink_num+0x70>
lea    0xdb3(%rip),%rdi
call   1060 <strlen@plt>
cltq
lea    (%rsp,%rax,1),%rdi
add    $0x41,%rdi
call   10b0 <atoi@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0xdc3(%rip),%rdi
mov    %r14d,%esi
call   1070 <printf@plt>
jmp    1283 <get_netlink_num+0xe3>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@MAX_LINKS = global i32 0
@.str = [12 x i8] c"netlink_num\00"
@.str.1 = [19 x i8] c"/proc/mwlan/config\00"
@.str.2 = [21 x i8] c"/proc/mwlan/config%d\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [21 x i8] c"Netlink number = %d\0A\00"
define i32 @get_netlink_num(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca [64 x i8]
%7 = alloca i8*
%8 = alloca [64 x i8]
store i32 %0, i32* %3
%9 = load i32, i32* @MAX_LINKS
%10 = sub nsw i32 %9, 1
store i32 %10, i32* %5
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8** %7
%11 = load i32, i32* %3
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %1
%14 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%15 = call i32 @strcpy(i8* %14, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %24
16: ; preds = %1
%17 = load i32, i32* %3
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%21 = load i32, i32* %3
%22 = call i32 @sprintf(i8* %20, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %21)
br label %23
23: ; preds = %19, %16
br label %24
24: ; preds = %23, %13
%25 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%26 = call i32* @fopen(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %26, i32** %4
%27 = load i32*, i32** %4
%28 = icmp ne i32* %27, null
br i1 %28, label %29, label %57
29: ; preds = %24
br label %30
30: ; preds = %53, %29
%31 = load i32*, i32** %4
%32 = call i32 @feof(i32* %31)
%33 = icmp ne i32 %32, 0
%34 = xor i1 %33, true
br i1 %34, label %35, label %54
35: ; preds = %30
%36 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%37 = load i32*, i32** %4
%38 = call i32 @fgets(i8* %36, i32 64, i32* %37)
%39 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%40 = load i8*, i8** %7
%41 = load i8*, i8** %7
%42 = call i32 @strlen(i8* %41)
%43 = call i64 @strncmp(i8* %39, i8* %40, i32 %42)
%44 = icmp eq i64 %43, 0
br i1 %44, label %45, label %53
45: ; preds = %35
%46 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%47 = load i8*, i8** %7
%48 = call i32 @strlen(i8* %47)
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %46, i64 %49
%51 = getelementptr inbounds i8, i8* %50, i64 1
%52 = call i32 @atoi(i8* %51)
store i32 %52, i32* %5
br label %54
53: ; preds = %35
br label %30
54: ; preds = %45, %30
%55 = load i32*, i32** %4
%56 = call i32 @fclose(i32* %55)
br label %58
57: ; preds = %24
store i32 -1, i32* %2
br label %62
58: ; preds = %54
%59 = load i32, i32* %5
%60 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i32 %59)
%61 = load i32, i32* %5
store i32 %61, i32* %2
br label %62
62: ; preds = %58, %57
%63 = load i32, i32* %2
ret i32 %63
}

LLVM IR (-O2):
@MAX_LINKS = global i32 0
@.str = [12 x i8] c"netlink_num\00"
@.str.1 = [19 x i8] c"/proc/mwlan/config\00"
@.str.2 = [21 x i8] c"/proc/mwlan/config%d\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [21 x i8] c"Netlink number = %d\0A\00"
define i32 @get_netlink_num(i32 %0) {
%2 = alloca [64 x i8]
%3 = alloca [64 x i8]
%4 = load i32, i32* @MAX_LINKS
%5 = add nsw i32 %4, -1
%6 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%7 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%8 = icmp eq i32 %0, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i32 @strcpy(i8* %7, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %15
11: ; preds = %1
%12 = icmp sgt i32 %0, 0
br i1 %12, label %13, label %15
13: ; preds = %11
%14 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %0)
br label %15
15: ; preds = %11, %13, %9
%16 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %36, label %18
18: ; preds = %15, %21
%19 = call i32 @feof(i32* %16)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %32
21: ; preds = %18
%22 = call i32 @fgets(i8* %6, i32 64, i32* %16)
%23 = call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%24 = call i64 @strncmp(i8* %6, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %23)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %18
26: ; preds = %21
%27 = call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 %28
%30 = getelementptr inbounds i8, i8* %29, i64 1
%31 = call i32 @atoi(i8* %30)
br label %32
32: ; preds = %18, %26
%33 = phi i32 [ %31, %26 ], [ %5, %18 ]
%34 = call i32 @fclose(i32* %16)
%35 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i32 %33)
br label %36
36: ; preds = %15, %32
%37 = phi i32 [ %33, %32 ], [ -1, %15 ]
ret i32 %37
}