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

/* Variables and functions */
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*/ strcat(char *, char *);
char *strchr(char *, char);
int /*<<< orphan*/ strcmp(char *, char *);
char *strdup(char *);
unsigned int strlen(char *);
char toupper(char);
char *kvp_mac_to_if_name(char *mac) {
  DIR *dir;
  struct dirent *entry;
  FILE *file;
  char *p, *q, *x;
  char *if_name = NULL;
  char buf[256];
  char *kvp_net_dir = "/sys/class/net/";
  char dev_id[256];
  unsigned int i;
  dir = opendir(kvp_net_dir);
  if (dir == NULL)
    return NULL;
  snprintf(dev_id, sizeof(dev_id), "%s", kvp_net_dir);
  q = dev_id + strlen(kvp_net_dir);
  while ((entry = readdir(dir)) != NULL) {
    *q = '\0';
    strcat(dev_id, entry->d_name);
    strcat(dev_id, "/address");
    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    $0x280,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x48(%rbp)
lea    0xe12(%rip),%rax
mov    %rax,-0x158(%rbp)
mov    -0x158(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    121d <kvp_mac_to_if_name+0x4d>
movq   $0x0,-0x8(%rbp)
jmp    13d8 <kvp_mac_to_if_name+0x208>
lea    -0x260(%rbp),%rdi
mov    -0x158(%rbp),%rcx
mov    $0x100,%esi
lea    0xdd9(%rip),%rdx
call   1080 <snprintf@plt>
lea    -0x260(%rbp),%rax
mov    %rax,-0x270(%rbp)
mov    -0x158(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x270(%rbp),%rax
mov    %ecx,%ecx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   10c0 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     13c7 <kvp_mac_to_if_name+0x1f7>
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x260(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
call   10e0 <strcat@plt>
lea    -0x260(%rbp),%rdi
lea    0xd6c(%rip),%rsi
call   10e0 <strcat@plt>
lea    -0x260(%rbp),%rdi
lea    0xd62(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12d3 <kvp_mac_to_if_name+0x103>
jmp    1268 <kvp_mac_to_if_name+0x98>
lea    -0x150(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    $0x100,%esi
call   10a0 <fgets@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     13b9 <kvp_mac_to_if_name+0x1e9>
mov    -0x30(%rbp),%rdi
mov    $0xa,%esi
call   1070 <strchr@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     131b <kvp_mac_to_if_name+0x14b>
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x0,-0x264(%rbp)
mov    -0x264(%rbp),%eax
mov    %eax,-0x274(%rbp)
mov    -0x30(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x274(%rbp),%eax
cmp    %ecx,%eax
jae    1380 <kvp_mac_to_if_name+0x1b0>
mov    -0x30(%rbp),%rax
mov    -0x264(%rbp),%ecx
movsbl (%rax,%rcx,1),%edi
call   1030 <toupper@plt>
mov    %al,%dl
mov    -0x30(%rbp),%rax
mov    -0x264(%rbp),%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0x264(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x264(%rbp)
jmp    1325 <kvp_mac_to_if_name+0x155>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10b0 <strcmp@plt>
cmp    $0x0,%eax
jne    13b4 <kvp_mac_to_if_name+0x1e4>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   10f0 <strdup@plt>
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
jmp    13c7 <kvp_mac_to_if_name+0x1f7>
jmp    13b9 <kvp_mac_to_if_name+0x1e9>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
jmp    1268 <kvp_mac_to_if_name+0x98>
mov    -0x18(%rbp),%rdi
call   1090 <closedir@plt>
mov    -0x48(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x280,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kvp_mac_to_if_name>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %rdi,0x8(%rsp)
lea    0xe13(%rip),%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     1344 <kvp_mac_to_if_name+0x174>
mov    %rax,%r12
lea    0xe0b(%rip),%rdx
lea    0xdf4(%rip),%rbx
lea    0x10(%rsp),%rdi
mov    $0x100,%esi
mov    %rbx,%rcx
call   1080 <snprintf@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %eax,%ebx
mov    %r12,%rdi
call   10c0 <readdir@plt>
test   %rax,%rax
je     1338 <kvp_mac_to_if_name+0x168>
mov    %rax,%r15
mov    %ebx,%ebx
lea    0x10(%rsp),%rbp
jmp    1289 <kvp_mac_to_if_name+0xb9>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r13,%rdi
mov    0x8(%rsp),%rsi
call   10b0 <strcmp@plt>
test   %eax,%eax
mov    %rbp,%r12
lea    0x10(%rsp),%rbp
je     135b <kvp_mac_to_if_name+0x18b>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %r12,%rdi
call   10c0 <readdir@plt>
mov    %rax,%r15
test   %rax,%rax
je     1338 <kvp_mac_to_if_name+0x168>
movb   $0x0,0x10(%rsp,%rbx,1)
mov    (%r15),%rsi
mov    %rbp,%rdi
call   10e0 <strcat@plt>
mov    %rbp,%rdi
lea    0xd70(%rip),%rsi
call   10e0 <strcat@plt>
mov    %rbp,%rdi
lea    0xd6a(%rip),%rsi
call   10d0 <fopen@plt>
test   %rax,%rax
je     1275 <kvp_mac_to_if_name+0xa5>
mov    %rax,%r14
lea    0x110(%rsp),%rdi
mov    $0x100,%esi
mov    %rax,%rdx
call   10a0 <fgets@plt>
test   %rax,%rax
je     126d <kvp_mac_to_if_name+0x9d>
mov    %rax,%r13
mov    %r12,%rbp
mov    %rax,%rdi
mov    $0xa,%esi
call   1070 <strchr@plt>
test   %rax,%rax
je     12f4 <kvp_mac_to_if_name+0x124>
movb   $0x0,(%rax)
mov    %r13,%rdi
call   1060 <strlen@plt>
test   %eax,%eax
je     1250 <kvp_mac_to_if_name+0x80>
xor    %r12d,%r12d
nopw   0x0(%rax,%rax,1)
movsbl 0x0(%r13,%r12,1),%edi
call   1030 <toupper@plt>
mov    %al,0x0(%r13,%r12,1)
add    $0x1,%r12
mov    %r13,%rdi
call   1060 <strlen@plt>
mov    %eax,%eax
cmp    %rax,%r12
jb     1310 <kvp_mac_to_if_name+0x140>
jmp    1250 <kvp_mac_to_if_name+0x80>
xor    %ebx,%ebx
mov    %r12,%rdi
call   1090 <closedir@plt>
jmp    1346 <kvp_mac_to_if_name+0x176>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r15),%rdi
call   10f0 <strdup@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <fclose@plt>
jmp    133a <kvp_mac_to_if_name+0x16a>

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

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