#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 */
unsigned int create_branch(unsigned long, unsigned long, unsigned int);
unsigned int create_cond_branch(unsigned long, unsigned long, unsigned int);
scalar_t__ instr_is_branch_bform(unsigned int);
scalar_t__ instr_is_branch_iform(unsigned int);
unsigned int set_branch_target(unsigned int insn, unsigned long iaddr,
                               unsigned long btarget) {
  if (instr_is_branch_iform(insn))
    return create_branch(iaddr, btarget, insn);
  else if (instr_is_branch_bform(insn))
    return create_cond_branch(iaddr, btarget, insn);
  else
    return insn;
}


x86 assembly (clang, -O0):
<set_branch_target>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <instr_is_branch_iform@plt>
cmp    $0x0,%rax
je     117d <set_branch_target+0x3d>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x8(%rbp),%edx
call   1060 <create_branch@plt>
mov    %eax,-0x4(%rbp)
jmp    11ad <set_branch_target+0x6d>
mov    -0x8(%rbp),%edi
call   1040 <instr_is_branch_bform@plt>
cmp    $0x0,%rax
je     11a7 <set_branch_target+0x67>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x8(%rbp),%edx
call   1030 <create_cond_branch@plt>
mov    %eax,-0x4(%rbp)
jmp    11ad <set_branch_target+0x6d>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_branch_target>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebx
call   1050 <instr_is_branch_iform@plt>
test   %rax,%rax
je     1169 <set_branch_target+0x29>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
pop    %rbx
pop    %r14
pop    %r15
jmp    1060 <create_branch@plt>
mov    %ebx,%edi
call   1040 <instr_is_branch_bform@plt>
test   %rax,%rax
je     1187 <set_branch_target+0x47>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <create_cond_branch@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i32 @set_branch_target(i32 %0, i64 %1, i64 %2) {
%4 = tail call i64 @instr_is_branch_iform(i32 %0)
%5 = icmp eq i64 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %3
%7 = tail call i32 @create_branch(i64 %1, i64 %2, i32 %0)
br label %13
8: ; preds = %3
%9 = tail call i64 @instr_is_branch_bform(i32 %0)
%10 = icmp eq i64 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %8
%12 = tail call i32 @create_cond_branch(i64 %1, i64 %2, i32 %0)
br label %13
13: ; preds = %8, %11, %6
%14 = phi i32 [ %7, %6 ], [ %12, %11 ], [ %0, %8 ]
ret i32 %14
}