#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 ENOMEM;
int /*<<< orphan*/ LOG_ERR;
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, char *);
char *malloc(scalar_t__);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
scalar_t__ strcmp(char *, char *);
scalar_t__ strlen(char *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, ...);
int _write_sysfs_string(char *filename, char *basedir, char *val, int verify) {
  int ret = 0;
  FILE *sysfsfp;
  char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
  if (temp == NULL) {
    syslog(LOG_ERR, "Memory allocation failed\n");
    return -ENOMEM;
  }
  sprintf(temp, "%s/%s", basedir, filename);
  sysfsfp = fopen(temp, "w");
  if (sysfsfp == NULL) {
    syslog(LOG_ERR, "Could not open %s\n", temp);
    ret = -errno;
    goto error_free;
  }
  fprintf(sysfsfp, "%s", val);
  fclose(sysfsfp);
  if (verify) {
    sysfsfp = fopen(temp, "r");
    if (sysfsfp == NULL) {
      syslog(LOG_ERR, "could not open file to verify\n");
      ret = -errno;
      goto error_free;
    }
    fscanf(sysfsfp, "%s", temp);
    if (strcmp(temp, val) != 0) {
      syslog(LOG_ERR,
             "Possible failure in string write of %s "
             "Should be %s "
             "written to %s\%s\n",
             temp, val, basedir, filename);
      ret = -1;
    }
  }
error_free:
  free(temp);
  return ret;
}


