#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*/ *);
int /*<<< orphan*/ free(char *);
scalar_t__ getline(char **, size_t *, int /*<<< orphan*/ *);
int is_vmflag_set(unsigned long, char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ *seek_to_smaps_entry(unsigned long);
int sscanf(char *, char *, unsigned long *);
int strlen(char *);
int /*<<< orphan*/ strstr(char *, char *);
bool is_vma_lock_on_fault(unsigned long addr) {
  bool ret = false;
  bool locked;
  FILE *smaps = NULL;
  unsigned long vma_size, vma_rss;
  char *line = NULL;
  char *value;
  size_t size = 0;
  locked = is_vmflag_set(addr, "lo");
  if (!locked)
    goto out;
  smaps = seek_to_smaps_entry(addr);
  if (!smaps) {
    printf("Unable to parse /proc/self/smaps\n");
    goto out;
  }
  while (getline(&line, &size, smaps) > 0) {
    if (!strstr(line, "Size:")) {
      free(line);
      line = NULL;
      size = 0;
      continue;
    }
    value = line + strlen("Size:");
    if (sscanf(value, "%lu kB", &vma_size) < 1) {
      printf("Unable to parse smaps entry for Size\n");
      goto out;
    }
    break;
  }
  while (getline(&line, &size, smaps) > 0) {
    if (!strstr(line, "Rss:")) {
      free(line);
      line = NULL;
      size = 0;
      continue;
    }
    value = line + strlen("Rss:");
    if (sscanf(value, "%lu kB", &vma_rss) < 1) {
      printf("Unable to parse smaps entry for Rss\n");
      goto out;
    }
    break;
  }
  ret = locked && (vma_rss < vma_size);
out:
  free(line);
  if (smaps)
    fclose(smaps);
  return ret;
}


x86 assembly (clang, -O0):
<is_vma_lock_on_fault>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe3a(%rip),%rsi
call   1040 <is_vmflag_set@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jne    11dd <is_vma_lock_on_fault+0x4d>
jmp    138a <is_vma_lock_on_fault+0x1fa>
mov    -0x8(%rbp),%rdi
call   1090 <seek_to_smaps_entry@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1206 <is_vma_lock_on_fault+0x76>
lea    0xe07(%rip),%rdi
call   1070 <printf@plt>
jmp    138a <is_vma_lock_on_fault+0x1fa>
jmp    120b <is_vma_lock_on_fault+0x7b>
mov    -0x18(%rbp),%rdx
lea    -0x30(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   10a0 <getline@plt>
cmp    $0x0,%rax
jle    12b4 <is_vma_lock_on_fault+0x124>
mov    -0x30(%rbp),%rdi
lea    0xdf4(%rip),%rsi
call   10b0 <strstr@plt>
cmp    $0x0,%eax
jne    125d <is_vma_lock_on_fault+0xcd>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x40(%rbp)
jmp    120b <is_vma_lock_on_fault+0x7b>
mov    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
lea    0xdb9(%rip),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
lea    0xd9f(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   1080 <sscanf@plt>
cmp    $0x1,%eax
jge    12af <is_vma_lock_on_fault+0x11f>
lea    0xd8d(%rip),%rdi
call   1070 <printf@plt>
jmp    138a <is_vma_lock_on_fault+0x1fa>
jmp    12b4 <is_vma_lock_on_fault+0x124>
jmp    12b9 <is_vma_lock_on_fault+0x129>
mov    -0x18(%rbp),%rdx
lea    -0x30(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   10a0 <getline@plt>
cmp    $0x0,%rax
jle    1362 <is_vma_lock_on_fault+0x1d2>
mov    -0x30(%rbp),%rdi
lea    0xd79(%rip),%rsi
call   10b0 <strstr@plt>
cmp    $0x0,%eax
jne    130b <is_vma_lock_on_fault+0x17b>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x40(%rbp)
jmp    12b9 <is_vma_lock_on_fault+0x129>
mov    -0x30(%rbp),%rax
mov    %rax,-0x50(%rbp)
lea    0xd3e(%rip),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x50(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
lea    0xcf1(%rip),%rsi
lea    -0x28(%rbp),%rdx
call   1080 <sscanf@plt>
cmp    $0x1,%eax
jge    135d <is_vma_lock_on_fault+0x1cd>
lea    0xd0a(%rip),%rdi
call   1070 <printf@plt>
jmp    138a <is_vma_lock_on_fault+0x1fa>
jmp    1362 <is_vma_lock_on_fault+0x1d2>
xor    %eax,%eax
cmpl   $0x0,-0x10(%rbp)
mov    %al,-0x51(%rbp)
je     137f <is_vma_lock_on_fault+0x1ef>
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
setb   %al
mov    %al,-0x51(%rbp)
mov    -0x51(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0xc(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x18(%rbp)
je     13a7 <is_vma_lock_on_fault+0x217>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0xc(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_vma_lock_on_fault>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%rbx
movq   $0x0,(%rsp)
movq   $0x0,0x8(%rsp)
lea    0xe48(%rip),%rsi
call   1050 <is_vmflag_set@plt>
test   %eax,%eax
je     124e <is_vma_lock_on_fault+0xbe>
mov    %rbx,%rdi
call   1090 <seek_to_smaps_entry@plt>
test   %rax,%rax
je     1242 <is_vma_lock_on_fault+0xb2>
mov    %rax,%r13
mov    %rsp,%rdi
lea    0x8(%rsp),%rsi
mov    %rax,%rdx
call   10a0 <getline@plt>
test   %rax,%rax
jle    128a <is_vma_lock_on_fault+0xfa>
lea    0xe0e(%rip),%r14
mov    %rsp,%r15
lea    0x8(%rsp),%r12
nopl   (%rax)
mov    (%rsp),%rdi
mov    %r14,%rsi
call   10b0 <strstr@plt>
mov    (%rsp),%rbx
test   %eax,%eax
jne    125d <is_vma_lock_on_fault+0xcd>
mov    %rbx,%rdi
call   1030 <free@plt>
movq   $0x0,(%rsp)
movq   $0x0,0x8(%rsp)
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   10a0 <getline@plt>
test   %rax,%rax
jg     1200 <is_vma_lock_on_fault+0x70>
jmp    128a <is_vma_lock_on_fault+0xfa>
lea    0xdcc(%rip),%rdi
call   1040 <puts@plt>
xor    %r14d,%r14d
xor    %edi,%edi
call   1030 <free@plt>
jmp    1367 <is_vma_lock_on_fault+0x1d7>
lea    0xd9f(%rip),%rdi
call   1070 <strlen@plt>
cltq
add    %rax,%rbx
lea    0xd94(%rip),%rsi
lea    0x18(%rsp),%rdx
mov    %rbx,%rdi
call   1080 <sscanf@plt>
test   %eax,%eax
jle    133e <is_vma_lock_on_fault+0x1ae>
mov    %rsp,%rdi
lea    0x8(%rsp),%rsi
mov    %r13,%rdx
call   10a0 <getline@plt>
test   %rax,%rax
jle    132b <is_vma_lock_on_fault+0x19b>
lea    0xd66(%rip),%r14
mov    %rsp,%r15
lea    0x8(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%rsp),%rdi
mov    %r14,%rsi
call   10b0 <strstr@plt>
mov    (%rsp),%rbx
test   %eax,%eax
jne    1302 <is_vma_lock_on_fault+0x172>
mov    %rbx,%rdi
call   1030 <free@plt>
movq   $0x0,(%rsp)
movq   $0x0,0x8(%rsp)
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   10a0 <getline@plt>
test   %rax,%rax
jg     12c0 <is_vma_lock_on_fault+0x130>
jmp    132b <is_vma_lock_on_fault+0x19b>
lea    0xd07(%rip),%rdi
call   1070 <strlen@plt>
cltq
add    %rax,%rbx
lea    0xcef(%rip),%rsi
lea    0x10(%rsp),%rdx
mov    %rbx,%rdi
call   1080 <sscanf@plt>
test   %eax,%eax
jle    1347 <is_vma_lock_on_fault+0x1b7>
mov    0x10(%rsp),%rax
xor    %r14d,%r14d
cmp    0x18(%rsp),%rax
setb   %r14b
jmp    1356 <is_vma_lock_on_fault+0x1c6>
lea    0xd15(%rip),%rdi
jmp    134e <is_vma_lock_on_fault+0x1be>
lea    0xce8(%rip),%rdi
call   1040 <puts@plt>
xor    %r14d,%r14d
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    %r13,%rdi
call   1060 <fclose@plt>
mov    %r14d,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"lo\00"
@.str.1 = [34 x i8] c"Unable to parse /proc/self/smaps\0A\00"
@.str.2 = [6 x i8] c"Size:\00"
@.str.3 = [7 x i8] c"%lu kB\00"
@.str.4 = [38 x i8] c"Unable to parse smaps entry for Size\0A\00"
@.str.5 = [5 x i8] c"Rss:\00"
@.str.6 = [37 x i8] c"Unable to parse smaps entry for Rss\0A\00"
define i32 @is_vma_lock_on_fault(i64 %0) {
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
store i64 %0, i64* %2
store i32 0, i32* %3
store i32* null, i32** %5
store i8* null, i8** %8
store i64 0, i64* %10
%11 = load i64, i64* %2
%12 = call i32 @is_vmflag_set(i64 %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 0
br i1 %14, label %16, label %15
15: ; preds = %1
br label %79
16: ; preds = %1
%17 = load i64, i64* %2
%18 = call i32* @seek_to_smaps_entry(i64 %17)
store i32* %18, i32** %5
%19 = load i32*, i32** %5
%20 = icmp ne i32* %19, null
br i1 %20, label %23, label %21
21: ; preds = %16
%22 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
br label %79
23: ; preds = %16
br label %24
24: ; preds = %32, %23
%25 = load i32*, i32** %5
%26 = call i64 @getline(i8** %8, i64* %10, i32* %25)
%27 = icmp sgt i64 %26, 0
br i1 %27, label %28, label %46
28: ; preds = %24
%29 = load i8*, i8** %8
%30 = call i32 @strstr(i8* %29, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%31 = icmp ne i32 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %28
%33 = load i8*, i8** %8
%34 = call i32 @free(i8* %33)
store i8* null, i8** %8
store i64 0, i64* %10
br label %24
35: ; preds = %28
%36 = load i8*, i8** %8
%37 = call i32 @strlen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
store i8* %39, i8** %9
%40 = load i8*, i8** %9
%41 = call i32 @sscanf(i8* %40, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i64* %6)
%42 = icmp slt i32 %41, 1
br i1 %42, label %43, label %45
43: ; preds = %35
%44 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0))
br label %79
45: ; preds = %35
br label %46
46: ; preds = %45, %24
br label %47
47: ; preds = %55, %46
%48 = load i32*, i32** %5
%49 = call i64 @getline(i8** %8, i64* %10, i32* %48)
%50 = icmp sgt i64 %49, 0
br i1 %50, label %51, label %69
51: ; preds = %47
%52 = load i8*, i8** %8
%53 = call i32 @strstr(i8* %52, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%54 = icmp ne i32 %53, 0
br i1 %54, label %58, label %55
55: ; preds = %51
%56 = load i8*, i8** %8
%57 = call i32 @free(i8* %56)
store i8* null, i8** %8
store i64 0, i64* %10
br label %47
58: ; preds = %51
%59 = load i8*, i8** %8
%60 = call i32 @strlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds i8, i8* %59, i64 %61
store i8* %62, i8** %9
%63 = load i8*, i8** %9
%64 = call i32 @sscanf(i8* %63, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i64* %7)
%65 = icmp slt i32 %64, 1
br i1 %65, label %66, label %68
66: ; preds = %58
%67 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0))
br label %79
68: ; preds = %58
br label %69
69: ; preds = %68, %47
%70 = load i32, i32* %4
%71 = icmp ne i32 %70, 0
br i1 %71, label %72, label %76
72: ; preds = %69
%73 = load i64, i64* %7
%74 = load i64, i64* %6
%75 = icmp ult i64 %73, %74
br label %76
76: ; preds = %72, %69
%77 = phi i1 [ false, %69 ], [ %75, %72 ]
%78 = zext i1 %77 to i32
store i32 %78, i32* %3
br label %79
79: ; preds = %76, %66, %43, %21, %15
%80 = load i8*, i8** %8
%81 = call i32 @free(i8* %80)
%82 = load i32*, i32** %5
%83 = icmp ne i32* %82, null
br i1 %83, label %84, label %87
84: ; preds = %79
%85 = load i32*, i32** %5
%86 = call i32 @fclose(i32* %85)
br label %87
87: ; preds = %84, %79
%88 = load i32, i32* %3
ret i32 %88
}

LLVM IR (-O2):
@.str = [3 x i8] c"lo\00"
@.str.2 = [6 x i8] c"Size:\00"
@.str.3 = [7 x i8] c"%lu kB\00"
@.str.5 = [5 x i8] c"Rss:\00"
@str = [33 x i8] c"Unable to parse /proc/self/smaps\00"
@str.7 = [36 x i8] c"Unable to parse smaps entry for Rss\00"
@str.8 = [37 x i8] c"Unable to parse smaps entry for Size\00"
define i32 @is_vma_lock_on_fault(i64 %0) {
%2 = alloca i64
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i64
%6 = bitcast i64* %2 to i8*
%7 = bitcast i64* %3 to i8*
%8 = bitcast i8** %4 to i8*
store i8* null, i8** %4
%9 = bitcast i64* %5 to i8*
store i64 0, i64* %5
%10 = tail call i32 @is_vmflag_set(i64 %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i32 %10, 0
br i1 %11, label %62, label %12
12: ; preds = %1
%13 = tail call i32* @seek_to_smaps_entry(i64 %0)
%14 = icmp eq i32* %13, null
br i1 %14, label %18, label %15
15: ; preds = %12
%16 = call i64 @getline(i8** %4, i64* %5, i32* %13)
%17 = icmp sgt i64 %16, 0
br i1 %17, label %20, label %37
18: ; preds = %12
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str, i64 0, i64 0))
br label %62
20: ; preds = %15, %25
%21 = load i8*, i8** %4
%22 = call i32 @strstr(i8* %21, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%23 = icmp eq i32 %22, 0
%24 = load i8*, i8** %4
br i1 %23, label %25, label %29
25: ; preds = %20
%26 = call i32 @free(i8* %24)
store i8* null, i8** %4
store i64 0, i64* %5
%27 = call i64 @getline(i8** %4, i64* %5, i32* %13)
%28 = icmp sgt i64 %27, 0
br i1 %28, label %20, label %37
29: ; preds = %20
%30 = call i32 @strlen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %24, i64 %31
%33 = call i32 @sscanf(i8* %32, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i64* %2)
%34 = icmp slt i32 %33, 1
br i1 %34, label %35, label %37
35: ; preds = %29
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str.8, i64 0, i64 0))
br label %64
37: ; preds = %25, %15, %29
%38 = call i64 @getline(i8** %4, i64* %5, i32* %13)
%39 = icmp sgt i64 %38, 0
br i1 %39, label %40, label %57
40: ; preds = %37, %45
%41 = load i8*, i8** %4
%42 = call i32 @strstr(i8* %41, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%43 = icmp eq i32 %42, 0
%44 = load i8*, i8** %4
br i1 %43, label %45, label %49
45: ; preds = %40
%46 = call i32 @free(i8* %44)
store i8* null, i8** %4
store i64 0, i64* %5
%47 = call i64 @getline(i8** %4, i64* %5, i32* %13)
%48 = icmp sgt i64 %47, 0
br i1 %48, label %40, label %57
49: ; preds = %40
%50 = call i32 @strlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %44, i64 %51
%53 = call i32 @sscanf(i8* %52, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i64* %3)
%54 = icmp slt i32 %53, 1
br i1 %54, label %55, label %57
55: ; preds = %49
%56 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.7, i64 0, i64 0))
br label %64
57: ; preds = %45, %37, %49
%58 = load i64, i64* %3
%59 = load i64, i64* %2
%60 = icmp ult i64 %58, %59
%61 = zext i1 %60 to i32
br label %64
62: ; preds = %18, %1
%63 = tail call i32 @free(i8* null)
br label %69
64: ; preds = %57, %55, %35
%65 = phi i32 [ 0, %35 ], [ 0, %55 ], [ %61, %57 ]
%66 = load i8*, i8** %4
%67 = call i32 @free(i8* %66)
%68 = call i32 @fclose(i32* %13)
br label %69
69: ; preds = %62, %64
%70 = phi i32 [ 0, %62 ], [ %65, %64 ]
ret i32 %70
}