#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 int int32_t;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, char *, char *);
int /*<<< orphan*/ stderr;
int32_t label_dist(int32_t pc, int32_t label, int resolution, int forwards,
                   int inverse) {
  int32_t dist = label - pc;
  if (inverse > 0)
    dist *= -1;
  if (forwards > 0 && dist < 0) {
    fprintf(stderr, "Waring: (@%d) %s %s", pc,
            "Instruction is bounded by reference direction",
            "yet actual reference is in a different direction!\n");
  }
  int32_t mask = 0x0;
  for (int i = 0; i < resolution; i++)
    mask = (mask + 1) << 1;
  mask = mask >> 1;
  return mask & dist;
}


x86 assembly (clang, -O0):
<label_dist>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %ecx,-0x10(%rbp)
mov    %r8d,-0x14(%rbp)
mov    -0x8(%rbp),%eax
sub    -0x4(%rbp),%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    1142 <label_dist+0x32>
imul   $0xffffffff,-0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x10(%rbp)
jle    117c <label_dist+0x6c>
cmpl   $0x0,-0x18(%rbp)
jge    117c <label_dist+0x6c>
mov    0x2e83(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
lea    0xe97(%rip),%rsi
lea    0xea4(%rip),%rcx
lea    0xecb(%rip),%r8
call   1030 <fprintf@plt>
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11b0 <label_dist+0xa0>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
shl    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    118a <label_dist+0x7a>
mov    -0x1c(%rbp),%eax
sar    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
and    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<label_dist>:
push   %rbp
push   %rbx
push   %rax
mov    %edx,%ebx
sub    %edi,%esi
mov    %esi,%ebp
neg    %ebp
test   %r8d,%r8d
cmovle %esi,%ebp
test   %ecx,%ecx
jle    114e <label_dist+0x3e>
test   %ebp,%ebp
jns    114e <label_dist+0x3e>
mov    %edi,%edx
mov    0x2eae(%rip),%rax
mov    (%rax),%edi
lea    0xec5(%rip),%rsi
lea    0xed2(%rip),%rcx
lea    0xef9(%rip),%r8
call   1030 <fprintf@plt>
test   %ebx,%ebx
jle    1163 <label_dist+0x53>
lea    -0x1(%rbx),%eax
mov    %ebx,%ecx
and    $0x7,%ecx
cmp    $0x7,%eax
jae    1167 <label_dist+0x57>
xor    %eax,%eax
jmp    117d <label_dist+0x6d>
xor    %eax,%eax
jmp    119c <label_dist+0x8c>
and    $0xfffffff8,%ebx
xor    %eax,%eax
nopl   0x0(%rax)
shl    $0x8,%eax
or     $0x1fe,%eax
add    $0xfffffff8,%ebx
jne    1170 <label_dist+0x60>
test   %ecx,%ecx
je     119a <label_dist+0x8a>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    %eax,%eax
or     $0x2,%eax
add    $0xffffffff,%ecx
jne    1190 <label_dist+0x80>
sar    %eax
and    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [20 x i8] c"Waring: (@%d) %s %s\00"
@.str.1 = [46 x i8] c"Instruction is bounded by reference direction\00"
@.str.2 = [51 x i8] c"yet actual reference is in a different direction!\0A\00"
define i32 @label_dist(i32 %0, i32 %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 %4, i32* %10
%14 = load i32, i32* %7
%15 = load i32, i32* %6
%16 = sub nsw i32 %14, %15
store i32 %16, i32* %11
%17 = load i32, i32* %10
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %5
%20 = load i32, i32* %11
%21 = mul nsw i32 %20, -1
store i32 %21, i32* %11
br label %22
22: ; preds = %19, %5
%23 = load i32, i32* %9
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %32
25: ; preds = %22
%26 = load i32, i32* %11
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %25
%29 = load i32, i32* @stderr
%30 = load i32, i32* %6
%31 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %30, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0))
br label %32
32: ; preds = %28, %25, %22
store i32 0, i32* %12
store i32 0, i32* %13
br label %33
33: ; preds = %41, %32
%34 = load i32, i32* %13
%35 = load i32, i32* %8
%36 = icmp slt i32 %34, %35
br i1 %36, label %37, label %44
37: ; preds = %33
%38 = load i32, i32* %12
%39 = add nsw i32 %38, 1
%40 = shl i32 %39, 1
store i32 %40, i32* %12
br label %41
41: ; preds = %37
%42 = load i32, i32* %13
%43 = add nsw i32 %42, 1
store i32 %43, i32* %13
br label %33
44: ; preds = %33
%45 = load i32, i32* %12
%46 = ashr i32 %45, 1
store i32 %46, i32* %12
%47 = load i32, i32* %12
%48 = load i32, i32* %11
%49 = and i32 %47, %48
ret i32 %49
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [20 x i8] c"Waring: (@%d) %s %s\00"
@.str.1 = [46 x i8] c"Instruction is bounded by reference direction\00"
@.str.2 = [51 x i8] c"yet actual reference is in a different direction!\0A\00"
define i32 @label_dist(i32 %0, i32 %1, i32 %2, i32 %3, i32 %4) {
%6 = sub nsw i32 %1, %0
%7 = icmp sgt i32 %4, 0
%8 = sub nsw i32 0, %6
%9 = select i1 %7, i32 %8, i32 %6
%10 = icmp sgt i32 %3, 0
%11 = icmp slt i32 %9, 0
%12 = select i1 %10, i1 %11, i1 false
br i1 %12, label %13, label %16
13: ; preds = %5
%14 = load i32, i32* @stderr
%15 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %0, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0))
br label %16
16: ; preds = %13, %5
%17 = icmp sgt i32 %2, 0
br i1 %17, label %18, label %38
18: ; preds = %16
%19 = add i32 %2, -1
%20 = and i32 %2, 7
%21 = icmp ult i32 %19, 7
br i1 %21, label %24, label %22
22: ; preds = %18
%23 = and i32 %2, -8
br label %41
24: ; preds = %41, %18
%25 = phi i32 [ undef, %18 ], [ %45, %41 ]
%26 = phi i32 [ 0, %18 ], [ %45, %41 ]
%27 = icmp eq i32 %20, 0
br i1 %27, label %35, label %28
28: ; preds = %24, %28
%29 = phi i32 [ %32, %28 ], [ %26, %24 ]
%30 = phi i32 [ %33, %28 ], [ 0, %24 ]
%31 = shl i32 %29, 1
%32 = or i32 %31, 2
%33 = add i32 %30, 1
%34 = icmp eq i32 %33, %20
br i1 %34, label %35, label %28
35: ; preds = %28, %24
%36 = phi i32 [ %25, %24 ], [ %32, %28 ]
%37 = ashr exact i32 %36, 1
br label %38
38: ; preds = %35, %16
%39 = phi i32 [ 0, %16 ], [ %37, %35 ]
%40 = and i32 %39, %9
ret i32 %40
41: ; preds = %41, %22
%42 = phi i32 [ 0, %22 ], [ %45, %41 ]
%43 = phi i32 [ 0, %22 ], [ %46, %41 ]
%44 = shl i32 %42, 8
%45 = or i32 %44, 510
%46 = add i32 %43, 8
%47 = icmp eq i32 %46, %23
br i1 %47, label %24, label %41
}