// File: /workspace/github_c_makefile_star2_O3/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 $0xd8, %rsp
movq %rsi, %r15
movl %edi, %ebx
movq (%rsi), %rax
movq %rax, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movl $0x1, %eax
leaq 0x1f959(%rip), %r12     # 0x23058
leaq 0x1f8fe(%rip), %r14     # 0x23004
movl $0x0, 0x8(%rsp)
movl $0x0, 0xc(%rsp)
xorl %ebp, %ebp
movl %eax, %r13d
movl %ebx, %edi
movq %r15, %rsi
movq %r12, %rdx
callq 0x3440
leal -0x62(%rax), %ecx
cmpl $0x14, %ecx
ja 0x3743
movslq (%r14,%rcx,4), %rcx
addq %r14, %rcx
xorl %eax, %eax
jmpq *%rcx
orl $0x20, %ebp
movl %r13d, %eax
jmp 0x3718
cmpl $0x49, %eax
jne 0x37ba
orl $0x80, %ebp
movl %r13d, %eax
jmp 0x3718
orl $0x2, %ebp
movl %r13d, %eax
jmp 0x3718
orl $0x4, %ebp
movl %r13d, %eax
jmp 0x3718
movq 0x2a866(%rip), %rax     # 0x2dfd0
movq (%rax), %rax
movq %rax, 0x18(%rsp)
movl %r13d, %eax
jmp 0x3718
orl $0x8, %ebp
movl %r13d, %eax
jmp 0x3718
movl $0x1, 0xc(%rsp)
movl %r13d, %eax
jmp 0x3718
orl $0x40, %ebp
movl %r13d, %eax
jmp 0x3718
orl $0x1, %ebp
movl %r13d, %eax
jmp 0x3718
orl $0x10, %ebp
movl %r13d, %eax
jmp 0x3718
movl $0x1, 0x8(%rsp)
movl %r13d, %eax
jmp 0x3718
cmpl $-0x1, %eax
jne 0x3ad3
movq 0x2a7fe(%rip), %rax     # 0x2dfc8
movslq (%rax), %r14
cmpl %r14d, %ebx
jle 0x3af8
movq (%r15,%r14,8), %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x3410
cmpl $-0x1, %eax
je 0x3afd
movl %eax, %r12d
leaq 0x48(%rsp), %rsi
movl %eax, %edi
callq 0x3500
testl %eax, %eax
jne 0x3b02
movq %r14, 0x20(%rsp)
movq 0x78(%rsp), %r14
movq %r14, %rdi
callq 0x3380
movl %r12d, %edi
movq %rax, 0x10(%rsp)
movq %rax, %rsi
movq %r14, %rdx
callq 0x32b0
movq %r14, 0x28(%rsp)
cmpq %r14, %rax
jne 0x3add
movl %r12d, %edi
callq 0x3230
movl $0x1, %edi
movl $0x4000000, %esi        # imm = 0x4000000
callq 0x32f0
movq %rax, %r14
movq %rax, %rdi
movl $0x4000000, %esi        # imm = 0x4000000
movl $0x100000, %edx         # imm = 0x100000
movl $0x100000, %ecx         # imm = 0x100000
xorl %r8d, %r8d
callq 0xa33c
movq %rax, %rdi
movl %ebp, %esi
movq %rax, %rbp
callq 0xce42
movq %rbp, %rdi
movq 0x18(%rsp), %rsi
callq 0xce49
cmpl $0x0, 0xc(%rsp)
je 0x3898
movq %rbp, %rdi
movl $0x3, %esi
callq 0x3b80
movq %rbp, %rdi
movq 0x10(%rsp), %r12
movq %r12, %rsi
movq 0x28(%rsp), %rdx
callq 0xa3e2
testl %eax, %eax
je 0x38d3
movq %r14, %rdi
callq 0x3030
movq %r12, %rdi
callq 0x3030
movq %rbp, %rdi
callq 0xa1eb
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x3ac1
movq %r12, %rdi
callq 0x3030
testl %r13d, %r13d
je 0x397c
movq %r14, 0x18(%rsp)
movq 0x20(%rsp), %rax
subl %eax, %ebx
leaq (%r15,%rax,8), %r12
leaq 0x30(%rsp), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x31d0
movq (%r15), %r13
movq 0x8(%r15), %r14
leaq 0x44(%rsp), %rsi
movq %rbp, %r15
movq %rbp, %rdi
movl %ebx, %edx
movq %r12, %rcx
callq 0xccdf
leaq 0x30(%rsp), %rdi
xorl %esi, %esi
callq 0x31d0
cmpl $0x0, 0x8(%rsp)
je 0x3974
movq 0x30(%rsp), %rax
subq %r13, %rax
movq 0x38(%rsp), %rcx
subq %r14, %rcx
imulq $0xf4240, %rax, %rax    # imm = 0xF4240
addq %rcx, %rax
movabsq $0x20c49ba5e353f7cf, %rcx # imm = 0x20C49BA5E353F7CF
imulq %rcx
movq %rdx, %rsi
shrq $0x3f, %rsi
shrq $0x7, %rdx
addl %edx, %esi
leaq 0x1f73a(%rip), %rdi     # 0x230a7
xorl %eax, %eax
callq 0x3190
movq 0x18(%rsp), %r14
movq %r15, %rbp
cmpl $0x0, 0x8(%rsp)
je 0x3aa7
movq %rbp, %rdi
callq 0xceb5
movq %rax, %rbx
movl $0xa, %edi
callq 0x3040
leaq 0x1f950(%rip), %rdi     # 0x232f3
callq 0x30e0
movl $0xa, %edi
callq 0x3040
movl (%rbx), %esi
leaq 0x1f729(%rip), %rdi     # 0x230e4
xorl %eax, %eax
callq 0x3190
movl 0x4(%rbx), %esi
leaq 0x1f731(%rip), %rdi     # 0x230fd
xorl %eax, %eax
callq 0x3190
movl 0x8(%rbx), %esi
leaq 0x1f739(%rip), %rdi     # 0x23116
xorl %eax, %eax
callq 0x3190
movl 0xc(%rbx), %esi
leaq 0x1f741(%rip), %rdi     # 0x2312f
xorl %eax, %eax
callq 0x3190
movl 0x10(%rbx), %esi
leaq 0x1f749(%rip), %rdi     # 0x23148
xorl %eax, %eax
callq 0x3190
movl $0xa, %edi
callq 0x3040
leaq 0x1f8f0(%rip), %rdi     # 0x23307
callq 0x30e0
movl $0xa, %edi
callq 0x3040
movl 0x24(%rbx), %esi
leaq 0x1f731(%rip), %rdi     # 0x23161
xorl %eax, %eax
callq 0x3190
movl 0x28(%rbx), %esi
leaq 0x1f739(%rip), %rdi     # 0x2317a
xorl %eax, %eax
callq 0x3190
movl 0x2c(%rbx), %esi
leaq 0x1f741(%rip), %rdi     # 0x23193
xorl %eax, %eax
callq 0x3190
movl 0x14(%rbx), %esi
leaq 0x1f749(%rip), %rdi     # 0x231ac
xorl %eax, %eax
callq 0x3190
movl 0x18(%rbx), %esi
leaq 0x1f751(%rip), %rdi     # 0x231c5
xorl %eax, %eax
callq 0x3190
movl 0x1c(%rbx), %esi
leaq 0x1f759(%rip), %rdi     # 0x231de
xorl %eax, %eax
callq 0x3190
movl 0x20(%rbx), %esi
leaq 0x1f761(%rip), %rdi     # 0x231f7
xorl %eax, %eax
callq 0x3190
movl $0xa, %edi
callq 0x3040
movq %rbp, %rdi
callq 0xcb90
movq %rbp, %rdi
callq 0xa1eb
movq %r14, %rdi
callq 0x3030
xorl %eax, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%rsp), %rdi
callq 0x3b07
movl $0x1, %edi
callq 0x34a0
movq 0x10(%rsp), %rdi
callq 0x3b07
xorl %edi, %edi
callq 0x34a0
callq 0x3530
callq 0x356a
callq 0x3555