#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;
  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) {
      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 "
             "writen 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)
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    120d <_write_sysfs_string+0x6d>
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %eax,%eax
mov    0x2ddd(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    132b <_write_sysfs_string+0x18b>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xdfa(%rip),%rsi
call   10c0 <sprintf@plt>
mov    -0x38(%rbp),%rdi
lea    0xdf0(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1269 <_write_sysfs_string+0xc9>
mov    -0x38(%rbp),%rsi
lea    0xdd3(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %eax,%eax
mov    0x2d79(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x28(%rbp)
jmp    131c <_write_sysfs_string+0x17c>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xda5(%rip),%rsi
call   1090 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
cmpl   $0x0,-0x24(%rbp)
je     1317 <_write_sysfs_string+0x177>
mov    -0x38(%rbp),%rdi
lea    0xd9a(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12c2 <_write_sysfs_string+0x122>
xor    %eax,%eax
mov    0x2d20(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x28(%rbp)
jmp    131c <_write_sysfs_string+0x17c>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rdx
lea    0xd4c(%rip),%rsi
call   1070 <fscanf@plt>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     1312 <_write_sysfs_string+0x172>
mov    -0x38(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%r8
lea    0xd33(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x28(%rbp)
jmp    1317 <_write_sysfs_string+0x177>
jmp    131c <_write_sysfs_string+0x17c>
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     1296 <_write_sysfs_string+0xe6>
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     12af <_write_sysfs_string+0xff>
mov    %rax,%rbp
mov    %r12,%rdi
mov    %rax,%rsi
call   1080 <fputs@plt>
mov    %rbp,%rdi
call   1050 <fclose@plt>
test   %r13d,%r13d
je     12cd <_write_sysfs_string+0x11d>
lea    0xdd5(%rip),%rsi
mov    %rbx,%rdi
call   10c0 <fopen@plt>
test   %rax,%rax
je     12cf <_write_sysfs_string+0x11f>
lea    0xda9(%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     12cd <_write_sysfs_string+0x11d>
lea    0xda1(%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    12da <_write_sysfs_string+0x12a>
lea    0xdc4(%rip),%rdi
call   1040 <puts@plt>
mov    0x2d37(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
jmp    12e2 <_write_sysfs_string+0x132>
lea    0xd52(%rip),%rdi
xor    %ebp,%ebp
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x2d0f(%rip),%rax
sub    (%rax),%ebp
jmp    12da <_write_sysfs_string+0x12a>
jmp    12da <_write_sysfs_string+0x12a>
mov    0x2d02(%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 = [68 x i8] c"Possible failure in string write of %s Should be %s writen 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
%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 %76
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 %72
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 %71
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 %56
53: ; preds = %48
%54 = load i32, i32* @errno
%55 = sub nsw i32 0, %54
store i32 %55, i32* %10
br label %72
56: ; preds = %48
%57 = load i32*, i32** %11
%58 = load i8*, i8** %12
%59 = call i32 @fscanf(i32* %57, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %58)
%60 = load i8*, i8** %12
%61 = load i8*, i8** %8
%62 = call i64 @strcmp(i8* %60, i8* %61)
%63 = icmp ne i64 %62, 0
br i1 %63, label %64, label %70
64: ; preds = %56
%65 = load i8*, i8** %12
%66 = load i8*, i8** %8
%67 = load i8*, i8** %7
%68 = load i8*, i8** %6
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.6, i64 0, i64 0), i8* %65, i8* %66, i8* %67, i8* %68)
store i32 -1, i32* %10
br label %70
70: ; preds = %64, %56
br label %71
71: ; preds = %70, %40
br label %72
72: ; preds = %71, %53, %35
%73 = load i8*, i8** %12
%74 = call i32 @free(i8* %73)
%75 = load i32, i32* %10
store i32 %75, i32* %5
br label %76
76: ; preds = %72, %22
%77 = load i32, i32* %5
ret i32 %77
}

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.6 = [68 x i8] c"Possible failure in string write of %s Should be %s writen to %s%s\0A\00"
@str = [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, i64 0, i64 0))
%13 = load i32, i32* @ENOMEM
%14 = sub nsw i32 0, %13
br label %42
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 %39
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 %39, 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 %33
30: ; preds = %27
%31 = load i32, i32* @errno
%32 = sub nsw i32 0, %31
br label %39
33: ; preds = %27
%34 = tail call i32 @fscanf(i32* %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %9)
%35 = tail call i64 @strcmp(i8* %9, i8* %2)
%36 = icmp eq i64 %35, 0
br i1 %36, label %39, label %37
37: ; preds = %33
%38 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([68 x i8], [68 x i8]* @.str.6, i64 0, i64 0), i8* %9, i8* %2, i8* %1, i8* %0)
br label %39
39: ; preds = %23, %37, %33, %30, %19
%40 = phi i32 [ %22, %19 ], [ %32, %30 ], [ -1, %37 ], [ undef, %33 ], [ undef, %23 ]
%41 = tail call i32 @free(i8* %9)
br label %42
42: ; preds = %39, %11
%43 = phi i32 [ %14, %11 ], [ %40, %39 ]
ret i32 %43
}