#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 stat {
  int st_mode;
};

/* Variables and functions */
int NAME_MAX;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char const *);
int /*<<< orphan*/ stat(char *, struct stat *);
int sensors_get_attr_mode(const char *device, const char *attr) {
  char path[NAME_MAX];
  struct stat st;
  int mode = 0;
  snprintf(path, NAME_MAX, "%s/%s", device, attr);
  if (!stat(path, &st)) {
    if (st.st_mode & S_IRUSR)
      mode |= 1;
    if (st.st_mode & S_IWUSR)
      mode |= 2;
  }
  return mode;
}


x86 assembly (clang, -O0):
<sensors_get_attr_mode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e91(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x38(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%rcx
mov    -0x10(%rbp),%r8
lea    0xe85(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x38(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%eax
jne    11d7 <sensors_get_attr_mode+0xb7>
mov    -0x28(%rbp),%eax
mov    0x2e38(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11b4 <sensors_get_attr_mode+0x94>
mov    -0x2c(%rbp),%eax
or     $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%eax
mov    0x2e2a(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11d2 <sensors_get_attr_mode+0xb2>
mov    -0x2c(%rbp),%eax
or     $0x2,%eax
mov    %eax,-0x2c(%rbp)
jmp    11d7 <sensors_get_attr_mode+0xb7>
mov    -0x2c(%rbp),%eax
mov    -0x18(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sensors_get_attr_mode>:
push   %rbp
mov    %rsp,%rbp
push   %rbx
push   %rax
mov    %rsi,%r8
mov    %rdi,%rcx
mov    0x2e95(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rbx
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xeb0(%rip),%rdx
mov    %rbx,%rdi
call   1030 <snprintf@plt>
lea    -0x10(%rbp),%rsi
mov    %rbx,%rdi
call   1040 <stat@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
jne    118c <sensors_get_attr_mode+0x6c>
mov    -0x10(%rbp),%ecx
mov    0x2e62(%rip),%rax
xor    %edx,%edx
test   %ecx,(%rax)
setne  %dl
mov    0x2e64(%rip),%rsi
lea    0x2(%rdx),%eax
test   %ecx,(%rsi)
cmove  %edx,%eax
lea    -0x8(%rbp),%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@NAME_MAX = global i32 0
@.str = [6 x i8] c"%s/%s\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
define i32 @sensors_get_attr_mode(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca %struct.stat
%8 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%9 = load i32, i32* @NAME_MAX
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %5
%12 = alloca i8, i64 %10
store i64 %10, i64* %6
store i32 0, i32* %8
%13 = load i32, i32* @NAME_MAX
%14 = load i8*, i8** %3
%15 = load i8*, i8** %4
%16 = call i32 @snprintf(i8* %12, i32 %13, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %14, i8* %15)
%17 = call i32 @stat(i8* %12, %struct.stat* %7)
%18 = icmp ne i32 %17, 0
br i1 %18, label %38, label %19
19: ; preds = %2
%20 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%21 = load i32, i32* %20
%22 = load i32, i32* @S_IRUSR
%23 = and i32 %21, %22
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %19
%26 = load i32, i32* %8
%27 = or i32 %26, 1
store i32 %27, i32* %8
br label %28
28: ; preds = %25, %19
%29 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%30 = load i32, i32* %29
%31 = load i32, i32* @S_IWUSR
%32 = and i32 %30, %31
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %28
%35 = load i32, i32* %8
%36 = or i32 %35, 2
store i32 %36, i32* %8
br label %37
37: ; preds = %34, %28
br label %38
38: ; preds = %37, %2
%39 = load i32, i32* %8
%40 = load i8*, i8** %5
ret i32 %39
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@NAME_MAX = global i32 0
@.str = [6 x i8] c"%s/%s\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
define i32 @sensors_get_attr_mode(i8* %0, i8* %1) {
%3 = alloca %struct.stat
%4 = load i32, i32* @NAME_MAX
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = bitcast %struct.stat* %3 to i8*
%8 = call i32 @snprintf(i8* %6, i32 %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%9 = call i32 @stat(i8* %6, %struct.stat* %3)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %23
11: ; preds = %2
%12 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%13 = load i32, i32* %12
%14 = load i32, i32* @S_IRUSR
%15 = and i32 %14, %13
%16 = icmp ne i32 %15, 0
%17 = zext i1 %16 to i32
%18 = load i32, i32* @S_IWUSR
%19 = and i32 %18, %13
%20 = icmp eq i32 %19, 0
%21 = or i32 %17, 2
%22 = select i1 %20, i32 %17, i32 %21
br label %23
23: ; preds = %11, %2
%24 = phi i32 [ 0, %2 ], [ %22, %11 ]
ret i32 %24
}