#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 utsname {
  int /*<<< orphan*/ release;
};

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ R_OK;
int access(char *, int /*<<< orphan*/);
int atoi(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ mbp_log(int /*<<< orphan*/, char *, int /*<<< orphan*/);
char *smprintf(char *, int, int);
char *strtok(int /*<<< orphan*/, char *);
int /*<<< orphan*/ uname(struct utsname *);
bool is_modern_sensors_path() {
  struct utsname kernel;
  uname(&kernel);
  char *str_kernel_version;
  str_kernel_version = strtok(kernel.release, ".");
  if (atoi(str_kernel_version) < 3) {
    mbp_log(LOG_ERR,
            "mbpfan detected a pre-3.x.x linux kernel. Detected version: %s. "
            "Exiting.\n",
            kernel.release);
    exit(EXIT_FAILURE);
  }
  int counter;
  for (counter = 0; counter < 12; counter++) {
    int temp;
    for (temp = 1; temp < 64; ++temp) {
      char *path = smprintf(
          "/sys/devices/platform/coretemp.0/hwmon/hwmon%d/temp%d_input",
          counter, temp);
      int res = access(path, R_OK);
      free(path);
      if (res == 0) {
        return 1;
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<is_modern_sensors_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    -0x8(%rbp),%rdi
call   1050 <uname@plt>
mov    -0x8(%rbp),%edi
lea    0xe65(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1090 <atoi@plt>
cmp    $0x3,%eax
jge    11dc <is_modern_sensors_path+0x5c>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xe39(%rip),%rsi
call   1070 <mbp_log@plt>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0xc,-0x14(%rbp)
jge    126e <is_modern_sensors_path+0xee>
movl   $0x1,-0x18(%rbp)
cmpl   $0x40,-0x18(%rbp)
jge    125b <is_modern_sensors_path+0xdb>
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    0xe41(%rip),%rdi
call   1040 <smprintf@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2db9(%rip),%rax
mov    (%rax),%esi
call   1060 <access@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x0,-0x24(%rbp)
jne    1248 <is_modern_sensors_path+0xc8>
movl   $0x1,-0x4(%rbp)
jmp    1275 <is_modern_sensors_path+0xf5>
jmp    124d <is_modern_sensors_path+0xcd>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11f4 <is_modern_sensors_path+0x74>
jmp    1260 <is_modern_sensors_path+0xe0>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11e3 <is_modern_sensors_path+0x63>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_modern_sensors_path>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsp,%rdi
call   1050 <uname@plt>
mov    (%rsp),%ebx
lea    0xe63(%rip),%rsi
mov    %ebx,%edi
call   1080 <strtok@plt>
mov    %rax,%rdi
call   1090 <atoi@plt>
cmp    $0x3,%eax
jl     1225 <is_modern_sensors_path+0xa5>
xor    %r12d,%r12d
lea    0xe91(%rip),%r14
mov    0x2e16(%rip),%r15
mov    $0x1,%ebp
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %r12d,%esi
mov    %ebp,%edx
call   1040 <smprintf@plt>
mov    %rax,%rbx
mov    (%r15),%esi
mov    %rax,%rdi
call   1060 <access@plt>
mov    %eax,%r13d
mov    %rbx,%rdi
call   1030 <free@plt>
test   %r13d,%r13d
je     1211 <is_modern_sensors_path+0x91>
add    $0x1,%ebp
cmp    $0x40,%ebp
jne    11d0 <is_modern_sensors_path+0x50>
add    $0x1,%r12d
cmp    $0xc,%r12d
jne    11c2 <is_modern_sensors_path+0x42>
xor    %eax,%eax
jmp    1216 <is_modern_sensors_path+0x96>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
lea    0xdcd(%rip),%rsi
mov    %ebx,%edx
call   1070 <mbp_log@plt>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.utsname = type { i32 }
@.str = [2 x i8] c".\00"
@LOG_ERR = global i32 0
@.str.1 = [74 x i8] c"mbpfan detected a pre-3.x.x linux kernel. Detected version: %s. Exiting.\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [60 x i8] c"/sys/devices/platform/coretemp.0/hwmon/hwmon%d/temp%d_input\00"
@R_OK = global i32 0
define i32 @is_modern_sensors_path() {
%1 = alloca i32
%2 = alloca %struct.utsname
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = call i32 @uname(%struct.utsname* %2)
%9 = getelementptr inbounds %struct.utsname, %struct.utsname* %2, i32 0, i32 0
%10 = load i32, i32* %9
%11 = call i8* @strtok(i32 %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %11, i8** %3
%12 = load i8*, i8** %3
%13 = call i32 @atoi(i8* %12)
%14 = icmp slt i32 %13, 3
br i1 %14, label %15, label %22
15: ; preds = %0
%16 = load i32, i32* @LOG_ERR
%17 = getelementptr inbounds %struct.utsname, %struct.utsname* %2, i32 0, i32 0
%18 = load i32, i32* %17
%19 = call i32 @mbp_log(i32 %16, i8* getelementptr inbounds ([74 x i8], [74 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = load i32, i32* @EXIT_FAILURE
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %0
store i32 0, i32* %4
br label %23
23: ; preds = %47, %22
%24 = load i32, i32* %4
%25 = icmp slt i32 %24, 12
br i1 %25, label %26, label %50
26: ; preds = %23
store i32 1, i32* %5
br label %27
27: ; preds = %43, %26
%28 = load i32, i32* %5
%29 = icmp slt i32 %28, 64
br i1 %29, label %30, label %46
30: ; preds = %27
%31 = load i32, i32* %4
%32 = load i32, i32* %5
%33 = call i8* @smprintf(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.2, i64 0, i64 0), i32 %31, i32 %32)
store i8* %33, i8** %6
%34 = load i8*, i8** %6
%35 = load i32, i32* @R_OK
%36 = call i32 @access(i8* %34, i32 %35)
store i32 %36, i32* %7
%37 = load i8*, i8** %6
%38 = call i32 @free(i8* %37)
%39 = load i32, i32* %7
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %30
store i32 1, i32* %1
br label %51
42: ; preds = %30
br label %43
43: ; preds = %42
%44 = load i32, i32* %5
%45 = add nsw i32 %44, 1
store i32 %45, i32* %5
br label %27
46: ; preds = %27
br label %47
47: ; preds = %46
%48 = load i32, i32* %4
%49 = add nsw i32 %48, 1
store i32 %49, i32* %4
br label %23
50: ; preds = %23
store i32 0, i32* %1
br label %51
51: ; preds = %50, %41
%52 = load i32, i32* %1
ret i32 %52
}

LLVM IR (-O2):
%struct.utsname = type { i32 }
@.str = [2 x i8] c".\00"
@LOG_ERR = global i32 0
@.str.1 = [74 x i8] c"mbpfan detected a pre-3.x.x linux kernel. Detected version: %s. Exiting.\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [60 x i8] c"/sys/devices/platform/coretemp.0/hwmon/hwmon%d/temp%d_input\00"
@R_OK = global i32 0
define i32 @is_modern_sensors_path() {
%1 = alloca %struct.utsname
%2 = bitcast %struct.utsname* %1 to i8*
%3 = call i32 @uname(%struct.utsname* %1)
%4 = getelementptr inbounds %struct.utsname, %struct.utsname* %1, i64 0, i32 0
%5 = load i32, i32* %4
%6 = tail call i8* @strtok(i32 %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @atoi(i8* %6)
%8 = icmp slt i32 %7, 3
br i1 %8, label %9, label %14
9: ; preds = %0
%10 = load i32, i32* @LOG_ERR
%11 = tail call i32 @mbp_log(i32 %10, i8* getelementptr inbounds ([74 x i8], [74 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %0, %26
%15 = phi i32 [ %27, %26 ], [ 0, %0 ]
br label %19
16: ; preds = %19
%17 = add nuw nsw i32 %20, 1
%18 = icmp eq i32 %17, 64
br i1 %18, label %26, label %19
19: ; preds = %14, %16
%20 = phi i32 [ 1, %14 ], [ %17, %16 ]
%21 = tail call i8* @smprintf(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.2, i64 0, i64 0), i32 %15, i32 %20)
%22 = load i32, i32* @R_OK
%23 = tail call i32 @access(i8* %21, i32 %22)
%24 = tail call i32 @free(i8* %21)
%25 = icmp eq i32 %23, 0
br i1 %25, label %29, label %16
26: ; preds = %16
%27 = add nuw nsw i32 %15, 1
%28 = icmp eq i32 %27, 12
br i1 %28, label %29, label %14
29: ; preds = %26, %19
%30 = phi i32 [ 1, %19 ], [ 0, %26 ]
ret i32 %30
}