#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 *);
int /*<<< orphan*/ strcpy(char *, char *);
char *strdup(char *);
int strlen(char *);
char *kvp_get_mac_addr(char *guid) {
  DIR *dir;
  struct dirent *entry;
  FILE *file, *file2;
  char *p, *q, *x;
  char *mac_addr = NULL;
  char buf[256];
  char *kvp_dev_dir = "/sys/bus/vmbus/devices/";
  char dev_id[256];
  char dev_id2[256];
  dir = opendir(kvp_dev_dir);
  if (dir == NULL)
    return NULL;
  snprintf(dev_id, sizeof(dev_id), "%s", kvp_dev_dir);
  q = dev_id + strlen(kvp_dev_dir);
  while ((entry = readdir(dir)) != NULL) {
    *q = '\0';
    strcat(dev_id, entry->d_name);
    strcpy(dev_id2, dev_id);
    strcat(dev_id2, "/misc");
    strcat(dev_id, "/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)) {
        file2 = fopen(dev_id2, "r");
        if (file2 == NULL) {
          fclose(file);
          continue;
        }
        p = fgets(buf, sizeof(buf), file2);
        if (p) {
          x = strchr(p, '\n');
          if (x)
            *x = '\0';
          mac_addr = strdup(p);
          fclose(file2);
          fclose(file);
          break;
        }
        fclose(file2);
      }
    }
    fclose(file);
  }
  closedir(dir);
  return mac_addr;
}


x86 assembly (clang, -O0):
<kvp_get_mac_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x370,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x50(%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_get_mac_addr+0x4d>
movq   $0x0,-0x8(%rbp)
jmp    1421 <kvp_get_mac_addr+0x251>
lea    -0x260(%rbp),%rdi
mov    -0x158(%rbp),%rcx
mov    $0x100,%esi
lea    0xde1(%rip),%rdx
call   1080 <snprintf@plt>
lea    -0x260(%rbp),%rax
mov    %rax,-0x368(%rbp)
mov    -0x158(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x368(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   10c0 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1410 <kvp_get_mac_addr+0x240>
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x260(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
call   10e0 <strcat@plt>
lea    -0x360(%rbp),%rdi
lea    -0x260(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x360(%rbp),%rdi
lea    0xd60(%rip),%rsi
call   10e0 <strcat@plt>
lea    -0x260(%rbp),%rdi
lea    0xd53(%rip),%rsi
call   10e0 <strcat@plt>
lea    -0x260(%rbp),%rdi
lea    0xd4b(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12fa <kvp_get_mac_addr+0x12a>
jmp    1269 <kvp_get_mac_addr+0x99>
lea    -0x150(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    $0x100,%esi
call   10a0 <fgets@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1402 <kvp_get_mac_addr+0x232>
mov    -0x38(%rbp),%rdi
mov    $0xa,%esi
call   1070 <strchr@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     1342 <kvp_get_mac_addr+0x172>
mov    -0x48(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10b0 <strcmp@plt>
cmp    $0x0,%eax
jne    13fd <kvp_get_mac_addr+0x22d>
lea    -0x360(%rbp),%rdi
lea    0xcc6(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1388 <kvp_get_mac_addr+0x1b8>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
jmp    1269 <kvp_get_mac_addr+0x99>
lea    -0x150(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    $0x100,%esi
call   10a0 <fgets@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     13f4 <kvp_get_mac_addr+0x224>
mov    -0x38(%rbp),%rdi
mov    $0xa,%esi
call   1070 <strchr@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     13d0 <kvp_get_mac_addr+0x200>
mov    -0x48(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x38(%rbp),%rdi
call   10f0 <strdup@plt>
mov    %rax,-0x50(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
jmp    1410 <kvp_get_mac_addr+0x240>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
jmp    1402 <kvp_get_mac_addr+0x232>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
jmp    1269 <kvp_get_mac_addr+0x99>
mov    -0x18(%rbp),%rdi
call   1090 <closedir@plt>
mov    -0x50(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x370,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kvp_get_mac_addr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x318,%rsp
mov    %rdi,(%rsp)
lea    0xe14(%rip),%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     135d <kvp_get_mac_addr+0x18d>
mov    %rax,%r14
lea    0xe14(%rip),%rdx
lea    0xdf5(%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    %r14,%rdi
call   10c0 <readdir@plt>
test   %rax,%rax
je     1350 <kvp_get_mac_addr+0x180>
movslq %ebx,%rcx
mov    %rcx,0x8(%rsp)
lea    0x10(%rsp),%r12
lea    0x210(%rsp),%r13
jmp    1269 <kvp_get_mac_addr+0x99>
nop
mov    %rbp,%rdi
call   1040 <fclose@plt>
mov    %r14,%rdi
call   10c0 <readdir@plt>
test   %rax,%rax
je     1350 <kvp_get_mac_addr+0x180>
mov    0x8(%rsp),%rcx
movb   $0x0,0x10(%rsp,%rcx,1)
mov    (%rax),%rsi
mov    %r12,%rdi
call   10e0 <strcat@plt>
mov    %r13,%rdi
mov    %r12,%rsi
call   1030 <strcpy@plt>
mov    %r13,%rdi
lea    0xd88(%rip),%rsi
call   10e0 <strcat@plt>
mov    %r12,%rdi
lea    0xd7f(%rip),%rsi
call   10e0 <strcat@plt>
mov    %r12,%rdi
lea    0xd7b(%rip),%rsi
call   10d0 <fopen@plt>
test   %rax,%rax
je     1258 <kvp_get_mac_addr+0x88>
mov    %rax,%rbp
lea    0x110(%rsp),%rdi
mov    $0x100,%esi
mov    %rax,%rdx
call   10a0 <fgets@plt>
test   %rax,%rax
je     1250 <kvp_get_mac_addr+0x80>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0xa,%esi
call   1070 <strchr@plt>
test   %rax,%rax
je     12f4 <kvp_get_mac_addr+0x124>
movb   $0x0,(%rax)
mov    %rbx,%rdi
mov    (%rsp),%rsi
call   10b0 <strcmp@plt>
test   %eax,%eax
jne    1250 <kvp_get_mac_addr+0x80>
mov    %r13,%rdi
lea    0xd1a(%rip),%rsi
call   10d0 <fopen@plt>
test   %rax,%rax
je     1250 <kvp_get_mac_addr+0x80>
mov    %rax,%r15
mov    %r14,%rbx
lea    0x110(%rsp),%rdi
mov    $0x100,%esi
mov    %rax,%rdx
call   10a0 <fgets@plt>
test   %rax,%rax
jne    1375 <kvp_get_mac_addr+0x1a5>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %rbx,%r14
jmp    1250 <kvp_get_mac_addr+0x80>
xor    %r12d,%r12d
mov    %r14,%rdi
call   1090 <closedir@plt>
jmp    1360 <kvp_get_mac_addr+0x190>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x318,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rax,%r14
mov    %rax,%rdi
mov    $0xa,%esi
call   1070 <strchr@plt>
test   %rax,%rax
je     138d <kvp_get_mac_addr+0x1bd>
movb   $0x0,(%rax)
mov    %r14,%rdi
call   10f0 <strdup@plt>
mov    %rax,%r12
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
mov    %rbx,%r14
jmp    1353 <kvp_get_mac_addr+0x183>

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

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