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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int sscanf(char *, char *, void **, void **, int *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strncmp(char *, char *, int);
int find_vdso_map(void **start, void **end) {
  FILE *maps;
  char line[128];
  int found = 0;
  maps = fopen("/proc/self/maps", "r");
  if (!maps) {
    fprintf(stderr, "vdso: cannot open maps\n");
    return -1;
  }
  while (!found && fgets(line, sizeof(line), maps)) {
    int m = -1;
    if (2 != sscanf(line, "%p-%p r-xp %*x %*x:%*x %*u %n", start, end, &m)) {
      continue;
    }
    if (m < 0) {
      continue;
    }
    if (!strncmp(&line[m], "[vdso]", sizeof("[vdso]") - 1)) {
      found = 1;
    }
  }
  fclose(maps);
  return !found;
}


x86 assembly (clang, -O0):
<find_vdso_map>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0xa4(%rbp)
lea    0xe7c(%rip),%rdi
lea    0xe85(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c0 <find_vdso_map+0x60>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
lea    0xe63(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b8 <find_vdso_map+0x158>
jmp    11c5 <find_vdso_map+0x65>
xor    %eax,%eax
cmpl   $0x0,-0xa4(%rbp)
mov    %al,-0xa9(%rbp)
jne    11fc <find_vdso_map+0x9c>
lea    -0xa0(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x80,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0xa9(%rbp)
mov    -0xa9(%rbp),%al
test   $0x1,%al
jne    120f <find_vdso_map+0xaf>
jmp    129b <find_vdso_map+0x13b>
movl   $0xffffffff,-0xa8(%rbp)
ff ff ff
lea    -0xa0(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xdfb(%rip),%rsi
lea    -0xa8(%rbp),%r8
call   1070 <sscanf@plt>
mov    %eax,%ecx
mov    $0x2,%eax
cmp    %ecx,%eax
je     124f <find_vdso_map+0xef>
jmp    11c5 <find_vdso_map+0x65>
cmpl   $0x0,-0xa8(%rbp)
jge    1261 <find_vdso_map+0x101>
jmp    11c5 <find_vdso_map+0x65>
movslq -0xa8(%rbp),%rax
lea    -0xa0(%rbp),%rdi
add    %rax,%rdi
lea    0xdcf(%rip),%rsi
mov    $0x6,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    1296 <find_vdso_map+0x136>
movl   $0x1,-0xa4(%rbp)
jmp    11c5 <find_vdso_map+0x65>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
cmpl   $0x0,-0xa4(%rbp)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_vdso_map>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe82(%rip),%rdi
lea    0xe8b(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1230 <find_vdso_map+0xd0>
mov    %rax,%rbx
lea    0x10(%rsp),%r12
lea    0xc(%rsp),%rbp
jmp    11b4 <find_vdso_map+0x54>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
test   %ecx,%ecx
jne    121d <find_vdso_map+0xbd>
mov    %r12,%rdi
mov    $0x80,%esi
mov    %rbx,%rdx
call   1050 <fgets@plt>
mov    %rax,%r13
test   %rax,%rax
je     121d <find_vdso_map+0xbd>
movl   $0xffffffff,0xc(%rsp)
ff
mov    %r12,%rdi
lea    0xe4c(%rip),%rsi
mov    %r15,%rdx
mov    %r14,%rcx
mov    %rbp,%r8
call   1070 <sscanf@plt>
xor    %ecx,%ecx
cmp    $0x2,%eax
jne    11b0 <find_vdso_map+0x50>
mov    0xc(%rsp),%eax
test   %eax,%eax
js     11b0 <find_vdso_map+0x50>
lea    (%rsp,%rax,1),%rdi
add    $0x10,%rdi
lea    0xe3e(%rip),%rsi
mov    $0x6,%edx
call   1030 <strncmp@plt>
xor    %ecx,%ecx
test   %eax,%eax
sete   %cl
jmp    11b0 <find_vdso_map+0x50>
xor    %ebp,%ebp
test   %r13,%r13
sete   %bpl
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    124a <find_vdso_map+0xea>
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
lea    0xdd2(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x98,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"/proc/self/maps\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [24 x i8] c"vdso: cannot open maps\0A\00"
@.str.3 = [30 x i8] c"%p-%p r-xp %*x %*x:%*x %*u %n\00"
@.str.4 = [7 x i8] c"[vdso]\00"
define i32 @find_vdso_map(i8** %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca [128 x i8]
%8 = alloca i32
%9 = alloca i32
store i8** %0, i8*** %4
store i8** %1, i8*** %5
store i32 0, i32* %8
%10 = call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %10, i32** %6
%11 = load i32*, i32** %6
%12 = icmp ne i32* %11, null
br i1 %12, label %16, label %13
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %53
16: ; preds = %2
br label %17
17: ; preds = %45, %37, %33, %16
%18 = load i32, i32* %8
%19 = icmp ne i32 %18, 0
br i1 %19, label %25, label %20
20: ; preds = %17
%21 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%22 = load i32*, i32** %6
%23 = call i64 @fgets(i8* %21, i32 128, i32* %22)
%24 = icmp ne i64 %23, 0
br label %25
25: ; preds = %20, %17
%26 = phi i1 [ false, %17 ], [ %24, %20 ]
br i1 %26, label %27, label %46
27: ; preds = %25
store i32 -1, i32* %9
%28 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%29 = load i8**, i8*** %4
%30 = load i8**, i8*** %5
%31 = call i32 @sscanf(i8* %28, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8** %29, i8** %30, i32* %9)
%32 = icmp ne i32 2, %31
br i1 %32, label %33, label %34
33: ; preds = %27
br label %17
34: ; preds = %27
%35 = load i32, i32* %9
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %34
br label %17
38: ; preds = %34
%39 = load i32, i32* %9
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 %40
%42 = call i32 @strncmp(i8* %41, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i32 6)
%43 = icmp ne i32 %42, 0
br i1 %43, label %45, label %44
44: ; preds = %38
store i32 1, i32* %8
br label %45
45: ; preds = %44, %38
br label %17
46: ; preds = %25
%47 = load i32*, i32** %6
%48 = call i32 @fclose(i32* %47)
%49 = load i32, i32* %8
%50 = icmp ne i32 %49, 0
%51 = xor i1 %50, true
%52 = zext i1 %51 to i32
store i32 %52, i32* %3
br label %53
53: ; preds = %46, %13
%54 = load i32, i32* %3
ret i32 %54
}

LLVM IR (-O2):
@.str = [16 x i8] c"/proc/self/maps\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [24 x i8] c"vdso: cannot open maps\0A\00"
@.str.3 = [30 x i8] c"%p-%p r-xp %*x %*x:%*x %*u %n\00"
@.str.4 = [7 x i8] c"[vdso]\00"
define i32 @find_vdso_map(i8** %0, i8** %1) {
%3 = alloca [128 x i8]
%4 = alloca i32
%5 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%6 = tail call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %10, label %8
8: ; preds = %2
%9 = bitcast i32* %4 to i8*
br label %13
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %34
13: ; preds = %8, %28
%14 = call i64 @fgets(i8* %5, i32 128, i32* %6)
%15 = icmp eq i64 %14, 0
br i1 %15, label %31, label %16
16: ; preds = %13
store i32 -1, i32* %4
%17 = call i32 @sscanf(i8* %5, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8** %0, i8** %1, i32* %4)
%18 = icmp eq i32 %17, 2
br i1 %18, label %19, label %28
19: ; preds = %16
%20 = load i32, i32* %4
%21 = icmp slt i32 %20, 0
br i1 %21, label %28, label %22
22: ; preds = %19
%23 = zext i32 %20 to i64
%24 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 %23
%25 = call i32 @strncmp(i8* %24, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i32 6)
%26 = icmp eq i32 %25, 0
%27 = zext i1 %26 to i32
br label %28
28: ; preds = %19, %16, %22
%29 = phi i32 [ %27, %22 ], [ 0, %16 ], [ 0, %19 ]
%30 = icmp eq i32 %29, 0
br i1 %30, label %13, label %31
31: ; preds = %28, %13
%32 = call i32 @fclose(i32* %6)
%33 = zext i1 %15 to i32
br label %34
34: ; preds = %31, %10
%35 = phi i32 [ %33, %31 ], [ -1, %10 ]
ret i32 %35
}