#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 uint8_t;
typedef scalar_t__ uint16_t;
struct jit_state {
  int dummy;
};
typedef scalar_t__ int32_t;

/* Variables and functions */
int /*<<< orphan*/ emit_alu32(struct jit_state *, int, int, int);
int /*<<< orphan*/ emit_alu64(struct jit_state *, int, int, int);
int /*<<< orphan*/ emit_jcc(struct jit_state *, int, int);
int /*<<< orphan*/ emit_load_imm(struct jit_state *, int, scalar_t__);
int /*<<< orphan*/ emit_mov(struct jit_state *, int, int);
int /*<<< orphan*/ emit_pop(struct jit_state *, int);
int /*<<< orphan*/ emit_push(struct jit_state *, int);
int /*<<< orphan*/ emit_rex(struct jit_state *, int, int /*<<< orphan*/,
                            int /*<<< orphan*/, int /*<<< orphan*/);
void muldivmod(struct jit_state *state, uint16_t pc, uint8_t opcode, int src,
               int dst, int32_t imm) {
  bool mul = (opcode & 0xf0) == ((0x04 | 0x00 | 0x20) & 0xf0);
  bool div = (opcode & 0xf0) == ((0x04 | 0x00 | 0x30) & 0xf0);
  bool mod = (opcode & 0xf0) == ((0x04 | 0x00 | 0x90) & 0xf0);
  bool is64 = (opcode & 0x07) == 0x07;
  if (div || mod) {
    emit_load_imm(state, 1, pc);
    if (is64) {
      emit_alu64(state, 0x85, src, src);
    } else {
      emit_alu32(state, 0x85, src, src);
    }
    emit_jcc(state, 0x84, -2);
  }
  if (dst != 0) {
    emit_push(state, 0);
  }
  if (dst != 2) {
    emit_push(state, 2);
  }
  if (imm) {
    emit_load_imm(state, 1, imm);
  } else {
    emit_mov(state, src, 1);
  }
  emit_mov(state, dst, 0);
  if (div || mod) {
    emit_alu32(state, 0x31, 2, 2);
  }
  if (is64) {
    emit_rex(state, 1, 0, 0, 0);
  }
  emit_alu32(state, 0xf7, mul ? 4 : 6, 1);
  if (dst != 2) {
    if (mod) {
      emit_mov(state, 2, dst);
    }
    emit_pop(state, 2);
  }
  if (dst != 0) {
    if (div || mul) {
      emit_mov(state, 0, dst);
    }
    emit_pop(state, 0);
  }
}


