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

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
scalar_t__ strlen(char *);
int write(int, char *, scalar_t__);
int sysfs_value_write(char *path, int value) {
  char buffer[100];
  int fd = -1;
  int rc;
  if (path == NULL)
    return -1;
  fd = open(path, O_WRONLY);
  if (fd < 0)
    goto error;
  snprintf((char *)&buffer, sizeof(buffer), "%d\n", value);
  rc = write(fd, buffer, strlen(buffer));
  if (rc < (int)strlen(buffer))
    goto error;
  rc = 0;
  goto complete;
error:
  rc = -1;
complete:
  if (fd >= 0)
    close(fd);
  return rc;
}


x86 assembly (clang, -O0):
<sysfs_value_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0xffffffff,-0x84(%rbp)
ff ff ff
cmpq   $0x0,-0x10(%rbp)
jne    1183 <sysfs_value_write+0x33>
movl   $0xffffffff,-0x4(%rbp)
jmp    1265 <sysfs_value_write+0x115>
mov    -0x10(%rbp),%rdi
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x84(%rbp)
cmpl   $0x0,-0x84(%rbp)
jge    11ad <sysfs_value_write+0x5d>
jmp    123a <sysfs_value_write+0xea>
lea    -0x80(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    $0x64,%esi
lea    0xe40(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x84(%rbp),%eax
mov    %eax,-0x9c(%rbp)
lea    -0x80(%rbp),%rax
mov    %rax,-0x98(%rbp)
lea    -0x80(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x9c(%rbp),%edi
mov    -0x98(%rbp),%rsi
mov    %rax,%rdx
call   1030 <write@plt>
mov    %eax,-0x88(%rbp)
mov    -0x88(%rbp),%eax
mov    %eax,-0x8c(%rbp)
lea    -0x80(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x8c(%rbp),%eax
cmp    %ecx,%eax
jge    122b <sysfs_value_write+0xdb>
jmp    123a <sysfs_value_write+0xea>
movl   $0x0,-0x88(%rbp)
jmp    1244 <sysfs_value_write+0xf4>
movl   $0xffffffff,-0x88(%rbp)
ff ff ff
cmpl   $0x0,-0x84(%rbp)
jl     125c <sysfs_value_write+0x10c>
mov    -0x84(%rbp),%edi
call   1060 <close@plt>
mov    -0x88(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysfs_value_write>:
push   %rbp
push   %r14
push   %rbx
sub    $0x70,%rsp
mov    $0xffffffff,%ebp
test   %rdi,%rdi
je     11c5 <sysfs_value_write+0x75>
mov    %esi,%r14d
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     11c5 <sysfs_value_write+0x75>
mov    %eax,%ebx
lea    0xe80(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x64,%esi
mov    %r14d,%ecx
call   1050 <snprintf@plt>
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %rax,%rdx
call   1030 <write@plt>
mov    %eax,%r14d
mov    %rbp,%rdi
call   1040 <strlen@plt>
xor    %ebp,%ebp
cmp    %eax,%r14d
setl   %bpl
neg    %ebp
mov    %ebx,%edi
call   1060 <close@plt>
mov    %ebp,%eax
add    $0x70,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@.str = [4 x i8] c"%d\0A\00"
define i32 @sysfs_value_write(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [100 x i8]
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 -1, i32* %7
%9 = load i8*, i8** %4
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 -1, i32* %3
br label %44
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = load i32, i32* @O_WRONLY
%15 = call i32 @open(i8* %13, i32 %14)
store i32 %15, i32* %7
%16 = load i32, i32* %7
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %12
br label %35
19: ; preds = %12
%20 = bitcast [100 x i8]* %6 to i8*
%21 = load i32, i32* %5
%22 = call i32 @snprintf(i8* %20, i32 100, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = load i32, i32* %7
%24 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%25 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%26 = call i64 @strlen(i8* %25)
%27 = call i32 @write(i32 %23, i8* %24, i64 %26)
store i32 %27, i32* %8
%28 = load i32, i32* %8
%29 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%30 = call i64 @strlen(i8* %29)
%31 = trunc i64 %30 to i32
%32 = icmp slt i32 %28, %31
br i1 %32, label %33, label %34
33: ; preds = %19
br label %35
34: ; preds = %19
store i32 0, i32* %8
br label %36
35: ; preds = %33, %18
store i32 -1, i32* %8
br label %36
36: ; preds = %35, %34
%37 = load i32, i32* %7
%38 = icmp sge i32 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %36
%40 = load i32, i32* %7
%41 = call i32 @close(i32 %40)
br label %42
42: ; preds = %39, %36
%43 = load i32, i32* %8
store i32 %43, i32* %3
br label %44
44: ; preds = %42, %11
%45 = load i32, i32* %3
ret i32 %45
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@.str = [4 x i8] c"%d\0A\00"
define i32 @sysfs_value_write(i8* %0, i32 %1) {
%3 = alloca [100 x i8]
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%5 = icmp eq i8* %0, null
br i1 %5, label %19, label %6
6: ; preds = %2
%7 = load i32, i32* @O_WRONLY
%8 = tail call i32 @open(i8* %0, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %19, label %10
10: ; preds = %6
%11 = call i32 @snprintf(i8* %4, i32 100, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %1)
%12 = call i64 @strlen(i8* %4)
%13 = call i32 @write(i32 %8, i8* %4, i64 %12)
%14 = call i64 @strlen(i8* %4)
%15 = trunc i64 %14 to i32
%16 = icmp slt i32 %13, %15
%17 = sext i1 %16 to i32
%18 = call i32 @close(i32 %8)
br label %19
19: ; preds = %6, %10, %2
%20 = phi i32 [ -1, %2 ], [ %17, %10 ], [ -1, %6 ]
ret i32 %20
}