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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, unsigned long, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
uint64_t get_kpageflags(unsigned long pfn) {
  uint64_t flags;
  FILE *file;
  file = fopen("/proc/kpageflags", "r");
  if (!file) {
    perror("fopen kpageflags");
    _exit(1);
  }
  if (fseek(file, pfn * sizeof(flags), SEEK_SET)) {
    perror("fseek kpageflags");
    _exit(1);
  }
  if (fread(&flags, sizeof(flags), 1, file) != 1) {
    perror("fread kpageflags");
    _exit(1);
  }
  fclose(file);
  return flags;
}


x86 assembly (clang, -O0):
<get_kpageflags>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe8d(%rip),%rdi
lea    0xe97(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a4 <get_kpageflags+0x44>
lea    0xe7e(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
shl    $0x2,%rsi
mov    0x2e29(%rip),%rax
mov    (%rax),%edx
call   1060 <fseek@plt>
cmp    $0x0,%rax
je     11de <get_kpageflags+0x7e>
lea    0xe55(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>
mov    -0x18(%rbp),%rcx
lea    -0xc(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1040 <fread@plt>
cmp    $0x1,%eax
je     1214 <get_kpageflags+0xb4>
lea    0xe30(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_kpageflags>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
lea    0xe92(%rip),%rdi
lea    0xe9c(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11cf <get_kpageflags+0x6f>
mov    %rax,%r14
shl    $0x2,%rbx
mov    0x2e53(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <fseek@plt>
test   %rax,%rax
jne    11d8 <get_kpageflags+0x78>
lea    0x4(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
cmp    $0x1,%eax
jne    11e1 <get_kpageflags+0x81>
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    0x4(%rsp),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe3d(%rip),%rdi
jmp    11e8 <get_kpageflags+0x88>
lea    0xe45(%rip),%rdi
jmp    11e8 <get_kpageflags+0x88>
lea    0xe4d(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"/proc/kpageflags\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [17 x i8] c"fopen kpageflags\00"
@SEEK_SET = global i32 0
@.str.3 = [17 x i8] c"fseek kpageflags\00"
@.str.4 = [17 x i8] c"fread kpageflags\00"
define i32 @get_kpageflags(i64 %0) {
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32*
store i64 %0, i64* %2
%5 = call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** %4
%6 = load i32*, i32** %4
%7 = icmp ne i32* %6, null
br i1 %7, label %11, label %8
8: ; preds = %1
%9 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%10 = call i32 @_exit(i32 1)
unreachable
11: ; preds = %1
%12 = load i32*, i32** %4
%13 = load i64, i64* %2
%14 = mul i64 %13, 4
%15 = load i32, i32* @SEEK_SET
%16 = call i64 @fseek(i32* %12, i64 %14, i32 %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %11
%19 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%20 = call i32 @_exit(i32 1)
unreachable
21: ; preds = %11
%22 = load i32*, i32** %4
%23 = call i32 @fread(i32* %3, i32 4, i32 1, i32* %22)
%24 = icmp ne i32 %23, 1
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%27 = call i32 @_exit(i32 1)
unreachable
28: ; preds = %21
%29 = load i32*, i32** %4
%30 = call i32 @fclose(i32* %29)
%31 = load i32, i32* %3
ret i32 %31
}

LLVM IR (-O2):
@.str = [17 x i8] c"/proc/kpageflags\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [17 x i8] c"fopen kpageflags\00"
@SEEK_SET = global i32 0
@.str.3 = [17 x i8] c"fseek kpageflags\00"
@.str.4 = [17 x i8] c"fread kpageflags\00"
define i32 @get_kpageflags(i64 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%8 = tail call i32 @_exit(i32 1)
unreachable
9: ; preds = %1
%10 = shl i64 %0, 2
%11 = load i32, i32* @SEEK_SET
%12 = tail call i64 @fseek(i32* %4, i64 %10, i32 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %9
%15 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%16 = tail call i32 @_exit(i32 1)
unreachable
17: ; preds = %9
%18 = call i32 @fread(i32* %2, i32 4, i32 1, i32* %4)
%19 = icmp eq i32 %18, 1
br i1 %19, label %23, label %20
20: ; preds = %17
%21 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%22 = tail call i32 @_exit(i32 1)
unreachable
23: ; preds = %17
%24 = tail call i32 @fclose(i32* %4)
%25 = load i32, i32* %2
ret i32 %25
}