#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 void *uint64_t;
struct uffdio_copy {
  size_t len;
  scalar_t__ mode;
  void *src;
  void *dst;
};

/* Variables and functions */
int /*<<< orphan*/ DecompressPage(void *, void *, void *, size_t, void *);
#define EAGAIN 129
#define EINVAL 128
int /*<<< orphan*/ UFFDIO_COPY;
int errno;
scalar_t__ ioctl(int, int /*<<< orphan*/, struct uffdio_copy *);
int /*<<< orphan*/ perror(char *);
void HandlePageFault(int uffd, void *pagefault_address, void *cut_start,
                     void *compressed_start, size_t page_size, void *buffer) {
  DecompressPage(cut_start, compressed_start, pagefault_address, page_size,
                 buffer);
  struct uffdio_copy copy;
  copy.dst = (uint64_t)pagefault_address;
  copy.src = (uint64_t)buffer;
  copy.len = page_size;
  copy.mode = 0;
  if (ioctl(uffd, UFFDIO_COPY, &copy)) {
    switch (errno) {
    case EINVAL:
    case EAGAIN:
      perror("UFFDIO_COPY failed");
    }
  }
}


x86 assembly (clang, -O0):
<HandlePageFault>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    -0x30(%rbp),%r8
call   1040 <DecompressPage@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x50(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
lea    -0x50(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
je     11d2 <HandlePageFault+0xa2>
mov    0x2e32(%rip),%rax
mov    (%rax),%eax
add    $0xffffff80,%eax
sub    $0x1,%eax
ja     11cd <HandlePageFault+0x9d>
jmp    11c1 <HandlePageFault+0x91>
lea    0xe38(%rip),%rdi
call   1050 <perror@plt>
jmp    11d2 <HandlePageFault+0xa2>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<HandlePageFault>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %r9,%r15
mov    %r8,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %rdx,%rdi
mov    %rcx,%rsi
mov    %rbx,%rdx
mov    %r8,%rcx
mov    %r9,%r8
call   1040 <DecompressPage@plt>
mov    %rbx,0x20(%rsp)
mov    %r15,0x18(%rsp)
mov    %r14,0x8(%rsp)
movq   $0x0,0x10(%rsp)
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
je     119e <HandlePageFault+0x6e>
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
and    $0xfffffffe,%eax
cmp    $0x80,%eax
je     11a9 <HandlePageFault+0x79>
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe50(%rip),%rdi
call   1050 <perror@plt>
jmp    119e <HandlePageFault+0x6e>

LLVM IR (-O0):
%struct.uffdio_copy = type { i64, i64, i8*, i8* }
@UFFDIO_COPY = global i32 0
@errno = global i32 0
@.str = [19 x i8] c"UFFDIO_COPY failed\00"
define void @HandlePageFault(i32 %0, i8* %1, i8* %2, i8* %3, i64 %4, i8* %5) {
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i8*
%13 = alloca %struct.uffdio_copy
store i32 %0, i32* %7
store i8* %1, i8** %8
store i8* %2, i8** %9
store i8* %3, i8** %10
store i64 %4, i64* %11
store i8* %5, i8** %12
%14 = load i8*, i8** %9
%15 = load i8*, i8** %10
%16 = load i8*, i8** %8
%17 = load i64, i64* %11
%18 = load i8*, i8** %12
%19 = call i32 @DecompressPage(i8* %14, i8* %15, i8* %16, i64 %17, i8* %18)
%20 = load i8*, i8** %8
%21 = getelementptr inbounds %struct.uffdio_copy, %struct.uffdio_copy* %13, i32 0, i32 3
store i8* %20, i8** %21
%22 = load i8*, i8** %12
%23 = getelementptr inbounds %struct.uffdio_copy, %struct.uffdio_copy* %13, i32 0, i32 2
store i8* %22, i8** %23
%24 = load i64, i64* %11
%25 = getelementptr inbounds %struct.uffdio_copy, %struct.uffdio_copy* %13, i32 0, i32 0
store i64 %24, i64* %25
%26 = getelementptr inbounds %struct.uffdio_copy, %struct.uffdio_copy* %13, i32 0, i32 1
store i64 0, i64* %26
%27 = load i32, i32* %7
%28 = load i32, i32* @UFFDIO_COPY
%29 = call i64 @ioctl(i32 %27, i32 %28, %struct.uffdio_copy* %13)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %6
%32 = load i32, i32* @errno
switch i32 %32, label %35 [
i32 128, label %33
i32 129, label %33
]
33: ; preds = %31, %31
%34 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %35
35: ; preds = %33, %31
br label %36
36: ; preds = %35, %6
ret void
}

LLVM IR (-O2):
%struct.uffdio_copy = type { i64, i64, i8*, i8* }
@UFFDIO_COPY = global i32 0
@errno = global i32 0
@.str = [19 x i8] c"UFFDIO_COPY failed\00"
define void @HandlePageFault(i32 %0, i8* %1, i8* %2, i8* %3, i64 %4, i8* %5) {
%7 = alloca %struct.uffdio_copy
%8 = tail call i32 @DecompressPage(i8* %2, i8* %3, i8* %1, i64 %4, i8* %5)
%9 = bitcast %struct.uffdio_copy* %7 to i8*
%10 = getelementptr inbounds %struct.uffdio_copy, %struct.uffdio_copy* %7, i64 0, i32 3
store i8* %1, i8** %10
%11 = getelementptr inbounds %struct.uffdio_copy, %struct.uffdio_copy* %7, i64 0, i32 2
store i8* %5, i8** %11
%12 = getelementptr inbounds %struct.uffdio_copy, %struct.uffdio_copy* %7, i64 0, i32 0
store i64 %4, i64* %12
%13 = getelementptr inbounds %struct.uffdio_copy, %struct.uffdio_copy* %7, i64 0, i32 1
store i64 0, i64* %13
%14 = load i32, i32* @UFFDIO_COPY
%15 = call i64 @ioctl(i32 %0, i32 %14, %struct.uffdio_copy* %7)
%16 = icmp eq i64 %15, 0
br i1 %16, label %23, label %17
17: ; preds = %6
%18 = load i32, i32* @errno
%19 = and i32 %18, -2
%20 = icmp eq i32 %19, 128
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %17, %21, %6
ret void
}