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

/* Variables and functions */
int EIO;
int ENOENT;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ close(int);
int open(char const *, int /*<<< orphan*/);
scalar_t__ write(int, char const *, size_t);
int sysfs_write_attr_str(const char *path, const char *buff, size_t buff_len,
                         ssize_t *out_len) {
  int fd, result;
  ssize_t result_len;
  assert(buff_len < 4096);
  fd = open(path, O_WRONLY);
  if (fd < 0) {
    result = ENOENT;
    goto out;
  }
  result_len = write(fd, buff, buff_len);
  if (result_len < 0) {
    result = EIO;
    goto out;
  }
  *out_len = result_len;
  result = 0;
out:
  if (fd >= 0)
    close(fd);
  return result;
}


x86 assembly (clang, -O0):
<sysfs_write_attr_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
cmpq   $0x1000,-0x18(%rbp)
setb   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x8(%rbp),%rdi
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    119d <sysfs_write_attr_str+0x5d>
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
jmp    11df <sysfs_write_attr_str+0x9f>
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jge    11cd <sysfs_write_attr_str+0x8d>
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
jmp    11df <sysfs_write_attr_str+0x9f>
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x28(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     11f1 <sysfs_write_attr_str+0xb1>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysfs_write_attr_str>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%rbp
xor    %edi,%edi
cmp    $0x1000,%rdx
setb   %dil
call   1050 <assert@plt>
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     1195 <sysfs_write_attr_str+0x55>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
test   %rax,%rax
js     11a0 <sysfs_write_attr_str+0x60>
mov    %rax,(%r14)
xor    %ebx,%ebx
jmp    11a9 <sysfs_write_attr_str+0x69>
mov    0x2e34(%rip),%rax
mov    (%rax),%ebx
jmp    11b0 <sysfs_write_attr_str+0x70>
mov    0x2e39(%rip),%rax
mov    (%rax),%ebx
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@ENOENT = global i32 0
@EIO = global i32 0
define i32 @sysfs_write_attr_str(i8* %0, i8* %1, i64 %2, i64* %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i64* %3, i64** %8
%12 = load i64, i64* %7
%13 = icmp ult i64 %12, 4096
%14 = zext i1 %13 to i32
%15 = call i32 @assert(i32 %14)
%16 = load i8*, i8** %5
%17 = load i32, i32* @O_WRONLY
%18 = call i32 @open(i8* %16, i32 %17)
store i32 %18, i32* %9
%19 = load i32, i32* %9
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %4
%22 = load i32, i32* @ENOENT
store i32 %22, i32* %10
br label %35
23: ; preds = %4
%24 = load i32, i32* %9
%25 = load i8*, i8** %6
%26 = load i64, i64* %7
%27 = call i64 @write(i32 %24, i8* %25, i64 %26)
store i64 %27, i64* %11
%28 = load i64, i64* %11
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %23
%31 = load i32, i32* @EIO
store i32 %31, i32* %10
br label %35
32: ; preds = %23
%33 = load i64, i64* %11
%34 = load i64*, i64** %8
store i64 %33, i64* %34
store i32 0, i32* %10
br label %35
35: ; preds = %32, %30, %21
%36 = load i32, i32* %9
%37 = icmp sge i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %35
%39 = load i32, i32* %9
%40 = call i32 @close(i32 %39)
br label %41
41: ; preds = %38, %35
%42 = load i32, i32* %10
ret i32 %42
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@ENOENT = global i32 0
@EIO = global i32 0
define i32 @sysfs_write_attr_str(i8* nocapture %0, i8* nocapture %1, i64 %2, i64* nocapture writeonly %3) {
%5 = icmp ult i64 %2, 4096
%6 = zext i1 %5 to i32
%7 = tail call i32 @assert(i32 %6)
%8 = load i32, i32* @O_WRONLY
%9 = tail call i32 @open(i8* %0, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %17, label %11
11: ; preds = %4
%12 = tail call i64 @write(i32 %9, i8* %1, i64 %2)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = load i32, i32* @EIO
br label %19
16: ; preds = %11
store i64 %12, i64* %3
br label %19
17: ; preds = %4
%18 = load i32, i32* @ENOENT
br label %22
19: ; preds = %16, %14
%20 = phi i32 [ 0, %16 ], [ %15, %14 ]
%21 = tail call i32 @close(i32 %9)
br label %22
22: ; preds = %17, %19
%23 = phi i32 [ %20, %19 ], [ %18, %17 ]
ret i32 %23
}