#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 */
typedef scalar_t__ int64_t;
typedef int /*<<< orphan*/ int32_t;

/* Variables and functions */
scalar_t__ INT_MAX;
scalar_t__ INT_MIN;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ printf(char *, scalar_t__);
int32_t compute_jmp(void *from, void *to) {
  int64_t delta = (int64_t)to - (int64_t)from - 5;
  if (delta < INT_MIN || delta > INT_MAX) {
    printf("cannot do relative jump of size %li; did you compile with -fPIC?\n",
           delta);
    exit(1);
  }
  return (int32_t)delta;
}


x86 assembly (clang, -O0):
<compute_jmp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
sub    $0x5,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2e92(%rip),%rcx
cmp    (%rcx),%rax
jl     116b <compute_jmp+0x4b>
mov    -0x18(%rbp),%rax
mov    0x2e86(%rip),%rcx
cmp    (%rcx),%rax
jle    1185 <compute_jmp+0x65>
mov    -0x18(%rbp),%rsi
lea    0xe8a(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1040 <exit@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<compute_jmp>:
push   %rax
sub    %rdi,%rsi
add    $0xfffffffffffffffb,%rsi
mov    0x2eb1(%rip),%rax
cmp    (%rax),%rsi
jl     1144 <compute_jmp+0x24>
mov    0x2ead(%rip),%rax
cmp    (%rax),%rsi
jg     1144 <compute_jmp+0x24>
mov    %esi,%eax
pop    %rcx
ret
lea    0xeb5(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1040 <exit@plt>

LLVM IR (-O0):
@INT_MIN = global i64 0
@INT_MAX = global i64 0
@.str = [66 x i8] c"cannot do relative jump of size %li; did you compile with -fPIC?\0A\00"
define i32 @compute_jmp(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
store i8* %0, i8** %3
store i8* %1, i8** %4
%6 = load i8*, i8** %4
%7 = ptrtoint i8* %6 to i64
%8 = load i8*, i8** %3
%9 = ptrtoint i8* %8 to i64
%10 = sub nsw i64 %7, %9
%11 = sub nsw i64 %10, 5
store i64 %11, i64* %5
%12 = load i64, i64* %5
%13 = load i64, i64* @INT_MIN
%14 = icmp slt i64 %12, %13
br i1 %14, label %19, label %15
15: ; preds = %2
%16 = load i64, i64* %5
%17 = load i64, i64* @INT_MAX
%18 = icmp sgt i64 %16, %17
br i1 %18, label %19, label %23
19: ; preds = %15, %2
%20 = load i64, i64* %5
%21 = call i32 @printf(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str, i64 0, i64 0), i64 %20)
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %15
%24 = load i64, i64* %5
%25 = trunc i64 %24 to i32
ret i32 %25
}

LLVM IR (-O2):
@INT_MIN = global i64 0
@INT_MAX = global i64 0
@.str = [66 x i8] c"cannot do relative jump of size %li; did you compile with -fPIC?\0A\00"
define i32 @compute_jmp(i8* %0, i8* %1) {
%3 = ptrtoint i8* %1 to i64
%4 = ptrtoint i8* %0 to i64
%5 = sub nsw i64 %3, %4
%6 = add nsw i64 %5, -5
%7 = load i64, i64* @INT_MIN
%8 = icmp slt i64 %6, %7
%9 = load i64, i64* @INT_MAX
%10 = icmp sgt i64 %6, %9
%11 = select i1 %8, i1 true, i1 %10
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = tail call i32 @printf(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str, i64 0, i64 0), i64 %6)
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = trunc i64 %6 to i32
ret i32 %16
}