#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 fscanf(int /*<<< orphan*/ *, char *, unsigned long *);
int /*<<< orphan*/ ksft_print_msg(char *, char *);
unsigned long read_sysfs(char *str) {
  FILE *f;
  unsigned long val = 0;
  f = fopen(str, "r");
  if (!f) {
    ksft_print_msg("ERR: missing %s\n", str);
    return 0;
  }
  if (fscanf(f, "%lu", &val) != 1) {
    ksft_print_msg("ERR: parsing %s\n", str);
    val = 0;
  }
  fclose(f);
  return val;
}


x86 assembly (clang, -O0):
<read_sysfs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea1(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1190 <read_sysfs+0x50>
mov    -0x10(%rbp),%rsi
lea    0xe84(%rip),%rdi
call   1050 <ksft_print_msg@plt>
movq   $0x0,-0x8(%rbp)
jmp    11d6 <read_sysfs+0x96>
mov    -0x18(%rbp),%rdi
lea    0xe78(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   1040 <fscanf@plt>
cmp    $0x1,%eax
je     11c5 <read_sysfs+0x85>
mov    -0x10(%rbp),%rsi
lea    0xe5f(%rip),%rdi
call   1050 <ksft_print_msg@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_sysfs>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
movq   $0x0,(%rsp)
lea    0xeaa(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     119f <read_sysfs+0x5f>
mov    %rax,%rbx
lea    0xea9(%rip),%rsi
mov    %rsp,%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
cmp    $0x1,%eax
je     1191 <read_sysfs+0x51>
lea    0xe96(%rip),%rdi
mov    %r14,%rsi
call   1050 <ksft_print_msg@plt>
movq   $0x0,(%rsp)
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    (%rsp),%rax
jmp    11b0 <read_sysfs+0x70>
lea    0xe5c(%rip),%rdi
mov    %r14,%rsi
call   1050 <ksft_print_msg@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [17 x i8] c"ERR: missing %s\0A\00"
@.str.2 = [4 x i8] c"%lu\00"
@.str.3 = [17 x i8] c"ERR: parsing %s\0A\00"
define i64 @read_sysfs(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i64
store i8* %0, i8** %3
store i64 0, i64* %5
%6 = load i8*, i8** %3
%7 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %4
%8 = load i32*, i32** %4
%9 = icmp ne i32* %8, null
br i1 %9, label %13, label %10
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = call i32 @ksft_print_msg(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %11)
store i64 0, i64* %2
br label %24
13: ; preds = %1
%14 = load i32*, i32** %4
%15 = call i32 @fscanf(i32* %14, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64* %5)
%16 = icmp ne i32 %15, 1
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = load i8*, i8** %3
%19 = call i32 @ksft_print_msg(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %18)
store i64 0, i64* %5
br label %20
20: ; preds = %17, %13
%21 = load i32*, i32** %4
%22 = call i32 @fclose(i32* %21)
%23 = load i64, i64* %5
store i64 %23, i64* %2
br label %24
24: ; preds = %20, %10
%25 = load i64, i64* %2
ret i64 %25
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [17 x i8] c"ERR: missing %s\0A\00"
@.str.2 = [4 x i8] c"%lu\00"
@.str.3 = [17 x i8] c"ERR: parsing %s\0A\00"
define i64 @read_sysfs(i8* %0) {
%2 = alloca i64
%3 = bitcast i64* %2 to i8*
store i64 0, i64* %2
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = tail call i32 @ksft_print_msg(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %16
8: ; preds = %1
%9 = call i32 @fscanf(i32* %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64* %2)
%10 = icmp eq i32 %9, 1
br i1 %10, label %13, label %11
11: ; preds = %8
%12 = call i32 @ksft_print_msg(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %0)
store i64 0, i64* %2
br label %13
13: ; preds = %11, %8
%14 = call i32 @fclose(i32* %4)
%15 = load i64, i64* %2
br label %16
16: ; preds = %13, %6
%17 = phi i64 [ %15, %13 ], [ 0, %6 ]
ret i64 %17
}