#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 */
struct dirent {
  char *d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *, char const *);
char *strstr(char *, char *);
int get_ctlr_num(const char *netdev) {
  struct dirent *dp;
  int ctlr_num = -1;
  char path[1024];
  char *ctlr;
  DIR *dir;
  sprintf(path, "%s/%s", "/sys/class/net", netdev);
  dir = opendir(path);
  if (!dir)
    return -1;
  for (dp = readdir(dir); dp != NULL; dp = readdir(dir)) {
    if (dp->d_name[0] == '.' && dp->d_name[1] == '\0')
      continue;
    if (dp->d_name[1] == '.' && dp->d_name[2] == '\0')
      continue;
    ctlr = strstr(dp->d_name, "ctlr_");
    if (!ctlr)
      continue;
    ctlr_num = atoi(&ctlr[sizeof("ctlr_") - 1]);
    break;
  }
  closedir(dir);
  return ctlr_num;
}


x86 assembly (clang, -O0):
<get_ctlr_num>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%rcx
lea    0xe78(%rip),%rsi
lea    0xe77(%rip),%rdx
call   1070 <sprintf@plt>
lea    -0x420(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x430(%rbp)
cmpq   $0x0,-0x430(%rbp)
jne    11c1 <get_ctlr_num+0x61>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a1 <get_ctlr_num+0x141>
mov    -0x430(%rbp),%rdi
call   1050 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     128f <get_ctlr_num+0x12f>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    1208 <get_ctlr_num+0xa8>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movsbl 0x1(%rax),%eax
cmp    $0x0,%eax
jne    1208 <get_ctlr_num+0xa8>
jmp    127a <get_ctlr_num+0x11a>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movsbl 0x1(%rax),%eax
cmp    $0x2e,%eax
jne    1235 <get_ctlr_num+0xd5>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movsbl 0x2(%rax),%eax
cmp    $0x0,%eax
jne    1235 <get_ctlr_num+0xd5>
jmp    127a <get_ctlr_num+0x11a>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xdd2(%rip),%rsi
call   1080 <strstr@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jne    1262 <get_ctlr_num+0x102>
jmp    127a <get_ctlr_num+0x11a>
mov    -0x428(%rbp),%rdi
add    $0x5,%rdi
call   1060 <atoi@plt>
mov    %eax,-0x1c(%rbp)
jmp    128f <get_ctlr_num+0x12f>
mov    -0x430(%rbp),%rdi
call   1050 <readdir@plt>
mov    %rax,-0x18(%rbp)
jmp    11d1 <get_ctlr_num+0x71>
mov    -0x430(%rbp),%rdi
call   1040 <closedir@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_ctlr_num>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %rdi,%rcx
lea    0xe8b(%rip),%rsi
lea    0xe8a(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1070 <sprintf@plt>
mov    %rbx,%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     11f6 <get_ctlr_num+0x96>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <readdir@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
je     120d <get_ctlr_num+0xad>
lea    0xe64(%rip),%rbp
jmp    11da <get_ctlr_num+0x7a>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbp,%rsi
call   1080 <strstr@plt>
test   %rax,%rax
jne    11fe <get_ctlr_num+0x9e>
mov    %rbx,%rdi
call   1050 <readdir@plt>
test   %rax,%rax
je     120d <get_ctlr_num+0xad>
mov    (%rax),%rdi
movzbl 0x1(%rdi),%eax
cmpb   $0x2e,(%rdi)
jne    11ea <get_ctlr_num+0x8a>
test   %al,%al
je     11cd <get_ctlr_num+0x6d>
cmp    $0x2e,%al
jne    11c0 <get_ctlr_num+0x60>
cmpb   $0x0,0x2(%rdi)
jne    11c0 <get_ctlr_num+0x60>
jmp    11cd <get_ctlr_num+0x6d>
mov    $0xffffffff,%r14d
jmp    1215 <get_ctlr_num+0xb5>
add    $0x5,%rax
mov    %rax,%rdi
call   1060 <atoi@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
call   1040 <closedir@plt>
mov    %r14d,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [15 x i8] c"/sys/class/net\00"
@.str.2 = [6 x i8] c"ctlr_\00"
define i32 @get_ctlr_num(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.dirent*
%5 = alloca i32
%6 = alloca [1024 x i8]
%7 = alloca i8*
%8 = alloca i32*
store i8* %0, i8** %3
store i32 -1, i32* %5
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%10 = load i8*, i8** %3
%11 = call i32 @sprintf(i8* %9, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %10)
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%13 = call i32* @opendir(i8* %12)
store i32* %13, i32** %8
%14 = load i32*, i32** %8
%15 = icmp ne i32* %14, null
br i1 %15, label %17, label %16
16: ; preds = %1
store i32 -1, i32* %2
br label %76
17: ; preds = %1
%18 = load i32*, i32** %8
%19 = call %struct.dirent* @readdir(i32* %18)
store %struct.dirent* %19, %struct.dirent** %4
br label %20
20: ; preds = %69, %17
%21 = load %struct.dirent*, %struct.dirent** %4
%22 = icmp ne %struct.dirent* %21, null
br i1 %22, label %23, label %72
23: ; preds = %20
%24 = load %struct.dirent*, %struct.dirent** %4
%25 = getelementptr inbounds %struct.dirent, %struct.dirent* %24, i32 0, i32 0
%26 = load i8*, i8** %25
%27 = getelementptr inbounds i8, i8* %26, i64 0
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 46
br i1 %30, label %31, label %40
31: ; preds = %23
%32 = load %struct.dirent*, %struct.dirent** %4
%33 = getelementptr inbounds %struct.dirent, %struct.dirent* %32, i32 0, i32 0
%34 = load i8*, i8** %33
%35 = getelementptr inbounds i8, i8* %34, i64 1
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %31
br label %69
40: ; preds = %31, %23
%41 = load %struct.dirent*, %struct.dirent** %4
%42 = getelementptr inbounds %struct.dirent, %struct.dirent* %41, i32 0, i32 0
%43 = load i8*, i8** %42
%44 = getelementptr inbounds i8, i8* %43, i64 1
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = icmp eq i32 %46, 46
br i1 %47, label %48, label %57
48: ; preds = %40
%49 = load %struct.dirent*, %struct.dirent** %4
%50 = getelementptr inbounds %struct.dirent, %struct.dirent* %49, i32 0, i32 0
%51 = load i8*, i8** %50
%52 = getelementptr inbounds i8, i8* %51, i64 2
%53 = load i8, i8* %52
%54 = sext i8 %53 to i32
%55 = icmp eq i32 %54, 0
br i1 %55, label %56, label %57
56: ; preds = %48
br label %69
57: ; preds = %48, %40
%58 = load %struct.dirent*, %struct.dirent** %4
%59 = getelementptr inbounds %struct.dirent, %struct.dirent* %58, i32 0, i32 0
%60 = load i8*, i8** %59
%61 = call i8* @strstr(i8* %60, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i8* %61, i8** %7
%62 = load i8*, i8** %7
%63 = icmp ne i8* %62, null
br i1 %63, label %65, label %64
64: ; preds = %57
br label %69
65: ; preds = %57
%66 = load i8*, i8** %7
%67 = getelementptr inbounds i8, i8* %66, i64 5
%68 = call i32 @atoi(i8* %67)
store i32 %68, i32* %5
br label %72
69: ; preds = %64, %56, %39
%70 = load i32*, i32** %8
%71 = call %struct.dirent* @readdir(i32* %70)
store %struct.dirent* %71, %struct.dirent** %4
br label %20
72: ; preds = %65, %20
%73 = load i32*, i32** %8
%74 = call i32 @closedir(i32* %73)
%75 = load i32, i32* %5
store i32 %75, i32* %2
br label %76
76: ; preds = %72, %16
%77 = load i32, i32* %2
ret i32 %77
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [15 x i8] c"/sys/class/net\00"
@.str.2 = [6 x i8] c"ctlr_\00"
define i32 @get_ctlr_num(i8* %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%5 = call i32* @opendir(i8* %3)
%6 = icmp eq i32* %5, null
br i1 %6, label %38, label %7
7: ; preds = %1
%8 = tail call %struct.dirent* @readdir(i32* %5)
%9 = icmp eq %struct.dirent* %8, null
br i1 %9, label %35, label %10
10: ; preds = %7, %32
%11 = phi %struct.dirent* [ %33, %32 ], [ %8, %7 ]
%12 = getelementptr inbounds %struct.dirent, %struct.dirent* %11, i64 0, i32 0
%13 = load i8*, i8** %12
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 46
%16 = getelementptr inbounds i8, i8* %13, i64 1
%17 = load i8, i8* %16
%18 = icmp eq i8 %17, 0
%19 = select i1 %15, i1 %18, i1 false
br i1 %19, label %32, label %20
20: ; preds = %10
%21 = icmp eq i8 %17, 46
br i1 %21, label %22, label %26
22: ; preds = %20
%23 = getelementptr inbounds i8, i8* %13, i64 2
%24 = load i8, i8* %23
%25 = icmp eq i8 %24, 0
br i1 %25, label %32, label %26
26: ; preds = %22, %20
%27 = tail call i8* @strstr(i8* dereferenceable(1) %13, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%28 = icmp eq i8* %27, null
br i1 %28, label %32, label %29
29: ; preds = %26
%30 = getelementptr inbounds i8, i8* %27, i64 5
%31 = tail call i32 @atoi(i8* %30)
br label %35
32: ; preds = %10, %26, %22
%33 = tail call %struct.dirent* @readdir(i32* %5)
%34 = icmp eq %struct.dirent* %33, null
br i1 %34, label %35, label %10
35: ; preds = %32, %7, %29
%36 = phi i32 [ %31, %29 ], [ -1, %7 ], [ -1, %32 ]
%37 = tail call i32 @closedir(i32* %5)
br label %38
38: ; preds = %1, %35
%39 = phi i32 [ %36, %35 ], [ -1, %1 ]
ret i32 %39
}