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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int sscanf(char *, char *, ...);
int /*<<< orphan*/ stderr;
unsigned long long get_mapping_page_size(void *p) {
  FILE *f;
  char line[4096];
  char *tmp;
  unsigned long addr = (unsigned long)p;
  f = fopen("/proc/self/smaps", "r");
  if (!f) {
    fprintf(stderr, "ERR: "
                    "Unable to open /proc/self/smaps\n");
    return 0;
  }
  while ((tmp = fgets(line, 4096, f))) {
    unsigned long start, end, dummy;
    char map_name[256];
    char buf[64];
    int ret;
    ret = sscanf(line, "%lx-%lx %s %lx %s %ld %s", &start, &end, buf, &dummy,
                 buf, &dummy, map_name);
    if (ret < 7 || start > addr || end <= addr)
      continue;
    while ((tmp = fgets(line, 4096, f))) {
      unsigned long long page_size;
      ret = sscanf(line, "KernelPageSize: %lld kB", &page_size);
      if (ret == 0)
        continue;
      if (ret < 1 || page_size <= 0) {
        fprintf(stderr, "ERR: "
                        "Cannot parse /proc/self/smaps\n");
        page_size = 0;
      }
      fclose(f);
      return page_size * 1024;
    }
  }
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<get_mapping_page_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x11c0,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x1030(%rbp)
lea    0xe8f(%rip),%rdi
lea    0xe99(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ae <get_mapping_page_size+0x5e>
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
lea    0xe77(%rip),%rsi
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1336 <get_mapping_page_size+0x1e6>
jmp    11b3 <get_mapping_page_size+0x63>
lea    -0x1020(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x1000,%esi
call   1040 <fgets@plt>
mov    %rax,-0x1028(%rbp)
cmp    $0x0,%rax
je     1325 <get_mapping_page_size+0x1d5>
lea    -0x1020(%rbp),%rdi
lea    -0x1190(%rbp),%r8
lea    -0x1190(%rbp),%r11
lea    -0x1150(%rbp),%rax
lea    0xe3d(%rip),%rsi
lea    -0x1038(%rbp),%rdx
lea    -0x1040(%rbp),%rcx
lea    -0x1048(%rbp),%r10
mov    %r10,%r9
mov    %r11,(%rsp)
mov    %r10,0x8(%rsp)
mov    %rax,0x10(%rsp)
mov    $0x0,%al
call   1060 <sscanf@plt>
mov    %eax,-0x1194(%rbp)
cmpl   $0x7,-0x1194(%rbp)
jl     1264 <get_mapping_page_size+0x114>
mov    -0x1038(%rbp),%rax
cmp    -0x1030(%rbp),%rax
ja     1264 <get_mapping_page_size+0x114>
mov    -0x1040(%rbp),%rax
cmp    -0x1030(%rbp),%rax
ja     1269 <get_mapping_page_size+0x119>
jmp    11b3 <get_mapping_page_size+0x63>
jmp    126e <get_mapping_page_size+0x11e>
lea    -0x1020(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x1000,%esi
call   1040 <fgets@plt>
mov    %rax,-0x1028(%rbp)
cmp    $0x0,%rax
je     1320 <get_mapping_page_size+0x1d0>
lea    -0x1020(%rbp),%rdi
lea    0xdb0(%rip),%rsi
lea    -0x11a0(%rbp),%rdx
mov    $0x0,%al
call   1060 <sscanf@plt>
mov    %eax,-0x1194(%rbp)
cmpl   $0x0,-0x1194(%rbp)
jne    12c8 <get_mapping_page_size+0x178>
jmp    126e <get_mapping_page_size+0x11e>
cmpl   $0x1,-0x1194(%rbp)
jl     12e3 <get_mapping_page_size+0x193>
cmpq   $0x0,-0x11a0(%rbp)
ja     1303 <get_mapping_page_size+0x1b3>
mov    0x2cf6(%rip),%rax
mov    (%rax),%edi
lea    0xd77(%rip),%rsi
call   1050 <fprintf@plt>
movq   $0x0,-0x11a0(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x11a0(%rbp),%rax
shl    $0xa,%rax
mov    %rax,-0x8(%rbp)
jmp    1336 <get_mapping_page_size+0x1e6>
jmp    11b3 <get_mapping_page_size+0x63>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x11c0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_mapping_page_size>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1168,%rsp
mov    %rdi,%r14
lea    0xe95(%rip),%rdi
lea    0xe9f(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1295 <get_mapping_page_size+0x145>
mov    %rax,%r12
lea    0x160(%rsp),%rdi
mov    $0x1000,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     12ac <get_mapping_page_size+0x15c>
lea    0x160(%rsp),%rbp
lea    0xea2(%rip),%r13
mov    %rsp,%r15
jmp    11d9 <get_mapping_page_size+0x89>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbp,%rdi
mov    $0x1000,%esi
mov    %r12,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     12ac <get_mapping_page_size+0x15c>
sub    $0x8,%rsp
mov    %rbp,%rdi
lea    0xe52(%rip),%rsi
lea    0x18(%rsp),%rdx
lea    0x10(%rsp),%rcx
lea    0x28(%rsp),%r8
lea    0x20(%rsp),%r9
xor    %eax,%eax
lea    0x68(%rsp),%rbx
push   %rbx
push   %r9
push   %r8
call   1060 <sscanf@plt>
add    $0x20,%rsp
cmp    $0x7,%eax
jl     11c0 <get_mapping_page_size+0x70>
cmp    %r14,0x10(%rsp)
ja     11c0 <get_mapping_page_size+0x70>
cmp    %r14,0x8(%rsp)
jbe    11c0 <get_mapping_page_size+0x70>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbp,%rdi
mov    $0x1000,%esi
mov    %r12,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     11c0 <get_mapping_page_size+0x70>
mov    %rbp,%rdi
mov    %r13,%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1060 <sscanf@plt>
test   %eax,%eax
je     1230 <get_mapping_page_size+0xe0>
jle    1266 <get_mapping_page_size+0x116>
cmpq   $0x0,(%rsp)
jne    1283 <get_mapping_page_size+0x133>
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
lea    0xdf4(%rip),%rsi
call   1050 <fprintf@plt>
movq   $0x0,(%rsp)
mov    %r12,%rdi
call   1030 <fclose@plt>
mov    (%rsp),%rax
shl    $0xa,%rax
jmp    12b6 <get_mapping_page_size+0x166>
mov    0x2d44(%rip),%rax
mov    (%rax),%edi
lea    0xd6e(%rip),%rsi
call   1050 <fprintf@plt>
jmp    12b4 <get_mapping_page_size+0x164>
mov    %r12,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x1168,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"/proc/self/smaps\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [38 x i8] c"ERR: Unable to open /proc/self/smaps\0A\00"
@.str.3 = [25 x i8] c"%lx-%lx %s %lx %s %ld %s\00"
@.str.4 = [24 x i8] c"KernelPageSize: %lld kB\00"
@.str.5 = [36 x i8] c"ERR: Cannot parse /proc/self/smaps\0A\00"
define i64 @get_mapping_page_size(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca [4096 x i8]
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca [256 x i8]
%12 = alloca [64 x i8]
%13 = alloca i32
%14 = alloca i64
store i8* %0, i8** %3
%15 = load i8*, i8** %3
%16 = ptrtoint i8* %15 to i64
store i64 %16, i64* %7
%17 = 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* %17, i32** %4
%18 = load i32*, i32** %4
%19 = icmp ne i32* %18, null
br i1 %19, label %23, label %20
20: ; preds = %1
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
store i64 0, i64* %2
br label %76
23: ; preds = %1
br label %24
24: ; preds = %72, %45, %23
%25 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%26 = load i32*, i32** %4
%27 = call i8* @fgets(i8* %25, i32 4096, i32* %26)
store i8* %27, i8** %6
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %73
29: ; preds = %24
%30 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%31 = getelementptr inbounds [64 x i8], [64 x i8]* %12, i64 0, i64 0
%32 = getelementptr inbounds [64 x i8], [64 x i8]* %12, i64 0, i64 0
%33 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%34 = call i32 (i8*, i8*, ...) @sscanf(i8* %30, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i64* %8, i64* %9, i8* %31, i64* %10, i8* %32, i64* %10, i8* %33)
store i32 %34, i32* %13
%35 = load i32, i32* %13
%36 = icmp slt i32 %35, 7
br i1 %36, label %45, label %37
37: ; preds = %29
%38 = load i64, i64* %8
%39 = load i64, i64* %7
%40 = icmp ugt i64 %38, %39
br i1 %40, label %45, label %41
41: ; preds = %37
%42 = load i64, i64* %9
%43 = load i64, i64* %7
%44 = icmp ule i64 %42, %43
br i1 %44, label %45, label %46
45: ; preds = %41, %37, %29
br label %24
46: ; preds = %41
br label %47
47: ; preds = %57, %46
%48 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%49 = load i32*, i32** %4
%50 = call i8* @fgets(i8* %48, i32 4096, i32* %49)
store i8* %50, i8** %6
%51 = icmp ne i8* %50, null
br i1 %51, label %52, label %72
52: ; preds = %47
%53 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%54 = call i32 (i8*, i8*, ...) @sscanf(i8* %53, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i64* %14)
store i32 %54, i32* %13
%55 = load i32, i32* %13
%56 = icmp eq i32 %55, 0
br i1 %56, label %57, label %58
57: ; preds = %52
br label %47
58: ; preds = %52
%59 = load i32, i32* %13
%60 = icmp slt i32 %59, 1
br i1 %60, label %64, label %61
61: ; preds = %58
%62 = load i64, i64* %14
%63 = icmp ule i64 %62, 0
br i1 %63, label %64, label %67
64: ; preds = %61, %58
%65 = load i32, i32* @stderr
%66 = call i32 @fprintf(i32 %65, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0))
store i64 0, i64* %14
br label %67
67: ; preds = %64, %61
%68 = load i32*, i32** %4
%69 = call i32 @fclose(i32* %68)
%70 = load i64, i64* %14
%71 = mul i64 %70, 1024
store i64 %71, i64* %2
br label %76
72: ; preds = %47
br label %24
73: ; preds = %24
%74 = load i32*, i32** %4
%75 = call i32 @fclose(i32* %74)
store i64 0, i64* %2
br label %76
76: ; preds = %73, %67, %20
%77 = load i64, i64* %2
ret i64 %77
}

LLVM IR (-O2):
@.str = [17 x i8] c"/proc/self/smaps\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [38 x i8] c"ERR: Unable to open /proc/self/smaps\0A\00"
@.str.3 = [25 x i8] c"%lx-%lx %s %lx %s %ld %s\00"
@.str.4 = [24 x i8] c"KernelPageSize: %lld kB\00"
@.str.5 = [36 x i8] c"ERR: Cannot parse /proc/self/smaps\0A\00"
define i64 @get_mapping_page_size(i8* %0) {
%2 = alloca [4096 x i8]
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca [256 x i8]
%7 = alloca [64 x i8]
%8 = alloca i64
%9 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%10 = ptrtoint i8* %0 to i64
%11 = 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))
%12 = icmp eq i32* %11, null
br i1 %12, label %23, label %13
13: ; preds = %1
%14 = call i8* @fgets(i8* %9, i32 4096, i32* %11)
%15 = icmp eq i8* %14, null
br i1 %15, label %59, label %16
16: ; preds = %13
%17 = bitcast i64* %3 to i8*
%18 = bitcast i64* %4 to i8*
%19 = bitcast i64* %5 to i8*
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%21 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%22 = bitcast i64* %8 to i8*
br label %26
23: ; preds = %1
%24 = load i32, i32* @stderr
%25 = tail call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
br label %61
26: ; preds = %16, %56
%27 = call i32 (i8*, i8*, ...) @sscanf(i8* %9, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i64* %3, i64* %4, i8* %21, i64* %5, i8* %21, i64* %5, i8* %20)
%28 = icmp sgt i32 %27, 6
%29 = load i64, i64* %3
%30 = icmp ule i64 %29, %10
%31 = select i1 %28, i1 %30, i1 false
%32 = load i64, i64* %4
%33 = icmp ugt i64 %32, %10
%34 = select i1 %31, i1 %33, i1 false
br i1 %34, label %35, label %56
35: ; preds = %26
%36 = call i8* @fgets(i8* %9, i32 4096, i32* %11)
%37 = icmp eq i8* %36, null
br i1 %37, label %56, label %38
38: ; preds = %35, %53
%39 = call i32 (i8*, i8*, ...) @sscanf(i8* %9, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i64* %8)
%40 = icmp eq i32 %39, 0
br i1 %40, label %53, label %41
41: ; preds = %38
%42 = icmp slt i32 %39, 1
%43 = load i64, i64* %8
%44 = icmp eq i64 %43, 0
%45 = select i1 %42, i1 true, i1 %44
br i1 %45, label %46, label %49
46: ; preds = %41
%47 = load i32, i32* @stderr
%48 = call i32 @fprintf(i32 %47, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0))
store i64 0, i64* %8
br label %49
49: ; preds = %41, %46
%50 = call i32 @fclose(i32* %11)
%51 = load i64, i64* %8
%52 = shl i64 %51, 10
br label %61
53: ; preds = %38
%54 = call i8* @fgets(i8* %9, i32 4096, i32* %11)
%55 = icmp eq i8* %54, null
br i1 %55, label %56, label %38
56: ; preds = %53, %35, %26
%57 = call i8* @fgets(i8* %9, i32 4096, i32* %11)
%58 = icmp eq i8* %57, null
br i1 %58, label %59, label %26
59: ; preds = %56, %13
%60 = call i32 @fclose(i32* %11)
br label %61
61: ; preds = %49, %59, %23
%62 = phi i64 [ 0, %59 ], [ 0, %23 ], [ %52, %49 ]
ret i64 %62
}