#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*/ is_offset_in_branch_range(long);
unsigned int create_branch(unsigned long addr, unsigned long target,
                           int flags) {
  unsigned int instruction;
  long offset;
  offset = target;
  if (!(flags & 0x2))
    offset = offset - (unsigned long)addr;
  if (!is_offset_in_branch_range(offset))
    return 0;
  instruction = 0x48000000 | (flags & 0x3) | (offset & 0x03FFFFFC);
  return instruction;
}


x86 assembly (clang, -O0):
<create_branch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
jne    1146 <create_branch+0x36>
mov    -0x28(%rbp),%rax
sub    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <is_offset_in_branch_range@plt>
cmp    $0x0,%eax
jne    1164 <create_branch+0x54>
movl   $0x0,-0x4(%rbp)
jmp    1188 <create_branch+0x78>
mov    -0x1c(%rbp),%eax
and    $0x3,%eax
or     $0x48000000,%eax
cltq
mov    -0x28(%rbp),%rcx
and    $0x3fffffc,%rcx
or     %rcx,%rax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_branch>:
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%rbx
xor    %eax,%eax
test   $0x2,%r14b
cmove  %rdi,%rax
sub    %rax,%rbx
mov    %rbx,%rdi
call   1030 <is_offset_in_branch_range@plt>
and    $0x3,%r14d
and    $0x3fffffc,%ebx
test   %eax,%eax
lea    0x48000000(%r14,%rbx,1),%ecx
48
cmovne %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @create_branch(i64 %0, i64 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
store i64 %0, i64* %5
store i64 %1, i64* %6
store i32 %2, i32* %7
%10 = load i64, i64* %6
store i64 %10, i64* %9
%11 = load i32, i32* %7
%12 = and i32 %11, 2
%13 = icmp ne i32 %12, 0
br i1 %13, label %18, label %14
14: ; preds = %3
%15 = load i64, i64* %9
%16 = load i64, i64* %5
%17 = sub i64 %15, %16
store i64 %17, i64* %9
br label %18
18: ; preds = %14, %3
%19 = load i64, i64* %9
%20 = call i32 @is_offset_in_branch_range(i64 %19)
%21 = icmp ne i32 %20, 0
br i1 %21, label %23, label %22
22: ; preds = %18
store i32 0, i32* %4
br label %33
23: ; preds = %18
%24 = load i32, i32* %7
%25 = and i32 %24, 3
%26 = or i32 1207959552, %25
%27 = sext i32 %26 to i64
%28 = load i64, i64* %9
%29 = and i64 %28, 67108860
%30 = or i64 %27, %29
%31 = trunc i64 %30 to i32
store i32 %31, i32* %8
%32 = load i32, i32* %8
store i32 %32, i32* %4
br label %33
33: ; preds = %23, %22
%34 = load i32, i32* %4
ret i32 %34
}

LLVM IR (-O2):
define i32 @create_branch(i64 %0, i64 %1, i32 %2) {
%4 = and i32 %2, 2
%5 = icmp eq i32 %4, 0
%6 = select i1 %5, i64 %0, i64 0
%7 = sub i64 %1, %6
%8 = tail call i32 @is_offset_in_branch_range(i64 %7)
%9 = icmp eq i32 %8, 0
%10 = and i32 %2, 3
%11 = trunc i64 %7 to i32
%12 = and i32 %11, 67108860
%13 = or i32 %10, %12
%14 = or i32 %13, 1207959552
%15 = select i1 %9, i32 0, i32 %14
ret i32 %15
}