// File: /workspace/github_c_makefile_star2_O2/cogbee[P]vmir-latest/src/main.c
// Function: main
int
main(int argc, char **argv)
{
  int debug_flags = 0;
  int run = 1;
  const char *debugged_function = NULL;
  int opt;
  const char *argv0 = argv[0];
  int print_stats = 0;
  int verbose = 0;

  while((opt = getopt(argc, argv, "plidf:nhrbsjIv")) != -1) {
    switch(opt) {
    case 'p':
      debug_flags |= VMIR_DBG_DUMP_PARSED_FUNCTION;
      break;
    case 'l':
      debug_flags |= VMIR_DBG_DUMP_LOWERED_FUNCTION;
      break;
    case 'i':
      debug_flags |= VMIR_DBG_LIST_FUNCTIONS;
      break;
    case 'd':
      debug_flags |= VMIR_DBG_DUMP_DEV;
      break;
    case 'r':
      debug_flags |= VMIR_DBG_DUMP_REGALLOC;
      break;
    case 'b':
      debug_flags |= VMIR_DBG_BB_INSTRUMENT;
      break;
    case 'j':
      debug_flags |= VMIR_DBG_DISABLE_JIT;
      break;
    case 'I':
      debug_flags |= VMIR_DBG_IGNORE_UNRESOLVED_FUNCTIONS;
      break;
    case 'f':
      debugged_function = optarg;
      break;
    case 'h':
      usage(argv0);
      exit(0);
    case 'n':
      run = 0;
      break;
    case 's':
      print_stats = 1;
      break;
    case 'v':
      verbose = 1;
      break;
    default:
      usage(argv0);
      exit(1);
    }
  }

  if(optind >= argc) {
    fprintf(stderr, "Please provide a .wasm or .bc file as the first argument.\n");
    exit(1);
  }

  argc -= optind;
  argv += optind;

  int fd = open(argv[0], O_RDONLY);
  if(fd == -1) {
    perror("open");
    exit(1);
  }

  struct stat st;
  if(fstat(fd, &st)) {
    perror("stat");
    exit(1);
  }

  uint8_t *buf = malloc(st.st_size);
  if(read(fd, buf, st.st_size) != st.st_size) {
    exit(1);
  }
  close(fd);

#define MB(x) ((x) * 1024 * 1024)

  void *mem = calloc(1, MB(64));

  ir_unit_t *iu = vmir_create(mem, MB(64), MB(1), MB(1), NULL);

  vmir_set_debug_flags(iu, debug_flags);
  vmir_set_debugged_function(iu, debugged_function);

  if(verbose)
    vmir_set_log_level(iu, VMIR_LOG_DEBUG);

  int code = vmir_load(iu, buf, st.st_size);
  if(code) {
    free(mem);
    free(buf);
    vmir_destroy(iu);
    return -1;
  }
  free(buf);

  if(run) {
    int64_t ts = get_ts();
    int rval;
    code = vmir_run(iu, &rval, argc, argv);
    ts = get_ts() - ts;
    if(print_stats)
      printf("main() executed for %d ms\n", (int)(ts / 1000LL));
  }

  if(print_stats)
    dump_stats(iu);

  vmir_instrumentation_dump(iu);

  vmir_destroy(iu);

  free(mem);

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rsi, %r14
movl %edi, %ebx
movq (%rsi), %rax
movq %rax, 0x18(%rsp)
movq $0x0, 0x8(%rsp)
pushq $0x1
popq %rax
leaq 0x1aa0b(%rip), %r12     # 0x1e058
leaq 0x1a9b0(%rip), %r15     # 0x1e004
movl $0x0, 0x10(%rsp)
movl $0x0, 0x14(%rsp)
xorl %ebp, %ebp
movl %eax, %r13d
movl %ebx, %edi
movq %r14, %rsi
movq %r12, %rdx
callq 0x3440
leal -0x62(%rax), %ecx
cmpl $0x14, %ecx
ja 0x3691
movslq (%r15,%rcx,4), %rcx
addq %r15, %rcx
xorl %eax, %eax
jmpq *%rcx
orl $0x20, %ebp
movl %r13d, %eax
jmp 0x3666
cmpl $0x49, %eax
jne 0x3706
orl $0x80, %ebp
movl %r13d, %eax
jmp 0x3666
orl $0x2, %ebp
movl %r13d, %eax
jmp 0x3666
orl $0x4, %ebp
movl %r13d, %eax
jmp 0x3666
movq 0x27918(%rip), %rax     # 0x2afd0
movq (%rax), %rax
movq %rax, 0x8(%rsp)
movl %r13d, %eax
jmp 0x3666
orl $0x8, %ebp
movl %r13d, %eax
jmp 0x3666
movl %r13d, %eax
pushq $0x1
popq %rcx
movl %ecx, 0x14(%rsp)
jmp 0x3666
orl $0x40, %ebp
movl %r13d, %eax
jmp 0x3666
orl $0x1, %ebp
movl %r13d, %eax
jmp 0x3666
orl $0x10, %ebp
movl %r13d, %eax
jmp 0x3666
movl %r13d, %eax
pushq $0x1
popq %rcx
movl %ecx, 0x10(%rsp)
jmp 0x3666
cmpl $-0x1, %eax
jne 0x39d8
movq 0x278b2(%rip), %rax     # 0x2afc8
movslq (%rax), %r15
cmpl %r15d, %ebx
jle 0x39fb
movq (%r14,%r15,8), %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x3410
cmpl $-0x1, %eax
je 0x3a19
movl %eax, %r12d
leaq 0x38(%rsp), %rsi
movl %eax, %edi
callq 0x3500
testl %eax, %eax
jne 0x3a22
movq %r15, 0x20(%rsp)
movq 0x68(%rsp), %r15
movq %r15, %rdi
callq 0x3380
movl %r12d, %edi
movq %rax, 0x18(%rsp)
movq %rax, %rsi
movq %r15, %rdx
callq 0x32b0
movq %r15, 0x28(%rsp)
cmpq %r15, %rax
jne 0x39e2
movl %r12d, %edi
callq 0x3230
pushq $0x1
popq %rdi
movl $0x4000000, %esi        # imm = 0x4000000
callq 0x32f0
movq %rax, %r15
movq %rax, %rdi
movl $0x4000000, %esi        # imm = 0x4000000
movl $0x100000, %edx         # imm = 0x100000
movl $0x100000, %ecx         # imm = 0x100000
xorl %r8d, %r8d
callq 0xa19e
movq %rax, %rdi
movl %ebp, %esi
movq %rax, %rbp
callq 0xc209
movq %rbp, %rdi
movq 0x8(%rsp), %rsi
callq 0xc210
cmpl $0x0, 0x14(%rsp)
je 0x37e0
pushq $0x3
popq %rsi
movq %rbp, %rdi
callq 0x3ad0
movq %rbp, %rdi
movq 0x18(%rsp), %r12
movq %r12, %rsi
movq 0x28(%rsp), %rdx
callq 0xa242
testl %eax, %eax
je 0x3819
movq %r15, %rdi
callq 0x3030
movq %r12, %rdi
callq 0x3030
movq %rbp, %rdi
callq 0xa06b
pushq $-0x1
popq %rax
jmp 0x39c6
movq %rbp, 0x8(%rsp)
movq %r12, %rdi
callq 0x3030
testl %r13d, %r13d
movl 0x10(%rsp), %ebp
movq %r15, %r13
je 0x387f
movq 0x20(%rsp), %rax
subl %eax, %ebx
leaq (%r14,%rax,8), %r12
callq 0x3aa3
movq %rax, %r14
leaq 0x34(%rsp), %rsi
movq 0x8(%rsp), %rdi
movl %ebx, %edx
movq %r12, %rcx
callq 0xc0cb
callq 0x3aa3
testl %ebp, %ebp
je 0x387f
subq %r14, %rax
movl $0x3e8, %ecx            # imm = 0x3E8
cqto
idivq %rcx
leaq 0x1a831(%rip), %rdi     # 0x1e0a7
movl %eax, %esi
xorl %eax, %eax
callq 0x3190
testl %ebp, %ebp
movq 0x8(%rsp), %r15
je 0x39ac
movq %r15, %rdi
callq 0xc27c
movq %rax, %r14
pushq $0xa
popq %rbx
movl %ebx, %edi
callq 0x3040
leaq 0x1aa4b(%rip), %rdi     # 0x1e2f3
callq 0x30e0
movl %ebx, %edi
callq 0x3040
movl (%r14), %esi
leaq 0x1a826(%rip), %rdi     # 0x1e0e4
xorl %eax, %eax
callq 0x3190
movl 0x4(%r14), %esi
leaq 0x1a82d(%rip), %rdi     # 0x1e0fd
xorl %eax, %eax
callq 0x3190
movl 0x8(%r14), %esi
leaq 0x1a834(%rip), %rdi     # 0x1e116
xorl %eax, %eax
callq 0x3190
movl 0xc(%r14), %esi
leaq 0x1a83b(%rip), %rdi     # 0x1e12f
xorl %eax, %eax
callq 0x3190
movl 0x10(%r14), %esi
leaq 0x1a842(%rip), %rdi     # 0x1e148
xorl %eax, %eax
callq 0x3190
movl %ebx, %edi
callq 0x3040
leaq 0x1a9ec(%rip), %rdi     # 0x1e307
callq 0x30e0
movl %ebx, %edi
callq 0x3040
movl 0x24(%r14), %esi
leaq 0x1a82f(%rip), %rdi     # 0x1e161
xorl %eax, %eax
callq 0x3190
movl 0x28(%r14), %esi
leaq 0x1a836(%rip), %rdi     # 0x1e17a
xorl %eax, %eax
callq 0x3190
movl 0x2c(%r14), %esi
leaq 0x1a83d(%rip), %rdi     # 0x1e193
xorl %eax, %eax
callq 0x3190
movl 0x14(%r14), %esi
leaq 0x1a844(%rip), %rdi     # 0x1e1ac
xorl %eax, %eax
callq 0x3190
movl 0x18(%r14), %esi
leaq 0x1a84b(%rip), %rdi     # 0x1e1c5
xorl %eax, %eax
callq 0x3190
movl 0x1c(%r14), %esi
leaq 0x1a852(%rip), %rdi     # 0x1e1de
xorl %eax, %eax
callq 0x3190
movl 0x20(%r14), %esi
leaq 0x1a859(%rip), %rdi     # 0x1e1f7
xorl %eax, %eax
callq 0x3190
movl %ebx, %edi
callq 0x3040
movq %r15, %rdi
callq 0xbfc6
movq %r15, %rdi
callq 0xa06b
movq %r13, %rdi
callq 0x3030
xorl %eax, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%rsp), %rdi
callq 0x3a30
pushq $0x1
popq %rdi
callq 0x34a0
movq 0x18(%rsp), %rdi
callq 0x3a30
xorl %edi, %edi
callq 0x34a0
movq 0x275ee(%rip), %rax     # 0x2aff0
movq (%rax), %rcx
leaq 0x1a65b(%rip), %rdi     # 0x1e067
pushq $0x3a
popq %rsi
pushq $0x1
popq %rdx
callq 0x34b0
jmp 0x39e2
leaq 0x1c8b6(%rip), %rdi     # 0x202d6
jmp 0x3a29
leaq 0x1a679(%rip), %rdi     # 0x1e0a2
callq 0x3430
jmp 0x39e2