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

/* Variables and functions */
int /*<<< orphan*/ MADV_DONTNEED;
int MS_ASYNC;
int MS_INVALIDATE;
int MS_SYNC;
int madvise(void *, size_t, int /*<<< orphan*/);
int msync(void *, size_t, int);
size_t pageAlignLen(void *, size_t);
void *pageAlignStart(void *);
int sasMsyncPurge(void *startAddr, size_t size, int asyncBool) {
  int rc;
  int flags;
  void *alignAddr = pageAlignStart(startAddr);
  size_t alignSize = pageAlignLen(startAddr, size);
  if (asyncBool)
    flags = (MS_ASYNC | MS_INVALIDATE);
  else
    flags = (MS_SYNC | MS_INVALIDATE);
  rc = msync(alignAddr, alignSize, flags);
  if (rc)
    return rc;
  return madvise(alignAddr, alignSize, MADV_DONTNEED);
}


x86 assembly (clang, -O0):
<sasMsyncPurge>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <pageAlignStart@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <pageAlignLen@plt>
mov    %rax,-0x38(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1195 <sasMsyncPurge+0x55>
mov    0x2e5e(%rip),%rax
mov    (%rax),%eax
mov    0x2e5d(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x24(%rbp)
jmp    11aa <sasMsyncPurge+0x6a>
mov    0x2e2c(%rip),%rax
mov    (%rax),%eax
mov    0x2e43(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x24(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1040 <msync@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     11d2 <sasMsyncPurge+0x92>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11eb <sasMsyncPurge+0xab>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    0x2def(%rip),%rax
mov    (%rax),%edx
call   1060 <madvise@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sasMsyncPurge>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %rdi,%rbp
call   1030 <pageAlignStart@plt>
mov    %rax,%r14
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1050 <pageAlignLen@plt>
mov    %rax,%rbx
test   %r15d,%r15d
jne    1174 <sasMsyncPurge+0x34>
mov    0x2e56(%rip),%rax
jmp    117b <sasMsyncPurge+0x3b>
mov    0x2e65(%rip),%rax
mov    0x2e66(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <msync@plt>
test   %eax,%eax
je     11a0 <sasMsyncPurge+0x60>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e29(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
mov    %rbx,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <madvise@plt>

LLVM IR (-O0):
@MS_ASYNC = global i32 0
@MS_INVALIDATE = global i32 0
@MS_SYNC = global i32 0
@MADV_DONTNEED = global i32 0
define i32 @sasMsyncPurge(i8* %0, i64 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i64
store i8* %0, i8** %5
store i64 %1, i64* %6
store i32 %2, i32* %7
%12 = load i8*, i8** %5
%13 = call i8* @pageAlignStart(i8* %12)
store i8* %13, i8** %10
%14 = load i8*, i8** %5
%15 = load i64, i64* %6
%16 = call i64 @pageAlignLen(i8* %14, i64 %15)
store i64 %16, i64* %11
%17 = load i32, i32* %7
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %3
%20 = load i32, i32* @MS_ASYNC
%21 = load i32, i32* @MS_INVALIDATE
%22 = or i32 %20, %21
store i32 %22, i32* %9
br label %27
23: ; preds = %3
%24 = load i32, i32* @MS_SYNC
%25 = load i32, i32* @MS_INVALIDATE
%26 = or i32 %24, %25
store i32 %26, i32* %9
br label %27
27: ; preds = %23, %19
%28 = load i8*, i8** %10
%29 = load i64, i64* %11
%30 = load i32, i32* %9
%31 = call i32 @msync(i8* %28, i64 %29, i32 %30)
store i32 %31, i32* %8
%32 = load i32, i32* %8
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %27
%35 = load i32, i32* %8
store i32 %35, i32* %4
br label %41
36: ; preds = %27
%37 = load i8*, i8** %10
%38 = load i64, i64* %11
%39 = load i32, i32* @MADV_DONTNEED
%40 = call i32 @madvise(i8* %37, i64 %38, i32 %39)
store i32 %40, i32* %4
br label %41
41: ; preds = %36, %34
%42 = load i32, i32* %4
ret i32 %42
}

LLVM IR (-O2):
@MS_ASYNC = global i32 0
@MS_INVALIDATE = global i32 0
@MS_SYNC = global i32 0
@MADV_DONTNEED = global i32 0
define i32 @sasMsyncPurge(i8* %0, i64 %1, i32 %2) {
%4 = tail call i8* @pageAlignStart(i8* %0)
%5 = tail call i64 @pageAlignLen(i8* %0, i64 %1)
%6 = icmp eq i32 %2, 0
%7 = load i32, i32* @MS_SYNC
%8 = load i32, i32* @MS_ASYNC
%9 = select i1 %6, i32 %7, i32 %8
%10 = load i32, i32* @MS_INVALIDATE
%11 = or i32 %10, %9
%12 = tail call i32 @msync(i8* %4, i64 %5, i32 %11)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %3
%15 = load i32, i32* @MADV_DONTNEED
%16 = tail call i32 @madvise(i8* %4, i64 %5, i32 %15)
br label %17
17: ; preds = %3, %14
%18 = phi i32 [ %16, %14 ], [ %12, %3 ]
ret i32 %18
}