#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 *);
int strlen(char *);
char *kvp_get_if_name(char *guid) {
  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];
  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, "/device/device_id");
    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    $0x270,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x48(%rbp)
lea    0xe22(%rip),%rax
mov    %rax,-0x158(%rbp)
mov    -0x158(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    120d <kvp_get_if_name+0x4d>
movq   $0x0,-0x8(%rbp)
jmp    1364 <kvp_get_if_name+0x1a4>
lea    -0x260(%rbp),%rdi
mov    -0x158(%rbp),%rcx
mov    $0x100,%esi
lea    0xde9(%rip),%rdx
call   1070 <snprintf@plt>
lea    -0x260(%rbp),%rax
mov    %rax,-0x268(%rbp)
mov    -0x158(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x268(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   10b0 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1353 <kvp_get_if_name+0x193>
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x260(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
call   10d0 <strcat@plt>
lea    -0x260(%rbp),%rdi
lea    0xd7b(%rip),%rsi
call   10d0 <strcat@plt>
lea    -0x260(%rbp),%rdi
lea    0xd7a(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12c4 <kvp_get_if_name+0x104>
jmp    1259 <kvp_get_if_name+0x99>
lea    -0x150(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    $0x100,%esi
call   1090 <fgets@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1345 <kvp_get_if_name+0x185>
mov    -0x30(%rbp),%rdi
mov    $0xa,%esi
call   1060 <strchr@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     130c <kvp_get_if_name+0x14c>
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%eax
jne    1340 <kvp_get_if_name+0x180>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   10e0 <strdup@plt>
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
jmp    1353 <kvp_get_if_name+0x193>
jmp    1345 <kvp_get_if_name+0x185>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
jmp    1259 <kvp_get_if_name+0x99>
mov    -0x18(%rbp),%rdi
call   1080 <closedir@plt>
mov    -0x48(%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
mov    %rdi,0x8(%rsp)
lea    0xe23(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     12ea <kvp_get_if_name+0x12a>
mov    %rax,%r12
lea    0xe1b(%rip),%rdx
lea    0xe04(%rip),%rbx
lea    0x10(%rsp),%rdi
mov    $0x100,%esi
mov    %rbx,%rcx
call   1070 <snprintf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebx
mov    %r12,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     12de <kvp_get_if_name+0x11e>
mov    %rax,%r15
movslq %ebx,%rbp
lea    0x10(%rsp),%rbx
jmp    126d <kvp_get_if_name+0xad>
cs nopw 0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    0x8(%rsp),%rsi
call   10a0 <strcmp@plt>
test   %eax,%eax
je     1301 <kvp_get_if_name+0x141>
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %r12,%rdi
call   10b0 <readdir@plt>
mov    %rax,%r15
test   %rax,%rax
je     12de <kvp_get_if_name+0x11e>
movb   $0x0,0x10(%rsp,%rbp,1)
mov    (%r15),%rsi
mov    %rbx,%rdi
call   10d0 <strcat@plt>
mov    %rbx,%rdi
lea    0xd8c(%rip),%rsi
call   10d0 <strcat@plt>
mov    %rbx,%rdi
lea    0xd8f(%rip),%rsi
call   10c0 <fopen@plt>
test   %rax,%rax
je     125d <kvp_get_if_name+0x9d>
mov    %rax,%r14
lea    0x110(%rsp),%rdi
mov    $0x100,%esi
mov    %rax,%rdx
call   1090 <fgets@plt>
test   %rax,%rax
je     1255 <kvp_get_if_name+0x95>
mov    %rax,%r13
mov    %rax,%rdi
mov    $0xa,%esi
call   1060 <strchr@plt>
test   %rax,%rax
je     1240 <kvp_get_if_name+0x80>
movb   $0x0,(%rax)
jmp    1240 <kvp_get_if_name+0x80>
xor    %ebx,%ebx
mov    %r12,%rdi
call   1080 <closedir@plt>
jmp    12ec <kvp_get_if_name+0x12c>
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   10e0 <strdup@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1030 <fclose@plt>
jmp    12e0 <kvp_get_if_name+0x120>

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

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 = [18 x i8] c"/device/device_id\00"
@.str.3 = [2 x i8] c"r\00"
define noalias i8* @kvp_get_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 %46, 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 = sext 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 %43, label %15
15: ; preds = %8, %40
%16 = phi %struct.dirent* [ %41, %40 ], [ %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 ([18 x i8], [18 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 %40, 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 %38, 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 @strcmp(i8* %24, i8* dereferenceable(1) %0)
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %38
33: ; preds = %30
%34 = getelementptr inbounds %struct.dirent, %struct.dirent* %16, i64 0, i32 0
%35 = load i8*, i8** %34
%36 = call i8* @strdup(i8* %35)
%37 = call i32 @fclose(i32* %21)
br label %43
38: ; preds = %30, %23
%39 = call i32 @fclose(i32* %21)
br label %40
40: ; preds = %38, %15
%41 = call %struct.dirent* @readdir(i32* %6)
%42 = icmp eq %struct.dirent* %41, null
br i1 %42, label %43, label %15
43: ; preds = %40, %8, %33
%44 = phi i8* [ %36, %33 ], [ null, %8 ], [ null, %40 ]
%45 = call i32 @closedir(i32* %6)
br label %46
46: ; preds = %1, %43
%47 = phi i8* [ %44, %43 ], [ null, %1 ]
ret i8* %47
}