#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 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*/ printf(char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
scalar_t__ strcmp(char *, char *);
scalar_t__ strlen(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) {
    printf("Memory allocation failed\n");
    return -ENOMEM;
  }
  sprintf(temp, "%s/%s", basedir, filename);
  sysfsfp = fopen(temp, "w");
  if (sysfsfp == NULL) {
    printf("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) {
      printf("could not open file to verify\n");
      ret = -errno;
      goto error_free;
    }
    fscanf(sysfsfp, "%s", temp);
    if (strcmp(temp, val) != 0) {
      printf("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   10a0 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1214 <_write_sysfs_string+0x74>
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %eax,%eax
mov    0x2dd6(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1340 <_write_sysfs_string+0x1a0>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xdf3(%rip),%rsi
call   10c0 <sprintf@plt>
mov    -0x38(%rbp),%rdi
lea    0xde9(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1270 <_write_sysfs_string+0xd0>
mov    -0x38(%rbp),%rsi
lea    0xdcc(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %eax,%eax
mov    0x2d72(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x28(%rbp)
jmp    1331 <_write_sysfs_string+0x191>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xd9e(%rip),%rsi
call   1090 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
cmpl   $0x0,-0x24(%rbp)
je     132c <_write_sysfs_string+0x18c>
mov    -0x38(%rbp),%rdi
lea    0xd93(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12d7 <_write_sysfs_string+0x137>
lea    0xd7a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %eax,%eax
mov    0x2d0b(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x28(%rbp)
jmp    1331 <_write_sysfs_string+0x191>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rdx
lea    0xd37(%rip),%rsi
call   1070 <fscanf@plt>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     1327 <_write_sysfs_string+0x187>
mov    -0x38(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%r8
lea    0xd3d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x28(%rbp)
jmp    132c <_write_sysfs_string+0x18c>
jmp    1331 <_write_sysfs_string+0x191>
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   1060 <strlen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1060 <strlen@plt>
lea    (%rbx,%rax,1),%rdi
add    $0x2,%rdi
call   10b0 <malloc@plt>
test   %rax,%rax
je     1298 <_write_sysfs_string+0xe8>
mov    %rax,%rbx
lea    0xe06(%rip),%rsi
mov    %rax,%rdi
mov    %r15,%rdx
mov    %r14,%rcx
call   10d0 <sprintf@plt>
lea    0xdf7(%rip),%rsi
mov    %rbx,%rdi
call   10c0 <fopen@plt>
test   %rax,%rax
je     12b1 <_write_sysfs_string+0x101>
mov    %rax,%rbp
mov    %r12,%rdi
mov    %rax,%rsi
call   1080 <fputs@plt>
mov    %rbp,%rdi
call   1050 <fclose@plt>
xor    %ebp,%ebp
test   %r13d,%r13d
je     12e4 <_write_sysfs_string+0x134>
lea    0xdd3(%rip),%rsi
mov    %rbx,%rdi
call   10c0 <fopen@plt>
test   %rax,%rax
je     12cd <_write_sysfs_string+0x11d>
lea    0xda7(%rip),%rsi
mov    %rax,%rdi
mov    %rbx,%rdx
call   1090 <fscanf@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     12e4 <_write_sysfs_string+0x134>
lea    0xd9f(%rip),%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
mov    %r15,%rcx
mov    %r14,%r8
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0xffffffff,%ebp
jmp    12e4 <_write_sysfs_string+0x134>
lea    0xde1(%rip),%rdi
call   1040 <puts@plt>
mov    0x2d35(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
jmp    12ec <_write_sysfs_string+0x13c>
lea    0xd50(%rip),%rdi
xor    %ebp,%ebp
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x2d0d(%rip),%rax
jmp    12e2 <_write_sysfs_string+0x132>
lea    0xd8e(%rip),%rdi
call   1040 <puts@plt>
mov    0x2cf8(%rip),%rax
xor    %ebp,%ebp
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):
@.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 %26
22: ; preds = %4
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%24 = load i32, i32* @ENOMEM
%25 = sub nsw i32 0, %24
store i32 %25, i32* %5
br label %77
26: ; preds = %4
%27 = load i8*, i8** %12
%28 = load i8*, i8** %7
%29 = load i8*, i8** %6
%30 = call i32 @sprintf(i8* %27, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %28, i8* %29)
%31 = load i8*, i8** %12
%32 = call i32* @fopen(i8* %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %32, i32** %11
%33 = load i32*, i32** %11
%34 = icmp eq i32* %33, null
br i1 %34, label %35, label %40
35: ; preds = %26
%36 = load i8*, i8** %12
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %36)
%38 = load i32, i32* @errno
%39 = sub nsw i32 0, %38
store i32 %39, i32* %10
br label %73
40: ; preds = %26
%41 = load i32*, i32** %11
%42 = load i8*, i8** %8
%43 = call i32 @fprintf(i32* %41, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %42)
%44 = load i32*, i32** %11
%45 = call i32 @fclose(i32* %44)
%46 = load i32, i32* %9
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %72
48: ; preds = %40
%49 = load i8*, i8** %12
%50 = call i32* @fopen(i8* %49, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %50, i32** %11
%51 = load i32*, i32** %11
%52 = icmp eq i32* %51, null
br i1 %52, label %53, label %57
53: ; preds = %48
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0))
%55 = load i32, i32* @errno
%56 = sub nsw i32 0, %55
store i32 %56, i32* %10
br label %73
57: ; preds = %48
%58 = load i32*, i32** %11
%59 = load i8*, i8** %12
%60 = call i32 @fscanf(i32* %58, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %59)
%61 = load i8*, i8** %12
%62 = load i8*, i8** %8
%63 = call i64 @strcmp(i8* %61, i8* %62)
%64 = icmp ne i64 %63, 0
br i1 %64, label %65, label %71
65: ; preds = %57
%66 = load i8*, i8** %12
%67 = load i8*, i8** %8
%68 = load i8*, i8** %7
%69 = load i8*, i8** %6
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.7, i64 0, i64 0), i8* %66, i8* %67, i8* %68, i8* %69)
store i32 -1, i32* %10
br label %71
71: ; preds = %65, %57
br label %72
72: ; preds = %71, %40
br label %73
73: ; preds = %72, %53, %35
%74 = load i8*, i8** %12
%75 = call i32 @free(i8* %74)
%76 = load i32, i32* %10
store i32 %76, i32* %5
br label %77
77: ; preds = %73, %22
%78 = load i32, i32* %5
ret i32 %78
}

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