__attribute__((always_)) int fault_in_pages_writeable(char *uaddr, int size) {
  int ret;
  ret = ({
    long __pu_err;
    do {
      __typeof__(*(((uaddr)))) __pus_tmp = ((__typeof__(*(uaddr)))(0));
      __pu_err = 0;
      if (__builtin_expect(!!(__copy_to_user_ll(((uaddr)), &__pus_tmp,
                                                (sizeof(*(uaddr)))) != 0),
                           0))
        __pu_err = -14;
    } while (0);
    __pu_err;
  });
  if (ret == 0) {
    char *end = uaddr + size - 1;
    if (((unsigned long)uaddr & (~((1UL << 12) - 1))) !=
        ((unsigned long)end & (~((1UL << 12) - 1))))
      ret = ({
        long __pu_err;
        do {
          __typeof__(*(((end)))) __pus_tmp = ((__typeof__(*(end)))(0));
          __pu_err = 0;
          if (__builtin_expect(!!(__copy_to_user_ll(((end)), &__pus_tmp,
                                                    (sizeof(*(end)))) != 0),
                               0))
            __pu_err = -14;
        } while (0);
        __pu_err;
      });
  }
  return ret;
}


x86 assembly (clang, -O0):
<fault_in_pages_writeable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movb   $0x0,-0x19(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   1030 <__copy_to_user_ll@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
cltq
cmp    $0x0,%rax
je     1162 <fault_in_pages_writeable+0x52>
movq   $0xfffffffffffffff2,-0x18(%rbp)
ff
jmp    1167 <fault_in_pages_writeable+0x57>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jne    1212 <fault_in_pages_writeable+0x102>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rax
and    $0xfffffffffffff000,%rax
mov    -0x30(%rbp),%rcx
and    $0xfffffffffffff000,%rcx
cmp    %rcx,%rax
je     120d <fault_in_pages_writeable+0xfd>
jmp    11b6 <fault_in_pages_writeable+0xa6>
movb   $0x0,-0x39(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
lea    -0x39(%rbp),%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   1030 <__copy_to_user_ll@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
cltq
cmp    $0x0,%rax
je     11f9 <fault_in_pages_writeable+0xe9>
movq   $0xfffffffffffffff2,-0x38(%rbp)
ff
jmp    11fe <fault_in_pages_writeable+0xee>
mov    -0x38(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    %eax,-0x10(%rbp)
jmp    1212 <fault_in_pages_writeable+0x102>
mov    -0x10(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fault_in_pages_writeable>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
movb   $0x0,0xf(%rsp)
xor    %r14d,%r14d
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
xor    %eax,%eax
call   1030 <__copy_to_user_ll@plt>
test   %eax,%eax
jne    117a <fault_in_pages_writeable+0x6a>
movslq %ebp,%rax
lea    (%rbx,%rax,1),%rdi
add    $0xffffffffffffffff,%rdi
xor    %rdi,%rbx
cmp    $0x1000,%rbx
jb     116e <fault_in_pages_writeable+0x5e>
movb   $0x0,0xe(%rsp)
xor    %r14d,%r14d
lea    0xe(%rsp),%rsi
mov    $0x1,%edx
xor    %eax,%eax
call   1030 <__copy_to_user_ll@plt>
test   %eax,%eax
jne    117a <fault_in_pages_writeable+0x6a>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    $0xfffffff2,%r14d
jmp    116e <fault_in_pages_writeable+0x5e>

LLVM IR (-O0):
define i32 @fault_in_pages_writeable(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca i8
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i8
%12 = alloca i64
store i8* %0, i8** %3
store i32 %1, i32* %4
br label %13
13: ; preds = %2
store i8 0, i8* %7
store i64 0, i64* %6
%14 = load i8*, i8** %3
%15 = call i32 (i8*, i8*, i64, ...) bitcast (i32 (...)* @__copy_to_user_ll to i32 (i8*, i8*, i64, ...)*)(i8* %14, i8* %7, i64 1)
%16 = icmp ne i32 %15, 0
%17 = xor i1 %16, true
%18 = xor i1 %17, true
%19 = zext i1 %18 to i32
%20 = sext i32 %19 to i64
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %13
store i64 -14, i64* %6
br label %23
23: ; preds = %22, %13
br label %24
24: ; preds = %23
%25 = load i64, i64* %6
store i64 %25, i64* %8
%26 = load i64, i64* %8
%27 = trunc i64 %26 to i32
store i32 %27, i32* %5
%28 = load i32, i32* %5
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %60
30: ; preds = %24
%31 = load i8*, i8** %3
%32 = load i32, i32* %4
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
%35 = getelementptr inbounds i8, i8* %34, i64 -1
store i8* %35, i8** %9
%36 = load i8*, i8** %3
%37 = ptrtoint i8* %36 to i64
%38 = and i64 %37, -4096
%39 = load i8*, i8** %9
%40 = ptrtoint i8* %39 to i64
%41 = and i64 %40, -4096
%42 = icmp ne i64 %38, %41
br i1 %42, label %43, label %59
43: ; preds = %30
br label %44
44: ; preds = %43
store i8 0, i8* %11
store i64 0, i64* %10
%45 = load i8*, i8** %9
%46 = call i32 (i8*, i8*, i64, ...) bitcast (i32 (...)* @__copy_to_user_ll to i32 (i8*, i8*, i64, ...)*)(i8* %45, i8* %11, i64 1)
%47 = icmp ne i32 %46, 0
%48 = xor i1 %47, true
%49 = xor i1 %48, true
%50 = zext i1 %49 to i32
%51 = sext i32 %50 to i64
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %54
53: ; preds = %44
store i64 -14, i64* %10
br label %54
54: ; preds = %53, %44
br label %55
55: ; preds = %54
%56 = load i64, i64* %10
store i64 %56, i64* %12
%57 = load i64, i64* %12
%58 = trunc i64 %57 to i32
store i32 %58, i32* %5
br label %59
59: ; preds = %55, %30
br label %60
60: ; preds = %59, %24
%61 = load i32, i32* %5
ret i32 %61
}

LLVM IR (-O2):
define i32 @fault_in_pages_writeable(i8* %0, i32 %1) {
%3 = alloca i8
%4 = alloca i8
store i8 0, i8* %3
%5 = call i32 (i8*, i8*, i64, ...) bitcast (i32 (...)* @__copy_to_user_ll to i32 (i8*, i8*, i64, ...)*)(i8* %0, i8* %3, i64 1)
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %21
7: ; preds = %2
%8 = sext i32 %1 to i64
%9 = getelementptr inbounds i8, i8* %0, i64 %8
%10 = getelementptr inbounds i8, i8* %9, i64 -1
%11 = ptrtoint i8* %0 to i64
%12 = ptrtoint i8* %10 to i64
%13 = xor i64 %12, %11
%14 = icmp ult i64 %13, 4096
br i1 %14, label %21, label %15
15: ; preds = %7
store i8 0, i8* %4
%16 = call i32 (i8*, i8*, i64, ...) bitcast (i32 (...)* @__copy_to_user_ll to i32 (i8*, i8*, i64, ...)*)(i8* %10, i8* %4, i64 1)
%17 = icmp eq i32 %16, 0
br i1 %17, label %19, label %18
18: ; preds = %15
br label %19
19: ; preds = %18, %15
%20 = phi i32 [ -14, %18 ], [ 0, %15 ]
br label %21
21: ; preds = %2, %7, %19
%22 = phi i32 [ %20, %19 ], [ 0, %7 ], [ -14, %2 ]
ret i32 %22
}