#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 /*<<< orphan*/ path;
typedef int /*<<< orphan*/ nbuf;

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ __ploop_err(int /*<<< orphan*/, char *, char *, char *);
scalar_t__ memcmp(char const *, char *, int);
scalar_t__ read_line(char *, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char const *);
int sscanf(char *, char *, int *);
int ploop_get_attr(const char *device, const char *attr, int *res) {
  char path[PATH_MAX];
  char nbuf[4096];
  if (memcmp(device, "/dev/", 5) == 0)
    device += 5;
  snprintf(path, sizeof(path), "/sys/block/%s/pstate/%s", device, attr);
  if (read_line(path, nbuf, sizeof(nbuf)))
    return -1;
  if (sscanf(nbuf, "%d", res) != 1) {
    __ploop_err(0, "Unexpected format of %s: %s", path, nbuf);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<ploop_get_attr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1050,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x1048(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x1040(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe58(%rip),%rsi
mov    $0x5,%edx
call   1040 <memcmp@plt>
cmp    $0x0,%rax
jne    11c8 <ploop_get_attr+0x78>
mov    -0x10(%rbp),%rax
add    $0x5,%rax
mov    %rax,-0x10(%rbp)
mov    -0x1040(%rbp),%rdi
mov    -0x1048(%rbp),%rax
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
lea    0xe1f(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x1040(%rbp),%rdi
lea    -0x1030(%rbp),%rsi
mov    $0x1000,%edx
call   1060 <read_line@plt>
cmp    $0x0,%rax
je     1224 <ploop_get_attr+0xd4>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x1034(%rbp)
jmp    1287 <ploop_get_attr+0x137>
lea    -0x1030(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xde8(%rip),%rsi
call   1050 <sscanf@plt>
cmp    $0x1,%eax
je     1276 <ploop_get_attr+0x126>
mov    -0x1040(%rbp),%rdx
lea    -0x1030(%rbp),%rcx
xor    %edi,%edi
lea    0xdc6(%rip),%rsi
call   1070 <__ploop_err@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x1034(%rbp)
jmp    1287 <ploop_get_attr+0x137>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x1034(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ploop_get_attr>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1008,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r13
mov    0x2e7c(%rip),%rax
mov    (%rax),%r12d
mov    %rsp,%rbx
mov    %r12,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe6e(%rip),%rsi
mov    $0x5,%edx
call   1040 <memcmp@plt>
xor    %ecx,%ecx
test   %rax,%rax
sete   %cl
lea    (%rcx,%rcx,4),%rcx
add    %r13,%rcx
lea    0xe54(%rip),%rdx
mov    %rbx,%rdi
mov    %r12d,%esi
mov    %r15,%r8
call   1030 <snprintf@plt>
lea    -0x1030(%rbp),%rsi
mov    %rbx,%rdi
mov    $0x1000,%edx
call   1060 <read_line@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
jne    121d <ploop_get_attr+0xcd>
lea    0xe36(%rip),%rsi
lea    -0x1030(%rbp),%rdi
mov    %r14,%rdx
call   1050 <sscanf@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x1,%ecx
je     121d <ploop_get_attr+0xcd>
lea    0xe1a(%rip),%rsi
lea    -0x1030(%rbp),%rcx
xor    %edi,%edi
mov    %rbx,%rdx
call   1070 <__ploop_err@plt>
mov    $0xffffffff,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [6 x i8] c"/dev/\00"
@.str.1 = [24 x i8] c"/sys/block/%s/pstate/%s\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [28 x i8] c"Unexpected format of %s: %s\00"
define i32 @ploop_get_attr(i8* %0, i8* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca [4096 x i8]
%11 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32* %2, i32** %7
%12 = load i32, i32* @PATH_MAX
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %8
%15 = alloca i8, i64 %13
store i64 %13, i64* %9
%16 = load i8*, i8** %5
%17 = call i64 @memcmp(i8* %16, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 5)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %3
%20 = load i8*, i8** %5
%21 = getelementptr inbounds i8, i8* %20, i64 5
store i8* %21, i8** %5
br label %22
22: ; preds = %19, %3
%23 = trunc i64 %13 to i32
%24 = load i8*, i8** %5
%25 = load i8*, i8** %6
%26 = call i32 @snprintf(i8* %15, i32 %23, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %24, i8* %25)
%27 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%28 = call i64 @read_line(i8* %15, i8* %27, i32 4096)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %22
store i32 -1, i32* %4
store i32 1, i32* %11
br label %40
31: ; preds = %22
%32 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%33 = load i32*, i32** %7
%34 = call i32 @sscanf(i8* %32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %33)
%35 = icmp ne i32 %34, 1
br i1 %35, label %36, label %39
36: ; preds = %31
%37 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%38 = call i32 @__ploop_err(i32 0, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %15, i8* %37)
store i32 -1, i32* %4
store i32 1, i32* %11
br label %40
39: ; preds = %31
store i32 0, i32* %4
store i32 1, i32* %11
br label %40
40: ; preds = %39, %36, %30
%41 = load i8*, i8** %8
%42 = load i32, i32* %4
ret i32 %42
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [6 x i8] c"/dev/\00"
@.str.1 = [24 x i8] c"/sys/block/%s/pstate/%s\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [28 x i8] c"Unexpected format of %s: %s\00"
define i32 @ploop_get_attr(i8* %0, i8* %1, i32* %2) {
%4 = alloca [4096 x i8]
%5 = load i32, i32* @PATH_MAX
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%9 = tail call i64 @memcmp(i8* %0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 5)
%10 = icmp eq i64 %9, 0
%11 = select i1 %10, i64 5, i64 0
%12 = getelementptr i8, i8* %0, i64 %11
%13 = call i32 @snprintf(i8* %7, i32 %5, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %12, i8* %1)
%14 = call i64 @read_line(i8* %7, i8* %8, i32 4096)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %3
%17 = call i32 @sscanf(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %2)
%18 = icmp eq i32 %17, 1
br i1 %18, label %21, label %19
19: ; preds = %16
%20 = call i32 @__ploop_err(i32 0, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %7, i8* %8)
br label %21
21: ; preds = %16, %3, %19
%22 = phi i32 [ -1, %19 ], [ -1, %3 ], [ 0, %16 ]
ret i32 %22
}