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

/* Variables and functions */
scalar_t__ asprintf(char **, char *, int, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, unsigned int *);
int cpuidle_get_target_residency(int cpu, int state) {
  char *fpath;
  unsigned int tr;
  FILE *snf;
  if (asprintf(&fpath,
               "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/residency", cpu,
               state) < 0)
    return -1;
  snf = fopen(fpath, "r");
  if (!snf) {
    free(fpath);
    return -1;
  }
  fscanf(snf, "%u", &tr);
  fclose(snf);
  return tr;
}


x86 assembly (clang, -O0):
<cpuidle_get_target_residency>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edx
mov    -0xc(%rbp),%ecx
lea    -0x18(%rbp),%rdi
lea    0xe91(%rip),%rsi
call   1060 <asprintf@plt>
cmp    $0x0,%rax
jge    118a <cpuidle_get_target_residency+0x3a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e1 <cpuidle_get_target_residency+0x91>
mov    -0x18(%rbp),%rdi
lea    0xea3(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11be <cpuidle_get_target_residency+0x6e>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e1 <cpuidle_get_target_residency+0x91>
mov    -0x28(%rbp),%rdi
lea    0xe71(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   1050 <fscanf@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cpuidle_get_target_residency>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ecx
mov    %edi,%edx
lea    0xe9f(%rip),%rsi
lea    0x10(%rsp),%rdi
call   1060 <asprintf@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
js     11ba <cpuidle_get_target_residency+0x6a>
mov    0x10(%rsp),%rdi
lea    0xeb7(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11b0 <cpuidle_get_target_residency+0x60>
mov    %rax,%rbx
lea    0xea5(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rax,%rdi
call   1050 <fscanf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0xc(%rsp),%ebp
jmp    11ba <cpuidle_get_target_residency+0x6a>
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [56 x i8] c"/sys/devices/system/cpu/cpu%d/cpuidle/state%d/residency\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%u\00"
define i32 @cpuidle_get_target_residency(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
store i32 %0, i32* %4
store i32 %1, i32* %5
%9 = load i32, i32* %4
%10 = load i32, i32* %5
%11 = call i64 @asprintf(i8** %6, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i32 %9, i32 %10)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 -1, i32* %3
br label %28
14: ; preds = %2
%15 = load i8*, i8** %6
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %16, i32** %8
%17 = load i32*, i32** %8
%18 = icmp ne i32* %17, null
br i1 %18, label %22, label %19
19: ; preds = %14
%20 = load i8*, i8** %6
%21 = call i32 @free(i8* %20)
store i32 -1, i32* %3
br label %28
22: ; preds = %14
%23 = load i32*, i32** %8
%24 = call i32 @fscanf(i32* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %7)
%25 = load i32*, i32** %8
%26 = call i32 @fclose(i32* %25)
%27 = load i32, i32* %7
store i32 %27, i32* %3
br label %28
28: ; preds = %22, %19, %13
%29 = load i32, i32* %3
ret i32 %29
}

LLVM IR (-O2):
@.str = [56 x i8] c"/sys/devices/system/cpu/cpu%d/cpuidle/state%d/residency\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%u\00"
define i32 @cpuidle_get_target_residency(i32 %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = bitcast i8** %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = call i64 @asprintf(i8** %3, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %1)
%8 = icmp slt i64 %7, 0
br i1 %8, label %20, label %9
9: ; preds = %2
%10 = load i8*, i8** %3
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = load i8*, i8** %3
%15 = call i32 @free(i8* %14)
br label %20
16: ; preds = %9
%17 = call i32 @fscanf(i32* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %4)
%18 = call i32 @fclose(i32* %11)
%19 = load i32, i32* %4
br label %20
20: ; preds = %2, %16, %13
%21 = phi i32 [ %19, %16 ], [ -1, %13 ], [ -1, %2 ]
ret i32 %21
}