#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 uint32_t;

/* Variables and functions */
int abs16(int);
int /*<<< orphan*/ get_dreg(int);
int get_opa(int);
int get_opb(int);
int /*<<< orphan*/ set_reg(int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ sim_warning(char *);
int /*<<< orphan*/ update_flags(int);
void insn_arithmetic_op(uint32_t insn) {
  uint32_t opa, opb;
  uint32_t result;
  opa = get_opa(insn);
  opb = get_opb(insn);
  switch (insn & 0x07800000) {
  case 0x00000000:
    result = opa + opb;
    break;
  case 0x01800000:
    result = opa + opb;
    update_flags(result);
    break;
  case 0x02000000:
    result = opb - opa;
    break;
  case 0x03800000:
    result = opb - opa;
    update_flags(result);
    break;
  case 0x05800000:
    result = abs16(opa);
    update_flags(result);
    break;
  default:
    sim_warning("Unimplemented arithmetic instrution");
    break;
  }
  set_reg(get_dreg(insn), result, 0);
}


x86 assembly (clang, -O0):
<insn_arithmetic_op>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
call   1050 <get_opa@plt>
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%edi
call   1030 <get_opb@plt>
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%eax
and    $0x7800000,%eax
mov    %eax,-0x14(%rbp)
je     11f3 <insn_arithmetic_op+0x83>
jmp    11a7 <insn_arithmetic_op+0x37>
mov    -0x14(%rbp),%eax
sub    $0x1800000,%eax
je     1201 <insn_arithmetic_op+0x91>
jmp    11ba <insn_arithmetic_op+0x4a>
mov    -0x14(%rbp),%eax
sub    $0x2000000,%eax
je     1217 <insn_arithmetic_op+0xa7>
jmp    11cd <insn_arithmetic_op+0x5d>
mov    -0x14(%rbp),%eax
sub    $0x3800000,%eax
je     1225 <insn_arithmetic_op+0xb5>
jmp    11e0 <insn_arithmetic_op+0x70>
mov    -0x14(%rbp),%eax
sub    $0x5800000,%eax
je     123b <insn_arithmetic_op+0xcb>
jmp    1253 <insn_arithmetic_op+0xe3>
mov    -0x8(%rbp),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    125f <insn_arithmetic_op+0xef>
mov    -0x8(%rbp),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
call   1040 <update_flags@plt>
jmp    125f <insn_arithmetic_op+0xef>
mov    -0xc(%rbp),%eax
sub    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    125f <insn_arithmetic_op+0xef>
mov    -0xc(%rbp),%eax
sub    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
call   1040 <update_flags@plt>
jmp    125f <insn_arithmetic_op+0xef>
mov    -0x8(%rbp),%edi
call   1060 <abs16@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
call   1040 <update_flags@plt>
jmp    125f <insn_arithmetic_op+0xef>
lea    0xda6(%rip),%rdi
call   1070 <sim_warning@plt>
mov    -0x4(%rbp),%edi
call   1080 <get_dreg@plt>
mov    %eax,%edi
mov    -0x10(%rbp),%esi
xor    %edx,%edx
call   1090 <set_reg@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<insn_arithmetic_op>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%ebx
call   1050 <get_opa@plt>
mov    %eax,%r14d
mov    %ebx,%edi
call   1030 <get_opb@plt>
mov    %eax,%ebp
mov    %ebx,%eax
shr    $0x17,%eax
and    $0xf,%eax
cmp    $0xb,%eax
ja     11a9 <insn_arithmetic_op+0x39>
lea    0xe65(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
add    %r14d,%ebp
jmp    11d9 <insn_arithmetic_op+0x69>
lea    0xe80(%rip),%rdi
call   1070 <sim_warning@plt>
jmp    11d9 <insn_arithmetic_op+0x69>
add    %r14d,%ebp
jmp    11c4 <insn_arithmetic_op+0x54>
sub    %r14d,%ebp
jmp    11d9 <insn_arithmetic_op+0x69>
sub    %r14d,%ebp
mov    %ebp,%edi
jmp    11d4 <insn_arithmetic_op+0x64>
mov    %r14d,%edi
call   1060 <abs16@plt>
mov    %eax,%ebp
mov    %eax,%edi
call   1040 <update_flags@plt>
mov    %ebx,%edi
call   1080 <get_dreg@plt>
mov    %eax,%edi
mov    %ebp,%esi
xor    %edx,%edx
pop    %rbx
pop    %r14
pop    %rbp
jmp    1090 <set_reg@plt>

LLVM IR (-O0):
@.str = [36 x i8] c"Unimplemented arithmetic instrution\00"
define void @insn_arithmetic_op(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = call i32 @get_opa(i32 %6)
store i32 %7, i32* %3
%8 = load i32, i32* %2
%9 = call i32 @get_opb(i32 %8)
store i32 %9, i32* %4
%10 = load i32, i32* %2
%11 = and i32 %10, 125829120
switch i32 %11, label %37 [
i32 0, label %12
i32 25165824, label %16
i32 33554432, label %22
i32 58720256, label %26
i32 92274688, label %32
]
12: ; preds = %1
%13 = load i32, i32* %3
%14 = load i32, i32* %4
%15 = add nsw i32 %13, %14
store i32 %15, i32* %5
br label %39
16: ; preds = %1
%17 = load i32, i32* %3
%18 = load i32, i32* %4
%19 = add nsw i32 %17, %18
store i32 %19, i32* %5
%20 = load i32, i32* %5
%21 = call i32 @update_flags(i32 %20)
br label %39
22: ; preds = %1
%23 = load i32, i32* %4
%24 = load i32, i32* %3
%25 = sub nsw i32 %23, %24
store i32 %25, i32* %5
br label %39
26: ; preds = %1
%27 = load i32, i32* %4
%28 = load i32, i32* %3
%29 = sub nsw i32 %27, %28
store i32 %29, i32* %5
%30 = load i32, i32* %5
%31 = call i32 @update_flags(i32 %30)
br label %39
32: ; preds = %1
%33 = load i32, i32* %3
%34 = call i32 @abs16(i32 %33)
store i32 %34, i32* %5
%35 = load i32, i32* %5
%36 = call i32 @update_flags(i32 %35)
br label %39
37: ; preds = %1
%38 = call i32 @sim_warning(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %39
39: ; preds = %37, %32, %26, %22, %16, %12
%40 = load i32, i32* %2
%41 = call i32 @get_dreg(i32 %40)
%42 = load i32, i32* %5
%43 = call i32 @set_reg(i32 %41, i32 %42, i32 0)
ret void
}

LLVM IR (-O2):
@.str = [36 x i8] c"Unimplemented arithmetic instrution\00"
define void @insn_arithmetic_op(i32 %0) {
%2 = tail call i32 @get_opa(i32 %0)
%3 = tail call i32 @get_opb(i32 %0)
%4 = lshr i32 %0, 23
%5 = and i32 %4, 15
switch i32 %5, label %19 [
i32 0, label %6
i32 3, label %8
i32 4, label %11
i32 7, label %13
i32 11, label %16
]
6: ; preds = %1
%7 = add nsw i32 %3, %2
br label %21
8: ; preds = %1
%9 = add nsw i32 %3, %2
%10 = tail call i32 @update_flags(i32 %9)
br label %21
11: ; preds = %1
%12 = sub nsw i32 %3, %2
br label %21
13: ; preds = %1
%14 = sub nsw i32 %3, %2
%15 = tail call i32 @update_flags(i32 %14)
br label %21
16: ; preds = %1
%17 = tail call i32 @abs16(i32 %2)
%18 = tail call i32 @update_flags(i32 %17)
br label %21
19: ; preds = %1
%20 = tail call i32 @sim_warning(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %19, %16, %13, %11, %8, %6
%22 = phi i32 [ undef, %19 ], [ %17, %16 ], [ %14, %13 ], [ %12, %11 ], [ %9, %8 ], [ %7, %6 ]
%23 = tail call i32 @get_dreg(i32 %0)
%24 = tail call i32 @set_reg(i32 %23, i32 %22, i32 0)
ret void
}