#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*/ szVal;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int errno;
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int read(int, char *, int);
int sscanf(char *, char *, int *);
int sysfs_read_val(int fd, int *val) {
  int rc;
  char szVal[32] = {0};
  lseek(fd, 0, SEEK_SET);
  rc = read(fd, szVal, sizeof(szVal) - 1);
  if (rc < 0) {
    return -errno;
  }
  rc = sscanf(szVal, "%d", val);
  if (rc != 1) {
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<sysfs_read_val>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    -0x8(%rbp),%edi
mov    0x2e6f(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <lseek@plt>
mov    -0x8(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x1f,%edx
call   1050 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11a3 <sysfs_read_val+0x63>
xor    %eax,%eax
mov    0x2e47(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11d7 <sysfs_read_val+0x97>
lea    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe4e(%rip),%rsi
call   1060 <sscanf@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
je     11d0 <sysfs_read_val+0x90>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d7 <sysfs_read_val+0x97>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysfs_read_val>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
mov    0x2e88(%rip),%rax
mov    (%rax),%edx
xor    %ebx,%ebx
xor    %esi,%esi
call   1030 <lseek@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x1f,%edx
call   1040 <read@plt>
test   %eax,%eax
js     118c <sysfs_read_val+0x5c>
lea    0xe8b(%rip),%rsi
mov    %rsp,%rdi
mov    %r14,%rdx
call   1050 <sscanf@plt>
xor    %ebx,%ebx
cmp    $0x1,%eax
setne  %bl
neg    %ebx
jmp    1195 <sysfs_read_val+0x65>
mov    0x2e4d(%rip),%rax
sub    (%rax),%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_SET = global i32 0
@errno = global i32 0
@.str = [3 x i8] c"%d\00"
define i32 @sysfs_read_val(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca [32 x i8]
store i32 %0, i32* %4
store i32* %1, i32** %5
%8 = bitcast [32 x i8]* %7 to i8*
%9 = load i32, i32* %4
%10 = load i32, i32* @SEEK_SET
%11 = call i32 @lseek(i32 %9, i32 0, i32 %10)
%12 = load i32, i32* %4
%13 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%14 = call i32 @read(i32 %12, i8* %13, i32 31)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = load i32, i32* @errno
%19 = sub nsw i32 0, %18
store i32 %19, i32* %3
br label %28
20: ; preds = %2
%21 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%22 = load i32*, i32** %5
%23 = call i32 @sscanf(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %22)
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = icmp ne i32 %24, 1
br i1 %25, label %26, label %27
26: ; preds = %20
store i32 -1, i32* %3
br label %28
27: ; preds = %20
store i32 0, i32* %3
br label %28
28: ; preds = %27, %26, %17
%29 = load i32, i32* %3
ret i32 %29
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@errno = global i32 0
@.str = [3 x i8] c"%d\00"
define i32 @sysfs_read_val(i32 %0, i32* %1) {
%3 = alloca [32 x i8]
%4 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @SEEK_SET
%6 = tail call i32 @lseek(i32 %0, i32 0, i32 %5)
%7 = call i32 @read(i32 %0, i8* %4, i32 31)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i32, i32* @errno
%11 = sub nsw i32 0, %10
br label %16
12: ; preds = %2
%13 = call i32 @sscanf(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %1)
%14 = icmp ne i32 %13, 1
%15 = sext i1 %14 to i32
br label %16
16: ; preds = %12, %9
%17 = phi i32 [ %11, %9 ], [ %15, %12 ]
ret i32 %17
}