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

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ abort();
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *,
                           char const *, int /*<<< orphan*/);
int open_sysfs(char const *, char const *, int /*<<< orphan*/);
int snprintf(char *, int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
scalar_t__ write(int, char *, int);
void write_sysfs_val(const char *sys_pfx, const char *node, int val) {
  int fd, len;
  char buf[0x20];
  fd = open_sysfs(sys_pfx, node, O_RDWR);
  len = snprintf(buf, sizeof(buf), "%d", val);
  if (write(fd, buf, len) < 0) {
    fprintf(stderr, "unable to write to %s%s %s\n", sys_pfx, node,
            strerror(errno));
    close(fd);
    abort();
  }
  close(fd);
}


x86 assembly (clang, -O0):
<write_sysfs_val>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2e56(%rip),%rax
mov    (%rax),%edx
call   1050 <open_sysfs@plt>
mov    %eax,-0x18(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    $0x20,%esi
lea    0xe51(%rip),%rdx
call   1060 <snprintf@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <write@plt>
cmp    $0x0,%rax
jge    1223 <write_sysfs_val+0xb3>
mov    0x2e01(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rdx
mov    -0x48(%rbp),%rcx
mov    %eax,%r8d
lea    0xdf4(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_sysfs_val>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %edx,%ebx
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e5d(%rip),%rax
mov    (%rax),%edx
call   1050 <open_sysfs@plt>
mov    %eax,%ebp
lea    0xe65(%rip),%rdx
mov    %rsp,%r12
mov    %r12,%rdi
mov    $0x20,%esi
mov    %ebx,%ecx
call   1060 <snprintf@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1040 <write@plt>
test   %rax,%rax
js     11d2 <write_sysfs_val+0x62>
mov    %ebp,%edi
call   1070 <close@plt>
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dff(%rip),%rax
mov    (%rax),%ebx
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe13(%rip),%rsi
mov    %ebx,%edi
mov    %r15,%rdx
mov    %r14,%rcx
mov    %eax,%r8d
call   1080 <fprintf@plt>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@O_RDWR = global i32 0
@.str = [3 x i8] c"%d\00"
@stderr = global i32 0
@.str.1 = [28 x i8] c"unable to write to %s%s %s\0A\00"
@errno = global i32 0
define void @write_sysfs_val(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [32 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%10 = load i8*, i8** %4
%11 = load i8*, i8** %5
%12 = load i32, i32* @O_RDWR
%13 = call i32 @open_sysfs(i8* %10, i8* %11, i32 %12)
store i32 %13, i32* %7
%14 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%15 = load i32, i32* %6
%16 = call i32 @snprintf(i8* %14, i32 32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %15)
store i32 %16, i32* %8
%17 = load i32, i32* %7
%18 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%19 = load i32, i32* %8
%20 = call i64 @write(i32 %17, i8* %18, i32 %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %32
22: ; preds = %3
%23 = load i32, i32* @stderr
%24 = load i8*, i8** %4
%25 = load i8*, i8** %5
%26 = load i32, i32* @errno
%27 = call i32 @strerror(i32 %26)
%28 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %24, i8* %25, i32 %27)
%29 = load i32, i32* %7
%30 = call i32 @close(i32 %29)
%31 = call i32 (...) @abort()
unreachable
32: ; preds = %3
%33 = load i32, i32* %7
%34 = call i32 @close(i32 %33)
ret void
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@.str = [3 x i8] c"%d\00"
@stderr = global i32 0
@.str.1 = [28 x i8] c"unable to write to %s%s %s\0A\00"
@errno = global i32 0
define void @write_sysfs_val(i8* %0, i8* %1, i32 %2) {
%4 = alloca [32 x i8]
%5 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%6 = load i32, i32* @O_RDWR
%7 = tail call i32 @open_sysfs(i8* %0, i8* %1, i32 %6)
%8 = call i32 @snprintf(i8* %5, i32 32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %2)
%9 = call i64 @write(i32 %7, i8* %5, i32 %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %18
11: ; preds = %3
%12 = load i32, i32* @stderr
%13 = load i32, i32* @errno
%14 = call i32 @strerror(i32 %13)
%15 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %1, i32 %14)
%16 = call i32 @close(i32 %7)
%17 = call i32 (...) @abort()
unreachable
18: ; preds = %3
%19 = call i32 @close(i32 %7)
ret void
}