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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fwrite(char *, int /*<<< orphan*/, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ strlen(char *);
int sysfs_write(char *str, char *file) {
  FILE *f;
  int ret = -1;
  f = fopen(file, "w");
  if (f != NULL) {
    if (-1 != fwrite(str, strlen(str), 1, f)) {
      ret = 0;
    } else {
      perror("fwrite");
    }
  } else {
    perror("fopen");
  }
  fclose(f);
  return ret;
}


x86 assembly (clang, -O0):
<sysfs_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe8e(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11d7 <sysfs_write+0x87>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x28(%rbp),%rdi
mov    %eax,%esi
mov    -0x18(%rbp),%rcx
mov    $0x1,%edx
call   1070 <fwrite@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
je     11c6 <sysfs_write+0x76>
movl   $0x0,-0x1c(%rbp)
jmp    11d2 <sysfs_write+0x82>
lea    0xe35(%rip),%rdi
call   1060 <perror@plt>
jmp    11e3 <sysfs_write+0x93>
lea    0xe2b(%rip),%rdi
call   1060 <perror@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysfs_write>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rax
mov    %rdi,%rbx
lea    0xe9f(%rip),%rsi
mov    %rax,%rdi
call   1050 <fopen@plt>
mov    %rax,%r14
test   %rax,%rax
je     11a4 <sysfs_write+0x54>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rbx,%rdi
mov    %eax,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1070 <fwrite@plt>
xor    %ebx,%ebx
cmp    $0xffffffff,%eax
je     11ad <sysfs_write+0x5d>
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe5e(%rip),%rdi
jmp    11b4 <sysfs_write+0x64>
lea    0xe4e(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%ebx
jmp    1192 <sysfs_write+0x42>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [7 x i8] c"fwrite\00"
@.str.2 = [6 x i8] c"fopen\00"
define i32 @sysfs_write(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 -1, i32* %6
%7 = load i8*, i8** %4
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %5
%9 = load i32*, i32** %5
%10 = icmp ne i32* %9, null
br i1 %10, label %11, label %22
11: ; preds = %2
%12 = load i8*, i8** %3
%13 = load i8*, i8** %3
%14 = call i32 @strlen(i8* %13)
%15 = load i32*, i32** %5
%16 = call i32 @fwrite(i8* %12, i32 %14, i32 1, i32* %15)
%17 = icmp ne i32 -1, %16
br i1 %17, label %18, label %19
18: ; preds = %11
store i32 0, i32* %6
br label %21
19: ; preds = %11
%20 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %19, %18
br label %24
22: ; preds = %2
%23 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %24
24: ; preds = %22, %21
%25 = load i32*, i32** %5
%26 = call i32 @fclose(i32* %25)
%27 = load i32, i32* %6
ret i32 %27
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [7 x i8] c"fwrite\00"
@.str.2 = [6 x i8] c"fopen\00"
define i32 @sysfs_write(i8* %0, i8* nocapture %1) {
%3 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %9, label %5
5: ; preds = %2
%6 = tail call i32 @strlen(i8* %0)
%7 = tail call i32 @fwrite(i8* %0, i32 %6, i32 1, i32* %3)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %2, %5
%10 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %5 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), %2 ]
%11 = tail call i32 @perror(i8* %10)
br label %12
12: ; preds = %9, %5
%13 = phi i32 [ 0, %5 ], [ -1, %9 ]
%14 = tail call i32 @fclose(i32* %3)
ret i32 %13
}