// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
movl $0x1, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq 0x38a76(%rip), %rdx     # 0x3c0bc
callq 0x3410
movl %eax, -0x24(%rbp)
cmpl $-0x1, %eax
je 0x373c
movl -0x24(%rbp), %eax
addl $-0x49, %eax
movl %eax, %ecx
movq %rcx, -0x108(%rbp)
subl $0x2d, %eax
ja 0x3724
movq -0x108(%rbp), %rax
leaq 0x38987(%rip), %rcx     # 0x3c004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl -0x14(%rbp), %eax
orl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x3737
movl -0x14(%rbp), %eax
orl $0x2, %eax
movl %eax, -0x14(%rbp)
jmp 0x3737
movl -0x14(%rbp), %eax
orl $0x4, %eax
movl %eax, -0x14(%rbp)
jmp 0x3737
movl -0x14(%rbp), %eax
orl $0x8, %eax
movl %eax, -0x14(%rbp)
jmp 0x3737
movl -0x14(%rbp), %eax
orl $0x10, %eax
movl %eax, -0x14(%rbp)
jmp 0x3737
movl -0x14(%rbp), %eax
orl $0x20, %eax
movl %eax, -0x14(%rbp)
jmp 0x3737
movl -0x14(%rbp), %eax
orl $0x40, %eax
movl %eax, -0x14(%rbp)
jmp 0x3737
movl -0x14(%rbp), %eax
orl $0x80, %eax
movl %eax, -0x14(%rbp)
jmp 0x3737
movq 0x468e0(%rip), %rax     # 0x49fd0
movq (%rax), %rax
movq %rax, -0x20(%rbp)
jmp 0x3737
movq -0x30(%rbp), %rdi
callq 0x39d0
xorl %edi, %edi
callq 0x3470
movl $0x0, -0x18(%rbp)
jmp 0x3737
movl $0x1, -0x34(%rbp)
jmp 0x3737
movl $0x1, -0x38(%rbp)
jmp 0x3737
movq -0x30(%rbp), %rdi
callq 0x39d0
movl $0x1, %edi
callq 0x3470
jmp 0x3638
movq 0x46885(%rip), %rax     # 0x49fc8
movl (%rax), %eax
cmpl -0x8(%rbp), %eax
jl 0x376c
movq 0x4689f(%rip), %rax     # 0x49ff0
movq (%rax), %rdi
leaq 0x38970(%rip), %rsi     # 0x3c0cb
movb $0x0, %al
callq 0x32f0
movl $0x1, %edi
callq 0x3470
movq 0x46855(%rip), %rax     # 0x49fc8
movl (%rax), %ecx
movl -0x8(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x8(%rbp)
movq 0x46844(%rip), %rax     # 0x49fc8
movl (%rax), %ecx
movq -0x10(%rbp), %rax
movslq %ecx, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x33e0
movl %eax, -0x3c(%rbp)
cmpl $-0x1, -0x3c(%rbp)
jne 0x37c7
leaq 0x3abbd(%rip), %rdi     # 0x3e375
callq 0x3400
movl $0x1, %edi
callq 0x3470
movl -0x3c(%rbp), %edi
leaq -0xd0(%rbp), %rsi
callq 0x34d0
cmpl $0x0, %eax
je 0x37f1
leaq 0x38924(%rip), %rdi     # 0x3c106
callq 0x3400
movl $0x1, %edi
callq 0x3470
movq -0xa0(%rbp), %rdi
callq 0x3360
movq %rax, -0xd8(%rbp)
movl -0x3c(%rbp), %edi
movq -0xd8(%rbp), %rsi
movq -0xa0(%rbp), %rdx
callq 0x3280
cmpq -0xa0(%rbp), %rax
je 0x382d
movl $0x1, %edi
callq 0x3470
movl -0x3c(%rbp), %edi
callq 0x3210
movl $0x1, %edi
movl $0x4000000, %esi        # imm = 0x4000000
callq 0x32c0
movq %rax, -0xe0(%rbp)
movq -0xe0(%rbp), %rdi
movl $0x4000000, %esi        # imm = 0x4000000
movl $0x100000, %ecx         # imm = 0x100000
xorl %eax, %eax
movl %eax, %r8d
movl %ecx, %edx
callq 0x17290
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x18830
movq -0xe8(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x18850
cmpl $0x0, -0x38(%rbp)
je 0x38a5
movq -0xe8(%rbp), %rdi
movl $0x3, %esi
callq 0x3c40
movq -0xe8(%rbp), %rdi
movq -0xd8(%rbp), %rsi
movq -0xa0(%rbp), %rax
movl %eax, %edx
callq 0x173e0
movl %eax, -0xec(%rbp)
cmpl $0x0, -0xec(%rbp)
je 0x3900
movq -0xe0(%rbp), %rdi
callq 0x3030
movq -0xd8(%rbp), %rdi
callq 0x3030
movq -0xe8(%rbp), %rdi
callq 0x17030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x39b7
movq -0xd8(%rbp), %rdi
callq 0x3030
cmpl $0x0, -0x18(%rbp)
je 0x397a
callq 0x3a70
movq %rax, -0xf8(%rbp)
movq -0xe8(%rbp), %rdi
movl -0x8(%rbp), %edx
movq -0x10(%rbp), %rcx
leaq -0xfc(%rbp), %rsi
callq 0x18610
movl %eax, -0xec(%rbp)
callq 0x3a70
subq -0xf8(%rbp), %rax
movq %rax, -0xf8(%rbp)
cmpl $0x0, -0x34(%rbp)
je 0x3978
movq -0xf8(%rbp), %rax
movl $0x3e8, %ecx            # imm = 0x3E8
cqto
idivq %rcx
movl %eax, %esi
leaq 0x3879a(%rip), %rdi     # 0x3c10b
movb $0x0, %al
callq 0x3170
jmp 0x397a
cmpl $0x0, -0x34(%rbp)
je 0x398c
movq -0xe8(%rbp), %rdi
callq 0x3aa0
movq -0xe8(%rbp), %rdi
callq 0x18420
movq -0xe8(%rbp), %rdi
callq 0x17030
movq -0xe0(%rbp), %rdi
callq 0x3030
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
nopw %cs:(%rax,%rax)