#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);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *);
char *malloc(scalar_t__);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
scalar_t__ strlen(char *);
int _write_sysfs_int(char *filename, char *basedir, int val, int verify) {
  int ret;
  FILE *sysfsfp;
  int test;
  char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
  if (temp == NULL)
    return -ENOMEM;
  sprintf(temp, "%s/%s", basedir, filename);
  sysfsfp = fopen(temp, "w");
  if (sysfsfp == NULL) {
    printf("failed to open %s\n", temp);
    ret = -errno;
    goto error_free;
  }
  fprintf(sysfsfp, "%d", val);
  fclose(sysfsfp);
  if (verify) {
    sysfsfp = fopen(temp, "r");
    if (sysfsfp == NULL) {
      printf("failed to open %s\n", temp);
      ret = -errno;
      goto error_free;
    }
    fscanf(sysfsfp, "%d", &test);
    fclose(sysfsfp);
    if (test != val) {
      printf("Possible failure in int write %d to %s%s\n", val, basedir,
             filename);
      ret = -1;
    }
  }
error_free:
  free(temp);
  return ret;
}


x86 assembly (clang, -O0):
<_write_sysfs_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%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    11ee <_write_sysfs_int+0x5e>
xor    %eax,%eax
mov    0x2dfc(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1316 <_write_sysfs_int+0x186>
mov    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xdff(%rip),%rsi
call   10b0 <sprintf@plt>
mov    -0x40(%rbp),%rdi
lea    0xdf5(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    124a <_write_sysfs_int+0xba>
mov    -0x40(%rbp),%rsi
lea    0xdd8(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %eax,%eax
mov    0x2d98(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x24(%rbp)
jmp    1307 <_write_sysfs_int+0x177>
mov    -0x30(%rbp),%rdi
mov    -0x1c(%rbp),%edx
lea    0xdc3(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
cmpl   $0x0,-0x20(%rbp)
je     1302 <_write_sysfs_int+0x172>
mov    -0x40(%rbp),%rdi
lea    0xda3(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12b4 <_write_sysfs_int+0x124>
mov    -0x40(%rbp),%rsi
lea    0xd6e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %eax,%eax
mov    0x2d2e(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x24(%rbp)
jmp    1307 <_write_sysfs_int+0x177>
mov    -0x30(%rbp),%rdi
lea    0xd5c(%rip),%rsi
lea    -0x34(%rbp),%rdx
call   1070 <fscanf@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x34(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     12fd <_write_sysfs_int+0x16d>
mov    -0x1c(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xd31(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x24(%rbp)
jmp    1302 <_write_sysfs_int+0x172>
jmp    1307 <_write_sysfs_int+0x177>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_write_sysfs_int>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r13d
mov    %edx,%r12d
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     127e <_write_sysfs_int+0xee>
mov    %rax,%rbx
lea    0xe26(%rip),%rsi
mov    %rax,%rdi
mov    %r15,%rdx
mov    %r14,%rcx
call   10b0 <sprintf@plt>
lea    0xe17(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     128b <_write_sysfs_int+0xfb>
mov    %rax,%rbp
lea    0xe11(%rip),%rsi
mov    %rax,%rdi
mov    %r12d,%edx
call   1080 <fprintf@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
test   %r13d,%r13d
je     12a9 <_write_sysfs_int+0x119>
lea    0xdf1(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     128b <_write_sysfs_int+0xfb>
mov    %rax,%rbp
lea    0xdd7(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rax,%rdi
call   1070 <fscanf@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
cmp    %r12d,0x4(%rsp)
je     12a9 <_write_sysfs_int+0x119>
lea    0xdb9(%rip),%rdi
mov    %r12d,%esi
mov    %r15,%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%ebp
jmp    12a9 <_write_sysfs_int+0x119>
mov    0x2d5b(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
jmp    12b1 <_write_sysfs_int+0x121>
lea    0xd76(%rip),%rdi
xor    %ebp,%ebp
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2d33(%rip),%rax
sub    (%rax),%ebp
jmp    12a9 <_write_sysfs_int+0x119>
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):
@ENOMEM = global i32 0
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [19 x i8] c"failed to open %s\0A\00"
@errno = global i32 0
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [42 x i8] c"Possible failure in int write %d to %s%s\0A\00"
define i32 @_write_sysfs_int(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%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 i32 %2, i32* %8
store i32 %3, i32* %9
%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 %26
23: ; preds = %4
%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** %13
%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, i64 0, i64 0), i8* %28, i8* %29)
%31 = load i8*, i8** %13
%32 = call i32* @fopen(i8* %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, 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** %13
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, 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 i32, i32* %8
%43 = call i32 @fprintf(i32* %41, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %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** %13
%50 = call i32* @fopen(i8* %49, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, 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 %58
53: ; preds = %48
%54 = load i8*, i8** %13
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %54)
%56 = load i32, i32* @errno
%57 = sub nsw i32 0, %56
store i32 %57, i32* %10
br label %73
58: ; preds = %48
%59 = load i32*, i32** %11
%60 = call i32 @fscanf(i32* %59, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %12)
%61 = load i32*, i32** %11
%62 = call i32 @fclose(i32* %61)
%63 = load i32, i32* %12
%64 = load i32, i32* %8
%65 = icmp ne i32 %63, %64
br i1 %65, label %66, label %71
66: ; preds = %58
%67 = load i32, i32* %8
%68 = load i8*, i8** %7
%69 = load i8*, i8** %6
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.5, i64 0, i64 0), i32 %67, i8* %68, i8* %69)
store i32 -1, i32* %10
br label %71
71: ; preds = %66, %58
br label %72
72: ; preds = %71, %40
br label %73
73: ; preds = %72, %53, %35
%74 = load i8*, i8** %13
%75 = call i32 @free(i8* %74)
%76 = load i32, i32* %10
store i32 %76, i32* %5
br label %77
77: ; preds = %73, %23
%78 = load i32, i32* %5
ret i32 %78
}

LLVM IR (-O2):
@ENOMEM = global i32 0
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [19 x i8] c"failed to open %s\0A\00"
@errno = global i32 0
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [42 x i8] c"Possible failure in int write %d to %s%s\0A\00"
define i32 @_write_sysfs_int(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
%7 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%8 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%9 = add i64 %7, 2
%10 = add i64 %9, %8
%11 = tail call i8* @malloc(i64 %10)
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %16
13: ; preds = %4
%14 = load i32, i32* @ENOMEM
%15 = sub nsw i32 0, %14
br label %45
16: ; preds = %4
%17 = tail call i32 @sprintf(i8* %11, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %0)
%18 = tail call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %24
20: ; preds = %16
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %11)
%22 = load i32, i32* @errno
%23 = sub nsw i32 0, %22
br label %42
24: ; preds = %16
%25 = tail call i32 @fprintf(i32* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %2)
%26 = tail call i32 @fclose(i32* %18)
%27 = icmp eq i32 %3, 0
br i1 %27, label %42, label %28
28: ; preds = %24
%29 = tail call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%30 = icmp eq i32* %29, null
br i1 %30, label %31, label %35
31: ; preds = %28
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %11)
%33 = load i32, i32* @errno
%34 = sub nsw i32 0, %33
br label %42
35: ; preds = %28
%36 = call i32 @fscanf(i32* %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %5)
%37 = call i32 @fclose(i32* %29)
%38 = load i32, i32* %5
%39 = icmp eq i32 %38, %2
br i1 %39, label %42, label %40
40: ; preds = %35
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.5, i64 0, i64 0), i32 %2, i8* %1, i8* %0)
br label %42
42: ; preds = %24, %40, %35, %31, %20
%43 = phi i32 [ %23, %20 ], [ %34, %31 ], [ -1, %40 ], [ undef, %35 ], [ undef, %24 ]
%44 = call i32 @free(i8* %11)
br label %45
45: ; preds = %42, %13
%46 = phi i32 [ %15, %13 ], [ %43, %42 ]
ret i32 %46
}