#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 ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int errno;
int open(char const *, int /*<<< orphan*/);
int write(int, void const *, int);
int sysfs_write(const char *path, const void *buf, int len) {
  int fd;
  ssize_t ret;
  fd = open(path, O_WRONLY);
  if (fd == -1)
    return -errno;
  ret = write(fd, buf, len);
  if (ret == -1)
    ret = -errno;
  close(fd);
  return ret;
}


x86 assembly (clang, -O0):
<sysfs_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    1175 <sysfs_write+0x45>
xor    %eax,%eax
mov    0x2e75(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11ad <sysfs_write+0x7d>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    119f <sysfs_write+0x6f>
xor    %eax,%eax
mov    0x2e46(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysfs_write>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %rsi,%rbp
mov    0x2e8f(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     1176 <sysfs_write+0x46>
mov    %eax,%ebx
mov    %eax,%edi
mov    %rbp,%rsi
mov    %r14d,%edx
call   1030 <write@plt>
mov    0x2e7d(%rip),%rcx
xor    %ebp,%ebp
sub    (%rcx),%ebp
cmp    $0xffffffff,%eax
cmovne %eax,%ebp
mov    %ebx,%edi
call   1040 <close@plt>
jmp    1181 <sysfs_write+0x51>
mov    0x2e63(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@errno = global i32 0
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 i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
%11 = load i32, i32* @O_WRONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %3
%16 = load i32, i32* @errno
%17 = sub nsw i32 0, %16
store i32 %17, i32* %4
br label %32
18: ; preds = %3
%19 = load i32, i32* %8
%20 = load i8*, i8** %6
%21 = load i32, i32* %7
%22 = call i32 @write(i32 %19, i8* %20, i32 %21)
store i32 %22, i32* %9
%23 = load i32, i32* %9
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %18
%26 = load i32, i32* @errno
%27 = sub nsw i32 0, %26
store i32 %27, i32* %9
br label %28
28: ; preds = %25, %18
%29 = load i32, i32* %8
%30 = call i32 @close(i32 %29)
%31 = load i32, i32* %9
store i32 %31, i32* %4
br label %32
32: ; preds = %28, %15
%33 = load i32, i32* %4
ret i32 %33
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@errno = global i32 0
define i32 @sysfs_write(i8* nocapture %0, i8* nocapture %1, i32 %2) {
%4 = load i32, i32* @O_WRONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %10
7: ; preds = %3
%8 = load i32, i32* @errno
%9 = sub nsw i32 0, %8
br label %17
10: ; preds = %3
%11 = tail call i32 @write(i32 %5, i8* %1, i32 %2)
%12 = icmp eq i32 %11, -1
%13 = load i32, i32* @errno
%14 = sub nsw i32 0, %13
%15 = select i1 %12, i32 %14, i32 %11
%16 = tail call i32 @close(i32 %5)
br label %17
17: ; preds = %10, %7
%18 = phi i32 [ %9, %7 ], [ %15, %10 ]
ret i32 %18
}