#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 */
int /*<<< orphan*/ printf(char *, unsigned long);
int validate_addr(char *ptr, int high_addr) {
  unsigned long addr = (unsigned long)ptr;
  if (high_addr) {
    if (addr < (1UL << 47)) {
      printf("Bad address %lx\n", addr);
      return 1;
    }
    return 0;
  }
  if (addr > (1UL << 47)) {
    printf("Bad address %lx\n", addr);
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<validate_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     116d <validate_addr+0x5d>
movabs $0x800000000000,%rax
cmp    %rax,-0x20(%rbp)
jae    1161 <validate_addr+0x51>
mov    -0x20(%rbp),%rsi
lea    0xeb0(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11a4 <validate_addr+0x94>
movl   $0x0,-0x4(%rbp)
jmp    11a4 <validate_addr+0x94>
movabs $0x800000000000,%rax
cmp    %rax,-0x20(%rbp)
jbe    119d <validate_addr+0x8d>
mov    -0x20(%rbp),%rsi
lea    0xe74(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11a4 <validate_addr+0x94>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<validate_addr>:
mov    %rdi,%rcx
movabs $0x800000000000,%rdx
xor    %eax,%eax
test   %esi,%esi
je     1142 <validate_addr+0x32>
cmp    %rdx,%rcx
jae    1141 <validate_addr+0x31>
push   %rax
lea    0xed0(%rip),%rdi
mov    %rcx,%rsi
call   1030 <printf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
ret
cmp    %rdx,%rcx
ja     1128 <validate_addr+0x18>
jmp    1141 <validate_addr+0x31>

LLVM IR (-O0):
@.str = [17 x i8] c"Bad address %lx\0A\00"
define i32 @validate_addr(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i64
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = load i8*, i8** %4
%8 = ptrtoint i8* %7 to i64
store i64 %8, i64* %6
%9 = load i32, i32* %5
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %18
11: ; preds = %2
%12 = load i64, i64* %6
%13 = icmp ult i64 %12, 140737488355328
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i64, i64* %6
%16 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i64 %15)
store i32 1, i32* %3
br label %25
17: ; preds = %11
store i32 0, i32* %3
br label %25
18: ; preds = %2
%19 = load i64, i64* %6
%20 = icmp ugt i64 %19, 140737488355328
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = load i64, i64* %6
%23 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i64 %22)
store i32 1, i32* %3
br label %25
24: ; preds = %18
store i32 0, i32* %3
br label %25
25: ; preds = %24, %21, %17, %14
%26 = load i32, i32* %3
ret i32 %26
}

LLVM IR (-O2):
@.str = [17 x i8] c"Bad address %lx\0A\00"
define i32 @validate_addr(i8* %0, i32 %1) {
%3 = ptrtoint i8* %0 to i64
%4 = icmp eq i32 %1, 0
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = icmp ult i8* %0, inttoptr (i64 140737488355328 to i8*)
br i1 %6, label %9, label %11
7: ; preds = %2
%8 = icmp ugt i8* %0, inttoptr (i64 140737488355328 to i8*)
br i1 %8, label %9, label %11
9: ; preds = %7, %5
%10 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i64 %3)
br label %11
11: ; preds = %9, %7, %5
%12 = phi i32 [ 0, %5 ], [ 0, %7 ], [ 1, %9 ]
ret i32 %12
}