x86 assembly (clang, -O0):
<_write_sysfs_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x40(%rbp),%rdi
add    %rax,%rdi
add    $0x2,%rdi
call   1090 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    121d <_write_sysfs_string+0x7d>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
lea    0xdfd(%rip),%rsi
mov    $0x0,%al
call   10a0 <syslog@plt>
xor    %eax,%eax
mov    0x2dcd(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1364 <_write_sysfs_string+0x1c4>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xdea(%rip),%rsi
call   10c0 <sprintf@plt>
mov    -0x38(%rbp),%rdi
lea    0xde0(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1282 <_write_sysfs_string+0xe2>
mov    0x2d6d(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%rdx
lea    0xdba(%rip),%rsi
mov    $0x0,%al
call   10a0 <syslog@plt>
xor    %eax,%eax
mov    0x2d60(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x28(%rbp)
jmp    1355 <_write_sysfs_string+0x1b5>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xd8c(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
cmpl   $0x0,-0x24(%rbp)
je     1350 <_write_sysfs_string+0x1b0>
mov    -0x38(%rbp),%rdi
lea    0xd81(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12f2 <_write_sysfs_string+0x152>
mov    0x2cf9(%rip),%rax
mov    (%rax),%edi
lea    0xd5f(%rip),%rsi
mov    $0x0,%al
call   10a0 <syslog@plt>
xor    %eax,%eax
mov    0x2cf0(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x28(%rbp)
jmp    1355 <_write_sysfs_string+0x1b5>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rdx
lea    0xd1c(%rip),%rsi
call   1060 <fscanf@plt>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     134b <_write_sysfs_string+0x1ab>
mov    0x2ca4(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%r8
mov    -0x10(%rbp),%r9
lea    0xd19(%rip),%rsi
mov    $0x0,%al
call   10a0 <syslog@plt>
movl   $0xffffffff,-0x28(%rbp)
jmp    1350 <_write_sysfs_string+0x1b0>
jmp    1355 <_write_sysfs_string+0x1b5>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_write_sysfs_string>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r13d
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsi,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    (%rbx,%rax,1),%rdi
add    $0x2,%rdi
call   1090 <malloc@plt>
test   %rax,%rax
je     1299 <_write_sysfs_string+0xf9>
mov    %rax,%rbx
lea    0xe30(%rip),%rsi
mov    %rax,%rdi
mov    %r15,%rdx
mov    %r14,%rcx
call   10c0 <sprintf@plt>
lea    0xe21(%rip),%rsi
mov    %rbx,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12bd <_write_sysfs_string+0x11d>
mov    %rax,%rbp
mov    %r12,%rdi
mov    %rax,%rsi
call   1060 <fputs@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
xor    %ebp,%ebp
test   %r13d,%r13d
je     12fd <_write_sysfs_string+0x15d>
lea    0xdfd(%rip),%rsi
mov    %rbx,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12db <_write_sysfs_string+0x13b>
lea    0xdcd(%rip),%rsi
mov    %rax,%rdi
mov    %rbx,%rdx
call   1070 <fscanf@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     12fd <_write_sysfs_string+0x15d>
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
lea    0xdd7(%rip),%rsi
mov    %rbx,%rdx
mov    %r12,%rcx
mov    %r15,%r8
mov    %r14,%r9
xor    %eax,%eax
call   10a0 <syslog@plt>
mov    $0xffffffff,%ebp
jmp    12fd <_write_sysfs_string+0x15d>
mov    0x2d28(%rip),%rax
mov    (%rax),%edi
lea    0xd57(%rip),%rsi
xor    %ebp,%ebp
xor    %eax,%eax
call   10a0 <syslog@plt>
mov    0x2d27(%rip),%rax
sub    (%rax),%ebp
jmp    1305 <_write_sysfs_string+0x165>
mov    0x2d04(%rip),%rax
mov    (%rax),%edi
lea    0xd55(%rip),%rsi
xor    %ebp,%ebp
mov    %rbx,%rdx
xor    %eax,%eax
call   10a0 <syslog@plt>
jmp    12f4 <_write_sysfs_string+0x154>
mov    0x2ce6(%rip),%rax
mov    (%rax),%edi
lea    0xd4c(%rip),%rsi
xor    %ebp,%ebp
xor    %eax,%eax
call   10a0 <syslog@plt>
mov    0x2cdd(%rip),%rax
sub    (%rax),%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@LOG_ERR = global i32 0
@.str = [26 x i8] c"Memory allocation failed\0A\00"
@ENOMEM = global i32 0
@.str.1 = [6 x i8] c"%s/%s\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [19 x i8] c"Could not open %s\0A\00"
@errno = global i32 0
@.str.4 = [3 x i8] c"%s\00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [31 x i8] c"could not open file to verify\0A\00"
@.str.7 = [69 x i8] c"Possible failure in string write of %s Should be %s written to %s%s\0A\00"
define i32 @_write_sysfs_string(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32*
%12 = alloca i8*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
store i32 0, i32* %10
%13 = load i8*, i8** %7
%14 = call i64 @strlen(i8* %13)
%15 = load i8*, i8** %6
%16 = call i64 @strlen(i8* %15)
%17 = add nsw i64 %14, %16
%18 = add nsw i64 %17, 2
%19 = call i8* @malloc(i64 %18)
store i8* %19, i8** %12
%20 = load i8*, i8** %12
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %27
22: ; preds = %4
%23 = load i32, i32* @LOG_ERR
%24 = call i32 (i32, i8*, ...) @syslog(i32 %23, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%25 = load i32, i32* @ENOMEM
%26 = sub nsw i32 0, %25
store i32 %26, i32* %5
br label %81
27: ; preds = %4
%28 = load i8*, i8** %12
%29 = load i8*, i8** %7
%30 = load i8*, i8** %6
%31 = call i32 @sprintf(i8* %28, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %29, i8* %30)
%32 = load i8*, i8** %12
%33 = call i32* @fopen(i8* %32, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %33, i32** %11
%34 = load i32*, i32** %11
%35 = icmp eq i32* %34, null
br i1 %35, label %36, label %42
36: ; preds = %27
%37 = load i32, i32* @LOG_ERR
%38 = load i8*, i8** %12
%39 = call i32 (i32, i8*, ...) @syslog(i32 %37, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %38)
%40 = load i32, i32* @errno
%41 = sub nsw i32 0, %40
store i32 %41, i32* %10
br label %77
42: ; preds = %27
%43 = load i32*, i32** %11
%44 = load i8*, i8** %8
%45 = call i32 @fprintf(i32* %43, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %44)
%46 = load i32*, i32** %11
%47 = call i32 @fclose(i32* %46)
%48 = load i32, i32* %9
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %76
50: ; preds = %42
%51 = load i8*, i8** %12
%52 = call i32* @fopen(i8* %51, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %52, i32** %11
%53 = load i32*, i32** %11
%54 = icmp eq i32* %53, null
br i1 %54, label %55, label %60
55: ; preds = %50
%56 = load i32, i32* @LOG_ERR
%57 = call i32 (i32, i8*, ...) @syslog(i32 %56, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0))
%58 = load i32, i32* @errno
%59 = sub nsw i32 0, %58
store i32 %59, i32* %10
br label %77
60: ; preds = %50
%61 = load i32*, i32** %11
%62 = load i8*, i8** %12
%63 = call i32 @fscanf(i32* %61, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %62)
%64 = load i8*, i8** %12
%65 = load i8*, i8** %8
%66 = call i64 @strcmp(i8* %64, i8* %65)
%67 = icmp ne i64 %66, 0
br i1 %67, label %68, label %75
68: ; preds = %60
%69 = load i32, i32* @LOG_ERR
%70 = load i8*, i8** %12
%71 = load i8*, i8** %8
%72 = load i8*, i8** %7
%73 = load i8*, i8** %6
%74 = call i32 (i32, i8*, ...) @syslog(i32 %69, i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.7, i64 0, i64 0), i8* %70, i8* %71, i8* %72, i8* %73)
store i32 -1, i32* %10
br label %75
75: ; preds = %68, %60
br label %76
76: ; preds = %75, %42
br label %77
77: ; preds = %76, %55, %36
%78 = load i8*, i8** %12
%79 = call i32 @free(i8* %78)
%80 = load i32, i32* %10
store i32 %80, i32* %5
br label %81
81: ; preds = %77, %22
%82 = load i32, i32* %5
ret i32 %82
}

LLVM IR (-O2):
@LOG_ERR = global i32 0
@.str = [26 x i8] c"Memory allocation failed\0A\00"
@ENOMEM = global i32 0
@.str.1 = [6 x i8] c"%s/%s\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [19 x i8] c"Could not open %s\0A\00"
@errno = global i32 0
@.str.4 = [3 x i8] c"%s\00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [31 x i8] c"could not open file to verify\0A\00"
@.str.7 = [69 x i8] c"Possible failure in string write of %s Should be %s written to %s%s\0A\00"
define i32 @_write_sysfs_string(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%6 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%7 = add i64 %5, 2
%8 = add i64 %7, %6
%9 = tail call i8* @malloc(i64 %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %16
11: ; preds = %4
%12 = load i32, i32* @LOG_ERR
%13 = tail call i32 (i32, i8*, ...) @syslog(i32 %12, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @ENOMEM
%15 = sub nsw i32 0, %14
br label %47
16: ; preds = %4
%17 = tail call i32 @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %1, i8* %0)
%18 = tail call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %25
20: ; preds = %16
%21 = load i32, i32* @LOG_ERR
%22 = tail call i32 (i32, i8*, ...) @syslog(i32 %21, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %9)
%23 = load i32, i32* @errno
%24 = sub nsw i32 0, %23
br label %44
25: ; preds = %16
%26 = tail call i32 @fputs(i8* %2, i32* %18)
%27 = tail call i32 @fclose(i32* %18)
%28 = icmp eq i32 %3, 0
br i1 %28, label %44, label %29
29: ; preds = %25
%30 = tail call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %37
32: ; preds = %29
%33 = load i32, i32* @LOG_ERR
%34 = tail call i32 (i32, i8*, ...) @syslog(i32 %33, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0))
%35 = load i32, i32* @errno
%36 = sub nsw i32 0, %35
br label %44
37: ; preds = %29
%38 = tail call i32 @fscanf(i32* %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %9)
%39 = tail call i64 @strcmp(i8* %9, i8* %2)
%40 = icmp eq i64 %39, 0
br i1 %40, label %44, label %41
41: ; preds = %37
%42 = load i32, i32* @LOG_ERR
%43 = tail call i32 (i32, i8*, ...) @syslog(i32 %42, i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.7, i64 0, i64 0), i8* %9, i8* %2, i8* %1, i8* %0)
br label %44
44: ; preds = %25, %41, %37, %32, %20
%45 = phi i32 [ %24, %20 ], [ %36, %32 ], [ -1, %41 ], [ 0, %37 ], [ 0, %25 ]
%46 = tail call i32 @free(i8* %9)
br label %47
47: ; preds = %44, %11
%48 = phi i32 [ %15, %11 ], [ %45, %44 ]
ret i32 %48
}