#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 unsigned long uint64_t;

/* Variables and functions */
unsigned long branch_bform_target(unsigned int const);
unsigned long branch_iform_target(unsigned int const);
scalar_t__ has_branch_target(unsigned int const);
scalar_t__ instr_is_branch_iform(unsigned int const);
int /*<<< orphan*/ is_branch_absolute(unsigned long);
uint64_t branch_target(const unsigned int instr, unsigned long pc) {
  if (has_branch_target(instr)) {
    unsigned long addr;
    if (instr_is_branch_iform(instr))
      addr = branch_iform_target(instr);
    else
      addr = branch_bform_target(instr);
    if (!is_branch_absolute(pc))
      addr += pc;
    return addr;
  }
  return 0;
}


x86 assembly (clang, -O0):
<branch_target>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <has_branch_target@plt>
cmp    $0x0,%rax
je     11cb <branch_target+0x7b>
mov    -0xc(%rbp),%edi
call   1050 <instr_is_branch_iform@plt>
cmp    $0x0,%rax
je     1194 <branch_target+0x44>
mov    -0xc(%rbp),%edi
call   1060 <branch_iform_target@plt>
mov    %rax,-0x20(%rbp)
jmp    11a0 <branch_target+0x50>
mov    -0xc(%rbp),%edi
call   1030 <branch_bform_target@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <is_branch_absolute@plt>
cmp    $0x0,%eax
jne    11be <branch_target+0x6e>
mov    -0x18(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11d3 <branch_target+0x83>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<branch_target>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
call   1040 <has_branch_target@plt>
test   %rax,%rax
je     1177 <branch_target+0x27>
mov    %ebp,%edi
call   1050 <instr_is_branch_iform@plt>
mov    %ebp,%edi
test   %rax,%rax
je     117b <branch_target+0x2b>
call   1060 <branch_iform_target@plt>
jmp    1180 <branch_target+0x30>
xor    %eax,%eax
jmp    1198 <branch_target+0x48>
call   1030 <branch_bform_target@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1070 <is_branch_absolute@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
cmove  %rbx,%rax
add    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i64 @branch_target(i32 %0, i64 %1) {
%3 = alloca i64
%4 = alloca i32
%5 = alloca i64
%6 = alloca i64
store i32 %0, i32* %4
store i64 %1, i64* %5
%7 = load i32, i32* %4
%8 = call i64 @has_branch_target(i32 %7)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %30
10: ; preds = %2
%11 = load i32, i32* %4
%12 = call i64 @instr_is_branch_iform(i32 %11)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = load i32, i32* %4
%16 = call i64 @branch_iform_target(i32 %15)
store i64 %16, i64* %6
br label %20
17: ; preds = %10
%18 = load i32, i32* %4
%19 = call i64 @branch_bform_target(i32 %18)
store i64 %19, i64* %6
br label %20
20: ; preds = %17, %14
%21 = load i64, i64* %5
%22 = call i32 @is_branch_absolute(i64 %21)
%23 = icmp ne i32 %22, 0
br i1 %23, label %28, label %24
24: ; preds = %20
%25 = load i64, i64* %5
%26 = load i64, i64* %6
%27 = add i64 %26, %25
store i64 %27, i64* %6
br label %28
28: ; preds = %24, %20
%29 = load i64, i64* %6
store i64 %29, i64* %3
br label %31
30: ; preds = %2
store i64 0, i64* %3
br label %31
31: ; preds = %30, %28
%32 = load i64, i64* %3
ret i64 %32
}

LLVM IR (-O2):
define i64 @branch_target(i32 %0, i64 %1) {
%3 = tail call i64 @has_branch_target(i32 %0)
%4 = icmp eq i64 %3, 0
br i1 %4, label %18, label %5
5: ; preds = %2
%6 = tail call i64 @instr_is_branch_iform(i32 %0)
%7 = icmp eq i64 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %5
%9 = tail call i64 @branch_iform_target(i32 %0)
br label %12
10: ; preds = %5
%11 = tail call i64 @branch_bform_target(i32 %0)
br label %12
12: ; preds = %10, %8
%13 = phi i64 [ %9, %8 ], [ %11, %10 ]
%14 = tail call i32 @is_branch_absolute(i64 %1)
%15 = icmp eq i32 %14, 0
%16 = select i1 %15, i64 %1, i64 0
%17 = add i64 %16, %13
br label %18
18: ; preds = %2, %12
%19 = phi i64 [ %17, %12 ], [ 0, %2 ]
ret i64 %19
}