#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 */
#define ENOENT 129
#define EPERM 128
int /*<<< orphan*/ O_RDWR;
scalar_t__ close(int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
int /*<<< orphan*/ geteuid();
int /*<<< orphan*/ getuid();
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int);
scalar_t__ strlen(char const *);
scalar_t__ write(int, char const *, scalar_t__);
int _write_sysfs(const char *name, const char *val, const char *type) {
  int fd;
  int ret = 0;
  if ((fd = open(name, O_RDWR)) < 0)
    switch (errno) {
    case EPERM:
      if (!getuid() && !geteuid())
        ++ret;
      break;
    case ENOENT:
      break;
    default:
      fprintf(stderr, "Error opening %s: %s\n", type, strerror(errno));
      ret = -1000;
    }
  else {
    ++ret;
    if (write(fd, val, strlen(val)) != (ssize_t)strlen(val)) {
      fprintf(stderr, "Error writing %s: %s\n", type, strerror(errno));
      ret = -1000;
    }
    if (close(fd)) {
      fprintf(stderr, "Error closing %s: %s\n", type, strerror(errno));
      ret = -1000;
    }
  }
  return ret;
}


x86 assembly (clang, -O0):
<_write_sysfs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    125f <_write_sysfs+0xdf>
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
sub    $0x80,%eax
je     11e8 <_write_sysfs+0x68>
jmp    11d5 <_write_sysfs+0x55>
mov    -0x24(%rbp),%eax
sub    $0x81,%eax
je     1216 <_write_sysfs+0x96>
jmp    121b <_write_sysfs+0x9b>
mov    $0x0,%al
call   1050 <getuid@plt>
cmp    $0x0,%eax
jne    1211 <_write_sysfs+0x91>
mov    $0x0,%al
call   1060 <geteuid@plt>
cmp    $0x0,%eax
jne    1211 <_write_sysfs+0x91>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    125a <_write_sysfs+0xda>
jmp    125a <_write_sysfs+0xda>
mov    0x2db6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xdb2(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xfffffc18,-0x20(%rbp)
jmp    1340 <_write_sysfs+0x1c0>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rsi
mov    %rax,%rdx
call   1030 <write@plt>
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
cmp    %rcx,%rax
je     12ea <_write_sysfs+0x16a>
mov    0x2d26(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    0x2d0a(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x5c(%rbp),%edi
mov    -0x58(%rbp),%rdx
mov    %rax,%rcx
lea    0xd38(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xfffffc18,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
cmp    $0x0,%rax
je     133b <_write_sysfs+0x1bb>
mov    0x2cd5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x6c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    0x2cb9(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x6c(%rbp),%edi
mov    -0x68(%rbp),%rdx
mov    %rax,%rcx
lea    0xcfd(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xfffffc18,-0x20(%rbp)
jmp    1340 <_write_sysfs+0x1c0>
mov    -0x20(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_write_sysfs>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
test   %eax,%eax
js     122f <_write_sysfs+0xaf>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1030 <write@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    $0x1,%ebx
cmp    %rax,%r15
je     11ff <_write_sysfs+0x7f>
mov    0x2e02(%rip),%rax
mov    (%rax),%ebx
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xe29(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1080 <fprintf@plt>
mov    $0xfffffc18,%ebx
mov    %ebp,%edi
call   1070 <close@plt>
test   %rax,%rax
je     1290 <_write_sysfs+0x110>
mov    0x2dc2(%rip),%rax
mov    (%rax),%ebx
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdff(%rip),%rsi
jmp    127e <_write_sysfs+0xfe>
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
cmp    $0x81,%edi
je     1265 <_write_sysfs+0xe5>
cmp    $0x80,%edi
jne    1269 <_write_sysfs+0xe9>
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <getuid@plt>
test   %eax,%eax
jne    1290 <_write_sysfs+0x110>
xor    %eax,%eax
call   1060 <geteuid@plt>
xor    %ebx,%ebx
test   %eax,%eax
sete   %bl
jmp    1290 <_write_sysfs+0x110>
xor    %ebx,%ebx
jmp    1290 <_write_sysfs+0x110>
mov    0x2d68(%rip),%rax
mov    (%rax),%ebx
call   10a0 <strerror@plt>
lea    0xd82(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1080 <fprintf@plt>
mov    $0xfffffc18,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@errno = global i32 0
@stderr = global i32 0
@.str = [22 x i8] c"Error opening %s: %s\0A\00"
@.str.1 = [22 x i8] c"Error writing %s: %s\0A\00"
@.str.2 = [22 x i8] c"Error closing %s: %s\0A\00"
define i32 @_write_sysfs(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
store i32 0, i32* %8
%9 = load i8*, i8** %4
%10 = load i32, i32* @O_RDWR
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %7
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %33
13: ; preds = %3
%14 = load i32, i32* @errno
switch i32 %14, label %26 [
i32 128, label %15
i32 129, label %25
]
15: ; preds = %13
%16 = call i32 (...) @getuid()
%17 = icmp ne i32 %16, 0
br i1 %17, label %24, label %18
18: ; preds = %15
%19 = call i32 (...) @geteuid()
%20 = icmp ne i32 %19, 0
br i1 %20, label %24, label %21
21: ; preds = %18
%22 = load i32, i32* %8
%23 = add nsw i32 %22, 1
store i32 %23, i32* %8
br label %24
24: ; preds = %21, %18, %15
br label %32
25: ; preds = %13
br label %32
26: ; preds = %13
%27 = load i32, i32* @stderr
%28 = load i8*, i8** %6
%29 = load i32, i32* @errno
%30 = call i8* @strerror(i32 %29)
%31 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %28, i8* %30)
store i32 -1000, i32* %8
br label %32
32: ; preds = %26, %25, %24
br label %61
33: ; preds = %3
%34 = load i32, i32* %8
%35 = add nsw i32 %34, 1
store i32 %35, i32* %8
%36 = load i32, i32* %7
%37 = load i8*, i8** %5
%38 = load i8*, i8** %5
%39 = call i64 @strlen(i8* %38)
%40 = call i64 @write(i32 %36, i8* %37, i64 %39)
%41 = load i8*, i8** %5
%42 = call i64 @strlen(i8* %41)
%43 = icmp ne i64 %40, %42
br i1 %43, label %44, label %50
44: ; preds = %33
%45 = load i32, i32* @stderr
%46 = load i8*, i8** %6
%47 = load i32, i32* @errno
%48 = call i8* @strerror(i32 %47)
%49 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %46, i8* %48)
store i32 -1000, i32* %8
br label %50
50: ; preds = %44, %33
%51 = load i32, i32* %7
%52 = call i64 @close(i32 %51)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %60
54: ; preds = %50
%55 = load i32, i32* @stderr
%56 = load i8*, i8** %6
%57 = load i32, i32* @errno
%58 = call i8* @strerror(i32 %57)
%59 = call i32 @fprintf(i32 %55, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %56, i8* %58)
store i32 -1000, i32* %8
br label %60
60: ; preds = %54, %50
br label %61
61: ; preds = %60, %32
%62 = load i32, i32* %8
ret i32 %62
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@errno = global i32 0
@stderr = global i32 0
@.str = [22 x i8] c"Error opening %s: %s\0A\00"
@.str.1 = [22 x i8] c"Error writing %s: %s\0A\00"
@.str.2 = [22 x i8] c"Error closing %s: %s\0A\00"
define i32 @_write_sysfs(i8* nocapture %0, i8* nocapture %1, i8* %2) {
%4 = load i32, i32* @O_RDWR
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %20
7: ; preds = %3
%8 = load i32, i32* @errno
switch i32 %8, label %16 [
i32 128, label %9
i32 129, label %39
]
9: ; preds = %7
%10 = tail call i32 (...) @getuid()
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %39
12: ; preds = %9
%13 = tail call i32 (...) @geteuid()
%14 = icmp eq i32 %13, 0
%15 = zext i1 %14 to i32
br label %39
16: ; preds = %7
%17 = load i32, i32* @stderr
%18 = tail call i8* @strerror(i32 %8)
%19 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %2, i8* %18)
br label %39
20: ; preds = %3
%21 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%22 = tail call i64 @write(i32 %5, i8* %1, i64 %21)
%23 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%24 = icmp eq i64 %22, %23
br i1 %24, label %30, label %25
25: ; preds = %20
%26 = load i32, i32* @stderr
%27 = load i32, i32* @errno
%28 = tail call i8* @strerror(i32 %27)
%29 = tail call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %2, i8* %28)
br label %30
30: ; preds = %25, %20
%31 = phi i32 [ -1000, %25 ], [ 1, %20 ]
%32 = tail call i64 @close(i32 %5)
%33 = icmp eq i64 %32, 0
br i1 %33, label %39, label %34
34: ; preds = %30
%35 = load i32, i32* @stderr
%36 = load i32, i32* @errno
%37 = tail call i8* @strerror(i32 %36)
%38 = tail call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %2, i8* %37)
br label %39
39: ; preds = %12, %30, %34, %7, %16, %9
%40 = phi i32 [ -1000, %16 ], [ 0, %7 ], [ 0, %9 ], [ -1000, %34 ], [ %31, %30 ], [ %15, %12 ]
ret i32 %40
}