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

/* Variables and functions */
int EINVAL;
int ENODEV;
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
int /*<<< orphan*/ sprintf(char *, char *, int);
scalar_t__ stat(char *, struct stat *);
scalar_t__ strcmp(char *, char const *);
scalar_t__ sysfs_write_file(char *, char *, int);
int sysfs_set_sched(const char *smt_mc, int val) {
  char linebuf[255];
  char path[255];
  struct stat statbuf;
  if (strcmp("mc", smt_mc) && strcmp("smt", smt_mc))
    return -EINVAL;
  snprintf(path, sizeof(path),
           "/sys/devices/system/cpu/"
           "sched_%s_power_savings",
           smt_mc);
  sprintf(linebuf, "%d", val);
  if (stat(path, &statbuf) != 0)
    return -ENODEV;
  if (sysfs_write_file(path, linebuf, 255) == 0)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<sysfs_set_sched>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe93(%rip),%rdi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     11a9 <sysfs_set_sched+0x59>
mov    -0x10(%rbp),%rsi
lea    0xe7c(%rip),%rdi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     11a9 <sysfs_set_sched+0x59>
xor    %eax,%eax
mov    0x2e41(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1240 <sysfs_set_sched+0xf0>
lea    -0x220(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0xff,%esi
lea    0xe47(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x120(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xe60(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x220(%rbp),%rdi
lea    -0x228(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
je     120b <sysfs_set_sched+0xbb>
xor    %eax,%eax
mov    0x2dd7(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1240 <sysfs_set_sched+0xf0>
lea    -0x220(%rbp),%rdi
lea    -0x120(%rbp),%rsi
mov    $0xff,%edx
call   1040 <sysfs_write_file@plt>
cmp    $0x0,%rax
jne    1239 <sysfs_set_sched+0xe9>
movl   $0xffffffff,-0x4(%rbp)
jmp    1240 <sysfs_set_sched+0xf0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysfs_set_sched>:
push   %rbp
push   %r14
push   %rbx
sub    $0x210,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
lea    0xe99(%rip),%rdi
mov    %rbx,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     1191 <sysfs_set_sched+0x41>
lea    0xe88(%rip),%rdi
mov    %rbx,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     1191 <sysfs_set_sched+0x41>
mov    0x2e51(%rip),%rcx
jmp    11dc <sysfs_set_sched+0x8c>
lea    0xe6f(%rip),%rdx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0xff,%esi
mov    %rbx,%rcx
call   1030 <snprintf@plt>
lea    0xe82(%rip),%rsi
lea    0x110(%rsp),%rdi
mov    %ebp,%edx
call   1070 <sprintf@plt>
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
call   1060 <stat@plt>
test   %rax,%rax
je     11e2 <sysfs_set_sched+0x92>
mov    0x2dfc(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
jmp    11ff <sysfs_set_sched+0xaf>
lea    0x10(%rsp),%rdi
lea    0x110(%rsp),%rsi
mov    $0xff,%edx
call   1040 <sysfs_write_file@plt>
cmp    $0x1,%rax
sbb    %eax,%eax
add    $0x210,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [3 x i8] c"mc\00"
@.str.1 = [4 x i8] c"smt\00"
@EINVAL = global i32 0
@.str.2 = [47 x i8] c"/sys/devices/system/cpu/sched_%s_power_savings\00"
@.str.3 = [3 x i8] c"%d\00"
@ENODEV = global i32 0
define i32 @sysfs_set_sched(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [255 x i8]
%7 = alloca [255 x i8]
%8 = alloca %struct.stat
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i8*, i8** %4
%10 = call i64 @strcmp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %19
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = call i64 @strcmp(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = load i32, i32* @EINVAL
%18 = sub nsw i32 0, %17
store i32 %18, i32* %3
br label %39
19: ; preds = %12, %2
%20 = getelementptr inbounds [255 x i8], [255 x i8]* %7, i64 0, i64 0
%21 = load i8*, i8** %4
%22 = call i32 @snprintf(i8* %20, i32 255, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i8* %21)
%23 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%24 = load i32, i32* %5
%25 = call i32 @sprintf(i8* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %24)
%26 = getelementptr inbounds [255 x i8], [255 x i8]* %7, i64 0, i64 0
%27 = call i64 @stat(i8* %26, %struct.stat* %8)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %19
%30 = load i32, i32* @ENODEV
%31 = sub nsw i32 0, %30
store i32 %31, i32* %3
br label %39
32: ; preds = %19
%33 = getelementptr inbounds [255 x i8], [255 x i8]* %7, i64 0, i64 0
%34 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%35 = call i64 @sysfs_write_file(i8* %33, i8* %34, i32 255)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %32
store i32 -1, i32* %3
br label %39
38: ; preds = %32
store i32 0, i32* %3
br label %39
39: ; preds = %38, %37, %29, %16
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [3 x i8] c"mc\00"
@.str.1 = [4 x i8] c"smt\00"
@EINVAL = global i32 0
@.str.2 = [47 x i8] c"/sys/devices/system/cpu/sched_%s_power_savings\00"
@.str.3 = [3 x i8] c"%d\00"
@ENODEV = global i32 0
define i32 @sysfs_set_sched(i8* %0, i32 %1) {
%3 = alloca [255 x i8]
%4 = alloca [255 x i8]
%5 = alloca %struct.stat
%6 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%8 = bitcast %struct.stat* %5 to i8*
%9 = tail call i64 @strcmp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%10 = icmp eq i64 %9, 0
br i1 %10, label %17, label %11
11: ; preds = %2
%12 = tail call i64 @strcmp(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%13 = icmp eq i64 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %11
%15 = load i32, i32* @EINVAL
%16 = sub nsw i32 0, %15
br label %29
17: ; preds = %11, %2
%18 = call i32 @snprintf(i8* %7, i32 255, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%19 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %1)
%20 = call i64 @stat(i8* %7, %struct.stat* %5)
%21 = icmp eq i64 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %17
%23 = load i32, i32* @ENODEV
%24 = sub nsw i32 0, %23
br label %29
25: ; preds = %17
%26 = call i64 @sysfs_write_file(i8* %7, i8* %6, i32 255)
%27 = icmp eq i64 %26, 0
%28 = sext i1 %27 to i32
br label %29
29: ; preds = %25, %22, %14
%30 = phi i32 [ %16, %14 ], [ %24, %22 ], [ %28, %25 ]
ret i32 %30
}