#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 {
  int /*<<< orphan*/ d_name;
};
typedef int /*<<< orphan*/ dev_id;
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ FILE;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, int /*<<< orphan*/);
char *strchr(char *, char);
int /*<<< orphan*/ strcmp(char *, char *);
char *strdup(int /*<<< orphan*/);
unsigned int strlen(char *);
char toupper(char);
char *kvp_mac_to_if_name(char *mac) {
  DIR *dir;
  struct dirent *entry;
  FILE *file;
  char *p, *x;
  char *if_name = NULL;
  char buf[256];
  char dev_id[PATH_MAX];
  unsigned int i;
  dir = opendir("/sys/class/net/");
  if (dir == NULL)
    return NULL;
  while ((entry = readdir(dir)) != NULL) {
    snprintf(dev_id, sizeof(dev_id), "%s%s/address", "/sys/class/net/",
             entry->d_name);
    file = fopen(dev_id, "r");
    if (file == NULL)
      continue;
    p = fgets(buf, sizeof(buf), file);
    if (p) {
      x = strchr(p, '\n');
      if (x)
        *x = '\0';
      for (i = 0; i < strlen(p); i++)
        p[i] = toupper(p[i]);
      if (!strcmp(p, mac)) {
        if_name = strdup(entry->d_name);
        fclose(file);
        break;
      }
    }
    fclose(file);
  }
  closedir(dir);
  return if_name;
}


