#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 */
typedef int uint32_t;
struct dirent {
  scalar_t__ d_type;
  char *d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ DT_DIR;
scalar_t__ malloc(int);
int /*<<< orphan*/ memcmp(char *, char *, int);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, int);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, int, ...);
int strlen(char *);
void GetGPUHWMonPath(char **HWMonPath, uint32_t GPUIdx) {
  DIR *hwmon;
  char TempPath[256];
  struct dirent *inner_hwmon;
  *HWMonPath = NULL;
  sprintf(TempPath, "/sys/class/drm/card%d/device/hwmon", GPUIdx);
  hwmon = opendir(TempPath);
  if (!hwmon) {
    printf("Unable to open hwmon dir for GPU %d.\n", GPUIdx);
    return;
  }
  for (;;) {
    inner_hwmon = readdir(hwmon);
    if (!inner_hwmon)
      break;
    if (inner_hwmon->d_type != DT_DIR)
      continue;
    if (!memcmp(inner_hwmon->d_name, "hwmon", 5))
      break;
  }
  if (!inner_hwmon) {
    printf("Unable to open hwmon dir for GPU %d.\n", GPUIdx);
    return;
  }
  *HWMonPath =
      (char *)malloc(sizeof(char) * (256 + strlen(inner_hwmon->d_name)));
  sprintf(*HWMonPath, "/sys/class/drm/card%d/device/hwmon/%s", GPUIdx,
          inner_hwmon->d_name);
  return;
}


