#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 */
struct dirent {
  int /*<<< orphan*/ d_name;
};
typedef scalar_t__ ssize_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ asprintf(char **, char *, char *, int /*<<< orphan*/,
                            char const *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
int open(char *, int /*<<< orphan*/);
scalar_t__ read(int, char *, int);
int sscanf(char *, char *, int *);
int read_from_file(const struct dirent *dir, const char *file) {
  int fd, value;
  char *filename;
  ssize_t bytes;
  char buf[32];
  asprintf(&filename, "%s/%s/%s", "/sys/bus/pci/devices", dir->d_name, file);
  fd = open(filename, O_RDONLY);
  free(filename);
  if (fd == -1)
    return -1;
  bytes = read(fd, buf, sizeof(buf) - 1);
  close(fd);
  buf[sizeof(buf) - 1] = '\0';
  if (bytes > 0 && sscanf(buf, "%i", &value) == 1)
    return value;
  else
    return -1;
}


x86 assembly (clang, -O0):
<read_from_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%r8
lea    -0x28(%rbp),%rdi
lea    0xe7b(%rip),%rsi
lea    0xe7d(%rip),%rdx
call   1070 <asprintf@plt>
mov    -0x28(%rbp),%rdi
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11c5 <read_from_file+0x65>
movl   $0xffffffff,-0x4(%rbp)
jmp    1220 <read_from_file+0xc0>
mov    -0x1c(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x1f,%edx
call   1050 <read@plt>
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movb   $0x0,-0x31(%rbp)
cmpq   $0x0,-0x30(%rbp)
jle    1219 <read_from_file+0xb9>
lea    -0x50(%rbp),%rdi
lea    0xe22(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   1060 <sscanf@plt>
cmp    $0x1,%eax
jne    1219 <read_from_file+0xb9>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1220 <read_from_file+0xc0>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_from_file>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %rsi,%r8
mov    (%rdi),%ecx
lea    0xe8c(%rip),%rsi
lea    0xe8e(%rip),%rdx
lea    0x8(%rsp),%rdi
call   1070 <asprintf@plt>
mov    0x8(%rsp),%rdi
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,%ebp
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%ebp
je     11f7 <read_from_file+0x97>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1f,%edx
call   1050 <read@plt>
mov    %rax,%r14
mov    %ebp,%edi
call   1040 <close@plt>
movb   $0x0,0x2f(%rsp)
test   %r14,%r14
jle    11f7 <read_from_file+0x97>
lea    0xe44(%rip),%rsi
lea    0x10(%rsp),%rdi
lea    0x4(%rsp),%rdx
call   1060 <sscanf@plt>
mov    $0xffffffff,%ebx
cmp    $0x1,%eax
jne    11f7 <read_from_file+0x97>
mov    0x4(%rsp),%ebx
mov    %ebx,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
@.str = [9 x i8] c"%s/%s/%s\00"
@.str.1 = [21 x i8] c"/sys/bus/pci/devices\00"
@O_RDONLY = global i32 0
@.str.2 = [3 x i8] c"%i\00"
define i32 @read_from_file(%struct.dirent* %0, i8* %1) {
%3 = alloca i32
%4 = alloca %struct.dirent*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca [32 x i8]
store %struct.dirent* %0, %struct.dirent** %4
store i8* %1, i8** %5
%11 = load %struct.dirent*, %struct.dirent** %4
%12 = getelementptr inbounds %struct.dirent, %struct.dirent* %11, i32 0, i32 0
%13 = load i32, i32* %12
%14 = load i8*, i8** %5
%15 = call i32 @asprintf(i8** %8, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %13, i8* %14)
%16 = load i8*, i8** %8
%17 = load i32, i32* @O_RDONLY
%18 = call i32 @open(i8* %16, i32 %17)
store i32 %18, i32* %6
%19 = load i8*, i8** %8
%20 = call i32 @free(i8* %19)
%21 = load i32, i32* %6
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %24
23: ; preds = %2
store i32 -1, i32* %3
br label %40
24: ; preds = %2
%25 = load i32, i32* %6
%26 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 0
%27 = call i64 @read(i32 %25, i8* %26, i32 31)
store i64 %27, i64* %9
%28 = load i32, i32* %6
%29 = call i32 @close(i32 %28)
%30 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 31
store i8 0, i8* %30
%31 = load i64, i64* %9
%32 = icmp sgt i64 %31, 0
br i1 %32, label %33, label %39
33: ; preds = %24
%34 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 0
%35 = call i32 @sscanf(i8* %34, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %7)
%36 = icmp eq i32 %35, 1
br i1 %36, label %37, label %39
37: ; preds = %33
%38 = load i32, i32* %7
store i32 %38, i32* %3
br label %40
39: ; preds = %33, %24
store i32 -1, i32* %3
br label %40
40: ; preds = %39, %37, %23
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [9 x i8] c"%s/%s/%s\00"
@.str.1 = [21 x i8] c"/sys/bus/pci/devices\00"
@O_RDONLY = global i32 0
@.str.2 = [3 x i8] c"%i\00"
define i32 @read_from_file(%struct.dirent* nocapture %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca [32 x i8]
%6 = bitcast i32* %3 to i8*
%7 = bitcast i8** %4 to i8*
%8 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%9 = getelementptr inbounds %struct.dirent, %struct.dirent* %0, i64 0, i32 0
%10 = load i32, i32* %9
%11 = call i32 @asprintf(i8** %4, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %10, i8* %1)
%12 = load i8*, i8** %4
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %12, i32 %13)
%15 = load i8*, i8** %4
%16 = call i32 @free(i8* %15)
%17 = icmp eq i32 %14, -1
br i1 %17, label %28, label %18
18: ; preds = %2
%19 = call i64 @read(i32 %14, i8* %8, i32 31)
%20 = call i32 @close(i32 %14)
%21 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 31
store i8 0, i8* %21
%22 = icmp sgt i64 %19, 0
br i1 %22, label %23, label %28
23: ; preds = %18
%24 = call i32 @sscanf(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%25 = icmp eq i32 %24, 1
%26 = load i32, i32* %3
%27 = select i1 %25, i32 %26, i32 -1
br label %28
28: ; preds = %23, %18, %2
%29 = phi i32 [ -1, %2 ], [ -1, %18 ], [ %27, %23 ]
ret i32 %29
}