#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
typedef scalar_t__ uint64_t;
struct TYPE_2__ {
  size_t len;
  scalar_t__ start;
};
struct uffdio_register {
  int /*<<< orphan*/ mode;
  TYPE_1__ range;
  int /*<<< orphan*/ features;
  int /*<<< orphan*/ api;
};
struct uffdio_api {
  int /*<<< orphan*/ mode;
  TYPE_1__ range;
  int /*<<< orphan*/ features;
  int /*<<< orphan*/ api;
};

/* Variables and functions */
int O_CLOEXEC;
int O_NONBLOCK;
int /*<<< orphan*/ UFFDIO_API;
int /*<<< orphan*/ UFFDIO_REGISTER;
int /*<<< orphan*/ UFFDIO_REGISTER_MODE_MISSING;
int /*<<< orphan*/ UFFD_API;
int /*<<< orphan*/ UFFD_FEATURE_EVENT_FORK;
int /*<<< orphan*/ __NR_userfaultfd;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct uffdio_register *);
int /*<<< orphan*/ perror(char *);
int syscall(int /*<<< orphan*/, int);
int SetupUserfaultFd(void *cut_start, size_t cut_length) {
  int uffd = syscall(__NR_userfaultfd, O_NONBLOCK | O_CLOEXEC);
  if (uffd == -1) {
    perror("Userfaultfd syscall failed");
    return -1;
  }
  struct uffdio_api api = {.api = UFFD_API,
                           .features = UFFD_FEATURE_EVENT_FORK};
  if (ioctl(uffd, UFFDIO_API, &api)) {
    perror("ioctl UFFDIO_API failed");
    close(uffd);
    return -1;
  }
  struct uffdio_register uffd_register;
  uffd_register.range.start = (uint64_t)cut_start;
  uffd_register.range.len = cut_length;
  uffd_register.mode = UFFDIO_REGISTER_MODE_MISSING;
  if (ioctl(uffd, UFFDIO_REGISTER, &uffd_register)) {
    perror("ioctl UFFDIO_REGISTER failed");
    close(uffd);
    return -1;
  }
  return uffd;
}


