#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*/ free(char *);
char *kvp_get_mac_addr(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 *);
int strlen(char *);
char *kvp_get_if_name(char *guid) {
  DIR *dir;
  struct dirent *entry;
  FILE *file;
  char *p, *q, *x;
  char *mac_addr;
  char *if_name = NULL;
  char buf[256];
  char *kvp_net_dir = "/sys/class/net/";
  char dev_id[256];
  mac_addr = kvp_get_mac_addr(guid);
  if (mac_addr == NULL)
    return NULL;
  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';
      if (!strcmp(p, mac_addr)) {
        if_name = strdup(entry->d_name);
        fclose(file);
        break;
      }
    }
    fclose(file);
  }
  closedir(dir);
  free(mac_addr);
  return if_name;
}


x86 assembly (clang, -O0):
<kvp_get_if_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x270,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x50(%rbp)
lea    0xe02(%rip),%rax
mov    %rax,-0x158(%rbp)
mov    -0x10(%rbp),%rdi
call   10b0 <kvp_get_mac_addr@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    122a <kvp_get_if_name+0x4a>
movq   $0x0,-0x8(%rbp)
jmp    13b2 <kvp_get_if_name+0x1d2>
mov    -0x158(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1252 <kvp_get_if_name+0x72>
movq   $0x0,-0x8(%rbp)
jmp    13b2 <kvp_get_if_name+0x1d2>
lea    -0x260(%rbp),%rdi
mov    -0x158(%rbp),%rcx
mov    $0x100,%esi
lea    0xda4(%rip),%rdx
call   1080 <snprintf@plt>
lea    -0x260(%rbp),%rax
mov    %rax,-0x268(%rbp)
mov    -0x158(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x268(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   10d0 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1398 <kvp_get_if_name+0x1b8>
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x260(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
call   10f0 <strcat@plt>
lea    -0x260(%rbp),%rdi
lea    0xd36(%rip),%rsi
call   10f0 <strcat@plt>
lea    -0x260(%rbp),%rdi
lea    0xd2c(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1309 <kvp_get_if_name+0x129>
jmp    129e <kvp_get_if_name+0xbe>
lea    -0x150(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    $0x100,%esi
call   10a0 <fgets@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     138a <kvp_get_if_name+0x1aa>
mov    -0x30(%rbp),%rdi
mov    $0xa,%esi
call   1070 <strchr@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     1351 <kvp_get_if_name+0x171>
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x30(%rbp),%rdi
mov    -0x48(%rbp),%rsi
call   10c0 <strcmp@plt>
cmp    $0x0,%eax
jne    1385 <kvp_get_if_name+0x1a5>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1100 <strdup@plt>
mov    %rax,-0x50(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
jmp    1398 <kvp_get_if_name+0x1b8>
jmp    138a <kvp_get_if_name+0x1aa>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
jmp    129e <kvp_get_if_name+0xbe>
mov    -0x18(%rbp),%rdi
call   1090 <closedir@plt>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x50(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x270,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kvp_get_if_name>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
call   10b0 <kvp_get_mac_addr@plt>
mov    %rax,0x8(%rsp)
test   %rax,%rax
je     130e <kvp_get_if_name+0x12e>
lea    0xdf5(%rip),%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     130e <kvp_get_if_name+0x12e>
mov    %rax,%r12
lea    0xded(%rip),%rdx
lea    0xdd6(%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   10d0 <readdir@plt>
test   %rax,%rax
je     1312 <kvp_get_if_name+0x132>
mov    %rax,%r15
movslq %ebx,%rbp
lea    0x10(%rsp),%rbx
jmp    129d <kvp_get_if_name+0xbd>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r13,%rdi
mov    0x8(%rsp),%rsi
call   10c0 <strcmp@plt>
test   %eax,%eax
je     133b <kvp_get_if_name+0x15b>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %r12,%rdi
call   10d0 <readdir@plt>
mov    %rax,%r15
test   %rax,%rax
je     1312 <kvp_get_if_name+0x132>
movb   $0x0,0x10(%rsp,%rbp,1)
mov    (%r15),%rsi
mov    %rbx,%rdi
call   10f0 <strcat@plt>
mov    %rbx,%rdi
lea    0xd5c(%rip),%rsi
call   10f0 <strcat@plt>
mov    %rbx,%rdi
lea    0xd56(%rip),%rsi
call   10e0 <fopen@plt>
test   %rax,%rax
je     128d <kvp_get_if_name+0xad>
mov    %rax,%r14
lea    0x110(%rsp),%rdi
mov    $0x100,%esi
mov    %rax,%rdx
call   10a0 <fgets@plt>
test   %rax,%rax
je     1285 <kvp_get_if_name+0xa5>
mov    %rax,%r13
mov    %rax,%rdi
mov    $0xa,%esi
call   1070 <strchr@plt>
test   %rax,%rax
je     1270 <kvp_get_if_name+0x90>
movb   $0x0,(%rax)
jmp    1270 <kvp_get_if_name+0x90>
xor    %ebx,%ebx
jmp    1326 <kvp_get_if_name+0x146>
xor    %ebx,%ebx
mov    %r12,%rdi
call   1090 <closedir@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r15),%rdi
call   1100 <strdup@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <fclose@plt>
jmp    1314 <kvp_get_if_name+0x134>

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

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_get_if_name(i8* %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 i8* @kvp_get_mac_addr(i8* %0)
%7 = icmp eq i8* %6, null
br i1 %7, label %50, label %8
8: ; preds = %1
%9 = tail call i32* @opendir(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %50, label %11
11: ; preds = %8
%12 = 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))
%13 = call i32 @strlen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %14
%16 = call %struct.dirent* @readdir(i32* %9)
%17 = icmp eq %struct.dirent* %16, null
br i1 %17, label %46, label %18
18: ; preds = %11, %43
%19 = phi %struct.dirent* [ %44, %43 ], [ %16, %11 ]
store i8 0, i8* %15
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %19, i64 0, i32 0
%21 = load i8*, i8** %20
%22 = call i32 @strcat(i8* %5, i8* %21)
%23 = call i32 @strcat(i8* %5, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%25 = icmp eq i32* %24, null
br i1 %25, label %43, label %26
26: ; preds = %18
%27 = call i8* @fgets(i8* %4, i32 256, i32* %24)
%28 = icmp eq i8* %27, null
br i1 %28, label %41, label %29
29: ; preds = %26
%30 = call i8* @strchr(i8* %27, i8 signext 10)
%31 = icmp eq i8* %30, null
br i1 %31, label %33, label %32
32: ; preds = %29
store i8 0, i8* %30
br label %33
33: ; preds = %32, %29
%34 = call i32 @strcmp(i8* %27, i8* %6)
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %41
36: ; preds = %33
%37 = getelementptr inbounds %struct.dirent, %struct.dirent* %19, i64 0, i32 0
%38 = load i8*, i8** %37
%39 = call i8* @strdup(i8* %38)
%40 = call i32 @fclose(i32* %24)
br label %46
41: ; preds = %33, %26
%42 = call i32 @fclose(i32* %24)
br label %43
43: ; preds = %41, %18
%44 = call %struct.dirent* @readdir(i32* %9)
%45 = icmp eq %struct.dirent* %44, null
br i1 %45, label %46, label %18
46: ; preds = %43, %11, %36
%47 = phi i8* [ %39, %36 ], [ null, %11 ], [ null, %43 ]
%48 = call i32 @closedir(i32* %9)
%49 = call i32 @free(i8* %6)
br label %50
50: ; preds = %8, %1, %46
%51 = phi i8* [ %47, %46 ], [ null, %1 ], [ null, %8 ]
ret i8* %51
}