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

/* Variables and functions */
scalar_t__ DT_DIR;
int PATH_MAX;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ process_elapsed_time(int /*<<< orphan*/, unsigned long);
int read_elapsed_time(int /*<<< orphan*/, unsigned long *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *);
scalar_t__ strcmp(int /*<<< orphan*/, char *);
void scan_heartbeat_regions(void) {
  int ret = -1;
  DIR *dir = NULL;
  struct dirent *ent;
  char path[PATH_MAX];
  unsigned long elapsed;
  sprintf(path, "/sys/kernel/debug"
                "/o2hb");
  dir = opendir(path);
  if (!dir)
    return;
  do {
    ent = readdir(dir);
    if (ent && ent->d_type == DT_DIR && strcmp(ent->d_name, ".") &&
        strcmp(ent->d_name, "..")) {
      ret = read_elapsed_time(ent->d_name, &elapsed);
      if (!ret)
        process_elapsed_time(ent->d_name, elapsed);
    }
  } while (ent);
  if (dir)
    closedir(dir);
}


x86 assembly (clang, -O0):
<scan_heartbeat_regions>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0xffffffff,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x40(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x28(%rbp)
lea    0xe46(%rip),%rsi
call   1080 <sprintf@plt>
mov    -0x40(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11e3 <scan_heartbeat_regions+0x73>
movl   $0x1,-0x34(%rbp)
jmp    12ae <scan_heartbeat_regions+0x13e>
jmp    11e8 <scan_heartbeat_regions+0x78>
mov    -0x10(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1283 <scan_heartbeat_regions+0x113>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    0x2dca(%rip),%rcx
cmp    (%rcx),%rax
jne    1283 <scan_heartbeat_regions+0x113>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%edi
lea    0xdf3(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     1283 <scan_heartbeat_regions+0x113>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%edi
lea    0xdd5(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     1283 <scan_heartbeat_regions+0x113>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%edi
lea    -0x30(%rbp),%rsi
call   1030 <read_elapsed_time@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jne    127e <scan_heartbeat_regions+0x10e>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%edi
mov    -0x30(%rbp),%rsi
call   1090 <process_elapsed_time@plt>
jmp    1283 <scan_heartbeat_regions+0x113>
jmp    1288 <scan_heartbeat_regions+0x118>
cmpq   $0x0,-0x18(%rbp)
jne    11e8 <scan_heartbeat_regions+0x78>
cmpq   $0x0,-0x10(%rbp)
je     12a7 <scan_heartbeat_regions+0x137>
mov    -0x10(%rbp),%rdi
call   1050 <closedir@plt>
movl   $0x0,-0x34(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scan_heartbeat_regions>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e7b(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
mov    %rcx,%rdi
sub    %rax,%rdi
mov    %rdi,%rsp
neg    %rax
movups 0xe6b(%rip),%xmm0
movups %xmm0,(%rcx,%rax,1)
movabs $0x6268326f2f6775,%rax
mov    %rax,0xf(%rdi)
call   1040 <opendir@plt>
test   %rax,%rax
je     123f <scan_heartbeat_regions+0xdf>
mov    %rax,%r13
mov    %rax,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     1237 <scan_heartbeat_regions+0xd7>
mov    %rax,%rbx
mov    0x2e09(%rip),%r12
lea    0xe42(%rip),%r14
lea    0xe3a(%rip),%r15
jmp    11f0 <scan_heartbeat_regions+0x90>
nop
mov    %r13,%rdi
call   1070 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1237 <scan_heartbeat_regions+0xd7>
mov    (%rbx),%rax
cmp    (%r12),%rax
jne    11e0 <scan_heartbeat_regions+0x80>
mov    0x8(%rbx),%edi
mov    %r14,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11e0 <scan_heartbeat_regions+0x80>
mov    0x8(%rbx),%edi
mov    %r15,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11e0 <scan_heartbeat_regions+0x80>
mov    0x8(%rbx),%edi
lea    -0x30(%rbp),%rsi
call   1030 <read_elapsed_time@plt>
test   %eax,%eax
jne    11e0 <scan_heartbeat_regions+0x80>
mov    0x8(%rbx),%edi
mov    -0x30(%rbp),%rsi
call   1080 <process_elapsed_time@plt>
jmp    11e0 <scan_heartbeat_regions+0x80>
mov    %r13,%rdi
call   1050 <closedir@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i64, i32 }
@PATH_MAX = global i32 0
@.str = [23 x i8] c"/sys/kernel/debug/o2hb\00"
@DT_DIR = global i64 0
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
define void @scan_heartbeat_regions() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca %struct.dirent*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32
store i32 -1, i32* %1
store i32* null, i32** %2
%8 = load i32, i32* @PATH_MAX
%9 = zext i32 %8 to i64
%10 = call i8* @llvm.stacksave()
store i8* %10, i8** %4
%11 = alloca i8, i64 %9
store i64 %9, i64* %5
%12 = call i32 @sprintf(i8* %11, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%13 = call i32* @opendir(i8* %11)
store i32* %13, i32** %2
%14 = load i32*, i32** %2
%15 = icmp ne i32* %14, null
br i1 %15, label %17, label %16
16: ; preds = %0
store i32 1, i32* %7
br label %66
17: ; preds = %0
br label %18
18: ; preds = %56, %17
%19 = load i32*, i32** %2
%20 = call %struct.dirent* @readdir(i32* %19)
store %struct.dirent* %20, %struct.dirent** %3
%21 = load %struct.dirent*, %struct.dirent** %3
%22 = icmp ne %struct.dirent* %21, null
br i1 %22, label %23, label %55
23: ; preds = %18
%24 = load %struct.dirent*, %struct.dirent** %3
%25 = getelementptr inbounds %struct.dirent, %struct.dirent* %24, i32 0, i32 0
%26 = load i64, i64* %25
%27 = load i64, i64* @DT_DIR
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %55
29: ; preds = %23
%30 = load %struct.dirent*, %struct.dirent** %3
%31 = getelementptr inbounds %struct.dirent, %struct.dirent* %30, i32 0, i32 1
%32 = load i32, i32* %31
%33 = call i64 @strcmp(i32 %32, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %55
35: ; preds = %29
%36 = load %struct.dirent*, %struct.dirent** %3
%37 = getelementptr inbounds %struct.dirent, %struct.dirent* %36, i32 0, i32 1
%38 = load i32, i32* %37
%39 = call i64 @strcmp(i32 %38, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %55
41: ; preds = %35
%42 = load %struct.dirent*, %struct.dirent** %3
%43 = getelementptr inbounds %struct.dirent, %struct.dirent* %42, i32 0, i32 1
%44 = load i32, i32* %43
%45 = call i32 @read_elapsed_time(i32 %44, i64* %6)
store i32 %45, i32* %1
%46 = load i32, i32* %1
%47 = icmp ne i32 %46, 0
br i1 %47, label %54, label %48
48: ; preds = %41
%49 = load %struct.dirent*, %struct.dirent** %3
%50 = getelementptr inbounds %struct.dirent, %struct.dirent* %49, i32 0, i32 1
%51 = load i32, i32* %50
%52 = load i64, i64* %6
%53 = call i32 @process_elapsed_time(i32 %51, i64 %52)
br label %54
54: ; preds = %48, %41
br label %55
55: ; preds = %54, %35, %29, %23, %18
br label %56
56: ; preds = %55
%57 = load %struct.dirent*, %struct.dirent** %3
%58 = icmp ne %struct.dirent* %57, null
br i1 %58, label %18, label %59
59: ; preds = %56
%60 = load i32*, i32** %2
%61 = icmp ne i32* %60, null
br i1 %61, label %62, label %65
62: ; preds = %59
%63 = load i32*, i32** %2
%64 = call i32 @closedir(i32* %63)
br label %65
65: ; preds = %62, %59
store i32 0, i32* %7
br label %66
66: ; preds = %65, %16
%67 = load i8*, i8** %4
%68 = load i32, i32* %7
switch i32 %68, label %70 [
i32 0, label %69
i32 1, label %69
]
69: ; preds = %66, %66
ret void
70: ; preds = %66
unreachable
}

LLVM IR (-O2):
%struct.dirent = type { i64, i32 }
@PATH_MAX = global i32 0
@.str = [23 x i8] c"/sys/kernel/debug/o2hb\00"
@DT_DIR = global i64 0
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
define void @scan_heartbeat_regions() {
%1 = alloca i64
%2 = load i32, i32* @PATH_MAX
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = bitcast i64* %1 to i8*
%6 = call i32* @opendir(i8* %4)
%7 = icmp eq i32* %6, null
br i1 %7, label %39, label %8
8: ; preds = %0
%9 = call %struct.dirent* @readdir(i32* %6)
%10 = icmp eq %struct.dirent* %9, null
br i1 %10, label %37, label %11
11: ; preds = %8, %34
%12 = phi %struct.dirent* [ %35, %34 ], [ %9, %8 ]
%13 = getelementptr inbounds %struct.dirent, %struct.dirent* %12, i64 0, i32 0
%14 = load i64, i64* %13
%15 = load i64, i64* @DT_DIR
%16 = icmp eq i64 %14, %15
br i1 %16, label %17, label %34
17: ; preds = %11
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %12, i64 0, i32 1
%19 = load i32, i32* %18
%20 = call i64 @strcmp(i32 %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%21 = icmp eq i64 %20, 0
br i1 %21, label %34, label %22
22: ; preds = %17
%23 = load i32, i32* %18
%24 = call i64 @strcmp(i32 %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%25 = icmp eq i64 %24, 0
br i1 %25, label %34, label %26
26: ; preds = %22
%27 = load i32, i32* %18
%28 = call i32 @read_elapsed_time(i32 %27, i64* %1)
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %26
%31 = load i32, i32* %18
%32 = load i64, i64* %1
%33 = call i32 @process_elapsed_time(i32 %31, i64 %32)
br label %34
34: ; preds = %11, %17, %22, %30, %26
%35 = call %struct.dirent* @readdir(i32* %6)
%36 = icmp eq %struct.dirent* %35, null
br i1 %36, label %37, label %11
37: ; preds = %34, %8
%38 = call i32 @closedir(i32* %6)
br label %39
39: ; preds = %0, %37
ret void
}