#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 /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char const *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, char *, ...);
void write_battery_prop(int battery_id, const char *property,
                        const char *value) {
  int buflen = 256;
  char *buf = malloc(buflen);
  if (battery_id < 0)
    sprintf(buf, "%s/%s", "/sys/devices/platform/smapi", property);
  else
    sprintf(buf, "%s/BAT%d/%s", "/sys/devices/platform/smapi", battery_id,
            property);
  FILE *f = fopen(buf, "w");
  if (f != NULL) {
    fprintf(f, "%s\n", value);
    fclose(f);
  }
  free(buf);
}


x86 assembly (clang, -O0):
<write_battery_prop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x100,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    11b2 <write_battery_prop+0x52>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rcx
lea    0xe61(%rip),%rsi
lea    0xe60(%rip),%rdx
mov    $0x0,%al
call   1080 <sprintf@plt>
jmp    11d2 <write_battery_prop+0x72>
mov    -0x28(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    -0x10(%rbp),%r8
lea    0xe5e(%rip),%rsi
lea    0xe3b(%rip),%rdx
mov    $0x0,%al
call   1080 <sprintf@plt>
mov    -0x28(%rbp),%rdi
lea    0xe51(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     120e <write_battery_prop+0xae>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe30(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_battery_prop>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebp
mov    $0x100,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
test   %ebp,%ebp
js     119f <write_battery_prop+0x3f>
lea    0xe9b(%rip),%rsi
lea    0xe78(%rip),%rdx
mov    %rbx,%rdi
mov    %ebp,%ecx
mov    %r15,%r8
xor    %eax,%eax
call   1080 <sprintf@plt>
jmp    11ba <write_battery_prop+0x5a>
lea    0xe5a(%rip),%rsi
lea    0xe59(%rip),%rdx
mov    %rbx,%rdi
mov    %r15,%rcx
xor    %eax,%eax
call   1080 <sprintf@plt>
lea    0xe6d(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11eb <write_battery_prop+0x8b>
mov    %rax,%rbp
lea    0xe58(%rip),%rsi
mov    %rax,%rdi
mov    %r14,%rdx
call   1050 <fprintf@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [28 x i8] c"/sys/devices/platform/smapi\00"
@.str.2 = [12 x i8] c"%s/BAT%d/%s\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [4 x i8] c"%s\0A\00"
define void @write_battery_prop(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32*
store i32 %0, i32* %4
store i8* %1, i8** %5
store i8* %2, i8** %6
store i32 256, i32* %7
%10 = load i32, i32* %7
%11 = call i8* @malloc(i32 %10)
store i8* %11, i8** %8
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %3
%15 = load i8*, i8** %8
%16 = load i8*, i8** %5
%17 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %15, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %16)
br label %23
18: ; preds = %3
%19 = load i8*, i8** %8
%20 = load i32, i32* %4
%21 = load i8*, i8** %5
%22 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %19, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %20, i8* %21)
br label %23
23: ; preds = %18, %14
%24 = load i8*, i8** %8
%25 = call i32* @fopen(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %25, i32** %9
%26 = load i32*, i32** %9
%27 = icmp ne i32* %26, null
br i1 %27, label %28, label %34
28: ; preds = %23
%29 = load i32*, i32** %9
%30 = load i8*, i8** %6
%31 = call i32 @fprintf(i32* %29, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %30)
%32 = load i32*, i32** %9
%33 = call i32 @fclose(i32* %32)
br label %34
34: ; preds = %28, %23
%35 = load i8*, i8** %8
%36 = call i32 @free(i8* %35)
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [28 x i8] c"/sys/devices/platform/smapi\00"
@.str.2 = [12 x i8] c"%s/BAT%d/%s\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [4 x i8] c"%s\0A\00"
define void @write_battery_prop(i32 %0, i8* %1, i8* %2) {
%4 = tail call dereferenceable_or_null(256) i8* @malloc(i32 256)
%5 = icmp slt i32 %0, 0
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 (i8*, i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %4, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %10
8: ; preds = %3
%9 = tail call i32 (i8*, i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %4, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %0, i8* %1)
br label %10
10: ; preds = %8, %6
%11 = tail call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %16, label %13
13: ; preds = %10
%14 = tail call i32 @fprintf(i32* %11, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %2)
%15 = tail call i32 @fclose(i32* %11)
br label %16
16: ; preds = %13, %10
%17 = tail call i32 @free(i8* %4)
ret void
}