#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;
typedef int /*<<< orphan*/ opcode;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
scalar_t__ fread(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  if (argc <= 1) {
    printf("Usage: mips86emu <flat binary>\n");
    return 0;
  }
  const char *fname = argv[1];
  FILE *f = fopen(fname, "rb");
  if (!f) {
    printf("Could not open file.\n");
    return 1;
  }
  uint32_t opcode;
  while (fread(&opcode, sizeof(opcode), 1, f) != 0) {
    uint32_t op = opcode & (1 << 6) - 1;
    if (op == 0) {
      uint32_t rs = opcode & ((1 << 5) - 1) << 6;
      uint32_t rt = opcode & ((1 << 5) - 1) << 11;
      uint32_t rd = opcode & ((1 << 5) - 1) << 16;
      uint32_t shamt = opcode & ((1 << 5) - 1) << 21;
      uint32_t func = opcode & ((1 << 6) - 1) << 26;
      printf("Got opcode: op = 0x%x rs = 0x%x rt = 0x%x rd = 0x%x\n", op, rs,
             rt, rd);
    }
  }
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jg     117a <main+0x3a>
lea    0xe99(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    125f <main+0x11f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe8f(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11bf <main+0x7f>
lea    0xe77(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    125f <main+0x11f>
jmp    11c4 <main+0x84>
mov    -0x20(%rbp),%rcx
lea    -0x24(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x0,%rax
je     124f <main+0x10f>
mov    -0x24(%rbp),%eax
and    $0x3f,%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    124a <main+0x10a>
mov    -0x24(%rbp),%eax
and    $0x7c0,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x24(%rbp),%eax
and    $0xf800,%eax
mov    %eax,-0x30(%rbp)
mov    -0x24(%rbp),%eax
and    $0x1f0000,%eax
mov    %eax,-0x34(%rbp)
mov    -0x24(%rbp),%eax
and    $0x3e00000,%eax
mov    %eax,-0x38(%rbp)
mov    -0x24(%rbp),%eax
and    $0xfc000000,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x28(%rbp),%esi
mov    -0x2c(%rbp),%edx
mov    -0x30(%rbp),%ecx
mov    -0x34(%rbp),%r8d
lea    0xdf6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    11c4 <main+0x84>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
cmp    $0x1,%edi
jg     116f <main+0x1f>
lea    0xee8(%rip),%rdi
call   1030 <puts@plt>
jmp    1214 <main+0xc4>
mov    0x8(%rsi),%rdi
lea    0xe86(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1218 <main+0xc8>
mov    %rax,%rbx
lea    0xc(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
test   %rax,%rax
je     120c <main+0xbc>
lea    0xe55(%rip),%r14
lea    0xc(%rsp),%r15
jmp    11da <main+0x8a>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15,%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
test   %rax,%rax
je     120c <main+0xbc>
mov    0xc(%rsp),%r8d
test   $0x3f,%r8b
jne    11c0 <main+0x70>
mov    %r8d,%edx
and    $0x7c0,%edx
mov    %r8d,%ecx
and    $0xf800,%ecx
and    $0x1f0000,%r8d
mov    %r14,%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    11c0 <main+0x70>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
jmp    1229 <main+0xd9>
lea    0xe19(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [32 x i8] c"Usage: mips86emu <flat binary>\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [22 x i8] c"Could not open file.\0A\00"
@.str.3 = [53 x i8] c"Got opcode: op = 0x%x rs = 0x%x rt = 0x%x rd = 0x%x\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = load i32, i32* %4
%16 = icmp sle i32 %15, 1
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %59
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
store i8* %22, i8** %6
%23 = load i8*, i8** %6
%24 = call i32* @fopen(i8* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %24, i32** %7
%25 = load i32*, i32** %7
%26 = icmp ne i32* %25, null
br i1 %26, label %29, label %27
27: ; preds = %19
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %59
29: ; preds = %19
br label %30
30: ; preds = %55, %29
%31 = load i32*, i32** %7
%32 = call i64 @fread(i32* %8, i32 4, i32 1, i32* %31)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %56
34: ; preds = %30
%35 = load i32, i32* %8
%36 = and i32 %35, 63
store i32 %36, i32* %9
%37 = load i32, i32* %9
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %55
39: ; preds = %34
%40 = load i32, i32* %8
%41 = and i32 %40, 1984
store i32 %41, i32* %10
%42 = load i32, i32* %8
%43 = and i32 %42, 63488
store i32 %43, i32* %11
%44 = load i32, i32* %8
%45 = and i32 %44, 2031616
store i32 %45, i32* %12
%46 = load i32, i32* %8
%47 = and i32 %46, 65011712
store i32 %47, i32* %13
%48 = load i32, i32* %8
%49 = and i32 %48, -67108864
store i32 %49, i32* %14
%50 = load i32, i32* %9
%51 = load i32, i32* %10
%52 = load i32, i32* %11
%53 = load i32, i32* %12
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0), i32 %50, i32 %51, i32 %52, i32 %53)
br label %55
55: ; preds = %39, %34
br label %30
56: ; preds = %30
%57 = load i32*, i32** %7
%58 = call i32 @fclose(i32* %57)
store i32 0, i32* %3
br label %59
59: ; preds = %56, %27, %17
%60 = load i32, i32* %3
ret i32 %60
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"rb\00"
@.str.3 = [53 x i8] c"Got opcode: op = 0x%x rs = 0x%x rt = 0x%x rd = 0x%x\0A\00"
@str = [21 x i8] c"Could not open file.\00"
@str.4 = [31 x i8] c"Usage: mips86emu <flat binary>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = icmp slt i32 %0, 2
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.4, i64 0, i64 0))
br label %32
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32* @fopen(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %14
12: ; preds = %7
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
br label %32
14: ; preds = %7
%15 = bitcast i32* %3 to i8*
%16 = call i64 @fread(i32* %3, i32 4, i32 1, i32* %10)
%17 = icmp eq i64 %16, 0
br i1 %17, label %30, label %18
18: ; preds = %14, %27
%19 = load i32, i32* %3
%20 = and i32 %19, 63
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %18
%23 = and i32 %19, 1984
%24 = and i32 %19, 63488
%25 = and i32 %19, 2031616
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0), i32 0, i32 %23, i32 %24, i32 %25)
br label %27
27: ; preds = %22, %18
%28 = call i64 @fread(i32* %3, i32 4, i32 1, i32* %10)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %18
30: ; preds = %27, %14
%31 = tail call i32 @fclose(i32* %10)
br label %32
32: ; preds = %12, %30, %5
%33 = phi i32 [ 0, %5 ], [ 0, %30 ], [ 1, %12 ]
ret i32 %33
}