x86 assembly (clang, -O0):
<SetupUserfaultFd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e79(%rip),%rax
mov    (%rax),%edi
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
mov    0x2e2f(%rip),%rax
or     (%rax),%esi
call   1060 <syscall@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11a5 <SetupUserfaultFd+0x55>
lea    0xe6c(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126d <SetupUserfaultFd+0x11d>
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1030 <memset@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2df0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
je     120c <SetupUserfaultFd+0xbc>
lea    0xe28(%rip),%rdi
call   1070 <perror@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126d <SetupUserfaultFd+0x11d>
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    0x2db5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x60(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2d96(%rip),%rax
mov    (%rax),%esi
lea    -0x60(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
je     1267 <SetupUserfaultFd+0x117>
lea    0xde5(%rip),%rdi
call   1070 <perror@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126d <SetupUserfaultFd+0x11d>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SetupUserfaultFd>:
push   %r15
push   %r14
push   %rbx
sub    $0x40,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e8a(%rip),%rax
mov    (%rax),%edi
mov    0x2e89(%rip),%rax
mov    0x2e42(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1050 <syscall@plt>
cmp    $0xffffffff,%eax
je     11f9 <SetupUserfaultFd+0xb9>
mov    %eax,%ebx
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
movq   $0x0,0x10(%rsp)
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e1a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
jne    1207 <SetupUserfaultFd+0xc7>
mov    %r15,0x30(%rsp)
mov    %r14,0x28(%rsp)
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
lea    0x20(%rsp),%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
jne    1210 <SetupUserfaultFd+0xd0>
mov    %ebx,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe00(%rip),%rdi
call   1060 <perror@plt>
jmp    1223 <SetupUserfaultFd+0xe3>
lea    0xe0d(%rip),%rdi
jmp    1217 <SetupUserfaultFd+0xd7>
lea    0xe1c(%rip),%rdi
call   1060 <perror@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebx
jmp    11ed <SetupUserfaultFd+0xad>

LLVM IR (-O0):
%struct.uffdio_api = type { i32, %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i64, i64 }
%struct.uffdio_register = type { i32, %struct.TYPE_2__, i32, i32 }
@__NR_userfaultfd = global i32 0
@O_NONBLOCK = global i32 0
@O_CLOEXEC = global i32 0
@.str = [27 x i8] c"Userfaultfd syscall failed\00"
@UFFD_FEATURE_EVENT_FORK = global i32 0
@UFFD_API = global i32 0
@UFFDIO_API = global i32 0
@.str.1 = [24 x i8] c"ioctl UFFDIO_API failed\00"
@UFFDIO_REGISTER_MODE_MISSING = global i32 0
@UFFDIO_REGISTER = global i32 0
@.str.2 = [29 x i8] c"ioctl UFFDIO_REGISTER failed\00"
define i32 @SetupUserfaultFd(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca %struct.uffdio_api
%8 = alloca %struct.uffdio_register
store i8* %0, i8** %4
store i64 %1, i64* %5
%9 = load i32, i32* @__NR_userfaultfd
%10 = load i32, i32* @O_NONBLOCK
%11 = load i32, i32* @O_CLOEXEC
%12 = or i32 %10, %11
%13 = call i32 @syscall(i32 %9, i32 %12)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %53
18: ; preds = %2
%19 = bitcast %struct.uffdio_api* %7 to i8*
%20 = getelementptr inbounds %struct.uffdio_api, %struct.uffdio_api* %7, i32 0, i32 2
%21 = load i32, i32* @UFFD_FEATURE_EVENT_FORK
store i32 %21, i32* %20
%22 = getelementptr inbounds %struct.uffdio_api, %struct.uffdio_api* %7, i32 0, i32 3
%23 = load i32, i32* @UFFD_API
store i32 %23, i32* %22
%24 = load i32, i32* %6
%25 = load i32, i32* @UFFDIO_API
%26 = bitcast %struct.uffdio_api* %7 to %struct.uffdio_register*
%27 = call i64 @ioctl(i32 %24, i32 %25, %struct.uffdio_register* %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %18
%30 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%31 = load i32, i32* %6
%32 = call i32 @close(i32 %31)
store i32 -1, i32* %3
br label %53
33: ; preds = %18
%34 = load i8*, i8** %4
%35 = ptrtoint i8* %34 to i64
%36 = getelementptr inbounds %struct.uffdio_register, %struct.uffdio_register* %8, i32 0, i32 1
%37 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %36, i32 0, i32 1
store i64 %35, i64* %37
%38 = load i64, i64* %5
%39 = getelementptr inbounds %struct.uffdio_register, %struct.uffdio_register* %8, i32 0, i32 1
%40 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %39, i32 0, i32 0
store i64 %38, i64* %40
%41 = load i32, i32* @UFFDIO_REGISTER_MODE_MISSING
%42 = getelementptr inbounds %struct.uffdio_register, %struct.uffdio_register* %8, i32 0, i32 0
store i32 %41, i32* %42
%43 = load i32, i32* %6
%44 = load i32, i32* @UFFDIO_REGISTER
%45 = call i64 @ioctl(i32 %43, i32 %44, %struct.uffdio_register* %8)
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %33
%48 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%49 = load i32, i32* %6
%50 = call i32 @close(i32 %49)
store i32 -1, i32* %3
br label %53
51: ; preds = %33
%52 = load i32, i32* %6
store i32 %52, i32* %3
br label %53
53: ; preds = %51, %47, %29, %16
%54 = load i32, i32* %3
ret i32 %54
}

LLVM IR (-O2):
%struct.uffdio_api = type { i32, %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i64, i64 }
%struct.uffdio_register = type { i32, %struct.TYPE_2__, i32, i32 }
@__NR_userfaultfd = global i32 0
@O_NONBLOCK = global i32 0
@O_CLOEXEC = global i32 0
@.str = [27 x i8] c"Userfaultfd syscall failed\00"
@UFFD_FEATURE_EVENT_FORK = global i32 0
@UFFD_API = global i32 0
@UFFDIO_API = global i32 0
@.str.1 = [24 x i8] c"ioctl UFFDIO_API failed\00"
@UFFDIO_REGISTER_MODE_MISSING = global i32 0
@UFFDIO_REGISTER = global i32 0
@.str.2 = [29 x i8] c"ioctl UFFDIO_REGISTER failed\00"
define i32 @SetupUserfaultFd(i8* %0, i64 %1) {
%3 = alloca %struct.uffdio_api
%4 = alloca %struct.uffdio_register
%5 = load i32, i32* @__NR_userfaultfd
%6 = load i32, i32* @O_NONBLOCK
%7 = load i32, i32* @O_CLOEXEC
%8 = or i32 %7, %6
%9 = tail call i32 @syscall(i32 %5, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %43
13: ; preds = %2
%14 = bitcast %struct.uffdio_api* %3 to i8*
%15 = getelementptr inbounds %struct.uffdio_api, %struct.uffdio_api* %3, i64 0, i32 2
%16 = load i32, i32* @UFFD_FEATURE_EVENT_FORK
store i32 %16, i32* %15
%17 = getelementptr inbounds %struct.uffdio_api, %struct.uffdio_api* %3, i64 0, i32 3
%18 = load i32, i32* @UFFD_API
store i32 %18, i32* %17
%19 = load i32, i32* @UFFDIO_API
%20 = bitcast %struct.uffdio_api* %3 to %struct.uffdio_register*
%21 = call i64 @ioctl(i32 %9, i32 %19, %struct.uffdio_register* %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %13
%24 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @close(i32 %9)
br label %41
26: ; preds = %13
%27 = bitcast %struct.uffdio_register* %4 to i8*
%28 = ptrtoint i8* %0 to i64
%29 = getelementptr inbounds %struct.uffdio_register, %struct.uffdio_register* %4, i64 0, i32 1, i32 1
store i64 %28, i64* %29
%30 = getelementptr inbounds %struct.uffdio_register, %struct.uffdio_register* %4, i64 0, i32 1, i32 0
store i64 %1, i64* %30
%31 = load i32, i32* @UFFDIO_REGISTER_MODE_MISSING
%32 = getelementptr inbounds %struct.uffdio_register, %struct.uffdio_register* %4, i64 0, i32 0
store i32 %31, i32* %32
%33 = load i32, i32* @UFFDIO_REGISTER
%34 = call i64 @ioctl(i32 %9, i32 %33, %struct.uffdio_register* %4)
%35 = icmp eq i64 %34, 0
br i1 %35, label %39, label %36
36: ; preds = %26
%37 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%38 = call i32 @close(i32 %9)
br label %39
39: ; preds = %26, %36
%40 = phi i32 [ -1, %36 ], [ %9, %26 ]
br label %41
41: ; preds = %39, %23
%42 = phi i32 [ -1, %23 ], [ %40, %39 ]
br label %43
43: ; preds = %41, %11
%44 = phi i32 [ -1, %11 ], [ %42, %41 ]
ret i32 %44
}