x86 assembly (clang, -O0):
<kvp_mac_to_if_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x170,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x40(%rbp)
mov    0x2e12(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x168(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x148(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x160(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x150(%rbp)
lea    0xde6(%rip),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1245 <kvp_mac_to_if_name+0x85>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x158(%rbp)
jmp    13c0 <kvp_mac_to_if_name+0x200>
jmp    124a <kvp_mac_to_if_name+0x8a>
mov    -0x18(%rbp),%rdi
call   10c0 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     13a5 <kvp_mac_to_if_name+0x1e5>
mov    -0x160(%rbp),%rdi
mov    -0x168(%rbp),%rax
mov    %eax,%esi
mov    -0x20(%rbp),%rax
mov    (%rax),%r8d
lea    0xd91(%rip),%rdx
lea    0xd7a(%rip),%rcx
call   1080 <snprintf@plt>
mov    -0x160(%rbp),%rdi
lea    0xd84(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12b2 <kvp_mac_to_if_name+0xf2>
jmp    124a <kvp_mac_to_if_name+0x8a>
lea    -0x140(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    $0x100,%esi
call   10a0 <fgets@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1397 <kvp_mac_to_if_name+0x1d7>
mov    -0x30(%rbp),%rdi
mov    $0xa,%esi
call   1070 <strchr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     12fa <kvp_mac_to_if_name+0x13a>
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x0,-0x154(%rbp)
mov    -0x154(%rbp),%eax
mov    %eax,-0x16c(%rbp)
mov    -0x30(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x16c(%rbp),%eax
cmp    %ecx,%eax
jae    135f <kvp_mac_to_if_name+0x19f>
mov    -0x30(%rbp),%rax
mov    -0x154(%rbp),%ecx
movsbl (%rax,%rcx,1),%edi
call   1030 <toupper@plt>
mov    %al,%dl
mov    -0x30(%rbp),%rax
mov    -0x154(%rbp),%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0x154(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x154(%rbp)
jmp    1304 <kvp_mac_to_if_name+0x144>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10b0 <strcmp@plt>
cmp    $0x0,%eax
jne    1392 <kvp_mac_to_if_name+0x1d2>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   10e0 <strdup@plt>
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
jmp    13a5 <kvp_mac_to_if_name+0x1e5>
jmp    1397 <kvp_mac_to_if_name+0x1d7>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
jmp    124a <kvp_mac_to_if_name+0x8a>
mov    -0x18(%rbp),%rdi
call   1090 <closedir@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x158(%rbp)
mov    -0x148(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kvp_mac_to_if_name>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x128,%rsp
mov    %rdi,-0x40(%rbp)
mov    %rsp,%r12
mov    0x2e0e(%rip),%rax
mov    (%rax),%r13d
mov    %rsp,%rcx
lea    0xf(%r13),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
lea    0xdff(%rip),%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     1316 <kvp_mac_to_if_name+0x156>
mov    %rax,%r15
mov    %rax,%rdi
call   10c0 <readdir@plt>
test   %rax,%rax
je     131e <kvp_mac_to_if_name+0x15e>
mov    %rax,%r14
mov    %r12,-0x30(%rbp)
mov    %r13,-0x38(%rbp)
jmp    1267 <kvp_mac_to_if_name+0xa7>
mov    %r15,%rdi
mov    -0x40(%rbp),%rsi
call   10b0 <strcmp@plt>
test   %eax,%eax
mov    -0x38(%rbp),%r13
je     133d <kvp_mac_to_if_name+0x17d>
mov    %r12,%rdi
call   1040 <fclose@plt>
mov    %rbx,%r15
mov    %rbx,%rdi
call   10c0 <readdir@plt>
mov    %rax,%r14
test   %rax,%rax
je     131a <kvp_mac_to_if_name+0x15a>
mov    (%r14),%r8d
mov    -0x48(%rbp),%rbx
mov    %rbx,%rdi
mov    %r13d,%esi
lea    0xd95(%rip),%rdx
lea    0xd7e(%rip),%rcx
call   1080 <snprintf@plt>
mov    %rbx,%rdi
lea    0xd8c(%rip),%rsi
call   10d0 <fopen@plt>
test   %rax,%rax
mov    %r15,%rbx
je     1250 <kvp_mac_to_if_name+0x90>
mov    %rax,%r12
lea    -0x150(%rbp),%rdi
mov    $0x100,%esi
mov    %rax,%rdx
call   10a0 <fgets@plt>
test   %rax,%rax
je     1248 <kvp_mac_to_if_name+0x88>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0xa,%esi
call   1070 <strchr@plt>
test   %rax,%rax
je     12d2 <kvp_mac_to_if_name+0x112>
movb   $0x0,(%rax)
mov    %r15,%rdi
call   1060 <strlen@plt>
test   %eax,%eax
je     1230 <kvp_mac_to_if_name+0x70>
xor    %r13d,%r13d
cs nopw 0x0(%rax,%rax,1)
nop
movsbl (%r15,%r13,1),%edi
call   1030 <toupper@plt>
mov    %al,(%r15,%r13,1)
add    $0x1,%r13
mov    %r15,%rdi
call   1060 <strlen@plt>
mov    %eax,%eax
cmp    %rax,%r13
jb     12f0 <kvp_mac_to_if_name+0x130>
jmp    1230 <kvp_mac_to_if_name+0x70>
xor    %ebx,%ebx
jmp    1328 <kvp_mac_to_if_name+0x168>
mov    -0x30(%rbp),%r12
xor    %ebx,%ebx
mov    %r15,%rdi
call   1090 <closedir@plt>
mov    %r12,%rsp
mov    %rbx,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%r15
mov    (%r14),%edi
call   10e0 <strdup@plt>
mov    %rax,%rbx
mov    %r12,%rdi
call   1040 <fclose@plt>
mov    -0x30(%rbp),%r12
jmp    1320 <kvp_mac_to_if_name+0x160>

LLVM IR (-O0):
%struct.dirent = type { i32 }
@PATH_MAX = global i32 0
@.str = [16 x i8] c"/sys/class/net/\00"
@.str.1 = [13 x i8] c"%s%s/address\00"
@.str.2 = [2 x i8] c"r\00"
define i8* @kvp_mac_to_if_name(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca %struct.dirent*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca [256 x i8]
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i32
%14 = alloca i32
store i8* %0, i8** %3
store i8* null, i8** %9
%15 = load i32, i32* @PATH_MAX
%16 = zext i32 %15 to i64
%17 = call i8* @llvm.stacksave()
store i8* %17, i8** %11
%18 = alloca i8, i64 %16
store i64 %16, i64* %12
%19 = call i32* @opendir(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i32* %19, i32** %4
%20 = load i32*, i32** %4
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %23
22: ; preds = %1
store i8* null, i8** %2
store i32 1, i32* %14
br label %91
23: ; preds = %1
br label %24
24: ; preds = %84, %37, %23
%25 = load i32*, i32** %4
%26 = call %struct.dirent* @readdir(i32* %25)
store %struct.dirent* %26, %struct.dirent** %5
%27 = icmp ne %struct.dirent* %26, null
br i1 %27, label %28, label %87
28: ; preds = %24
%29 = trunc i64 %16 to i32
%30 = load %struct.dirent*, %struct.dirent** %5
%31 = getelementptr inbounds %struct.dirent, %struct.dirent* %30, i32 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 @snprintf(i8* %18, i32 %29, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %32)
%34 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %34, i32** %6
%35 = load i32*, i32** %6
%36 = icmp eq i32* %35, null
br i1 %36, label %37, label %38
37: ; preds = %28
br label %24
38: ; preds = %28
%39 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%40 = load i32*, i32** %6
%41 = call i8* @fgets(i8* %39, i32 256, i32* %40)
store i8* %41, i8** %7
%42 = load i8*, i8** %7
%43 = icmp ne i8* %42, null
br i1 %43, label %44, label %84
44: ; preds = %38
%45 = load i8*, i8** %7
%46 = call i8* @strchr(i8* %45, i8 signext 10)
store i8* %46, i8** %8
%47 = load i8*, i8** %8
%48 = icmp ne i8* %47, null
br i1 %48, label %49, label %51
49: ; preds = %44
%50 = load i8*, i8** %8
store i8 0, i8* %50
br label %51
51: ; preds = %49, %44
store i32 0, i32* %13
br label %52
52: ; preds = %68, %51
%53 = load i32, i32* %13
%54 = load i8*, i8** %7
%55 = call i32 @strlen(i8* %54)
%56 = icmp ult i32 %53, %55
br i1 %56, label %57, label %71
57: ; preds = %52
%58 = load i8*, i8** %7
%59 = load i32, i32* %13
%60 = zext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %58, i64 %60
%62 = load i8, i8* %61
%63 = call signext i8 @toupper(i8 signext %62)
%64 = load i8*, i8** %7
%65 = load i32, i32* %13
%66 = zext i32 %65 to i64
%67 = getelementptr inbounds i8, i8* %64, i64 %66
store i8 %63, i8* %67
br label %68
68: ; preds = %57
%69 = load i32, i32* %13
%70 = add i32 %69, 1
store i32 %70, i32* %13
br label %52
71: ; preds = %52
%72 = load i8*, i8** %7
%73 = load i8*, i8** %3
%74 = call i32 @strcmp(i8* %72, i8* %73)
%75 = icmp ne i32 %74, 0
br i1 %75, label %83, label %76
76: ; preds = %71
%77 = load %struct.dirent*, %struct.dirent** %5
%78 = getelementptr inbounds %struct.dirent, %struct.dirent* %77, i32 0, i32 0
%79 = load i32, i32* %78
%80 = call i8* @strdup(i32 %79)
store i8* %80, i8** %9
%81 = load i32*, i32** %6
%82 = call i32 @fclose(i32* %81)
br label %87
83: ; preds = %71
br label %84
84: ; preds = %83, %38
%85 = load i32*, i32** %6
%86 = call i32 @fclose(i32* %85)
br label %24
87: ; preds = %76, %24
%88 = load i32*, i32** %4
%89 = call i32 @closedir(i32* %88)
%90 = load i8*, i8** %9
store i8* %90, i8** %2
store i32 1, i32* %14
br label %91
91: ; preds = %87, %22
%92 = load i8*, i8** %11
%93 = load i8*, i8** %2
ret i8* %93
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@PATH_MAX = global i32 0
@.str = [16 x i8] c"/sys/class/net/\00"
@.str.1 = [13 x i8] c"%s%s/address\00"
@.str.2 = [2 x i8] c"r\00"
define i8* @kvp_mac_to_if_name(i8* nocapture %0) {
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @PATH_MAX
%5 = zext i32 %4 to i64
%6 = tail call i8* @llvm.stacksave()
%7 = alloca i8, i64 %5
%8 = tail call i32* @opendir(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %55, label %10
10: ; preds = %1
%11 = call %struct.dirent* @readdir(i32* %8)
%12 = icmp eq %struct.dirent* %11, null
br i1 %12, label %52, label %13
13: ; preds = %10, %49
%14 = phi %struct.dirent* [ %50, %49 ], [ %11, %10 ]
%15 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 0
%16 = load i32, i32* %15
%17 = call i32 @snprintf(i8* %7, i32 %4, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %49, label %20
20: ; preds = %13
%21 = call i8* @fgets(i8* %3, i32 256, i32* %18)
%22 = icmp eq i8* %21, null
br i1 %22, label %47, label %23
23: ; preds = %20
%24 = call i8* @strchr(i8* %21, i8 signext 10)
%25 = icmp eq i8* %24, null
br i1 %25, label %27, label %26
26: ; preds = %23
store i8 0, i8* %24
br label %27
27: ; preds = %26, %23
%28 = call i32 @strlen(i8* %21)
%29 = icmp eq i32 %28, 0
br i1 %29, label %39, label %30
30: ; preds = %27, %30
%31 = phi i64 [ %35, %30 ], [ 0, %27 ]
%32 = getelementptr inbounds i8, i8* %21, i64 %31
%33 = load i8, i8* %32
%34 = call signext i8 @toupper(i8 signext %33)
store i8 %34, i8* %32
%35 = add nuw nsw i64 %31, 1
%36 = call i32 @strlen(i8* %21)
%37 = zext i32 %36 to i64
%38 = icmp ult i64 %35, %37
br i1 %38, label %30, label %39
39: ; preds = %30, %27
%40 = call i32 @strcmp(i8* %21, i8* dereferenceable(1) %0)
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %47
42: ; preds = %39
%43 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 0
%44 = load i32, i32* %43
%45 = call i8* @strdup(i32 %44)
%46 = call i32 @fclose(i32* %18)
br label %52
47: ; preds = %39, %20
%48 = call i32 @fclose(i32* %18)
br label %49
49: ; preds = %47, %13
%50 = call %struct.dirent* @readdir(i32* %8)
%51 = icmp eq %struct.dirent* %50, null
br i1 %51, label %52, label %13
52: ; preds = %49, %10, %42
%53 = phi i8* [ %45, %42 ], [ null, %10 ], [ null, %49 ]
%54 = call i32 @closedir(i32* %8)
br label %55
55: ; preds = %1, %52
%56 = phi i8* [ %53, %52 ], [ null, %1 ]
ret i8* %56
}