#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 *, int *);
char *malloc(scalar_t__);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char const *);
int /*<<< orphan*/ stderr;
scalar_t__ strlen(char const *);
int _write_sysfs_int(const char *filename, const char *basedir, int val,
                     int verify, int type, int val2) {
  int ret = 0, tmp;
  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) {
    fprintf(stderr, "failed to open %s\n", temp);
    ret = -errno;
    goto error_free;
  }
  if (type)
    fprintf(sysfsfp, "%d %d", val, val2);
  else
    fprintf(sysfsfp, "%d", val);
  fclose(sysfsfp);
  if (verify) {
    sysfsfp = fopen(temp, "r");
    if (sysfsfp == NULL) {
      fprintf(stderr, "failed to open %s\n", temp);
      ret = -errno;
      goto error_free;
    }
    tmp = fscanf(sysfsfp, "%d", &test);
    if (tmp != 1 || test != val) {
      fprintf(stderr, "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    %r8d,-0x24(%rbp)
mov    %r9d,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x50(%rbp),%rdi
add    %rax,%rdi
add    $0x2,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    11ed <_write_sysfs_int+0x6d>
xor    %eax,%eax
mov    0x2dfd(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1361 <_write_sysfs_int+0x1e1>
mov    -0x48(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe00(%rip),%rsi
call   10a0 <sprintf@plt>
mov    -0x48(%rbp),%rdi
lea    0xdf6(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1252 <_write_sysfs_int+0xd2>
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
mov    -0x48(%rbp),%rdx
lea    0xdd0(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
xor    %eax,%eax
mov    0x2d88(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x2c(%rbp)
jmp    1352 <_write_sysfs_int+0x1d2>
cmpl   $0x0,-0x24(%rbp)
je     127b <_write_sysfs_int+0xfb>
mov    -0x38(%rbp),%rax
mov    %eax,%edi
mov    -0x1c(%rbp),%edx
mov    -0x28(%rbp),%ecx
lea    0xdac(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1292 <_write_sysfs_int+0x112>
mov    -0x38(%rbp),%rax
mov    %eax,%edi
mov    -0x1c(%rbp),%edx
lea    0xd93(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
cmpl   $0x0,-0x20(%rbp)
je     134d <_write_sysfs_int+0x1cd>
mov    -0x48(%rbp),%rdi
lea    0xd71(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    12f2 <_write_sysfs_int+0x172>
mov    0x2d0d(%rip),%rax
mov    (%rax),%edi
mov    -0x48(%rbp),%rdx
lea    0xd30(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
xor    %eax,%eax
mov    0x2ce8(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x2c(%rbp)
jmp    1352 <_write_sysfs_int+0x1d2>
mov    -0x38(%rbp),%rdi
lea    0xd21(%rip),%rsi
lea    -0x3c(%rbp),%rdx
call   1060 <fscanf@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x1,-0x30(%rbp)
jne    131f <_write_sysfs_int+0x19f>
mov    -0x3c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     1348 <_write_sysfs_int+0x1c8>
mov    0x2cb2(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%r8
lea    0xce9(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x2c(%rbp)
jmp    134d <_write_sysfs_int+0x1cd>
jmp    1352 <_write_sysfs_int+0x1d2>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%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
sub    $0x18,%rsp
mov    %r9d,%r13d
mov    %r8d,%r12d
mov    %ecx,%ebp
mov    %edx,0x8(%rsp)
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   1080 <malloc@plt>
test   %rax,%rax
je     1225 <_write_sysfs_int+0xa5>
mov    %rax,%rbx
mov    %r13d,0xc(%rsp)
mov    %ebp,0x10(%rsp)
lea    0xe28(%rip),%rsi
mov    %rax,%rdi
mov    %r15,%rdx
mov    %r14,%rcx
call   10a0 <sprintf@plt>
lea    0xe19(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     12be <_write_sysfs_int+0x13e>
mov    %rax,%r13
test   %r12d,%r12d
je     1235 <_write_sysfs_int+0xb5>
lea    0xe0e(%rip),%rsi
mov    %r13d,%edi
mov    0x8(%rsp),%r12d
mov    %r12d,%edx
mov    0xc(%rsp),%ecx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    124e <_write_sysfs_int+0xce>
mov    0x2db4(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
jmp    12eb <_write_sysfs_int+0x16b>
lea    0xde2(%rip),%rsi
mov    %r13d,%edi
mov    0x8(%rsp),%r12d
mov    %r12d,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r13,%rdi
call   1040 <fclose@plt>
xor    %ebp,%ebp
cmpl   $0x0,0x10(%rsp)
je     12e3 <_write_sysfs_int+0x163>
lea    0xdb7(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     12be <_write_sysfs_int+0x13e>
lea    0xda0(%rip),%rsi
lea    0x14(%rsp),%rdx
mov    %rax,%rdi
call   1060 <fscanf@plt>
cmp    $0x1,%eax
jne    1297 <_write_sysfs_int+0x117>
cmp    %r12d,0x14(%rsp)
je     12e3 <_write_sysfs_int+0x163>
mov    0x2d3a(%rip),%rax
mov    (%rax),%edi
lea    0xd7c(%rip),%rsi
mov    %r12d,%edx
mov    %r15,%rcx
mov    %r14,%r8
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    12e3 <_write_sysfs_int+0x163>
mov    0x2d13(%rip),%rax
mov    (%rax),%edi
lea    0xd3a(%rip),%rsi
xor    %ebp,%ebp
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x2cef(%rip),%rax
sub    (%rax),%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x18,%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"
@stderr = global i32 0
@.str.2 = [19 x i8] c"failed to open %s\0A\00"
@errno = global i32 0
@.str.3 = [6 x i8] c"%d %d\00"
@.str.4 = [3 x i8] c"%d\00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [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, i32 %4, i32 %5) {
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32*
%17 = alloca i32
%18 = alloca i8*
store i8* %0, i8** %8
store i8* %1, i8** %9
store i32 %2, i32* %10
store i32 %3, i32* %11
store i32 %4, i32* %12
store i32 %5, i32* %13
store i32 0, i32* %14
%19 = load i8*, i8** %9
%20 = call i64 @strlen(i8* %19)
%21 = load i8*, i8** %8
%22 = call i64 @strlen(i8* %21)
%23 = add nsw i64 %20, %22
%24 = add nsw i64 %23, 2
%25 = call i8* @malloc(i64 %24)
store i8* %25, i8** %18
%26 = load i8*, i8** %18
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %31
28: ; preds = %6
%29 = load i32, i32* @ENOMEM
%30 = sub nsw i32 0, %29
store i32 %30, i32* %7
br label %97
31: ; preds = %6
%32 = load i8*, i8** %18
%33 = load i8*, i8** %9
%34 = load i8*, i8** %8
%35 = call i32 @sprintf(i8* %32, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %33, i8* %34)
%36 = load i8*, i8** %18
%37 = call i32* @fopen(i8* %36, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %37, i32** %16
%38 = load i32*, i32** %16
%39 = icmp eq i32* %38, null
br i1 %39, label %40, label %46
40: ; preds = %31
%41 = load i32, i32* @stderr
%42 = load i8*, i8** %18
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %42)
%44 = load i32, i32* @errno
%45 = sub nsw i32 0, %44
store i32 %45, i32* %14
br label %93
46: ; preds = %31
%47 = load i32, i32* %12
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %55
49: ; preds = %46
%50 = load i32*, i32** %16
%51 = ptrtoint i32* %50 to i32
%52 = load i32, i32* %10
%53 = load i32, i32* %13
%54 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %52, i32 %53)
br label %60
55: ; preds = %46
%56 = load i32*, i32** %16
%57 = ptrtoint i32* %56 to i32
%58 = load i32, i32* %10
%59 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %58)
br label %60
60: ; preds = %55, %49
%61 = load i32*, i32** %16
%62 = call i32 @fclose(i32* %61)
%63 = load i32, i32* %11
%64 = icmp ne i32 %63, 0
br i1 %64, label %65, label %92
65: ; preds = %60
%66 = load i8*, i8** %18
%67 = call i32* @fopen(i8* %66, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %67, i32** %16
%68 = load i32*, i32** %16
%69 = icmp eq i32* %68, null
br i1 %69, label %70, label %76
70: ; preds = %65
%71 = load i32, i32* @stderr
%72 = load i8*, i8** %18
%73 = call i32 (i32, i8*, ...) @fprintf(i32 %71, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %72)
%74 = load i32, i32* @errno
%75 = sub nsw i32 0, %74
store i32 %75, i32* %14
br label %93
76: ; preds = %65
%77 = load i32*, i32** %16
%78 = call i32 @fscanf(i32* %77, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %17)
store i32 %78, i32* %15
%79 = load i32, i32* %15
%80 = icmp ne i32 %79, 1
br i1 %80, label %85, label %81
81: ; preds = %76
%82 = load i32, i32* %17
%83 = load i32, i32* %10
%84 = icmp ne i32 %82, %83
br i1 %84, label %85, label %91
85: ; preds = %81, %76
%86 = load i32, i32* @stderr
%87 = load i32, i32* %10
%88 = load i8*, i8** %9
%89 = load i8*, i8** %8
%90 = call i32 (i32, i8*, ...) @fprintf(i32 %86, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.6, i64 0, i64 0), i32 %87, i8* %88, i8* %89)
store i32 -1, i32* %14
br label %91
91: ; preds = %85, %81
br label %92
92: ; preds = %91, %60
br label %93
93: ; preds = %92, %70, %40
%94 = load i8*, i8** %18
%95 = call i32 @free(i8* %94)
%96 = load i32, i32* %14
store i32 %96, i32* %7
br label %97
97: ; preds = %93, %28
%98 = load i32, i32* %7
ret i32 %98
}

LLVM IR (-O2):
@ENOMEM = global i32 0
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.2 = [19 x i8] c"failed to open %s\0A\00"
@errno = global i32 0
@.str.3 = [6 x i8] c"%d %d\00"
@.str.4 = [3 x i8] c"%d\00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [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, i32 %4, i32 %5) {
%7 = alloca i32
%8 = bitcast i32* %7 to i8*
%9 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%10 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%11 = add i64 %9, 2
%12 = add i64 %11, %10
%13 = tail call i8* @malloc(i64 %12)
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %18
15: ; preds = %6
%16 = load i32, i32* @ENOMEM
%17 = sub nsw i32 0, %16
br label %58
18: ; preds = %6
%19 = tail call i32 @sprintf(i8* %13, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %0)
%20 = tail call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %27
22: ; preds = %18
%23 = load i32, i32* @stderr
%24 = tail call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %13)
%25 = load i32, i32* @errno
%26 = sub nsw i32 0, %25
br label %55
27: ; preds = %18
%28 = icmp eq i32 %4, 0
%29 = ptrtoint i32* %20 to i64
%30 = trunc i64 %29 to i32
br i1 %28, label %33, label %31
31: ; preds = %27
%32 = tail call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %2, i32 %5)
br label %35
33: ; preds = %27
%34 = tail call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %2)
br label %35
35: ; preds = %33, %31
%36 = tail call i32 @fclose(i32* %20)
%37 = icmp eq i32 %3, 0
br i1 %37, label %55, label %38
38: ; preds = %35
%39 = tail call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%40 = icmp eq i32* %39, null
br i1 %40, label %41, label %46
41: ; preds = %38
%42 = load i32, i32* @stderr
%43 = tail call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %13)
%44 = load i32, i32* @errno
%45 = sub nsw i32 0, %44
br label %55
46: ; preds = %38
%47 = call i32 @fscanf(i32* %39, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %7)
%48 = icmp eq i32 %47, 1
%49 = load i32, i32* %7
%50 = icmp eq i32 %49, %2
%51 = select i1 %48, i1 %50, i1 false
br i1 %51, label %55, label %52
52: ; preds = %46
%53 = load i32, i32* @stderr
%54 = call i32 (i32, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.6, i64 0, i64 0), i32 %2, i8* %1, i8* %0)
br label %55
55: ; preds = %46, %35, %52, %41, %22
%56 = phi i32 [ %26, %22 ], [ %45, %41 ], [ -1, %52 ], [ 0, %35 ], [ 0, %46 ]
%57 = call i32 @free(i8* %13)
br label %58
58: ; preds = %55, %15
%59 = phi i32 [ %17, %15 ], [ %56, %55 ]
ret i32 %59
}