#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 */
struct uffdio_api {
  int features;
  int /*<<< orphan*/ api;
};

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ O_CLOEXEC;
int /*<<< orphan*/ UFFDIO_API;
int /*<<< orphan*/ UFFD_API;
int UFFD_FEATURE_MISSING_SHMEM;
int UFFD_FEATURE_SIGBUS;
int /*<<< orphan*/ __NR_userfaultfd;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int ioctl(int, int /*<<< orphan*/, struct uffdio_api *);
int /*<<< orphan*/ perror(char *);
int syscall(int /*<<< orphan*/, int /*<<< orphan*/);
int register_uffd() {
  struct uffdio_api uffdio_api;
  int fd = syscall(__NR_userfaultfd, O_CLOEXEC);
  if (fd == -1) {
    do {
      perror("userfaultfd");
      exit(EXIT_FAILURE);
    } while (0);
  }
  uffdio_api.api = UFFD_API;
  uffdio_api.features = UFFD_FEATURE_SIGBUS | UFFD_FEATURE_MISSING_SHMEM;
  if (ioctl(fd, UFFDIO_API, &uffdio_api) == -1) {
    do {
      perror("ioctl-UFFDIO_API");
      exit(EXIT_FAILURE);
    } while (0);
  }
  return fd;
}


x86 assembly (clang, -O0):
<register_uffd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e91(%rip),%rax
mov    (%rax),%edi
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
call   1040 <syscall@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    118b <register_uffd+0x4b>
jmp    1171 <register_uffd+0x31>
lea    0xe88(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    0x2e26(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    0x2e49(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x8(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    11e9 <register_uffd+0xa9>
jmp    11cf <register_uffd+0x8f>
lea    0xe36(%rip),%rdi
call   1050 <perror@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<register_uffd>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e94(%rip),%rax
mov    (%rax),%edi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
call   1040 <syscall@plt>
cmp    $0xffffffff,%eax
je     11a8 <register_uffd+0x68>
mov    %eax,%ebx
mov    0x2e6e(%rip),%rax
mov    0x2e7f(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e3c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    %ecx,0x8(%rsp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
je     11b1 <register_uffd+0x71>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe51(%rip),%rdi
jmp    11b8 <register_uffd+0x78>
lea    0xe54(%rip),%rdi
call   1050 <perror@plt>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.uffdio_api = type { i32, i32 }
@__NR_userfaultfd = global i32 0
@O_CLOEXEC = global i32 0
@.str = [12 x i8] c"userfaultfd\00"
@EXIT_FAILURE = global i32 0
@UFFD_API = global i32 0
@UFFD_FEATURE_SIGBUS = global i32 0
@UFFD_FEATURE_MISSING_SHMEM = global i32 0
@UFFDIO_API = global i32 0
@.str.1 = [17 x i8] c"ioctl-UFFDIO_API\00"
define i32 @register_uffd() {
%1 = alloca %struct.uffdio_api
%2 = alloca i32
%3 = load i32, i32* @__NR_userfaultfd
%4 = load i32, i32* @O_CLOEXEC
%5 = call i32 @syscall(i32 %3, i32 %4)
store i32 %5, i32* %2
%6 = load i32, i32* %2
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %14
8: ; preds = %0
br label %9
9: ; preds = %8
%10 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @EXIT_FAILURE
%12 = call i32 @exit(i32 %11)
unreachable
13: ; No predecessors!
br label %14
14: ; preds = %13, %0
%15 = load i32, i32* @UFFD_API
%16 = getelementptr inbounds %struct.uffdio_api, %struct.uffdio_api* %1, i32 0, i32 1
store i32 %15, i32* %16
%17 = load i32, i32* @UFFD_FEATURE_SIGBUS
%18 = load i32, i32* @UFFD_FEATURE_MISSING_SHMEM
%19 = or i32 %17, %18
%20 = getelementptr inbounds %struct.uffdio_api, %struct.uffdio_api* %1, i32 0, i32 0
store i32 %19, i32* %20
%21 = load i32, i32* %2
%22 = load i32, i32* @UFFDIO_API
%23 = call i32 @ioctl(i32 %21, i32 %22, %struct.uffdio_api* %1)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %31
25: ; preds = %14
br label %26
26: ; preds = %25
%27 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%28 = load i32, i32* @EXIT_FAILURE
%29 = call i32 @exit(i32 %28)
unreachable
30: ; No predecessors!
br label %31
31: ; preds = %30, %14
%32 = load i32, i32* %2
ret i32 %32
}

LLVM IR (-O2):
%struct.uffdio_api = type { i32, i32 }
@__NR_userfaultfd = global i32 0
@O_CLOEXEC = global i32 0
@.str = [12 x i8] c"userfaultfd\00"
@EXIT_FAILURE = global i32 0
@UFFD_API = global i32 0
@UFFD_FEATURE_SIGBUS = global i32 0
@UFFD_FEATURE_MISSING_SHMEM = global i32 0
@UFFDIO_API = global i32 0
@.str.1 = [17 x i8] c"ioctl-UFFDIO_API\00"
define i32 @register_uffd() {
%1 = alloca %struct.uffdio_api
%2 = bitcast %struct.uffdio_api* %1 to i8*
%3 = load i32, i32* @__NR_userfaultfd
%4 = load i32, i32* @O_CLOEXEC
%5 = tail call i32 @syscall(i32 %3, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %11
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %0
%12 = load i32, i32* @UFFD_API
%13 = getelementptr inbounds %struct.uffdio_api, %struct.uffdio_api* %1, i64 0, i32 1
store i32 %12, i32* %13
%14 = load i32, i32* @UFFD_FEATURE_SIGBUS
%15 = load i32, i32* @UFFD_FEATURE_MISSING_SHMEM
%16 = or i32 %15, %14
%17 = getelementptr inbounds %struct.uffdio_api, %struct.uffdio_api* %1, i64 0, i32 0
store i32 %16, i32* %17
%18 = load i32, i32* @UFFDIO_API
%19 = call i32 @ioctl(i32 %5, i32 %18, %struct.uffdio_api* %1)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %25
21: ; preds = %11
%22 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %11
ret i32 %5
}