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

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char const *, int /*<<< orphan*/);
int write(int, char const *, size_t);
int sysfs_string_write(const char *path, const char *buffer, size_t length) {
  int fd = -1;
  int rc;
  if (path == NULL || buffer == NULL || length == 0)
    return -1;
  fd = open(path, O_WRONLY);
  if (fd < 0)
    goto error;
  rc = write(fd, buffer, length);
  if (rc < (int)length)
    goto error;
  rc = 0;
  goto complete;
error:
  rc = -1;
complete:
  if (fd >= 0)
    close(fd);
  return rc;
}


x86 assembly (clang, -O0):
<sysfs_string_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0xffffffff,-0x24(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     116c <sysfs_string_write+0x3c>
cmpq   $0x0,-0x18(%rbp)
je     116c <sysfs_string_write+0x3c>
cmpq   $0x0,-0x20(%rbp)
jne    1178 <sysfs_string_write+0x48>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ee <sysfs_string_write+0xbe>
mov    -0x10(%rbp),%rdi
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    119c <sysfs_string_write+0x6c>
jmp    11cf <sysfs_string_write+0x9f>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    -0x20(%rbp),%rcx
cmp    %ecx,%eax
jge    11c3 <sysfs_string_write+0x93>
jmp    11cf <sysfs_string_write+0x9f>
movl   $0x0,-0x28(%rbp)
jmp    11d6 <sysfs_string_write+0xa6>
movl   $0xffffffff,-0x28(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     11e8 <sysfs_string_write+0xb8>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysfs_string_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0xffffffff,%ebx
test   %rdi,%rdi
je     1183 <sysfs_string_write+0x53>
mov    %rsi,%r15
test   %rsi,%rsi
je     1183 <sysfs_string_write+0x53>
mov    %rdx,%r14
test   %rdx,%rdx
je     1183 <sysfs_string_write+0x53>
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
js     1183 <sysfs_string_write+0x53>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1030 <write@plt>
xor    %ebx,%ebx
cmp    %r14d,%eax
setl   %bl
neg    %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
define i32 @sysfs_string_write(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 -1, i32* %8
%10 = load i8*, i8** %5
%11 = icmp eq i8* %10, null
br i1 %11, label %18, label %12
12: ; preds = %3
%13 = load i8*, i8** %6
%14 = icmp eq i8* %13, null
br i1 %14, label %18, label %15
15: ; preds = %12
%16 = load i64, i64* %7
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %15, %12, %3
store i32 -1, i32* %4
br label %46
19: ; preds = %15
%20 = load i8*, i8** %5
%21 = load i32, i32* @O_WRONLY
%22 = call i32 @open(i8* %20, i32 %21)
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %19
br label %37
26: ; preds = %19
%27 = load i32, i32* %8
%28 = load i8*, i8** %6
%29 = load i64, i64* %7
%30 = call i32 @write(i32 %27, i8* %28, i64 %29)
store i32 %30, i32* %9
%31 = load i32, i32* %9
%32 = load i64, i64* %7
%33 = trunc i64 %32 to i32
%34 = icmp slt i32 %31, %33
br i1 %34, label %35, label %36
35: ; preds = %26
br label %37
36: ; preds = %26
store i32 0, i32* %9
br label %38
37: ; preds = %35, %25
store i32 -1, i32* %9
br label %38
38: ; preds = %37, %36
%39 = load i32, i32* %8
%40 = icmp sge i32 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %38
%42 = load i32, i32* %8
%43 = call i32 @close(i32 %42)
br label %44
44: ; preds = %41, %38
%45 = load i32, i32* %9
store i32 %45, i32* %4
br label %46
46: ; preds = %44, %18
%47 = load i32, i32* %4
ret i32 %47
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
define i32 @sysfs_string_write(i8* %0, i8* %1, i64 %2) {
%4 = icmp eq i8* %0, null
%5 = icmp eq i8* %1, null
%6 = or i1 %4, %5
%7 = icmp eq i64 %2, 0
%8 = or i1 %6, %7
br i1 %8, label %19, label %9
9: ; preds = %3
%10 = load i32, i32* @O_WRONLY
%11 = tail call i32 @open(i8* %0, i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %19, label %13
13: ; preds = %9
%14 = tail call i32 @write(i32 %11, i8* %1, i64 %2)
%15 = trunc i64 %2 to i32
%16 = icmp slt i32 %14, %15
%17 = sext i1 %16 to i32
%18 = tail call i32 @close(i32 %11)
br label %19
19: ; preds = %9, %13, %3
%20 = phi i32 [ -1, %3 ], [ %17, %13 ], [ -1, %9 ]
ret i32 %20
}