#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);
    fclose(sysfsfp);
    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    1349 <_write_sysfs_string+0x1a9>
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    133a <_write_sysfs_string+0x19a>
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     1335 <_write_sysfs_string+0x195>
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    133a <_write_sysfs_string+0x19a>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rdx
lea    0xd37(%rip),%rsi
call   1070 <fscanf@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     1330 <_write_sysfs_string+0x190>
mov    -0x38(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%r8
lea    0xd34(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x28(%rbp)
jmp    1335 <_write_sysfs_string+0x195>
jmp    133a <_write_sysfs_string+0x19a>
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     12a7 <_write_sysfs_string+0xf7>
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     12c0 <_write_sysfs_string+0x110>
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     12f3 <_write_sysfs_string+0x143>
lea    0xdd3(%rip),%rsi
mov    %rbx,%rdi
call   10c0 <fopen@plt>
test   %rax,%rax
je     12dc <_write_sysfs_string+0x12c>
mov    %rax,%r13
lea    0xda0(%rip),%rsi
mov    %rax,%rdi
mov    %rbx,%rdx
call   1090 <fscanf@plt>
mov    %r13,%rdi
call   1050 <fclose@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     12f3 <_write_sysfs_string+0x143>
lea    0xd90(%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    12f3 <_write_sysfs_string+0x143>
lea    0xdd2(%rip),%rdi
call   1040 <puts@plt>
mov    0x2d26(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
jmp    12fb <_write_sysfs_string+0x14b>
lea    0xd41(%rip),%rdi
xor    %ebp,%ebp
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x2cfe(%rip),%rax
jmp    12f1 <_write_sysfs_string+0x141>
lea    0xd7f(%rip),%rdi
call   1040 <puts@plt>
mov    0x2ce9(%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 %79
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 %75
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 %74
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 %75
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 i32*, i32** %11
%62 = call i32 @fclose(i32* %61)
%63 = load i8*, i8** %12
%64 = load i8*, i8** %8
%65 = call i64 @strcmp(i8* %63, i8* %64)
%66 = icmp ne i64 %65, 0
br i1 %66, label %67, label %73
67: ; preds = %57
%68 = load i8*, i8** %12
%69 = load i8*, i8** %8
%70 = load i8*, i8** %7
%71 = load i8*, i8** %6
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.7, i64 0, i64 0), i8* %68, i8* %69, i8* %70, i8* %71)
store i32 -1, i32* %10
br label %73
73: ; preds = %67, %57
br label %74
74: ; preds = %73, %40
br label %75
75: ; preds = %74, %53, %35
%76 = load i8*, i8** %12
%77 = call i32 @free(i8* %76)
%78 = load i32, i32* %10
store i32 %78, i32* %5
br label %79
79: ; preds = %75, %22
%80 = load i32, i32* %5
ret i32 %80
}

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 %44
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 %41
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 %41, 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 %41
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 i32 @fclose(i32* %28)
%37 = tail call i64 @strcmp(i8* %9, i8* %2)
%38 = icmp eq i64 %37, 0
br i1 %38, label %41, label %39
39: ; preds = %34
%40 = 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 %41
41: ; preds = %23, %39, %34, %30, %19
%42 = phi i32 [ %22, %19 ], [ %33, %30 ], [ -1, %39 ], [ 0, %34 ], [ 0, %23 ]
%43 = tail call i32 @free(i8* %9)
br label %44
44: ; preds = %41, %11
%45 = phi i32 [ %14, %11 ], [ %42, %41 ]
ret i32 %45
}