#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*/ line;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ asprintf(char **, char *, int, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
char *strdup(char *);
char *strtok(char *, char *);
char *cpuidle_cstate_name(int cpu, int state) {
  char *fpath, *name;
  FILE *snf;
  char line[256];
  if (asprintf(&fpath, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
               cpu, state) < 0)
    return NULL;
  snf = fopen(fpath, "r");
  free(fpath);
  if (!snf)
    return NULL;
  name = fgets(line, sizeof(line) / sizeof(line[0]), snf);
  fclose(snf);
  if (name) {
    name = strtok(name, "\n ");
    name = strdup(name);
  }
  return name;
}


x86 assembly (clang, -O0):
<cpuidle_cstate_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    -0xc(%rbp),%edx
mov    -0x10(%rbp),%ecx
lea    -0x18(%rbp),%rdi
lea    0xe6e(%rip),%rsi
call   1060 <asprintf@plt>
cmp    $0x0,%rax
jge    11ae <cpuidle_cstate_name+0x3e>
movq   $0x0,-0x8(%rbp)
jmp    1239 <cpuidle_cstate_name+0xc9>
mov    -0x18(%rbp),%rdi
lea    0xe7a(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x28(%rbp)
jne    11e3 <cpuidle_cstate_name+0x73>
movq   $0x0,-0x8(%rbp)
jmp    1239 <cpuidle_cstate_name+0xc9>
lea    -0x130(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    $0x100,%esi
call   1050 <fgets@plt>
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
cmpq   $0x0,-0x20(%rbp)
je     1231 <cpuidle_cstate_name+0xc1>
mov    -0x20(%rbp),%rdi
lea    0xe1a(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cpuidle_cstate_name>:
push   %r14
push   %rbx
sub    $0x118,%rsp
mov    %esi,%ecx
mov    %edi,%edx
lea    0xe7b(%rip),%rsi
lea    0x8(%rsp),%rdi
call   1060 <asprintf@plt>
test   %rax,%rax
js     11f2 <cpuidle_cstate_name+0x82>
mov    0x8(%rsp),%rdi
lea    0xe93(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
test   %rbx,%rbx
je     11f2 <cpuidle_cstate_name+0x82>
lea    0x10(%rsp),%rdi
mov    $0x100,%esi
mov    %rbx,%rdx
call   1050 <fgets@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1040 <fclose@plt>
test   %r14,%r14
je     11f2 <cpuidle_cstate_name+0x82>
lea    0xe55(%rip),%rsi
mov    %r14,%rdi
call   1080 <strtok@plt>
mov    %rax,%rdi
call   1090 <strdup@plt>
jmp    11f4 <cpuidle_cstate_name+0x84>
xor    %eax,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [51 x i8] c"/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"\0A \00"
define i8* @cpuidle_cstate_name(i32 %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca [256 x i8]
store i32 %0, i32* %4
store i32 %1, i32* %5
%10 = load i32, i32* %4
%11 = load i32, i32* %5
%12 = call i64 @asprintf(i8** %6, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i32 %10, i32 %11)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %2
store i8* null, i8** %3
br label %38
15: ; preds = %2
%16 = load i8*, i8** %6
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %17, i32** %8
%18 = load i8*, i8** %6
%19 = call i32 @free(i8* %18)
%20 = load i32*, i32** %8
%21 = icmp ne i32* %20, null
br i1 %21, label %23, label %22
22: ; preds = %15
store i8* null, i8** %3
br label %38
23: ; preds = %15
%24 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%25 = load i32*, i32** %8
%26 = call i8* @fgets(i8* %24, i32 256, i32* %25)
store i8* %26, i8** %7
%27 = load i32*, i32** %8
%28 = call i32 @fclose(i32* %27)
%29 = load i8*, i8** %7
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %36
31: ; preds = %23
%32 = load i8*, i8** %7
%33 = call i8* @strtok(i8* %32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i8* %33, i8** %7
%34 = load i8*, i8** %7
%35 = call i8* @strdup(i8* %34)
store i8* %35, i8** %7
br label %36
36: ; preds = %31, %23
%37 = load i8*, i8** %7
store i8* %37, i8** %3
br label %38
38: ; preds = %36, %22, %14
%39 = load i8*, i8** %3
ret i8* %39
}

LLVM IR (-O2):
@.str = [51 x i8] c"/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"\0A \00"
define noalias i8* @cpuidle_cstate_name(i32 %0, i32 %1) {
%3 = alloca i8*
%4 = alloca [256 x i8]
%5 = bitcast i8** %3 to i8*
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%7 = call i64 @asprintf(i8** %3, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %1)
%8 = icmp slt i64 %7, 0
br i1 %8, label %22, 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 = load i8*, i8** %3
%13 = call i32 @free(i8* %12)
%14 = icmp eq i32* %11, null
br i1 %14, label %22, label %15
15: ; preds = %9
%16 = call i8* @fgets(i8* %6, i32 256, i32* %11)
%17 = call i32 @fclose(i32* %11)
%18 = icmp eq i8* %16, null
br i1 %18, label %22, label %19
19: ; preds = %15
%20 = call i8* @strtok(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%21 = call i8* @strdup(i8* %20)
br label %22
22: ; preds = %15, %19, %9, %2
%23 = phi i8* [ null, %2 ], [ null, %9 ], [ %21, %19 ], [ null, %15 ]
ret i8* %23
}