#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 EINVAL;
int ENODEV;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, unsigned int *);
int battery_get_mv(void) {
  FILE *f;
  unsigned int reading;
  f = fopen("/sys/devices/platform/lf1000-adc/channel2", "r");
  if (!f) {
    ;
    return -ENODEV;
  }
  if (fscanf(f, "%u", &reading) != 1) {
    ;
    fclose(f);
    return -EINVAL;
  }
  fclose(f);
  return ((2012 * reading) / 256) + 0;
}


x86 assembly (clang, -O0):
<battery_get_mv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xec1(%rip),%rdi
lea    0xee4(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    116d <battery_get_mv+0x3d>
xor    %eax,%eax
mov    0x2e75(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11bf <battery_get_mv+0x8f>
mov    -0x10(%rbp),%rdi
lea    0xeb4(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1040 <fscanf@plt>
cmp    $0x1,%eax
je     11a6 <battery_get_mv+0x76>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
mov    0x2e44(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11bf <battery_get_mv+0x8f>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
imul   $0x7dc,-0x14(%rbp),%eax
shr    $0x8,%eax
add    $0x0,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<battery_get_mv>:
push   %rbp
push   %rbx
push   %rax
lea    0xec6(%rip),%rdi
lea    0xee9(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     117e <battery_get_mv+0x4e>
mov    %rax,%rbx
lea    0xed7(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
cmp    $0x1,%ebp
jne    1187 <battery_get_mv+0x57>
imul   $0x7dc,0x4(%rsp),%eax
shr    $0x8,%eax
jmp    1192 <battery_get_mv+0x62>
mov    0x2e53(%rip),%rcx
jmp    118e <battery_get_mv+0x5e>
mov    0x2e52(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [42 x i8] c"/sys/devices/platform/lf1000-adc/channel2\00"
@.str.1 = [2 x i8] c"r\00"
@ENODEV = global i32 0
@.str.2 = [3 x i8] c"%u\00"
@EINVAL = global i32 0
define i32 @battery_get_mv() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = call i32* @fopen(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %4, i32** %2
%5 = load i32*, i32** %2
%6 = icmp ne i32* %5, null
br i1 %6, label %10, label %7
7: ; preds = %0
%8 = load i32, i32* @ENODEV
%9 = sub nsw i32 0, %8
store i32 %9, i32* %1
br label %26
10: ; preds = %0
%11 = load i32*, i32** %2
%12 = call i32 @fscanf(i32* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%13 = icmp ne i32 %12, 1
br i1 %13, label %14, label %19
14: ; preds = %10
%15 = load i32*, i32** %2
%16 = call i32 @fclose(i32* %15)
%17 = load i32, i32* @EINVAL
%18 = sub nsw i32 0, %17
store i32 %18, i32* %1
br label %26
19: ; preds = %10
%20 = load i32*, i32** %2
%21 = call i32 @fclose(i32* %20)
%22 = load i32, i32* %3
%23 = mul i32 2012, %22
%24 = udiv i32 %23, 256
%25 = add i32 %24, 0
store i32 %25, i32* %1
br label %26
26: ; preds = %19, %14, %7
%27 = load i32, i32* %1
ret i32 %27
}

LLVM IR (-O2):
@.str = [42 x i8] c"/sys/devices/platform/lf1000-adc/channel2\00"
@.str.1 = [2 x i8] c"r\00"
@ENODEV = global i32 0
@.str.2 = [3 x i8] c"%u\00"
@EINVAL = global i32 0
define i32 @battery_get_mv() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = load i32, i32* @ENODEV
%7 = sub nsw i32 0, %6
br label %19
8: ; preds = %0
%9 = call i32 @fscanf(i32* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %1)
%10 = icmp eq i32 %9, 1
%11 = call i32 @fclose(i32* %3)
br i1 %10, label %15, label %12
12: ; preds = %8
%13 = load i32, i32* @EINVAL
%14 = sub nsw i32 0, %13
br label %19
15: ; preds = %8
%16 = load i32, i32* %1
%17 = mul i32 %16, 2012
%18 = lshr i32 %17, 8
br label %19
19: ; preds = %15, %12, %5
%20 = phi i32 [ %14, %12 ], [ %18, %15 ], [ %7, %5 ]
ret i32 %20
}