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

/* Variables and functions */
scalar_t__ ERANGE;
scalar_t__ errno;
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int, char const *);
int strtol(char *, char **, int /*<<< orphan*/);
scalar_t__ sysfs_read_file(char *, char *, int);
int sysfs_topology_read_file(unsigned int cpu, const char *fname, int *result) {
  char linebuf[4096];
  char *endp;
  char path[255];
  snprintf(path, sizeof(path),
           "/sys/devices/system/cpu/"
           "cpu%u/topology/%s",
           cpu, fname);
  if (sysfs_read_file(path, linebuf, 4096) == 0)
    return -1;
  *result = strtol(linebuf, &endp, 0);
  if (endp == linebuf || errno == ERANGE)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<sysfs_topology_read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1130,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x1130(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    -0x10(%rbp),%r8
mov    $0xff,%esi
lea    0xea0(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x1130(%rbp),%rdi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1040 <sysfs_read_file@plt>
cmp    $0x0,%rax
jne    1193 <sysfs_topology_read_file+0x63>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f4 <sysfs_topology_read_file+0xc4>
lea    -0x1020(%rbp),%rdi
lea    -0x1028(%rbp),%rsi
xor    %edx,%edx
call   1050 <strtol@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x1028(%rbp),%rax
lea    -0x1020(%rbp),%rcx
cmp    %rcx,%rax
je     11e1 <sysfs_topology_read_file+0xb1>
mov    0x2e0a(%rip),%rax
mov    (%rax),%rax
mov    0x2e10(%rip),%rcx
cmp    (%rcx),%rax
jne    11ed <sysfs_topology_read_file+0xbd>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f4 <sysfs_topology_read_file+0xc4>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysfs_topology_read_file>:
push   %rbp
push   %r14
push   %rbx
sub    $0x1110,%rsp
mov    %rdx,%r14
mov    %rsi,%r8
mov    %edi,%ecx
lea    0xeb6(%rip),%rdx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0xff,%esi
call   1030 <snprintf@plt>
lea    0x110(%rsp),%rbx
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1040 <sysfs_read_file@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
je     11b3 <sysfs_topology_read_file+0x83>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
xor    %edx,%edx
call   1050 <strtol@plt>
mov    %eax,(%r14)
cmp    %rbx,0x8(%rsp)
je     11b3 <sysfs_topology_read_file+0x83>
mov    0x2e3a(%rip),%rax
mov    (%rax),%rax
mov    0x2e40(%rip),%rcx
xor    %ebp,%ebp
cmp    (%rcx),%rax
sete   %bpl
neg    %ebp
mov    %ebp,%eax
add    $0x1110,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [42 x i8] c"/sys/devices/system/cpu/cpu%u/topology/%s\00"
@errno = global i64 0
@ERANGE = global i64 0
define i32 @sysfs_topology_read_file(i32 %0, i8* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca [4096 x i8]
%9 = alloca i8*
%10 = alloca [255 x i8]
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32* %2, i32** %7
%11 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%12 = load i32, i32* %5
%13 = load i8*, i8** %6
%14 = call i32 @snprintf(i8* %11, i32 255, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i32 %12, i8* %13)
%15 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%16 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%17 = call i64 @sysfs_read_file(i8* %15, i8* %16, i32 4096)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %3
store i32 -1, i32* %4
br label %33
20: ; preds = %3
%21 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%22 = call i32 @strtol(i8* %21, i8** %9, i32 0)
%23 = load i32*, i32** %7
store i32 %22, i32* %23
%24 = load i8*, i8** %9
%25 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%26 = icmp eq i8* %24, %25
br i1 %26, label %31, label %27
27: ; preds = %20
%28 = load i64, i64* @errno
%29 = load i64, i64* @ERANGE
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %32
31: ; preds = %27, %20
store i32 -1, i32* %4
br label %33
32: ; preds = %27
store i32 0, i32* %4
br label %33
33: ; preds = %32, %31, %19
%34 = load i32, i32* %4
ret i32 %34
}

LLVM IR (-O2):
@.str = [42 x i8] c"/sys/devices/system/cpu/cpu%u/topology/%s\00"
@errno = global i64 0
@ERANGE = global i64 0
define i32 @sysfs_topology_read_file(i32 %0, i8* %1, i32* nocapture writeonly %2) {
%4 = alloca [4096 x i8]
%5 = alloca i8*
%6 = alloca [255 x i8]
%7 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%8 = bitcast i8** %5 to i8*
%9 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%10 = call i32 @snprintf(i8* %9, i32 255, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i32 %0, i8* %1)
%11 = call i64 @sysfs_read_file(i8* %9, i8* %7, i32 4096)
%12 = icmp eq i64 %11, 0
br i1 %12, label %22, label %13
13: ; preds = %3
%14 = call i32 @strtol(i8* %7, i8** %5, i32 0)
store i32 %14, i32* %2
%15 = load i8*, i8** %5
%16 = icmp eq i8* %15, %7
br i1 %16, label %22, label %17
17: ; preds = %13
%18 = load i64, i64* @errno
%19 = load i64, i64* @ERANGE
%20 = icmp eq i64 %18, %19
%21 = sext i1 %20 to i32
br label %22
22: ; preds = %17, %13, %3
%23 = phi i32 [ -1, %3 ], [ -1, %13 ], [ %21, %17 ]
ret i32 %23
}