#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 /*<<< orphan*/ LOG_ERR;
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*/ sprintf(char *, char *, char *, char *);
scalar_t__ strlen(char *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, ...);
int _write_sysfs_int(char *filename, char *basedir, int val, int verify,
                     int type, int val2) {
  int ret = 0;
  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) {
    syslog(LOG_ERR, "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) {
      syslog(LOG_ERR, "failed to open %s\n", temp);
      ret = -errno;
      goto error_free;
    }
    fscanf(sysfsfp, "%d", &test);
    if (test != val) {
      syslog(LOG_ERR, "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    11fd <_write_sysfs_int+0x6d>
xor    %eax,%eax
mov    0x2ded(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1360 <_write_sysfs_int+0x1d0>
mov    -0x48(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xdf0(%rip),%rsi
call   10b0 <sprintf@plt>
mov    -0x48(%rbp),%rdi
lea    0xde6(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1262 <_write_sysfs_int+0xd2>
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
mov    -0x48(%rbp),%rdx
lea    0xdc0(%rip),%rsi
mov    $0x0,%al
call   1090 <syslog@plt>
xor    %eax,%eax
mov    0x2d80(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x2c(%rbp)
jmp    1351 <_write_sysfs_int+0x1c1>
cmpl   $0x0,-0x24(%rbp)
je     1289 <_write_sysfs_int+0xf9>
mov    -0x38(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x28(%rbp),%ecx
lea    0xd9e(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    129e <_write_sysfs_int+0x10e>
mov    -0x38(%rbp),%rdi
mov    -0x1c(%rbp),%edx
lea    0xd87(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
cmpl   $0x0,-0x20(%rbp)
je     134c <_write_sysfs_int+0x1bc>
mov    -0x48(%rbp),%rdi
lea    0xd65(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    12fe <_write_sysfs_int+0x16e>
mov    0x2cf1(%rip),%rax
mov    (%rax),%edi
mov    -0x48(%rbp),%rdx
lea    0xd24(%rip),%rsi
mov    $0x0,%al
call   1090 <syslog@plt>
xor    %eax,%eax
mov    0x2ce4(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x2c(%rbp)
jmp    1351 <_write_sysfs_int+0x1c1>
mov    -0x38(%rbp),%rdi
lea    0xd15(%rip),%rsi
lea    -0x3c(%rbp),%rdx
call   1060 <fscanf@plt>
mov    -0x3c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     1347 <_write_sysfs_int+0x1b7>
mov    0x2ca3(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%r8
lea    0xcea(%rip),%rsi
mov    $0x0,%al
call   1090 <syslog@plt>
movl   $0xffffffff,-0x2c(%rbp)
jmp    134c <_write_sysfs_int+0x1bc>
jmp    1351 <_write_sysfs_int+0x1c1>
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     1235 <_write_sysfs_int+0xa5>
mov    %rax,%rbx
mov    %r13d,0xc(%rsp)
mov    %ebp,0x10(%rsp)
lea    0xe18(%rip),%rsi
mov    %rax,%rdi
mov    %r15,%rdx
mov    %r14,%rcx
call   10b0 <sprintf@plt>
lea    0xe09(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     12c5 <_write_sysfs_int+0x135>
mov    %rax,%r13
test   %r12d,%r12d
je     1245 <_write_sysfs_int+0xb5>
lea    0xdfe(%rip),%rsi
mov    %r13,%rdi
mov    0x8(%rsp),%r12d
mov    %r12d,%edx
mov    0xc(%rsp),%ecx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    125e <_write_sysfs_int+0xce>
mov    0x2da4(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
jmp    12f2 <_write_sysfs_int+0x162>
lea    0xdd2(%rip),%rsi
mov    %r13,%rdi
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     12ea <_write_sysfs_int+0x15a>
lea    0xdab(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     12c5 <_write_sysfs_int+0x135>
lea    0xd94(%rip),%rsi
lea    0x14(%rsp),%rdx
mov    %rax,%rdi
call   1060 <fscanf@plt>
cmp    %r12d,0x14(%rsp)
je     12ea <_write_sysfs_int+0x15a>
mov    0x2d23(%rip),%rax
mov    (%rax),%edi
lea    0xd75(%rip),%rsi
mov    %r12d,%edx
mov    %r15,%rcx
mov    %r14,%r8
xor    %eax,%eax
call   1090 <syslog@plt>
mov    $0xffffffff,%ebp
jmp    12ea <_write_sysfs_int+0x15a>
mov    0x2cfc(%rip),%rax
mov    (%rax),%edi
lea    0xd33(%rip),%rsi
xor    %ebp,%ebp
mov    %rbx,%rdx
xor    %eax,%eax
call   1090 <syslog@plt>
mov    0x2cf0(%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"
@LOG_ERR = 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 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
%18 = load i8*, i8** %9
%19 = call i64 @strlen(i8* %18)
%20 = load i8*, i8** %8
%21 = call i64 @strlen(i8* %20)
%22 = add nsw i64 %19, %21
%23 = add nsw i64 %22, 2
%24 = call i8* @malloc(i64 %23)
store i8* %24, i8** %17
%25 = load i8*, i8** %17
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %30
27: ; preds = %6
%28 = load i32, i32* @ENOMEM
%29 = sub nsw i32 0, %28
store i32 %29, i32* %7
br label %91
30: ; preds = %6
%31 = load i8*, i8** %17
%32 = load i8*, i8** %9
%33 = load i8*, i8** %8
%34 = call i32 @sprintf(i8* %31, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %32, i8* %33)
%35 = load i8*, i8** %17
%36 = call i32* @fopen(i8* %35, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %36, i32** %15
%37 = load i32*, i32** %15
%38 = icmp eq i32* %37, null
br i1 %38, label %39, label %45
39: ; preds = %30
%40 = load i32, i32* @LOG_ERR
%41 = load i8*, i8** %17
%42 = call i32 (i32, i8*, ...) @syslog(i32 %40, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %41)
%43 = load i32, i32* @errno
%44 = sub nsw i32 0, %43
store i32 %44, i32* %14
br label %87
45: ; preds = %30
%46 = load i32, i32* %12
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %53
48: ; preds = %45
%49 = load i32*, i32** %15
%50 = load i32, i32* %10
%51 = load i32, i32* %13
%52 = call i32 (i32*, i8*, i32, ...) @fprintf(i32* %49, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %50, i32 %51)
br label %57
53: ; preds = %45
%54 = load i32*, i32** %15
%55 = load i32, i32* %10
%56 = call i32 (i32*, i8*, i32, ...) @fprintf(i32* %54, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %55)
br label %57
57: ; preds = %53, %48
%58 = load i32*, i32** %15
%59 = call i32 @fclose(i32* %58)
%60 = load i32, i32* %11
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %86
62: ; preds = %57
%63 = load i8*, i8** %17
%64 = call i32* @fopen(i8* %63, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %64, i32** %15
%65 = load i32*, i32** %15
%66 = icmp eq i32* %65, null
br i1 %66, label %67, label %73
67: ; preds = %62
%68 = load i32, i32* @LOG_ERR
%69 = load i8*, i8** %17
%70 = call i32 (i32, i8*, ...) @syslog(i32 %68, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %69)
%71 = load i32, i32* @errno
%72 = sub nsw i32 0, %71
store i32 %72, i32* %14
br label %87
73: ; preds = %62
%74 = load i32*, i32** %15
%75 = call i32 @fscanf(i32* %74, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %16)
%76 = load i32, i32* %16
%77 = load i32, i32* %10
%78 = icmp ne i32 %76, %77
br i1 %78, label %79, label %85
79: ; preds = %73
%80 = load i32, i32* @LOG_ERR
%81 = load i32, i32* %10
%82 = load i8*, i8** %9
%83 = load i8*, i8** %8
%84 = call i32 (i32, i8*, ...) @syslog(i32 %80, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.6, i64 0, i64 0), i32 %81, i8* %82, i8* %83)
store i32 -1, i32* %14
br label %85
85: ; preds = %79, %73
br label %86
86: ; preds = %85, %57
br label %87
87: ; preds = %86, %67, %39
%88 = load i8*, i8** %17
%89 = call i32 @free(i8* %88)
%90 = load i32, i32* %14
store i32 %90, i32* %7
br label %91
91: ; preds = %87, %27
%92 = load i32, i32* %7
ret i32 %92
}

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