#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 /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ *seek_to_smaps_entry(unsigned long);
int strlen(char *);
int /*<<< orphan*/ *strstr(char *, char const *);
bool is_vmflag_set(unsigned long addr, const char *vmflag) {
  char *line = NULL;
  char *flags;
  size_t size = 0;
  bool ret = false;
  FILE *smaps;
  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, "VmFlags:")) {
      free(line);
      line = NULL;
      size = 0;
      continue;
    }
    flags = line + strlen("VmFlags:");
    ret = (strstr(flags, vmflag) != NULL);
    goto out;
  }
out:
  free(line);
  fclose(smaps);
  return ret;
}


x86 assembly (clang, -O0):
<is_vmflag_set>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <seek_to_smaps_entry@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11c0 <is_vmflag_set+0x50>
lea    0xe4a(%rip),%rdi
call   1060 <printf@plt>
jmp    1262 <is_vmflag_set+0xf2>
jmp    11c5 <is_vmflag_set+0x55>
mov    -0x38(%rbp),%rdx
lea    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1080 <getline@plt>
cmp    $0x0,%rax
jle    125d <is_vmflag_set+0xed>
mov    -0x18(%rbp),%rdi
lea    0xe37(%rip),%rsi
call   1090 <strstr@plt>
cmp    $0x0,%rax
jne    1218 <is_vmflag_set+0xa8>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
jmp    11c5 <is_vmflag_set+0x55>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
lea    0xdfb(%rip),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1090 <strstr@plt>
cmp    $0x0,%rax
setne  %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x2c(%rbp)
jmp    1262 <is_vmflag_set+0xf2>
jmp    1262 <is_vmflag_set+0xf2>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x2c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_vmflag_set>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbp
movq   $0x0,(%rsp)
movq   $0x0,0x8(%rsp)
call   1070 <seek_to_smaps_entry@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1212 <is_vmflag_set+0xa2>
mov    %rsp,%rdi
lea    0x8(%rsp),%rsi
mov    %rbx,%rdx
call   1080 <getline@plt>
xor    %r15d,%r15d
test   %rax,%rax
jle    124b <is_vmflag_set+0xdb>
mov    %rbp,0x10(%rsp)
lea    0xe39(%rip),%r13
mov    %rsp,%r12
lea    0x8(%rsp),%rbp
nop
mov    (%rsp),%r14
mov    %r14,%rdi
mov    %r13,%rsi
call   1090 <strstr@plt>
test   %rax,%rax
jne    1223 <is_vmflag_set+0xb3>
mov    %r14,%rdi
call   1030 <free@plt>
movq   $0x0,(%rsp)
movq   $0x0,0x8(%rsp)
mov    %r12,%rdi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1080 <getline@plt>
test   %rax,%rax
jg     11d0 <is_vmflag_set+0x60>
jmp    124b <is_vmflag_set+0xdb>
lea    0xdf0(%rip),%rdi
call   1040 <puts@plt>
xor    %r15d,%r15d
jmp    124b <is_vmflag_set+0xdb>
lea    0xdd6(%rip),%rdi
call   1060 <strlen@plt>
cltq
add    %rax,%r14
mov    %r14,%rdi
mov    0x10(%rsp),%rsi
call   1090 <strstr@plt>
xor    %r15d,%r15d
test   %rax,%rax
setne  %r15b
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [34 x i8] c"Unable to parse /proc/self/smaps\0A\00"
@.str.1 = [9 x i8] c"VmFlags:\00"
define i32 @is_vmflag_set(i64 %0, i8* %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32*
store i64 %0, i64* %3
store i8* %1, i8** %4
store i8* null, i8** %5
store i64 0, i64* %7
store i32 0, i32* %8
%10 = load i64, i64* %3
%11 = call i32* @seek_to_smaps_entry(i64 %10)
store i32* %11, i32** %9
%12 = load i32*, i32** %9
%13 = icmp ne i32* %12, null
br i1 %13, label %16, label %14
14: ; preds = %2
%15 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
br label %39
16: ; preds = %2
br label %17
17: ; preds = %25, %16
%18 = load i32*, i32** %9
%19 = call i64 @getline(i8** %5, i64* %7, i32* %18)
%20 = icmp sgt i64 %19, 0
br i1 %20, label %21, label %38
21: ; preds = %17
%22 = load i8*, i8** %5
%23 = call i32* @strstr(i8* %22, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%24 = icmp ne i32* %23, null
br i1 %24, label %28, label %25
25: ; preds = %21
%26 = load i8*, i8** %5
%27 = call i32 @free(i8* %26)
store i8* null, i8** %5
store i64 0, i64* %7
br label %17
28: ; preds = %21
%29 = load i8*, i8** %5
%30 = call i32 @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
store i8* %32, i8** %6
%33 = load i8*, i8** %6
%34 = load i8*, i8** %4
%35 = call i32* @strstr(i8* %33, i8* %34)
%36 = icmp ne i32* %35, null
%37 = zext i1 %36 to i32
store i32 %37, i32* %8
br label %39
38: ; preds = %17
br label %39
39: ; preds = %38, %28, %14
%40 = load i8*, i8** %5
%41 = call i32 @free(i8* %40)
%42 = load i32*, i32** %9
%43 = call i32 @fclose(i32* %42)
%44 = load i32, i32* %8
ret i32 %44
}

LLVM IR (-O2):
@.str.1 = [9 x i8] c"VmFlags:\00"
@str = [33 x i8] c"Unable to parse /proc/self/smaps\00"
define i32 @is_vmflag_set(i64 %0, i8* nocapture %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%6 = bitcast i64* %4 to i8*
store i64 0, i64* %4
%7 = tail call i32* @seek_to_smaps_entry(i64 %0)
%8 = icmp eq i32* %7, null
br i1 %8, label %12, label %9
9: ; preds = %2
%10 = call i64 @getline(i8** %3, i64* %4, i32* %7)
%11 = icmp sgt i64 %10, 0
br i1 %11, label %14, label %29
12: ; preds = %2
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str, i64 0, i64 0))
br label %29
14: ; preds = %9, %18
%15 = load i8*, i8** %3
%16 = call i32* @strstr(i8* dereferenceable(1) %15, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %22
18: ; preds = %14
%19 = call i32 @free(i8* %15)
store i8* null, i8** %3
store i64 0, i64* %4
%20 = call i64 @getline(i8** %3, i64* %4, i32* %7)
%21 = icmp sgt i64 %20, 0
br i1 %21, label %14, label %29
22: ; preds = %14
%23 = call i32 @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %15, i64 %24
%26 = call i32* @strstr(i8* dereferenceable(1) %25, i8* dereferenceable(1) %1)
%27 = icmp ne i32* %26, null
%28 = zext i1 %27 to i32
br label %29
29: ; preds = %18, %9, %22, %12
%30 = phi i32 [ %28, %22 ], [ 0, %12 ], [ 0, %9 ], [ 0, %18 ]
%31 = load i8*, i8** %3
%32 = call i32 @free(i8* %31)
%33 = call i32 @fclose(i32* %7)
ret i32 %30
}