// File: /workspace/github_c_makefile_star2_O1/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, 0x20(%rsp)
movl $0x1, %eax
leaq 0x1f9b9(%rip), %r12     # 0x23058
leaq 0x1f95e(%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 0x36e3
movslq (%r14,%rcx,4), %rcx
addq %r14, %rcx
xorl %eax, %eax
jmpq *%rcx
orl $0x20, %ebp
movl %r13d, %eax
jmp 0x36b8
cmpl $0x49, %eax
jne 0x375a
orl $0x80, %ebp
movl %r13d, %eax
jmp 0x36b8
orl $0x2, %ebp
movl %r13d, %eax
jmp 0x36b8
orl $0x4, %ebp
movl %r13d, %eax
jmp 0x36b8
movq 0x2a8c6(%rip), %rax     # 0x2dfd0
movq (%rax), %rax
movq %rax, 0x20(%rsp)
movl %r13d, %eax
jmp 0x36b8
orl $0x8, %ebp
movl %r13d, %eax
jmp 0x36b8
movl $0x1, 0xc(%rsp)
movl %r13d, %eax
jmp 0x36b8
orl $0x40, %ebp
movl %r13d, %eax
jmp 0x36b8
orl $0x1, %ebp
movl %r13d, %eax
jmp 0x36b8
orl $0x10, %ebp
movl %r13d, %eax
jmp 0x36b8
movl $0x1, 0x8(%rsp)
movl %r13d, %eax
jmp 0x36b8
cmpl $-0x1, %eax
jne 0x3a63
movq 0x2a85e(%rip), %rax     # 0x2dfc8
movslq (%rax), %r14
cmpl %r14d, %ebx
jle 0x3a88
movq (%r15,%r14,8), %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x3410
cmpl $-0x1, %eax
je 0x3a8d
movq %r14, 0x28(%rsp)
leaq 0x48(%rsp), %rsi
movl %eax, %r14d
movl %eax, %edi
callq 0x3500
testl %eax, %eax
jne 0x3a92
movq 0x78(%rsp), %r12
movq %r12, %rdi
callq 0x3380
movl %r14d, %edi
movq %rax, 0x10(%rsp)
movq %rax, %rsi
movq %r12, %rdx
callq 0x32b0
cmpq %r12, %rax
jne 0x3a6d
movl %r14d, %edi
callq 0x3230
movl $0x1, %edi
movl $0x4000000, %esi        # imm = 0x4000000
callq 0x32f0
movq %rax, 0x18(%rsp)
movq %rax, %rdi
movl $0x4000000, %esi        # imm = 0x4000000
movl $0x100000, %edx         # imm = 0x100000
movl $0x100000, %ecx         # imm = 0x100000
xorl %r8d, %r8d
callq 0xa2f2
movq %rax, %r12
movq %rax, %rdi
movl %ebp, %esi
callq 0xce1e
movq %r12, %rdi
movq 0x20(%rsp), %rsi
callq 0xce25
cmpl $0x0, 0xc(%rsp)
je 0x3835
movq %r12, %rdi
movl $0x3, %esi
callq 0x3b10
movl 0x78(%rsp), %edx
movq %r12, %rdi
movq 0x10(%rsp), %rbp
movq %rbp, %rsi
callq 0xa398
testl %eax, %eax
je 0x3871
movq 0x18(%rsp), %rdi
callq 0x3030
movq %rbp, %rdi
callq 0x3030
movq %r12, %rdi
callq 0xa1a6
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x3a51
movq %rbp, %rdi
callq 0x3030
testl %r13d, %r13d
je 0x390a
movq 0x28(%rsp), %rax
subl %eax, %ebx
leaq (%r15,%rax,8), %r14
leaq 0x30(%rsp), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x31d0
movq (%r15), %r13
movq 0x8(%r15), %r15
leaq 0x44(%rsp), %rsi
movq %r12, %rdi
movl %ebx, %edx
movq %r14, %rcx
callq 0xccbd
leaq 0x30(%rsp), %rdi
xorl %esi, %esi
callq 0x31d0
cmpl $0x0, 0x8(%rsp)
je 0x390a
movq 0x30(%rsp), %rax
subq %r13, %rax
movq 0x38(%rsp), %rcx
subq %r15, %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 0x1f7a4(%rip), %rdi     # 0x230a7
xorl %eax, %eax
callq 0x3190
cmpl $0x0, 0x8(%rsp)
je 0x3a35
movq %r12, %rdi
callq 0xce91
movq %rax, %rbx
movl $0xa, %edi
callq 0x3040
leaq 0x1f9c2(%rip), %rdi     # 0x232f3
callq 0x30e0
movl $0xa, %edi
callq 0x3040
movl (%rbx), %esi
leaq 0x1f79b(%rip), %rdi     # 0x230e4
xorl %eax, %eax
callq 0x3190
movl 0x4(%rbx), %esi
leaq 0x1f7a3(%rip), %rdi     # 0x230fd
xorl %eax, %eax
callq 0x3190
movl 0x8(%rbx), %esi
leaq 0x1f7ab(%rip), %rdi     # 0x23116
xorl %eax, %eax
callq 0x3190
movl 0xc(%rbx), %esi
leaq 0x1f7b3(%rip), %rdi     # 0x2312f
xorl %eax, %eax
callq 0x3190
movl 0x10(%rbx), %esi
leaq 0x1f7bb(%rip), %rdi     # 0x23148
xorl %eax, %eax
callq 0x3190
movl $0xa, %edi
callq 0x3040
leaq 0x1f962(%rip), %rdi     # 0x23307
callq 0x30e0
movl $0xa, %edi
callq 0x3040
movl 0x24(%rbx), %esi
leaq 0x1f7a3(%rip), %rdi     # 0x23161
xorl %eax, %eax
callq 0x3190
movl 0x28(%rbx), %esi
leaq 0x1f7ab(%rip), %rdi     # 0x2317a
xorl %eax, %eax
callq 0x3190
movl 0x2c(%rbx), %esi
leaq 0x1f7b3(%rip), %rdi     # 0x23193
xorl %eax, %eax
callq 0x3190
movl 0x14(%rbx), %esi
leaq 0x1f7bb(%rip), %rdi     # 0x231ac
xorl %eax, %eax
callq 0x3190
movl 0x18(%rbx), %esi
leaq 0x1f7c3(%rip), %rdi     # 0x231c5
xorl %eax, %eax
callq 0x3190
movl 0x1c(%rbx), %esi
leaq 0x1f7cb(%rip), %rdi     # 0x231de
xorl %eax, %eax
callq 0x3190
movl 0x20(%rbx), %esi
leaq 0x1f7d3(%rip), %rdi     # 0x231f7
xorl %eax, %eax
callq 0x3190
movl $0xa, %edi
callq 0x3040
movq %r12, %rdi
callq 0xcb6b
movq %r12, %rdi
callq 0xa1a6
movq 0x18(%rsp), %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 0x3a97
movl $0x1, %edi
callq 0x34a0
movq 0x10(%rsp), %rdi
callq 0x3a97
xorl %edi, %edi
callq 0x34a0
callq 0x3530
callq 0x356a
callq 0x3555