#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 uint64_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *);
int /*<<< orphan*/ stderr;
float getpower() {
  FILE *fd;
  int voltage_now, current_now;
  fd = fopen("/sys/class/power_supply/BAT0/voltage_now", "r");
  if (fd == NULL) {
    fprintf(stderr, "Error opening voltage_now.\n");
    return -1;
  }
  fscanf(fd, "%d", &voltage_now);
  fclose(fd);
  fd = fopen("/sys/class/power_supply/BAT0/current_now", "r");
  if (fd == NULL) {
    fprintf(stderr, "Error opening current_now.\n");
    return -1;
  }
  fscanf(fd, "%d", &current_now);
  fclose(fd);
  uint64_t pwr = voltage_now;
  pwr *= current_now;
  pwr /= 1000000000;
  return (float)pwr / 1000.0;
}


x86 assembly (clang, -O0):
<getpower>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xebd(%rip),%rdi
lea    0xedf(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1191 <getpower+0x51>
mov    0x2e6f(%rip),%rax
mov    (%rax),%edi
lea    0xebd(%rip),%rsi
call   1050 <fprintf@plt>
movss  0xe81(%rip),%xmm0
movss  %xmm0,-0x4(%rbp)
jmp    1250 <getpower+0x110>
mov    -0x10(%rbp),%rdi
lea    0xeb7(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1040 <fscanf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xea1(%rip),%rdi
lea    0xe79(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11f7 <getpower+0xb7>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
lea    0xe9f(%rip),%rsi
call   1050 <fprintf@plt>
movss  0xe1b(%rip),%xmm0
movss  %xmm0,-0x4(%rbp)
jmp    1250 <getpower+0x110>
mov    -0x10(%rbp),%rdi
lea    0xe51(%rip),%rsi
lea    -0x18(%rbp),%rdx
call   1040 <fscanf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
imul   -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x3b9aca00,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1c(%rbp)
cvtsi2ssl -0x1c(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xdbd(%rip),%xmm1
divsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x4(%rbp)
movss  -0x4(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getpower>:
push   %rbx
sub    $0x10,%rsp
lea    0xebc(%rip),%rdi
lea    0xede(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11e4 <getpower+0xa4>
mov    %rax,%rbx
lea    0xee4(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
lea    0xecb(%rip),%rdi
lea    0xea3(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11f6 <getpower+0xb6>
mov    %rax,%rbx
lea    0xead(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x8(%rsp),%eax
imul   0xc(%rsp),%eax
cltq
imul   $0x44b82fa1,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x3c,%rax
add    %ecx,%eax
cvtsi2ss %eax,%xmm0
divss  0xe1e(%rip),%xmm0
jmp    1213 <getpower+0xd3>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
lea    0xe3f(%rip),%rsi
jmp    1206 <getpower+0xc6>
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
lea    0xe75(%rip),%rsi
call   1050 <fprintf@plt>
movss  0xdf1(%rip),%xmm0
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [41 x i8] c"/sys/class/power_supply/BAT0/voltage_now\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [28 x i8] c"Error opening voltage_now.\0A\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [41 x i8] c"/sys/class/power_supply/BAT0/current_now\00"
@.str.5 = [28 x i8] c"Error opening current_now.\0A\00"
define float @getpower() {
%1 = alloca float
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = call i32* @fopen(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %2
%7 = load i32*, i32** %2
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = load i32, i32* @stderr
%11 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
store float -1.000000e+00, float* %1
br label %39
12: ; preds = %0
%13 = load i32*, i32** %2
%14 = call i32 @fscanf(i32* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %3)
%15 = load i32*, i32** %2
%16 = call i32 @fclose(i32* %15)
%17 = call i32* @fopen(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %17, i32** %2
%18 = load i32*, i32** %2
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %23
20: ; preds = %12
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0))
store float -1.000000e+00, float* %1
br label %39
23: ; preds = %12
%24 = load i32*, i32** %2
%25 = call i32 @fscanf(i32* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %4)
%26 = load i32*, i32** %2
%27 = call i32 @fclose(i32* %26)
%28 = load i32, i32* %3
store i32 %28, i32* %5
%29 = load i32, i32* %4
%30 = load i32, i32* %5
%31 = mul nsw i32 %30, %29
store i32 %31, i32* %5
%32 = load i32, i32* %5
%33 = sdiv i32 %32, 1000000000
store i32 %33, i32* %5
%34 = load i32, i32* %5
%35 = sitofp i32 %34 to float
%36 = fpext float %35 to double
%37 = fdiv double %36, 1.000000e+03
%38 = fptrunc double %37 to float
store float %38, float* %1
br label %39
39: ; preds = %23, %20, %9
%40 = load float, float* %1
ret float %40
}

LLVM IR (-O2):
@.str = [41 x i8] c"/sys/class/power_supply/BAT0/voltage_now\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [28 x i8] c"Error opening voltage_now.\0A\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [41 x i8] c"/sys/class/power_supply/BAT0/current_now\00"
@.str.5 = [28 x i8] c"Error opening current_now.\0A\00"
define float @getpower() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %27
10: ; preds = %0
%11 = call i32 @fscanf(i32* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %1)
%12 = call i32 @fclose(i32* %5)
%13 = call i32* @fopen(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0))
br label %27
18: ; preds = %10
%19 = call i32 @fscanf(i32* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %2)
%20 = call i32 @fclose(i32* %13)
%21 = load i32, i32* %1
%22 = load i32, i32* %2
%23 = mul nsw i32 %22, %21
%24 = sdiv i32 %23, 1000000000
%25 = sitofp i32 %24 to float
%26 = fdiv float %25, 1.000000e+03
br label %27
27: ; preds = %18, %15, %7
%28 = phi float [ -1.000000e+00, %7 ], [ -1.000000e+00, %15 ], [ %26, %18 ]
ret float %28
}