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

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char const *, int /*<<< orphan*/);
size_t strlen(char const *);
size_t write(int, char const *, size_t);
int om_sysfs_writefile(const char *pathname, const char *str) {
  int res = 0;
  size_t ssize = strlen(str);
  ssize_t count;
  int fd = -1;
  fd = open(pathname, O_WRONLY);
  if (fd < 0)
    return fd;
  count = write(fd, str, ssize);
  if (count != ssize)
    res = count;
  if (fd >= 0)
    close(fd);
  return res;
}


x86 assembly (clang, -O0):
<om_sysfs_writefile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
movl   $0xffffffff,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    1195 <om_sysfs_writefile+0x55>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d6 <om_sysfs_writefile+0x96>
mov    -0x34(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x28(%rbp),%rax
je     11be <om_sysfs_writefile+0x7e>
mov    -0x30(%rbp),%rax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x34(%rbp)
jl     11d0 <om_sysfs_writefile+0x90>
mov    -0x34(%rbp),%edi
call   1050 <close@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<om_sysfs_writefile>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    %rax,%r15
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1190 <om_sysfs_writefile+0x50>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1030 <write@plt>
mov    %rax,%r14
xor    %eax,%eax
cmp    %r15,%r14
cmove  %eax,%r14d
mov    %ebx,%edi
call   1050 <close@plt>
mov    %r14d,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@O_WRONLY = global i32 0
define i32 @om_sysfs_writefile(i8* nocapture %0, i8* nocapture %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%4 = load i32, i32* @O_WRONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %13, label %7
7: ; preds = %2
%8 = tail call i64 @write(i32 %5, i8* %1, i64 %3)
%9 = icmp eq i64 %8, %3
%10 = trunc i64 %8 to i32
%11 = select i1 %9, i32 0, i32 %10
%12 = tail call i32 @close(i32 %5)
br label %13
13: ; preds = %2, %7
%14 = phi i32 [ %11, %7 ], [ %5, %2 ]
ret i32 %14
}