#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*/);
char *kvp_get_if_name(char *guid) {
  DIR *dir;
  struct dirent *entry;
  FILE *file;
  char *p, *x;
  char *if_name = NULL;
  char buf[256];
  char dev_id[PATH_MAX];
  dir = opendir("/sys/class/net/");
  if (dir == NULL)
    return NULL;
  while ((entry = readdir(dir)) != NULL) {
    snprintf(dev_id, sizeof(dev_id), "%s%s/device/device_id", "/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';
      if (!strcmp(p, guid)) {
        if_name = strdup(entry->d_name);
        fclose(file);
        break;
      }
    }
    fclose(file);
  }
  closedir(dir);
  return if_name;
}


x86 assembly (clang, -O0):
<kvp_get_if_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x170,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x40(%rbp)
mov    0x2e32(%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    0xe06(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1225 <kvp_get_if_name+0x85>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x154(%rbp)
jmp    133b <kvp_get_if_name+0x19b>
jmp    122a <kvp_get_if_name+0x8a>
mov    -0x18(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1320 <kvp_get_if_name+0x180>
mov    -0x160(%rbp),%rdi
mov    -0x168(%rbp),%rax
mov    %eax,%esi
mov    -0x20(%rbp),%rax
mov    (%rax),%r8d
lea    0xdb1(%rip),%rdx
lea    0xd9a(%rip),%rcx
call   1060 <snprintf@plt>
mov    -0x160(%rbp),%rdi
lea    0xdad(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1292 <kvp_get_if_name+0xf2>
jmp    122a <kvp_get_if_name+0x8a>
lea    -0x140(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    $0x100,%esi
call   1080 <fgets@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1312 <kvp_get_if_name+0x172>
mov    -0x30(%rbp),%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     12da <kvp_get_if_name+0x13a>
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%eax
jne    130d <kvp_get_if_name+0x16d>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   10c0 <strdup@plt>
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
jmp    1320 <kvp_get_if_name+0x180>
jmp    1312 <kvp_get_if_name+0x172>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
jmp    122a <kvp_get_if_name+0x8a>
mov    -0x18(%rbp),%rdi
call   1070 <closedir@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x154(%rbp)
mov    -0x148(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kvp_get_if_name>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %rdi,-0x38(%rbp)
mov    %rsp,%r15
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
mov    %rax,-0x40(%rbp)
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe20(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     12aa <kvp_get_if_name+0x10a>
mov    %rax,%r13
mov    %rax,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     12b2 <kvp_get_if_name+0x112>
mov    %rax,%r14
mov    %r15,-0x30(%rbp)
jmp    123c <kvp_get_if_name+0x9c>
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    -0x38(%rbp),%rsi
call   1090 <strcmp@plt>
test   %eax,%eax
je     12d1 <kvp_get_if_name+0x131>
mov    %r12,%rdi
call   1030 <fclose@plt>
mov    %r13,%rdi
call   10a0 <readdir@plt>
mov    %rax,%r14
test   %rax,%rax
je     12ae <kvp_get_if_name+0x10e>
mov    (%r14),%r8d
mov    %rbx,%rdi
mov    -0x40(%rbp),%rsi
lea    0xdc3(%rip),%rdx
lea    0xdac(%rip),%rcx
call   1060 <snprintf@plt>
mov    %rbx,%rdi
lea    0xdc3(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     122c <kvp_get_if_name+0x8c>
mov    %rax,%r12
lea    -0x140(%rbp),%rdi
mov    $0x100,%esi
mov    %rax,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     1224 <kvp_get_if_name+0x84>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     1210 <kvp_get_if_name+0x70>
movb   $0x0,(%rax)
jmp    1210 <kvp_get_if_name+0x70>
xor    %ebx,%ebx
jmp    12bc <kvp_get_if_name+0x11c>
mov    -0x30(%rbp),%r15
xor    %ebx,%ebx
mov    %r13,%rdi
call   1070 <closedir@plt>
mov    %r15,%rsp
mov    %rbx,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r14),%edi
call   10c0 <strdup@plt>
mov    %rax,%rbx
mov    %r12,%rdi
call   1030 <fclose@plt>
mov    -0x30(%rbp),%r15
jmp    12b4 <kvp_get_if_name+0x114>

LLVM IR (-O0):
%struct.dirent = type { i32 }
@PATH_MAX = global i32 0
@.str = [16 x i8] c"/sys/class/net/\00"
@.str.1 = [22 x i8] c"%s%s/device/device_id\00"
@.str.2 = [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 [256 x i8]
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i32
store i8* %0, i8** %3
store i8* null, i8** %9
%14 = load i32, i32* @PATH_MAX
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %11
%17 = alloca i8, i64 %15
store i64 %15, i64* %12
%18 = call i32* @opendir(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i32* %18, i32** %4
%19 = load i32*, i32** %4
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %22
21: ; preds = %1
store i8* null, i8** %2
store i32 1, i32* %13
br label %70
22: ; preds = %1
br label %23
23: ; preds = %63, %36, %22
%24 = load i32*, i32** %4
%25 = call %struct.dirent* @readdir(i32* %24)
store %struct.dirent* %25, %struct.dirent** %5
%26 = icmp ne %struct.dirent* %25, null
br i1 %26, label %27, label %66
27: ; preds = %23
%28 = trunc i64 %15 to i32
%29 = load %struct.dirent*, %struct.dirent** %5
%30 = getelementptr inbounds %struct.dirent, %struct.dirent* %29, i32 0, i32 0
%31 = load i32, i32* %30
%32 = call i32 @snprintf(i8* %17, i32 %28, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %31)
%33 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %33, i32** %6
%34 = load i32*, i32** %6
%35 = icmp eq i32* %34, null
br i1 %35, label %36, label %37
36: ; preds = %27
br label %23
37: ; preds = %27
%38 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%39 = load i32*, i32** %6
%40 = call i8* @fgets(i8* %38, i32 256, i32* %39)
store i8* %40, i8** %7
%41 = load i8*, i8** %7
%42 = icmp ne i8* %41, null
br i1 %42, label %43, label %63
43: ; preds = %37
%44 = load i8*, i8** %7
%45 = call i8* @strchr(i8* %44, i8 signext 10)
store i8* %45, i8** %8
%46 = load i8*, i8** %8
%47 = icmp ne i8* %46, null
br i1 %47, label %48, label %50
48: ; preds = %43
%49 = load i8*, i8** %8
store i8 0, i8* %49
br label %50
50: ; preds = %48, %43
%51 = load i8*, i8** %7
%52 = load i8*, i8** %3
%53 = call i32 @strcmp(i8* %51, i8* %52)
%54 = icmp ne i32 %53, 0
br i1 %54, label %62, label %55
55: ; preds = %50
%56 = load %struct.dirent*, %struct.dirent** %5
%57 = getelementptr inbounds %struct.dirent, %struct.dirent* %56, i32 0, i32 0
%58 = load i32, i32* %57
%59 = call i8* @strdup(i32 %58)
store i8* %59, i8** %9
%60 = load i32*, i32** %6
%61 = call i32 @fclose(i32* %60)
br label %66
62: ; preds = %50
br label %63
63: ; preds = %62, %37
%64 = load i32*, i32** %6
%65 = call i32 @fclose(i32* %64)
br label %23
66: ; preds = %55, %23
%67 = load i32*, i32** %4
%68 = call i32 @closedir(i32* %67)
%69 = load i8*, i8** %9
store i8* %69, i8** %2
store i32 1, i32* %13
br label %70
70: ; preds = %66, %21
%71 = load i8*, i8** %11
%72 = load i8*, i8** %2
ret i8* %72
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@PATH_MAX = global i32 0
@.str = [16 x i8] c"/sys/class/net/\00"
@.str.1 = [22 x i8] c"%s%s/device/device_id\00"
@.str.2 = [2 x i8] c"r\00"
define i8* @kvp_get_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 %43, label %10
10: ; preds = %1
%11 = call %struct.dirent* @readdir(i32* %8)
%12 = icmp eq %struct.dirent* %11, null
br i1 %12, label %40, label %13
13: ; preds = %10, %37
%14 = phi %struct.dirent* [ %38, %37 ], [ %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 ([22 x i8], [22 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 %37, 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 %35, 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 @strcmp(i8* %21, i8* dereferenceable(1) %0)
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %27
%31 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 0
%32 = load i32, i32* %31
%33 = call i8* @strdup(i32 %32)
%34 = call i32 @fclose(i32* %18)
br label %40
35: ; preds = %27, %20
%36 = call i32 @fclose(i32* %18)
br label %37
37: ; preds = %35, %13
%38 = call %struct.dirent* @readdir(i32* %8)
%39 = icmp eq %struct.dirent* %38, null
br i1 %39, label %40, label %13
40: ; preds = %37, %10, %30
%41 = phi i8* [ %33, %30 ], [ null, %10 ], [ null, %37 ]
%42 = call i32 @closedir(i32* %8)
br label %43
43: ; preds = %1, %40
%44 = phi i8* [ %41, %40 ], [ null, %1 ]
ret i8* %44
}