#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 pid_t;

/* Variables and functions */
int /*<<< orphan*/ AT_SYSINFO_EHDR;
unsigned long ENOENT;
int /*<<< orphan*/ SYS_exit;
int WEXITSTATUS(int);
int /*<<< orphan*/ WIFEXITED(int);
int errno;
int fork();
unsigned long getauxval(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ syscall(int /*<<< orphan*/, int);
int try_to_remap(void *, unsigned long);
int waitpid(int, int *, int /*<<< orphan*/);
int main(int argc, char **argv, char **envp) {
  pid_t child;
  child = fork();
  if (child == -1) {
    printf("[WARN]\tfailed to fork (%d): %m\n", errno);
    return 1;
  }
  if (child == 0) {
    unsigned long vdso_size = 4096;
    unsigned long auxval;
    int ret = -1;
    auxval = getauxval(AT_SYSINFO_EHDR);
    printf("\tAT_SYSINFO_EHDR is %#lx\n", auxval);
    if (!auxval || auxval == -ENOENT) {
      printf("[WARN]\tgetauxval failed\n");
      return 0;
    }
    while (ret < 0) {
      ret = try_to_remap((void *)auxval, vdso_size);
      vdso_size += 4096;
    }
    syscall(SYS_exit, ret);
  } else {
    int status;
    if (waitpid(child, &status, 0) != child || !WIFEXITED(status)) {
      printf("[FAIL]\tmremap() of the vDSO does not work on this kernel!\n");
      return 1;
    } else if (WEXITSTATUS(status) != 0) {
      printf("[FAIL]\tChild failed with %d\n", WEXITSTATUS(status));
      return 1;
    }
    printf("[OK]\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11d1 <main+0x51>
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1325 <main+0x1a5>
cmpl   $0x0,-0x1c(%rbp)
jne    1294 <main+0x114>
movq   $0x1000,-0x28(%rbp)
movl   $0xffffffff,-0x34(%rbp)
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
call   1050 <getauxval@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rsi
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpq   $0x0,-0x30(%rbp)
je     1232 <main+0xb2>
mov    -0x30(%rbp),%rax
xor    %ecx,%ecx
mov    0x2d9a(%rip),%rdx
sub    (%rdx),%rcx
cmp    %rcx,%rax
jne    124c <main+0xcc>
lea    0xe01(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1325 <main+0x1a5>
jmp    1251 <main+0xd1>
cmpl   $0x0,-0x34(%rbp)
jge    127e <main+0xfe>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1080 <try_to_remap@plt>
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1000,%rax
mov    %rax,-0x28(%rbp)
jmp    1251 <main+0xd1>
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
mov    -0x34(%rbp),%esi
call   1040 <syscall@plt>
jmp    131e <main+0x19e>
mov    -0x1c(%rbp),%edi
lea    -0x38(%rbp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
cmp    -0x1c(%rbp),%eax
jne    12bc <main+0x13c>
mov    -0x38(%rbp),%edi
call   1090 <WIFEXITED@plt>
cmp    $0x0,%eax
jne    12d6 <main+0x156>
lea    0xd90(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1325 <main+0x1a5>
mov    -0x38(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
cmp    $0x0,%eax
je     130b <main+0x18b>
mov    -0x38(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xd96(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1325 <main+0x1a5>
jmp    1310 <main+0x190>
lea    0xd94(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     11c6 <main+0x36>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    120f <main+0x7f>
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
lea    0xe4b(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%ebp
jmp    12a6 <main+0x116>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
call   1060 <getauxval@plt>
mov    %rax,%rbx
lea    0xe42(%rip),%rdi
xor    %ebp,%ebp
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
test   %rbx,%rbx
je     11fe <main+0x6e>
mov    0x2dca(%rip),%rax
mov    (%rax),%rax
add    %rbx,%rax
jne    1265 <main+0xd5>
lea    0xe91(%rip),%rdi
call   1030 <puts@plt>
jmp    12a6 <main+0x116>
lea    0x4(%rsp),%rsi
mov    %ebx,%edi
xor    %edx,%edx
call   1080 <waitpid@plt>
cmp    %ebx,%eax
jne    1252 <main+0xc2>
mov    0x4(%rsp),%edi
call   10a0 <WIFEXITED@plt>
test   %eax,%eax
je     1252 <main+0xc2>
mov    0x4(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
test   %eax,%eax
je     1298 <main+0x108>
mov    0x4(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
lea    0xdef(%rip),%rdi
mov    %eax,%esi
jmp    11b5 <main+0x25>
lea    0xdfe(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebp
jmp    12a6 <main+0x116>
mov    $0x1000,%ebp
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1090 <try_to_remap@plt>
add    $0x1000,%rbp
test   %eax,%eax
js     1270 <main+0xe0>
mov    0x2d5b(%rip),%rcx
mov    (%rcx),%edi
mov    %eax,%esi
call   1050 <syscall@plt>
jmp    12a4 <main+0x114>
lea    0xdf2(%rip),%rdi
call   1030 <puts@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [32 x i8] c"[WARN]\09failed to fork (%d): %m\0A\00"
@errno = global i32 0
@AT_SYSINFO_EHDR = global i32 0
@.str.1 = [26 x i8] c"\09AT_SYSINFO_EHDR is %#lx\0A\00"
@ENOENT = global i64 0
@.str.2 = [25 x i8] c"[WARN]\09getauxval failed\0A\00"
@SYS_exit = global i32 0
@.str.3 = [59 x i8] c"[FAIL]\09mremap() of the vDSO does not work on this kernel!\0A\00"
@.str.4 = [29 x i8] c"[FAIL]\09Child failed with %d\0A\00"
@.str.5 = [6 x i8] c"[OK]\0A\00"
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca i32
%9 = alloca i64
%10 = alloca i64
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %4
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
%13 = call i32 (...) @fork()
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %3
%17 = load i32, i32* @errno
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %17)
store i32 1, i32* %4
br label %74
19: ; preds = %3
%20 = load i32, i32* %8
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %51
22: ; preds = %19
store i64 4096, i64* %9
store i32 -1, i32* %11
%23 = load i32, i32* @AT_SYSINFO_EHDR
%24 = call i64 @getauxval(i32 %23)
store i64 %24, i64* %10
%25 = load i64, i64* %10
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i64 %25)
%27 = load i64, i64* %10
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %22
%30 = load i64, i64* %10
%31 = load i64, i64* @ENOENT
%32 = sub i64 0, %31
%33 = icmp eq i64 %30, %32
br i1 %33, label %34, label %36
34: ; preds = %29, %22
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %4
br label %74
36: ; preds = %29
br label %37
37: ; preds = %40, %36
%38 = load i32, i32* %11
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %47
40: ; preds = %37
%41 = load i64, i64* %10
%42 = inttoptr i64 %41 to i8*
%43 = load i64, i64* %9
%44 = call i32 @try_to_remap(i8* %42, i64 %43)
store i32 %44, i32* %11
%45 = load i64, i64* %9
%46 = add i64 %45, 4096
store i64 %46, i64* %9
br label %37
47: ; preds = %37
%48 = load i32, i32* @SYS_exit
%49 = load i32, i32* %11
%50 = call i32 @syscall(i32 %48, i32 %49)
br label %73
51: ; preds = %19
%52 = load i32, i32* %8
%53 = call i32 @waitpid(i32 %52, i32* %12, i32 0)
%54 = load i32, i32* %8
%55 = icmp ne i32 %53, %54
br i1 %55, label %60, label %56
56: ; preds = %51
%57 = load i32, i32* %12
%58 = call i32 @WIFEXITED(i32 %57)
%59 = icmp ne i32 %58, 0
br i1 %59, label %62, label %60
60: ; preds = %56, %51
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %4
br label %74
62: ; preds = %56
%63 = load i32, i32* %12
%64 = call i32 @WEXITSTATUS(i32 %63)
%65 = icmp ne i32 %64, 0
br i1 %65, label %66, label %70
66: ; preds = %62
%67 = load i32, i32* %12
%68 = call i32 @WEXITSTATUS(i32 %67)
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %68)
store i32 1, i32* %4
br label %74
70: ; preds = %62
br label %71
71: ; preds = %70
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %73
73: ; preds = %71, %47
store i32 0, i32* %4
br label %74
74: ; preds = %73, %66, %60, %34, %16
%75 = load i32, i32* %4
ret i32 %75
}

LLVM IR (-O2):
@.str = [32 x i8] c"[WARN]\09failed to fork (%d): %m\0A\00"
@errno = global i32 0
@AT_SYSINFO_EHDR = global i32 0
@.str.1 = [26 x i8] c"\09AT_SYSINFO_EHDR is %#lx\0A\00"
@ENOENT = global i64 0
@SYS_exit = global i32 0
@.str.4 = [29 x i8] c"[FAIL]\09Child failed with %d\0A\00"
@str = [58 x i8] c"[FAIL]\09mremap() of the vDSO does not work on this kernel!\00"
@str.6 = [5 x i8] c"[OK]\00"
@str.7 = [24 x i8] c"[WARN]\09getauxval failed\00"
define i32 @main(i32 %0, i8** nocapture readnone %1, i8** nocapture readnone %2) {
%4 = alloca i32
%5 = tail call i32 (...) @fork()
switch i32 %5, label %30 [
i32 -1, label %6
i32 0, label %9
]
6: ; preds = %3
%7 = load i32, i32* @errno
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %7)
br label %51
9: ; preds = %3
%10 = load i32, i32* @AT_SYSINFO_EHDR
%11 = tail call i64 @getauxval(i32 %10)
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i64 %11)
%13 = icmp eq i64 %11, 0
br i1 %13, label %20, label %14
14: ; preds = %9
%15 = load i64, i64* @ENOENT
%16 = sub i64 0, %15
%17 = icmp eq i64 %11, %16
br i1 %17, label %20, label %18
18: ; preds = %14
%19 = inttoptr i64 %11 to i8*
br label %22
20: ; preds = %14, %9
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.7, i64 0, i64 0))
br label %51
22: ; preds = %18, %22
%23 = phi i64 [ 4096, %18 ], [ %25, %22 ]
%24 = tail call i32 @try_to_remap(i8* %19, i64 %23)
%25 = add i64 %23, 4096
%26 = icmp slt i32 %24, 0
br i1 %26, label %22, label %27
27: ; preds = %22
%28 = load i32, i32* @SYS_exit
%29 = tail call i32 @syscall(i32 %28, i32 %24)
br label %51
30: ; preds = %3
%31 = bitcast i32* %4 to i8*
%32 = call i32 @waitpid(i32 %5, i32* %4, i32 0)
%33 = icmp eq i32 %32, %5
br i1 %33, label %34, label %38
34: ; preds = %30
%35 = load i32, i32* %4
%36 = call i32 @WIFEXITED(i32 %35)
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %34, %30
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([58 x i8], [58 x i8]* @str, i64 0, i64 0))
br label %48
40: ; preds = %34
%41 = load i32, i32* %4
%42 = call i32 @WEXITSTATUS(i32 %41)
%43 = icmp eq i32 %42, 0
br i1 %43, label %49, label %44
44: ; preds = %40
%45 = load i32, i32* %4
%46 = call i32 @WEXITSTATUS(i32 %45)
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %46)
br label %48
48: ; preds = %44, %38
br label %51
49: ; preds = %40
%50 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str.6, i64 0, i64 0))
br label %51
51: ; preds = %27, %49, %48, %20, %6
%52 = phi i32 [ 1, %6 ], [ 0, %20 ], [ 1, %48 ], [ 0, %49 ], [ 0, %27 ]
ret i32 %52
}