#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 */
scalar_t__ EFAULT;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int open(char *, int /*<<< orphan*/);
int write(int, void *, int);
int is_illegal_pointer(void *ptr) {
  int ret = 0;
  int randomfd = open("/dev/random", O_WRONLY);
  if (write(randomfd, ptr, sizeof(void *)) == -1 && errno == EFAULT)
    ret = 1;
  close(randomfd);
  return ret;
}


x86 assembly (clang, -O0):
<is_illegal_pointer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    0x2e7e(%rip),%rax
mov    (%rax),%esi
lea    0xead(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    $0x8,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1196 <is_illegal_pointer+0x66>
mov    0x2e5c(%rip),%rax
mov    (%rax),%rax
mov    0x2e72(%rip),%rcx
cmp    (%rcx),%rax
jne    1196 <is_illegal_pointer+0x66>
movl   $0x1,-0xc(%rbp)
mov    -0x10(%rbp),%edi
call   1040 <close@plt>
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_illegal_pointer>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e8b(%rip),%rax
mov    (%rax),%esi
lea    0xeba(%rip),%rdi
call   1050 <open@plt>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x8,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
sete   %al
mov    0x2e6f(%rip),%rcx
mov    (%rcx),%rcx
mov    0x2e85(%rip),%rdx
cmp    (%rdx),%rcx
sete   %cl
and    %al,%cl
movzbl %cl,%ebx
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"/dev/random\00"
@O_WRONLY = global i32 0
@errno = global i64 0
@EFAULT = global i64 0
define i32 @is_illegal_pointer(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %3
%5 = load i32, i32* @O_WRONLY
%6 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %4
%7 = load i32, i32* %4
%8 = load i8*, i8** %2
%9 = call i32 @write(i32 %7, i8* %8, i32 8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %16
11: ; preds = %1
%12 = load i64, i64* @errno
%13 = load i64, i64* @EFAULT
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %16
15: ; preds = %11
store i32 1, i32* %3
br label %16
16: ; preds = %15, %11, %1
%17 = load i32, i32* %4
%18 = call i32 @close(i32 %17)
%19 = load i32, i32* %3
ret i32 %19
}

LLVM IR (-O2):
@.str = [12 x i8] c"/dev/random\00"
@O_WRONLY = global i32 0
@errno = global i64 0
@EFAULT = global i64 0
define i32 @is_illegal_pointer(i8* nocapture %0) {
%2 = load i32, i32* @O_WRONLY
%3 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = tail call i32 @write(i32 %3, i8* %0, i32 8)
%5 = icmp eq i32 %4, -1
%6 = load i64, i64* @errno
%7 = load i64, i64* @EFAULT
%8 = icmp eq i64 %6, %7
%9 = select i1 %5, i1 %8, i1 false
%10 = zext i1 %9 to i32
%11 = tail call i32 @close(i32 %3)
ret i32 %10
}