#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*/ fscanf(int /*<<< orphan*/ *, char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
scalar_t__ strcmp(char *, char *);
int _write_sysfs_string(char *filename, char *basedir, char *val, int verify) {
  int ret = 0;
  FILE *sysfsfp;
  char temp[64];
  if (temp == NULL) {
    printf("Memory allocation failed\n");
    return -ENOMEM;
  }
  sprintf(temp, "%s/%s", basedir, filename);
  printf("%s\n", temp);
  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:
  return ret;
}


x86 assembly (clang, -O0):
<_write_sysfs_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
lea    -0x70(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x8(%rbp),%rcx
lea    0xe5f(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x70(%rbp),%rsi
lea    0xe66(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x70(%rbp),%rdi
lea    0xe43(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11fc <_write_sysfs_string+0x8c>
lea    -0x70(%rbp),%rsi
lea    0xe26(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    0x2dee(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x20(%rbp)
jmp    12c6 <_write_sysfs_string+0x156>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xdf8(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
cmpl   $0x0,-0x1c(%rbp)
je     12c1 <_write_sysfs_string+0x151>
lea    -0x70(%rbp),%rdi
lea    0xded(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1263 <_write_sysfs_string+0xf3>
lea    0xdd4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    0x2d87(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x20(%rbp)
jmp    12c6 <_write_sysfs_string+0x156>
mov    -0x28(%rbp),%rdi
lea    -0x70(%rbp),%rdx
lea    0xd91(%rip),%rsi
call   1050 <fscanf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x70(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     12bc <_write_sysfs_string+0x14c>
lea    -0x70(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    -0x8(%rbp),%r8
lea    0xd8e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x20(%rbp)
jmp    12c1 <_write_sysfs_string+0x151>
jmp    12c6 <_write_sysfs_string+0x156>
mov    -0x20(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_write_sysfs_string>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %ecx,%ebx
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r12
lea    0xe60(%rip),%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    %r14,%rdx
mov    %r12,%rcx
call   10a0 <sprintf@plt>
mov    %rbp,%rdi
call   1030 <puts@plt>
lea    0xe46(%rip),%rsi
mov    %rbp,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1256 <_write_sysfs_string+0xd6>
mov    %rax,%rbp
mov    %r15,%rdi
mov    %rax,%rsi
call   1060 <fputs@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
xor    %ebp,%ebp
test   %ebx,%ebx
je     1289 <_write_sysfs_string+0x109>
lea    0xe23(%rip),%rsi
mov    %rsp,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1272 <_write_sysfs_string+0xf2>
mov    %rax,%rbx
lea    0xdf4(%rip),%rsi
mov    %rsp,%r13
mov    %rax,%rdi
mov    %r13,%rdx
call   1070 <fscanf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r13,%rdi
mov    %r15,%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     1289 <_write_sysfs_string+0x109>
lea    0xde1(%rip),%rdi
mov    %rsp,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
mov    %r12,%r8
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%ebp
jmp    1289 <_write_sysfs_string+0x109>
lea    0xdab(%rip),%rdi
xor    %ebp,%ebp
mov    %rsp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d70(%rip),%rax
jmp    1287 <_write_sysfs_string+0x107>
lea    0xde9(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d5b(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
mov    %ebp,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [4 x i8] c"%s\0A\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"
@ENOMEM = global i32 0
define i32 @_write_sysfs_string(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
%11 = alloca [64 x i8]
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 %3, i32* %8
store i32 0, i32* %9
%12 = getelementptr inbounds [64 x i8], [64 x i8]* %11, i64 0, i64 0
%13 = load i8*, i8** %6
%14 = load i8*, i8** %5
%15 = call i32 @sprintf(i8* %12, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %13, i8* %14)
%16 = getelementptr inbounds [64 x i8], [64 x i8]* %11, i64 0, i64 0
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %16)
%18 = getelementptr inbounds [64 x i8], [64 x i8]* %11, i64 0, i64 0
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %19, i32** %10
%20 = load i32*, i32** %10
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %27
22: ; preds = %4
%23 = getelementptr inbounds [64 x i8], [64 x i8]* %11, i64 0, i64 0
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %23)
%25 = load i32, i32* @errno
%26 = sub nsw i32 0, %25
store i32 %26, i32* %9
br label %62
27: ; preds = %4
%28 = load i32*, i32** %10
%29 = load i8*, i8** %7
%30 = call i32 @fprintf(i32* %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %29)
%31 = load i32*, i32** %10
%32 = call i32 @fclose(i32* %31)
%33 = load i32, i32* %8
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %61
35: ; preds = %27
%36 = getelementptr inbounds [64 x i8], [64 x i8]* %11, i64 0, i64 0
%37 = call i32* @fopen(i8* %36, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %37, i32** %10
%38 = load i32*, i32** %10
%39 = icmp eq i32* %38, null
br i1 %39, label %40, label %44
40: ; preds = %35
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0))
%42 = load i32, i32* @errno
%43 = sub nsw i32 0, %42
store i32 %43, i32* %9
br label %62
44: ; preds = %35
%45 = load i32*, i32** %10
%46 = getelementptr inbounds [64 x i8], [64 x i8]* %11, i64 0, i64 0
%47 = call i32 @fscanf(i32* %45, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %46)
%48 = load i32*, i32** %10
%49 = call i32 @fclose(i32* %48)
%50 = getelementptr inbounds [64 x i8], [64 x i8]* %11, i64 0, i64 0
%51 = load i8*, i8** %7
%52 = call i64 @strcmp(i8* %50, i8* %51)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %60
54: ; preds = %44
%55 = getelementptr inbounds [64 x i8], [64 x i8]* %11, i64 0, i64 0
%56 = load i8*, i8** %7
%57 = load i8*, i8** %6
%58 = load i8*, i8** %5
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.7, i64 0, i64 0), i8* %55, i8* %56, i8* %57, i8* %58)
store i32 -1, i32* %9
br label %60
60: ; preds = %54, %44
br label %61
61: ; preds = %60, %27
br label %62
62: ; preds = %61, %40, %22
%63 = load i32, i32* %9
ret i32 %63
}

LLVM IR (-O2):
@.str = [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"
@ENOMEM = global i32 0
@str = [30 x i8] c"could not open file to verify\00"
define i32 @_write_sysfs_string(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca [64 x i8]
%6 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%7 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %0)
%8 = call i32 @puts(i8* %6)
%9 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %15
11: ; preds = %4
%12 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %6)
%13 = load i32, i32* @errno
%14 = sub nsw i32 0, %13
br label %33
15: ; preds = %4
%16 = tail call i32 @fputs(i8* %2, i32* %9)
%17 = tail call i32 @fclose(i32* %9)
%18 = icmp eq i32 %3, 0
br i1 %18, label %33, label %19
19: ; preds = %15
%20 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %26
22: ; preds = %19
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
%24 = load i32, i32* @errno
%25 = sub nsw i32 0, %24
br label %33
26: ; preds = %19
%27 = call i32 @fscanf(i32* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %6)
%28 = call i32 @fclose(i32* %20)
%29 = call i64 @strcmp(i8* %6, i8* %2)
%30 = icmp eq i64 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %26
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([69 x i8], [69 x i8]* @.str.7, i64 0, i64 0), i8* %6, i8* %2, i8* %1, i8* %0)
br label %33
33: ; preds = %15, %31, %26, %22, %11
%34 = phi i32 [ %14, %11 ], [ %25, %22 ], [ -1, %31 ], [ 0, %26 ], [ 0, %15 ]
ret i32 %34
}