x86 assembly (clang, -O0):
<GetGPUHWMonPath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
movq   $0x0,(%rax)
lea    -0x120(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xe62(%rip),%rsi
mov    $0x0,%al
call   1090 <sprintf@plt>
lea    -0x120(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d4 <GetGPUHWMonPath+0x64>
mov    -0xc(%rbp),%esi
lea    0xe59(%rip),%rdi
call   1050 <printf@plt>
jmp    12bb <GetGPUHWMonPath+0x14b>
jmp    11d9 <GetGPUHWMonPath+0x69>
mov    -0x18(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x128(%rbp)
cmpq   $0x0,-0x128(%rbp)
jne    11fc <GetGPUHWMonPath+0x8c>
jmp    124a <GetGPUHWMonPath+0xda>
mov    -0x128(%rbp),%rax
mov    (%rax),%rax
mov    0x2dd3(%rip),%rcx
cmp    (%rcx),%rax
je     121b <GetGPUHWMonPath+0xab>
jmp    11d9 <GetGPUHWMonPath+0x69>
mov    -0x128(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdf0(%rip),%rsi
mov    $0x5,%edx
call   1060 <memcmp@plt>
cmp    $0x0,%eax
jne    1245 <GetGPUHWMonPath+0xd5>
jmp    124a <GetGPUHWMonPath+0xda>
jmp    11d9 <GetGPUHWMonPath+0x69>
cmpq   $0x0,-0x128(%rbp)
jne    126c <GetGPUHWMonPath+0xfc>
mov    -0xc(%rbp),%esi
lea    0xdc1(%rip),%rdi
call   1050 <printf@plt>
jmp    12bb <GetGPUHWMonPath+0x14b>
mov    -0x128(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <strlen@plt>
add    $0x100,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0xc(%rbp),%edx
mov    -0x128(%rbp),%rax
mov    0x8(%rax),%rcx
lea    0xd95(%rip),%rsi
mov    $0x0,%al
call   1090 <sprintf@plt>
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GetGPUHWMonPath>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
mov    %esi,%r15d
mov    %rdi,%r14
movq   $0x0,(%rdi)
lea    0xe6b(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %r15d,%edx
xor    %eax,%eax
call   1090 <sprintf@plt>
mov    %rbx,%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     124f <GetGPUHWMonPath+0xdf>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     124f <GetGPUHWMonPath+0xdf>
mov    %rax,%rbp
mov    0x2e0c(%rip),%r13
lea    0xe42(%rip),%r12
jmp    11f0 <GetGPUHWMonPath+0x80>
nopl   (%rax)
mov    %rbx,%rdi
call   1070 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     124f <GetGPUHWMonPath+0xdf>
mov    0x0(%rbp),%rax
cmp    0x0(%r13),%rax
jne    11e0 <GetGPUHWMonPath+0x70>
mov    0x8(%rbp),%rdi
mov    %r12,%rsi
mov    $0x5,%edx
call   1060 <memcmp@plt>
test   %eax,%eax
jne    11e0 <GetGPUHWMonPath+0x70>
mov    0x8(%rbp),%rdi
call   1040 <strlen@plt>
lea    0x100(%rax),%edi
call   1080 <malloc@plt>
mov    %rax,(%r14)
mov    0x8(%rbp),%rcx
lea    0xe18(%rip),%rsi
mov    %rax,%rdi
mov    %r15d,%edx
xor    %eax,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1090 <sprintf@plt>
lea    0xdcd(%rip),%rdi
mov    %r15d,%esi
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <printf@plt>

LLVM IR (-O0):
%struct.dirent = type { i64, i8* }
@.str = [35 x i8] c"/sys/class/drm/card%d/device/hwmon\00"
@.str.1 = [38 x i8] c"Unable to open hwmon dir for GPU %d.\0A\00"
@DT_DIR = global i64 0
@.str.2 = [6 x i8] c"hwmon\00"
@.str.3 = [38 x i8] c"/sys/class/drm/card%d/device/hwmon/%s\00"
define void @GetGPUHWMonPath(i8** %0, i32 %1) {
%3 = alloca i8**
%4 = alloca i32
%5 = alloca i32*
%6 = alloca [256 x i8]
%7 = alloca %struct.dirent*
store i8** %0, i8*** %3
store i32 %1, i32* %4
%8 = load i8**, i8*** %3
store i8* null, i8** %8
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%10 = load i32, i32* %4
%11 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %9, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%13 = call i32* @opendir(i8* %12)
store i32* %13, i32** %5
%14 = load i32*, i32** %5
%15 = icmp ne i32* %14, null
br i1 %15, label %19, label %16
16: ; preds = %2
%17 = load i32, i32* %4
%18 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %17)
br label %66
19: ; preds = %2
br label %20
20: ; preds = %40, %32, %19
%21 = load i32*, i32** %5
%22 = call %struct.dirent* @readdir(i32* %21)
store %struct.dirent* %22, %struct.dirent** %7
%23 = load %struct.dirent*, %struct.dirent** %7
%24 = icmp ne %struct.dirent* %23, null
br i1 %24, label %26, label %25
25: ; preds = %20
br label %41
26: ; preds = %20
%27 = load %struct.dirent*, %struct.dirent** %7
%28 = getelementptr inbounds %struct.dirent, %struct.dirent* %27, i32 0, i32 0
%29 = load i64, i64* %28
%30 = load i64, i64* @DT_DIR
%31 = icmp ne i64 %29, %30
br i1 %31, label %32, label %33
32: ; preds = %26
br label %20
33: ; preds = %26
%34 = load %struct.dirent*, %struct.dirent** %7
%35 = getelementptr inbounds %struct.dirent, %struct.dirent* %34, i32 0, i32 1
%36 = load i8*, i8** %35
%37 = call i32 @memcmp(i8* %36, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 5)
%38 = icmp ne i32 %37, 0
br i1 %38, label %40, label %39
39: ; preds = %33
br label %41
40: ; preds = %33
br label %20
41: ; preds = %39, %25
%42 = load %struct.dirent*, %struct.dirent** %7
%43 = icmp ne %struct.dirent* %42, null
br i1 %43, label %47, label %44
44: ; preds = %41
%45 = load i32, i32* %4
%46 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %45)
br label %66
47: ; preds = %41
%48 = load %struct.dirent*, %struct.dirent** %7
%49 = getelementptr inbounds %struct.dirent, %struct.dirent* %48, i32 0, i32 1
%50 = load i8*, i8** %49
%51 = call i32 @strlen(i8* %50)
%52 = add nsw i32 256, %51
%53 = sext i32 %52 to i64
%54 = mul i64 1, %53
%55 = trunc i64 %54 to i32
%56 = call i64 @malloc(i32 %55)
%57 = inttoptr i64 %56 to i8*
%58 = load i8**, i8*** %3
store i8* %57, i8** %58
%59 = load i8**, i8*** %3
%60 = load i8*, i8** %59
%61 = load i32, i32* %4
%62 = load %struct.dirent*, %struct.dirent** %7
%63 = getelementptr inbounds %struct.dirent, %struct.dirent* %62, i32 0, i32 1
%64 = load i8*, i8** %63
%65 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %60, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %61, i8* %64)
br label %66
66: ; preds = %47, %44, %16
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i64, i8* }
@.str = [35 x i8] c"/sys/class/drm/card%d/device/hwmon\00"
@.str.1 = [38 x i8] c"Unable to open hwmon dir for GPU %d.\0A\00"
@DT_DIR = global i64 0
@.str.2 = [6 x i8] c"hwmon\00"
@.str.3 = [38 x i8] c"/sys/class/drm/card%d/device/hwmon/%s\00"
define void @GetGPUHWMonPath(i8** nocapture writeonly %0, i32 %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
store i8* null, i8** %0
%5 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %1)
%6 = call i32* @opendir(i8* %4)
%7 = icmp eq i32* %6, null
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = tail call %struct.dirent* @readdir(i32* %6)
%10 = icmp eq %struct.dirent* %9, null
br i1 %10, label %27, label %13
11: ; preds = %2
%12 = tail call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %1)
br label %38
13: ; preds = %8, %19
%14 = phi %struct.dirent* [ %20, %19 ], [ %9, %8 ]
%15 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 0
%16 = load i64, i64* %15
%17 = load i64, i64* @DT_DIR
%18 = icmp eq i64 %16, %17
br i1 %18, label %22, label %19
19: ; preds = %13, %22
%20 = tail call %struct.dirent* @readdir(i32* %6)
%21 = icmp eq %struct.dirent* %20, null
br i1 %21, label %27, label %13
22: ; preds = %13
%23 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 1
%24 = load i8*, i8** %23
%25 = tail call i32 @memcmp(i8* %24, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 5)
%26 = icmp eq i32 %25, 0
br i1 %26, label %29, label %19
27: ; preds = %19, %8
%28 = tail call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %1)
br label %38
29: ; preds = %22
%30 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 1
%31 = load i8*, i8** %30
%32 = tail call i32 @strlen(i8* %31)
%33 = add nsw i32 %32, 256
%34 = tail call i64 @malloc(i32 %33)
%35 = inttoptr i64 %34 to i8*
store i8* %35, i8** %0
%36 = load i8*, i8** %30
%37 = tail call i32 (i8*, i8*, i32, ...) @sprintf(i8* dereferenceable(1) %35, i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %1, i8* %36)
br label %38
38: ; preds = %29, %27, %11
ret void
}