#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 */

/* Variables and functions */
int NAME_MAX;
int readlink(char *, char *, int);
int sensors_read_one_sysfs_chip(char *, char *, char const *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
char *strrchr(char *, char);
int sensors_add_hwmon_device(const char *path, const char *classdev) {
  char linkpath[NAME_MAX];
  char device[NAME_MAX], *device_p;
  int dev_len, err;
  (void)classdev;
  snprintf(linkpath, NAME_MAX, "%s/device", path);
  dev_len = readlink(linkpath, device, NAME_MAX - 1);
  if (dev_len < 0) {
    err = sensors_read_one_sysfs_chip(NULL, NULL, path);
  } else {
    device[dev_len] = '\0';
    device_p = strrchr(device, '/') + 1;
    err = sensors_read_one_sysfs_chip(linkpath, device_p, path);
    if (err == 0)
      err = sensors_read_one_sysfs_chip(linkpath, device_p, linkpath);
  }
  if (err < 0)
    return err;
  return 0;
}


x86 assembly (clang, -O0):
<sensors_add_hwmon_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e81(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%rdx
mov    %rdx,-0x20(%rbp)
mov    %rcx,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rdi
sub    %rdx,%rdi
mov    %rdi,-0x58(%rbp)
mov    %rdi,%rsp
mov    %rcx,-0x28(%rbp)
mov    (%rax),%eax
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rcx
lea    0xe52(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    0x2e16(%rip),%rax
mov    (%rax),%edx
sub    $0x1,%edx
call   1040 <readlink@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    11f1 <sensors_add_hwmon_device+0xb1>
mov    -0x10(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%esi
mov    %rsi,%rdi
call   1030 <sensors_read_one_sysfs_chip@plt>
mov    %eax,-0x40(%rbp)
jmp    1245 <sensors_add_hwmon_device+0x105>
mov    -0x50(%rbp),%rdi
movslq -0x3c(%rbp),%rax
movb   $0x0,(%rdi,%rax,1)
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    -0x58(%rbp),%rdi
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1030 <sensors_read_one_sysfs_chip@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
jne    1240 <sensors_add_hwmon_device+0x100>
mov    -0x58(%rbp),%rdx
mov    -0x38(%rbp),%rsi
mov    %rdx,%rdi
call   1030 <sensors_read_one_sysfs_chip@plt>
mov    %eax,-0x40(%rbp)
jmp    1245 <sensors_add_hwmon_device+0x105>
cmpl   $0x0,-0x40(%rbp)
jge    1261 <sensors_add_hwmon_device+0x121>
mov    -0x40(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    126f <sensors_add_hwmon_device+0x12f>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sensors_add_hwmon_device>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r14
mov    0x2e83(%rip),%r12
mov    (%r12),%esi
mov    %rsp,%r15
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    %rsp,%rbx
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe83(%rip),%rdx
mov    %r15,%rdi
mov    %r14,%rcx
call   1050 <snprintf@plt>
mov    (%r12),%edx
add    $0xffffffff,%edx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1040 <readlink@plt>
test   %eax,%eax
js     11d5 <sensors_add_hwmon_device+0x95>
mov    %eax,%eax
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,%rbx
add    $0x1,%rbx
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <sensors_read_one_sysfs_chip@plt>
test   %eax,%eax
jne    11e1 <sensors_add_hwmon_device+0xa1>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
jmp    11dc <sensors_add_hwmon_device+0x9c>
xor    %edi,%edi
xor    %esi,%esi
mov    %r14,%rdx
call   1030 <sensors_read_one_sysfs_chip@plt>
mov    %eax,%ecx
sar    $0x1f,%ecx
and    %eax,%ecx
mov    %ecx,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@NAME_MAX = global i32 0
@.str = [10 x i8] c"%s/device\00"
define i32 @sensors_add_hwmon_device(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%13 = load i32, i32* @NAME_MAX
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %6
%16 = alloca i8, i64 %14
store i64 %14, i64* %7
%17 = load i32, i32* @NAME_MAX
%18 = zext i32 %17 to i64
%19 = alloca i8, i64 %18
store i64 %18, i64* %8
%20 = load i8*, i8** %5
%21 = load i32, i32* @NAME_MAX
%22 = load i8*, i8** %4
%23 = call i32 @snprintf(i8* %16, i32 %21, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %22)
%24 = load i32, i32* @NAME_MAX
%25 = sub nsw i32 %24, 1
%26 = call i32 @readlink(i8* %16, i8* %19, i32 %25)
store i32 %26, i32* %10
%27 = load i32, i32* %10
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %2
%30 = load i8*, i8** %4
%31 = call i32 @sensors_read_one_sysfs_chip(i8* null, i8* null, i8* %30)
store i32 %31, i32* %11
br label %47
32: ; preds = %2
%33 = load i32, i32* %10
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %19, i64 %34
store i8 0, i8* %35
%36 = call i8* @strrchr(i8* %19, i8 signext 47)
%37 = getelementptr inbounds i8, i8* %36, i64 1
store i8* %37, i8** %9
%38 = load i8*, i8** %9
%39 = load i8*, i8** %4
%40 = call i32 @sensors_read_one_sysfs_chip(i8* %16, i8* %38, i8* %39)
store i32 %40, i32* %11
%41 = load i32, i32* %11
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %32
%44 = load i8*, i8** %9
%45 = call i32 @sensors_read_one_sysfs_chip(i8* %16, i8* %44, i8* %16)
store i32 %45, i32* %11
br label %46
46: ; preds = %43, %32
br label %47
47: ; preds = %46, %29
%48 = load i32, i32* %11
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %47
%51 = load i32, i32* %11
store i32 %51, i32* %3
store i32 1, i32* %12
br label %53
52: ; preds = %47
store i32 0, i32* %3
store i32 1, i32* %12
br label %53
53: ; preds = %52, %50
%54 = load i8*, i8** %6
%55 = load i32, i32* %3
ret i32 %55
}

LLVM IR (-O2):
@NAME_MAX = global i32 0
@.str = [10 x i8] c"%s/device\00"
define i32 @sensors_add_hwmon_device(i8* %0, i8* nocapture readnone %1) {
%3 = load i32, i32* @NAME_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = alloca i8, i64 %4
%7 = call i32 @snprintf(i8* %5, i32 %3, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %0)
%8 = load i32, i32* @NAME_MAX
%9 = add nsw i32 %8, -1
%10 = call i32 @readlink(i8* %5, i8* %6, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @sensors_read_one_sysfs_chip(i8* null, i8* null, i8* %0)
br label %23
14: ; preds = %2
%15 = zext i32 %10 to i64
%16 = getelementptr inbounds i8, i8* %6, i64 %15
store i8 0, i8* %16
%17 = call i8* @strrchr(i8* %6, i8 signext 47)
%18 = getelementptr inbounds i8, i8* %17, i64 1
%19 = call i32 @sensors_read_one_sysfs_chip(i8* %5, i8* %18, i8* %0)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = call i32 @sensors_read_one_sysfs_chip(i8* %5, i8* %18, i8* %5)
br label %23
23: ; preds = %14, %21, %12
%24 = phi i32 [ %13, %12 ], [ %22, %21 ], [ %19, %14 ]
%25 = icmp slt i32 %24, 0
%26 = select i1 %25, i32 %24, i32 0
ret i32 %26
}