#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 errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fprintf(int /*<<< orphan*/ *, char *, long);
int inv_sysfs_write(char *filename, long data) {
  FILE *fp;
  int count;
  if (!filename)
    return -1;
  fp = fopen(filename, "w");
  if (!fp)
    return -errno;
  count = fprintf(fp, "%ld", data);
  fclose(fp);
  return count;
}


x86 assembly (clang, -O0):
<inv_sysfs_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1157 <inv_sysfs_write+0x27>
movl   $0xffffffff,-0x4(%rbp)
jmp    11af <inv_sysfs_write+0x7f>
mov    -0x10(%rbp),%rdi
lea    0xe9e(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1189 <inv_sysfs_write+0x59>
xor    %eax,%eax
mov    0x2e61(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11af <inv_sysfs_write+0x7f>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe6a(%rip),%rsi
call   1040 <fprintf@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<inv_sysfs_write>:
push   %rbp
push   %r14
push   %rbx
test   %rdi,%rdi
je     1170 <inv_sysfs_write+0x40>
mov    %rsi,%r14
lea    0xebd(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     1177 <inv_sysfs_write+0x47>
mov    %rax,%rbx
lea    0xeab(%rip),%rsi
mov    %rax,%rdi
mov    %r14,%rdx
call   1040 <fprintf@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %ebp,%eax
jmp    1182 <inv_sysfs_write+0x52>
mov    $0xffffffff,%eax
jmp    1182 <inv_sysfs_write+0x52>
mov    0x2e62(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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