#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 *, ...);
int /*<<< orphan*/ free(char *);
int fscanf(int /*<<< orphan*/ *, char *, char *);
char *malloc(scalar_t__);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char const *);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char const *);
scalar_t__ strlen(char const *);
int _write_sysfs_string(const char *filename, const char *basedir,
                        const char *val, int verify) {
  int ret = 0, tmp;
  FILE *sysfsfp;
  char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
  if (temp == NULL) {
    fprintf(stderr, "Memory allocation failed\n");
    return -ENOMEM;
  }
  sprintf(temp, "%s/%s", basedir, filename);
  sysfsfp = fopen(temp, "w");
  if (sysfsfp == NULL) {
    fprintf(stderr, "Could not open %s\n", temp);
    ret = -errno;
    goto error_free;
  }
  fprintf(sysfsfp, "%s\n", val);
  fclose(sysfsfp);
  if (verify) {
    sysfsfp = fopen(temp, "r");
    if (sysfsfp == NULL) {
      fprintf(stderr, "could not open file to verify\n");
      ret = -errno;
      goto error_free;
    }
    tmp = fscanf(sysfsfp, "%s", temp);
    if (tmp != 1 || strcmp(temp, val) != 0) {
      fprintf(stderr,
              "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    $0x50,%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,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x48(%rbp),%rdi
add    %rax,%rdi
add    $0x2,%rdi
call   1090 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    120d <_write_sysfs_string+0x7d>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
lea    0xe0d(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
xor    %eax,%eax
mov    0x2ddd(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1365 <_write_sysfs_string+0x1d5>
mov    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xdfa(%rip),%rsi
call   10b0 <sprintf@plt>
mov    -0x40(%rbp),%rdi
lea    0xdf0(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1272 <_write_sysfs_string+0xe2>
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
mov    -0x40(%rbp),%rdx
lea    0xdca(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
xor    %eax,%eax
mov    0x2d68(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x28(%rbp)
jmp    1356 <_write_sysfs_string+0x1c6>
mov    -0x38(%rbp),%rax
mov    %eax,%edi
mov    -0x20(%rbp),%rdx
lea    0xdae(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
cmpl   $0x0,-0x24(%rbp)
je     1351 <_write_sysfs_string+0x1c1>
mov    -0x40(%rbp),%rdi
lea    0xd8d(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    12e6 <_write_sysfs_string+0x156>
mov    0x2d15(%rip),%rax
mov    (%rax),%edi
lea    0xd6b(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
xor    %eax,%eax
mov    0x2cf4(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x28(%rbp)
jmp    1356 <_write_sysfs_string+0x1c6>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rdx
lea    0xd28(%rip),%rsi
call   1060 <fscanf@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x1,-0x2c(%rbp)
jne    131e <_write_sysfs_string+0x18e>
mov    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     134c <_write_sysfs_string+0x1bc>
mov    0x2cb3(%rip),%rax
mov    (%rax),%edi
mov    -0x40(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%r8
mov    -0x10(%rbp),%r9
lea    0xd18(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x28(%rbp)
jmp    1351 <_write_sysfs_string+0x1c1>
jmp    1356 <_write_sysfs_string+0x1c6>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%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,%rbp
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     129d <_write_sysfs_string+0x10d>
mov    %rax,%rbx
lea    0xe40(%rip),%rsi
mov    %rax,%rdi
mov    %r15,%rdx
mov    %r14,%rcx
call   10b0 <sprintf@plt>
lea    0xe31(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     12c1 <_write_sysfs_string+0x131>
mov    %rax,%r12
lea    0xe27(%rip),%rsi
mov    %rbp,%rdx
xor    %ebp,%ebp
mov    %r12d,%edi
mov    %rdx,(%rsp)
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %r12,%rdi
call   1040 <fclose@plt>
test   %r13d,%r13d
je     1301 <_write_sysfs_string+0x171>
lea    0xe00(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     12df <_write_sysfs_string+0x14f>
lea    0xdd0(%rip),%rsi
mov    %rax,%rdi
mov    %rbx,%rdx
call   1060 <fscanf@plt>
cmp    $0x1,%eax
jne    1272 <_write_sysfs_string+0xe2>
mov    %rbx,%rdi
mov    (%rsp),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1301 <_write_sysfs_string+0x171>
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
lea    0xdd4(%rip),%rsi
mov    %rbx,%rdx
mov    (%rsp),%rcx
mov    %r15,%r8
mov    %r14,%r9
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    1301 <_write_sysfs_string+0x171>
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
lea    0xd53(%rip),%rsi
xor    %ebp,%ebp
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    0x2d23(%rip),%rax
sub    (%rax),%ebp
jmp    1309 <_write_sysfs_string+0x179>
mov    0x2d10(%rip),%rax
mov    (%rax),%edi
lea    0xd51(%rip),%rsi
xor    %ebp,%ebp
mov    %rbx,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    12f8 <_write_sysfs_string+0x168>
mov    0x2cf2(%rip),%rax
mov    (%rax),%edi
lea    0xd48(%rip),%rsi
xor    %ebp,%ebp
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    0x2cd1(%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):
@stderr = 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 = [4 x i8] c"%s\0A\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 = [3 x i8] c"%s\00"
@.str.8 = [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 i32*
%13 = 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
%14 = load i8*, i8** %7
%15 = call i64 @strlen(i8* %14)
%16 = load i8*, i8** %6
%17 = call i64 @strlen(i8* %16)
%18 = add nsw i64 %15, %17
%19 = add nsw i64 %18, 2
%20 = call i8* @malloc(i64 %19)
store i8* %20, i8** %13
%21 = load i8*, i8** %13
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %28
23: ; preds = %4
%24 = load i32, i32* @stderr
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%26 = load i32, i32* @ENOMEM
%27 = sub nsw i32 0, %26
store i32 %27, i32* %5
br label %86
28: ; preds = %4
%29 = load i8*, i8** %13
%30 = load i8*, i8** %7
%31 = load i8*, i8** %6
%32 = call i32 @sprintf(i8* %29, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %30, i8* %31)
%33 = load i8*, i8** %13
%34 = call i32* @fopen(i8* %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %34, i32** %12
%35 = load i32*, i32** %12
%36 = icmp eq i32* %35, null
br i1 %36, label %37, label %43
37: ; preds = %28
%38 = load i32, i32* @stderr
%39 = load i8*, i8** %13
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %39)
%41 = load i32, i32* @errno
%42 = sub nsw i32 0, %41
store i32 %42, i32* %10
br label %82
43: ; preds = %28
%44 = load i32*, i32** %12
%45 = ptrtoint i32* %44 to i32
%46 = load i8*, i8** %8
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %46)
%48 = load i32*, i32** %12
%49 = call i32 @fclose(i32* %48)
%50 = load i32, i32* %9
%51 = icmp ne i32 %50, 0
br i1 %51, label %52, label %81
52: ; preds = %43
%53 = load i8*, i8** %13
%54 = call i32* @fopen(i8* %53, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %54, i32** %12
%55 = load i32*, i32** %12
%56 = icmp eq i32* %55, null
br i1 %56, label %57, label %62
57: ; preds = %52
%58 = load i32, i32* @stderr
%59 = call i32 (i32, i8*, ...) @fprintf(i32 %58, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0))
%60 = load i32, i32* @errno
%61 = sub nsw i32 0, %60
store i32 %61, i32* %10
br label %82
62: ; preds = %52
%63 = load i32*, i32** %12
%64 = load i8*, i8** %13
%65 = call i32 @fscanf(i32* %63, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* %64)
store i32 %65, i32* %11
%66 = load i32, i32* %11
%67 = icmp ne i32 %66, 1
br i1 %67, label %73, label %68
68: ; preds = %62
%69 = load i8*, i8** %13
%70 = load i8*, i8** %8
%71 = call i64 @strcmp(i8* %69, i8* %70)
%72 = icmp ne i64 %71, 0
br i1 %72, label %73, label %80
73: ; preds = %68, %62
%74 = load i32, i32* @stderr
%75 = load i8*, i8** %13
%76 = load i8*, i8** %8
%77 = load i8*, i8** %7
%78 = load i8*, i8** %6
%79 = call i32 (i32, i8*, ...) @fprintf(i32 %74, i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.8, i64 0, i64 0), i8* %75, i8* %76, i8* %77, i8* %78)
store i32 -1, i32* %10
br label %80
80: ; preds = %73, %68
br label %81
81: ; preds = %80, %43
br label %82
82: ; preds = %81, %57, %37
%83 = load i8*, i8** %13
%84 = call i32 @free(i8* %83)
%85 = load i32, i32* %10
store i32 %85, i32* %5
br label %86
86: ; preds = %82, %23
%87 = load i32, i32* %5
ret i32 %87
}

LLVM IR (-O2):
@stderr = 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 = [4 x i8] c"%s\0A\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 = [3 x i8] c"%s\00"
@.str.8 = [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* @stderr
%13 = tail call i32 (i32, i8*, ...) @fprintf(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 %51
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* @stderr
%22 = tail call i32 (i32, i8*, ...) @fprintf(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 %48
25: ; preds = %16
%26 = ptrtoint i32* %18 to i64
%27 = trunc i64 %26 to i32
%28 = tail call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %2)
%29 = tail call i32 @fclose(i32* %18)
%30 = icmp eq i32 %3, 0
br i1 %30, label %48, label %31
31: ; preds = %25
%32 = tail call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%33 = icmp eq i32* %32, null
br i1 %33, label %34, label %39
34: ; preds = %31
%35 = load i32, i32* @stderr
%36 = tail call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0))
%37 = load i32, i32* @errno
%38 = sub nsw i32 0, %37
br label %48
39: ; preds = %31
%40 = tail call i32 @fscanf(i32* %32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* %9)
%41 = icmp eq i32 %40, 1
br i1 %41, label %42, label %45
42: ; preds = %39
%43 = tail call i64 @strcmp(i8* %9, i8* %2)
%44 = icmp eq i64 %43, 0
br i1 %44, label %48, label %45
45: ; preds = %42, %39
%46 = load i32, i32* @stderr
%47 = tail call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.8, i64 0, i64 0), i8* %9, i8* %2, i8* %1, i8* %0)
br label %48
48: ; preds = %25, %45, %42, %34, %20
%49 = phi i32 [ %24, %20 ], [ %38, %34 ], [ -1, %45 ], [ 0, %42 ], [ 0, %25 ]
%50 = tail call i32 @free(i8* %9)
br label %51
51: ; preds = %48, %11
%52 = phi i32 [ %15, %11 ], [ %49, %48 ]
ret i32 %52
}