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

/* Variables and functions */
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char const *);
int sysfs_write(const char *path, const char *filename, int value) {
  FILE *fd;
  char buf[100];
  snprintf(buf, sizeof(buf), "%s%s", path, filename);
  fd = fopen(buf, "w");
  if (fd == NULL) {
    perror(buf);
    return -1;
  }
  fprintf(fd, "%d", value);
  if (fclose(fd) != 0)
    perror(buf);
  return 0;
}


x86 assembly (clang, -O0):
<sysfs_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
mov    $0x64,%esi
lea    0xe7f(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x90(%rbp),%rdi
lea    0xe71(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c0 <sysfs_write+0x70>
lea    -0x90(%rbp),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f9 <sysfs_write+0xa9>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%edx
lea    0xe39(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
cmp    $0x0,%rax
je     11f2 <sysfs_write+0xa2>
lea    -0x90(%rbp),%rdi
call   1070 <perror@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysfs_write>:
push   %rbp
push   %rbx
sub    $0x68,%rsp
mov    %edx,%ebp
mov    %rsi,%r8
mov    %rdi,%rcx
lea    0xe9b(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x64,%esi
call   1040 <snprintf@plt>
lea    0xe89(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11b5 <sysfs_write+0x65>
mov    %rax,%rbx
lea    0xe74(%rip),%rsi
mov    %rax,%rdi
mov    %ebp,%edx
call   1050 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %ebx,%ebx
test   %rax,%rax
jne    11ba <sysfs_write+0x6a>
mov    %ebx,%eax
add    $0x68,%rsp
pop    %rbx
pop    %rbp
ret
mov    $0xffffffff,%ebx
mov    %rsp,%rdi
call   1070 <perror@plt>
jmp    11ac <sysfs_write+0x5c>

LLVM IR (-O0):
@.str = [5 x i8] c"%s%s\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [3 x i8] c"%d\00"
define i32 @sysfs_write(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca [100 x i8]
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%11 = load i8*, i8** %5
%12 = load i8*, i8** %6
%13 = call i32 @snprintf(i8* %10, i32 100, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %11, i8* %12)
%14 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %15, i32** %8
%16 = load i32*, i32** %8
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %21
18: ; preds = %3
%19 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%20 = call i32 @perror(i8* %19)
store i32 -1, i32* %4
br label %32
21: ; preds = %3
%22 = load i32*, i32** %8
%23 = load i32, i32* %7
%24 = call i32 @fprintf(i32* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %23)
%25 = load i32*, i32** %8
%26 = call i64 @fclose(i32* %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %21
%29 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%30 = call i32 @perror(i8* %29)
br label %31
31: ; preds = %28, %21
store i32 0, i32* %4
br label %32
32: ; preds = %31, %18
%33 = load i32, i32* %4
ret i32 %33
}

LLVM IR (-O2):
@.str = [5 x i8] c"%s%s\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [3 x i8] c"%d\00"
define i32 @sysfs_write(i8* %0, i8* %1, i32 %2) {
%4 = alloca [100 x i8]
%5 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%6 = call i32 @snprintf(i8* %5, i32 100, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%7 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %13, label %9
9: ; preds = %3
%10 = call i32 @fprintf(i32* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %2)
%11 = call i64 @fclose(i32* %7)
%12 = icmp eq i64 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %9, %3
%14 = phi i32 [ -1, %3 ], [ 0, %9 ]
%15 = call i32 @perror(i8* %5)
br label %16
16: ; preds = %13, %9
%17 = phi i32 [ 0, %9 ], [ %14, %13 ]
ret i32 %17
}