x86 assembly (clang, -O0):
<muldivmod>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    %r9,-0x28(%rbp)
mov    -0x14(%rbp),%eax
and    $0xf0,%eax
cmp    $0x20,%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x14(%rbp),%eax
and    $0xf0,%eax
cmp    $0x30,%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x30(%rbp)
mov    -0x14(%rbp),%eax
and    $0xf0,%eax
cmp    $0x90,%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x34(%rbp)
mov    -0x14(%rbp),%eax
and    $0x7,%eax
cmp    $0x7,%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x30(%rbp)
jne    120a <muldivmod+0x8a>
cmpl   $0x0,-0x34(%rbp)
je     1266 <muldivmod+0xe6>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x1,%esi
call   1080 <emit_load_imm@plt>
cmpl   $0x0,-0x38(%rbp)
je     123f <muldivmod+0xbf>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%edx
mov    -0x18(%rbp),%ecx
mov    $0x85,%esi
call   1040 <emit_alu64@plt>
jmp    1253 <muldivmod+0xd3>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%edx
mov    -0x18(%rbp),%ecx
mov    $0x85,%esi
call   1050 <emit_alu32@plt>
mov    -0x8(%rbp),%rdi
mov    $0x84,%esi
mov    $0xfffffffe,%edx
call   1090 <emit_jcc@plt>
cmpl   $0x0,-0x1c(%rbp)
je     127b <muldivmod+0xfb>
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
call   1070 <emit_push@plt>
cmpl   $0x2,-0x1c(%rbp)
je     1293 <muldivmod+0x113>
mov    -0x8(%rbp),%rdi
mov    $0x2,%esi
call   1070 <emit_push@plt>
cmpq   $0x0,-0x28(%rbp)
je     12b5 <muldivmod+0x135>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    $0x1,%esi
call   1080 <emit_load_imm@plt>
jmp    12c6 <muldivmod+0x146>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    $0x1,%edx
call   1060 <emit_mov@plt>
mov    -0x8(%rbp),%rdi
mov    -0x1c(%rbp),%esi
xor    %edx,%edx
call   1060 <emit_mov@plt>
cmpl   $0x0,-0x30(%rbp)
jne    12e8 <muldivmod+0x168>
cmpl   $0x0,-0x34(%rbp)
je     12fd <muldivmod+0x17d>
mov    -0x8(%rbp),%rdi
mov    $0x31,%esi
mov    $0x2,%ecx
mov    %ecx,%edx
call   1050 <emit_alu32@plt>
cmpl   $0x0,-0x38(%rbp)
je     131e <muldivmod+0x19e>
mov    -0x8(%rbp),%rdi
mov    $0x1,%esi
xor    %r8d,%r8d
mov    %r8d,%edx
mov    %r8d,%ecx
call   10a0 <emit_rex@plt>
mov    -0x8(%rbp),%rdi
mov    -0x2c(%rbp),%ecx
mov    $0x6,%edx
mov    $0x4,%eax
cmp    $0x0,%ecx
cmovne %eax,%edx
mov    $0xf7,%esi
mov    $0x1,%ecx
call   1050 <emit_alu32@plt>
cmpl   $0x2,-0x1c(%rbp)
je     1377 <muldivmod+0x1f7>
cmpl   $0x0,-0x34(%rbp)
je     1369 <muldivmod+0x1e9>
mov    -0x8(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    $0x2,%esi
call   1060 <emit_mov@plt>
mov    -0x8(%rbp),%rdi
mov    $0x2,%esi
call   1030 <emit_pop@plt>
cmpl   $0x0,-0x1c(%rbp)
je     13ae <muldivmod+0x22e>
cmpl   $0x0,-0x30(%rbp)
jne    1395 <muldivmod+0x215>
cmpl   $0x0,-0x2c(%rbp)
je     13a3 <muldivmod+0x223>
mov    -0x8(%rbp),%rdi
mov    -0x1c(%rbp),%edx
xor    %esi,%esi
call   1060 <emit_mov@plt>
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
call   1030 <emit_pop@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<muldivmod>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9,0x10(%rsp)
mov    %r8d,%r13d
mov    %ecx,%r12d
mov    %edx,%r15d
mov    %rsi,%rdx
mov    %rdi,%rbx
mov    %r15d,%ebp
and    $0xf0,%ebp
mov    %r15d,%r14d
and    $0x7,%r14d
cmp    $0x30,%bpl
je     11be <muldivmod+0x3e>
cmp    $0x90,%bpl
jne    11fd <muldivmod+0x7d>
mov    %rbx,%rdi
mov    $0x1,%esi
call   1080 <emit_load_imm@plt>
mov    %rbx,%rdi
mov    $0x85,%esi
mov    %r12d,%edx
mov    %r12d,%ecx
cmp    $0x7,%r14d
jne    11e6 <muldivmod+0x66>
call   1040 <emit_alu64@plt>
jmp    11eb <muldivmod+0x6b>
call   1050 <emit_alu32@plt>
mov    %rbx,%rdi
mov    $0x84,%esi
mov    $0xfffffffe,%edx
call   1090 <emit_jcc@plt>
test   %r13d,%r13d
je     1214 <muldivmod+0x94>
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <emit_push@plt>
mov    $0x1,%al
cmp    $0x2,%r13d
je     1223 <muldivmod+0xa3>
mov    %rbx,%rdi
mov    $0x2,%esi
call   1070 <emit_push@plt>
xor    %eax,%eax
mov    %eax,0xc(%rsp)
mov    0x10(%rsp),%rdx
mov    %rbx,%rdi
test   %rdx,%rdx
je     1240 <muldivmod+0xc0>
mov    $0x1,%esi
call   1080 <emit_load_imm@plt>
jmp    124d <muldivmod+0xcd>
mov    %r12d,%esi
mov    $0x1,%edx
call   1060 <emit_mov@plt>
mov    %rbx,%rdi
mov    %r13d,%esi
xor    %edx,%edx
call   1060 <emit_mov@plt>
cmp    $0x30,%bpl
je     1266 <muldivmod+0xe6>
cmp    $0x90,%bpl
jne    127d <muldivmod+0xfd>
mov    %rbx,%rdi
mov    $0x31,%esi
mov    $0x2,%edx
mov    $0x2,%ecx
call   1050 <emit_alu32@plt>
cmp    $0x7,%r14d
jne    1297 <muldivmod+0x117>
mov    %rbx,%rdi
mov    $0x1,%esi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10a0 <emit_rex@plt>
xor    %eax,%eax
cmp    $0x20,%ebp
setne  %al
lea    (%rax,%rax,1),%edx
add    $0x4,%edx
mov    %rbx,%rdi
mov    $0xf7,%esi
mov    $0x1,%ecx
call   1050 <emit_alu32@plt>
cmpb   $0x0,0xc(%rsp)
jne    12e3 <muldivmod+0x163>
cmp    $0x90,%ebp
jne    12d6 <muldivmod+0x156>
mov    %rbx,%rdi
mov    $0x2,%esi
mov    %r13d,%edx
call   1060 <emit_mov@plt>
mov    %rbx,%rdi
mov    $0x2,%esi
call   1030 <emit_pop@plt>
test   %r13d,%r13d
je     131a <muldivmod+0x19a>
and    $0xe0,%r15d
cmp    $0x20,%r15d
jne    1302 <muldivmod+0x182>
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r13d,%edx
call   1060 <emit_mov@plt>
mov    %rbx,%rdi
xor    %esi,%esi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <emit_pop@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.jit_state = type { i32 }
define void @muldivmod(%struct.jit_state* %0, i64 %1, i32 %2, i32 %3, i32 %4, i64 %5) {
%7 = alloca %struct.jit_state*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i64
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
store %struct.jit_state* %0, %struct.jit_state** %7
store i64 %1, i64* %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
store i64 %5, i64* %12
%17 = load i32, i32* %9
%18 = and i32 %17, 240
%19 = icmp eq i32 %18, 32
%20 = zext i1 %19 to i32
store i32 %20, i32* %13
%21 = load i32, i32* %9
%22 = and i32 %21, 240
%23 = icmp eq i32 %22, 48
%24 = zext i1 %23 to i32
store i32 %24, i32* %14
%25 = load i32, i32* %9
%26 = and i32 %25, 240
%27 = icmp eq i32 %26, 144
%28 = zext i1 %27 to i32
store i32 %28, i32* %15
%29 = load i32, i32* %9
%30 = and i32 %29, 7
%31 = icmp eq i32 %30, 7
%32 = zext i1 %31 to i32
store i32 %32, i32* %16
%33 = load i32, i32* %14
%34 = icmp ne i32 %33, 0
br i1 %34, label %38, label %35
35: ; preds = %6
%36 = load i32, i32* %15
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %57
38: ; preds = %35, %6
%39 = load %struct.jit_state*, %struct.jit_state** %7
%40 = load i64, i64* %8
%41 = call i32 @emit_load_imm(%struct.jit_state* %39, i32 1, i64 %40)
%42 = load i32, i32* %16
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %49
44: ; preds = %38
%45 = load %struct.jit_state*, %struct.jit_state** %7
%46 = load i32, i32* %10
%47 = load i32, i32* %10
%48 = call i32 @emit_alu64(%struct.jit_state* %45, i32 133, i32 %46, i32 %47)
br label %54
49: ; preds = %38
%50 = load %struct.jit_state*, %struct.jit_state** %7
%51 = load i32, i32* %10
%52 = load i32, i32* %10
%53 = call i32 @emit_alu32(%struct.jit_state* %50, i32 133, i32 %51, i32 %52)
br label %54
54: ; preds = %49, %44
%55 = load %struct.jit_state*, %struct.jit_state** %7
%56 = call i32 @emit_jcc(%struct.jit_state* %55, i32 132, i32 -2)
br label %57
57: ; preds = %54, %35
%58 = load i32, i32* %11
%59 = icmp ne i32 %58, 0
br i1 %59, label %60, label %63
60: ; preds = %57
%61 = load %struct.jit_state*, %struct.jit_state** %7
%62 = call i32 @emit_push(%struct.jit_state* %61, i32 0)
br label %63
63: ; preds = %60, %57
%64 = load i32, i32* %11
%65 = icmp ne i32 %64, 2
br i1 %65, label %66, label %69
66: ; preds = %63
%67 = load %struct.jit_state*, %struct.jit_state** %7
%68 = call i32 @emit_push(%struct.jit_state* %67, i32 2)
br label %69
69: ; preds = %66, %63
%70 = load i64, i64* %12
%71 = icmp ne i64 %70, 0
br i1 %71, label %72, label %76
72: ; preds = %69
%73 = load %struct.jit_state*, %struct.jit_state** %7
%74 = load i64, i64* %12
%75 = call i32 @emit_load_imm(%struct.jit_state* %73, i32 1, i64 %74)
br label %80
76: ; preds = %69
%77 = load %struct.jit_state*, %struct.jit_state** %7
%78 = load i32, i32* %10
%79 = call i32 @emit_mov(%struct.jit_state* %77, i32 %78, i32 1)
br label %80
80: ; preds = %76, %72
%81 = load %struct.jit_state*, %struct.jit_state** %7
%82 = load i32, i32* %11
%83 = call i32 @emit_mov(%struct.jit_state* %81, i32 %82, i32 0)
%84 = load i32, i32* %14
%85 = icmp ne i32 %84, 0
br i1 %85, label %89, label %86
86: ; preds = %80
%87 = load i32, i32* %15
%88 = icmp ne i32 %87, 0
br i1 %88, label %89, label %92
89: ; preds = %86, %80
%90 = load %struct.jit_state*, %struct.jit_state** %7
%91 = call i32 @emit_alu32(%struct.jit_state* %90, i32 49, i32 2, i32 2)
br label %92
92: ; preds = %89, %86
%93 = load i32, i32* %16
%94 = icmp ne i32 %93, 0
br i1 %94, label %95, label %98
95: ; preds = %92
%96 = load %struct.jit_state*, %struct.jit_state** %7
%97 = call i32 @emit_rex(%struct.jit_state* %96, i32 1, i32 0, i32 0, i32 0)
br label %98
98: ; preds = %95, %92
%99 = load %struct.jit_state*, %struct.jit_state** %7
%100 = load i32, i32* %13
%101 = icmp ne i32 %100, 0
%102 = zext i1 %101 to i64
%103 = select i1 %101, i32 4, i32 6
%104 = call i32 @emit_alu32(%struct.jit_state* %99, i32 247, i32 %103, i32 1)
%105 = load i32, i32* %11
%106 = icmp ne i32 %105, 2
br i1 %106, label %107, label %117
107: ; preds = %98
%108 = load i32, i32* %15
%109 = icmp ne i32 %108, 0
br i1 %109, label %110, label %114
110: ; preds = %107
%111 = load %struct.jit_state*, %struct.jit_state** %7
%112 = load i32, i32* %11
%113 = call i32 @emit_mov(%struct.jit_state* %111, i32 2, i32 %112)
br label %114
114: ; preds = %110, %107
%115 = load %struct.jit_state*, %struct.jit_state** %7
%116 = call i32 @emit_pop(%struct.jit_state* %115, i32 2)
br label %117
117: ; preds = %114, %98
%118 = load i32, i32* %11
%119 = icmp ne i32 %118, 0
br i1 %119, label %120, label %133
120: ; preds = %117
%121 = load i32, i32* %14
%122 = icmp ne i32 %121, 0
br i1 %122, label %126, label %123
123: ; preds = %120
%124 = load i32, i32* %13
%125 = icmp ne i32 %124, 0
br i1 %125, label %126, label %130
126: ; preds = %123, %120
%127 = load %struct.jit_state*, %struct.jit_state** %7
%128 = load i32, i32* %11
%129 = call i32 @emit_mov(%struct.jit_state* %127, i32 0, i32 %128)
br label %130
130: ; preds = %126, %123
%131 = load %struct.jit_state*, %struct.jit_state** %7
%132 = call i32 @emit_pop(%struct.jit_state* %131, i32 0)
br label %133
133: ; preds = %130, %117
ret void
}

LLVM IR (-O2):
%struct.jit_state = type { i32 }
define void @muldivmod(%struct.jit_state* %0, i64 %1, i32 %2, i32 %3, i32 %4, i64 %5) {
%7 = and i32 %2, 240
%8 = icmp eq i32 %7, 32
%9 = icmp eq i32 %7, 144
%10 = and i32 %2, 7
%11 = icmp eq i32 %10, 7
%12 = trunc i32 %7 to i8
switch i8 %12, label %21 [
i8 -112, label %13
i8 48, label %13
]
13: ; preds = %6, %6
%14 = tail call i32 @emit_load_imm(%struct.jit_state* %0, i32 1, i64 %1)
br i1 %11, label %15, label %17
15: ; preds = %13
%16 = tail call i32 @emit_alu64(%struct.jit_state* %0, i32 133, i32 %3, i32 %3)
br label %19
17: ; preds = %13
%18 = tail call i32 @emit_alu32(%struct.jit_state* %0, i32 133, i32 %3, i32 %3)
br label %19
19: ; preds = %17, %15
%20 = tail call i32 @emit_jcc(%struct.jit_state* %0, i32 132, i32 -2)
br label %21
21: ; preds = %6, %19
%22 = icmp eq i32 %4, 0
br i1 %22, label %26, label %23
23: ; preds = %21
%24 = tail call i32 @emit_push(%struct.jit_state* %0, i32 0)
%25 = icmp eq i32 %4, 2
br i1 %25, label %28, label %26
26: ; preds = %21, %23
%27 = tail call i32 @emit_push(%struct.jit_state* %0, i32 2)
br label %28
28: ; preds = %26, %23
%29 = phi i1 [ false, %26 ], [ true, %23 ]
%30 = icmp eq i64 %5, 0
br i1 %30, label %33, label %31
31: ; preds = %28
%32 = tail call i32 @emit_load_imm(%struct.jit_state* %0, i32 1, i64 %5)
br label %35
33: ; preds = %28
%34 = tail call i32 @emit_mov(%struct.jit_state* %0, i32 %3, i32 1)
br label %35
35: ; preds = %33, %31
%36 = tail call i32 @emit_mov(%struct.jit_state* %0, i32 %4, i32 0)
switch i8 %12, label %39 [
i8 -112, label %37
i8 48, label %37
]
37: ; preds = %35, %35
%38 = tail call i32 @emit_alu32(%struct.jit_state* %0, i32 49, i32 2, i32 2)
br label %39
39: ; preds = %35, %37
br i1 %11, label %40, label %42
40: ; preds = %39
%41 = tail call i32 @emit_rex(%struct.jit_state* %0, i32 1, i32 0, i32 0, i32 0)
br label %42
42: ; preds = %40, %39
%43 = select i1 %8, i32 4, i32 6
%44 = tail call i32 @emit_alu32(%struct.jit_state* %0, i32 247, i32 %43, i32 1)
br i1 %29, label %50, label %45
45: ; preds = %42
br i1 %9, label %46, label %48
46: ; preds = %45
%47 = tail call i32 @emit_mov(%struct.jit_state* %0, i32 2, i32 %4)
br label %48
48: ; preds = %46, %45
%49 = tail call i32 @emit_pop(%struct.jit_state* %0, i32 2)
br label %50
50: ; preds = %48, %42
br i1 %22, label %58, label %51
51: ; preds = %50
%52 = and i32 %2, 224
%53 = icmp eq i32 %52, 32
br i1 %53, label %54, label %56
54: ; preds = %51
%55 = tail call i32 @emit_mov(%struct.jit_state* %0, i32 0, i32 %4)
br label %56
56: ; preds = %51, %54
%57 = tail call i32 @emit_pop(%struct.jit_state* %0, i32 0)
br label %58
58: ; preds = %56, %50
ret void
}