#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 int32_t;
typedef int /*<<< orphan*/ file;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, short, short, short);
int32_t pciReadLong(unsigned short devbusfn, long offset) {
  char file[25];
  FILE *device;
  short bus = ((devbusfn >> 8) & 0xff);
  short dev = ((devbusfn & 0xff) >> 3);
  short function = (devbusfn & 0x7);
  snprintf(file, sizeof(file), "/proc/bus/pci/%02x/%02x.%x", bus, dev,
           function);
  if ((device = fopen(file, "r")) != NULL) {
    int32_t buffer;
    fseek(device, offset, SEEK_SET);
    fread(&buffer, sizeof(int32_t), 1, device);
    fclose(device);
    return buffer;
  }
  return -1;
}


x86 assembly (clang, -O0):
<pciReadLong>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %di,%ax
mov    %ax,-0x6(%rbp)
mov    %rsi,-0x10(%rbp)
movzwl -0x6(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    %ax,-0x3a(%rbp)
movzwl -0x6(%rbp),%eax
and    $0xff,%eax
sar    $0x3,%eax
mov    %ax,-0x3c(%rbp)
movzwl -0x6(%rbp),%eax
and    $0x7,%eax
mov    %ax,-0x3e(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x3a(%rbp),%cx
mov    -0x3c(%rbp),%ax
mov    $0x19,%esi
lea    0xe5a(%rip),%rdx
movswl %cx,%ecx
movswl %ax,%r8d
movswl -0x3e(%rbp),%r9d
call   1050 <snprintf@plt>
lea    -0x30(%rbp),%rdi
lea    0xe59(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     1216 <pciReadLong+0xc6>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2dfc(%rip),%rax
mov    (%rax),%edx
call   1060 <fseek@plt>
mov    -0x38(%rbp),%rcx
lea    -0x44(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    121d <pciReadLong+0xcd>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pciReadLong>:
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%r14
mov    %edi,%r9d
mov    %edi,%ecx
shr    $0x8,%ecx
mov    %edi,%r8d
shr    $0x3,%r8d
and    $0x1f,%r8d
and    $0x7,%r9d
lea    0xe88(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x19,%esi
call   1050 <snprintf@plt>
lea    0xe8a(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11da <pciReadLong+0x8a>
mov    %rax,%rbx
mov    0x2e38(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <fseek@plt>
lea    0xc(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0xc(%rsp),%eax
jmp    11df <pciReadLong+0x8f>
mov    $0xffffffff,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [27 x i8] c"/proc/bus/pci/%02x/%02x.%x\00"
@.str.1 = [2 x i8] c"r\00"
@SEEK_SET = global i32 0
define i32 @pciReadLong(i16 zeroext %0, i64 %1) {
%3 = alloca i32
%4 = alloca i16
%5 = alloca i64
%6 = alloca [25 x i8]
%7 = alloca i32*
%8 = alloca i16
%9 = alloca i16
%10 = alloca i16
%11 = alloca i32
store i16 %0, i16* %4
store i64 %1, i64* %5
%12 = load i16, i16* %4
%13 = zext i16 %12 to i32
%14 = ashr i32 %13, 8
%15 = and i32 %14, 255
%16 = trunc i32 %15 to i16
store i16 %16, i16* %8
%17 = load i16, i16* %4
%18 = zext i16 %17 to i32
%19 = and i32 %18, 255
%20 = ashr i32 %19, 3
%21 = trunc i32 %20 to i16
store i16 %21, i16* %9
%22 = load i16, i16* %4
%23 = zext i16 %22 to i32
%24 = and i32 %23, 7
%25 = trunc i32 %24 to i16
store i16 %25, i16* %10
%26 = getelementptr inbounds [25 x i8], [25 x i8]* %6, i64 0, i64 0
%27 = load i16, i16* %8
%28 = load i16, i16* %9
%29 = load i16, i16* %10
%30 = call i32 @snprintf(i8* %26, i32 25, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i16 signext %27, i16 signext %28, i16 signext %29)
%31 = getelementptr inbounds [25 x i8], [25 x i8]* %6, i64 0, i64 0
%32 = call i32* @fopen(i8* %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %32, i32** %7
%33 = icmp ne i32* %32, null
br i1 %33, label %34, label %44
34: ; preds = %2
%35 = load i32*, i32** %7
%36 = load i64, i64* %5
%37 = load i32, i32* @SEEK_SET
%38 = call i32 @fseek(i32* %35, i64 %36, i32 %37)
%39 = load i32*, i32** %7
%40 = call i32 @fread(i32* %11, i32 4, i32 1, i32* %39)
%41 = load i32*, i32** %7
%42 = call i32 @fclose(i32* %41)
%43 = load i32, i32* %11
store i32 %43, i32* %3
br label %45
44: ; preds = %2
store i32 -1, i32* %3
br label %45
45: ; preds = %44, %34
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
@.str = [27 x i8] c"/proc/bus/pci/%02x/%02x.%x\00"
@.str.1 = [2 x i8] c"r\00"
@SEEK_SET = global i32 0
define i32 @pciReadLong(i16 zeroext %0, i64 %1) {
%3 = alloca [25 x i8]
%4 = alloca i32
%5 = getelementptr inbounds [25 x i8], [25 x i8]* %3, i64 0, i64 0
%6 = lshr i16 %0, 8
%7 = lshr i16 %0, 3
%8 = and i16 %7, 31
%9 = and i16 %0, 7
%10 = call i32 @snprintf(i8* %5, i32 25, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i16 signext %6, i16 signext %8, i16 signext %9)
%11 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %20, label %13
13: ; preds = %2
%14 = bitcast i32* %4 to i8*
%15 = load i32, i32* @SEEK_SET
%16 = call i32 @fseek(i32* %11, i64 %1, i32 %15)
%17 = call i32 @fread(i32* %4, i32 4, i32 1, i32* %11)
%18 = call i32 @fclose(i32* %11)
%19 = load i32, i32* %4
br label %20
20: ; preds = %2, %13
%21 = phi i32 [ %19, %13 ], [ -1, %2 ]
ret i32 %21
}