// File: /workspace/github_c_makefile_star2_O0/cogbee[P]vmir-latest/src/vmir_wasm_parser.c
// Function: wasm_parse_module
static void
wasm_parse_module(ir_unit_t *iu, const void *start, const void *end)
{
  wasm_bytestream_t bs = {
    .start = start - 4,
    .ptr = start,
    .end = end
  };

  const uint32_t version = wbs_get_u32(&bs);
  vmir_log(iu, VMIR_LOG_DEBUG, "WASM version %d", version);

  wasm_add_fundamental_types(iu);

  while(bs.ptr < bs.end) {
    const uint32_t section_code = wbs_get_vu32(&bs);
    const uint32_t section_len  = wbs_get_vu32(&bs);
    const uint8_t *section_end = bs.ptr + section_len;

    if(section_code == 0) {
      char *section_name = wbs_get_string(iu, &bs);
      vmir_log(iu, VMIR_LOG_DEBUG, "Skipping named section %s", section_name);
      free(section_name);
      bs.ptr = section_end;
      continue;
    }

    switch(section_code) {
    case WASM_SECTION_TYPE:
      wasm_parse_section_type(iu, &bs);
      break;
    case WASM_SECTION_IMPORT:
      wasm_parse_section_import_decl(iu, &bs);
      break;
    case WASM_SECTION_FUNCTION:
      wasm_parse_section_functions(iu, &bs);
      break;
    case WASM_SECTION_TABLE:
      wasm_parse_section_table(iu, &bs);
      break;
    case WASM_SECTION_MEMORY:
      wasm_parse_section_memory(iu, &bs);
      break;
    case WASM_SECTION_GLOBAL:
      wasm_parse_section_global(iu, &bs);
      break;
    case WASM_SECTION_EXPORTS:
      wasm_parse_section_exports(iu, &bs);
      break;
    case WASM_SECTION_CODE:
      wasm_parse_section_code(iu, &bs);
      break;
    case WASM_SECTION_DATA:
      wasm_parse_section_data(iu, &bs);
      break;
    default:
      vmir_log(iu, VMIR_LOG_ERROR, "Skipping section type %d", section_code);
      break;
    }
    bs.ptr = section_end;
  }


  iu->iu_data_ptr = VMIR_ALIGN(iu->iu_data_ptr, 4096);

  iu->iu_data_ptr += 1024 * 1024;

  // WASM stack
  uint32_t *u32p = iu->iu_mem;
  u32p[1] = iu->iu_data_ptr;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
addq $-0x4, %rax
movq %rax, -0x20(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x1cf10
movl %eax, -0x34(%rbp)
movq -0x8(%rbp), %rdi
movl -0x34(%rbp), %ecx
movl $0x3, %esi
leaq 0x26bba(%rip), %rdx     # 0x3e799
movb $0x0, %al
callq 0x4650
movq -0x8(%rbp), %rdi
callq 0x1cf60
movq -0x30(%rbp), %rax
cmpq -0x28(%rbp), %rax
jae 0x17d44
leaq -0x30(%rbp), %rdi
callq 0x1d450
movl %eax, -0x38(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x1d450
movl %eax, -0x3c(%rbp)
movq -0x30(%rbp), %rax
movl -0x3c(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
cmpl $0x0, -0x38(%rbp)
jne 0x17c68
movq -0x8(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x1d4b0
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rcx
movl $0x3, %esi
leaq 0x26b5b(%rip), %rdx     # 0x3e7a9
movb $0x0, %al
callq 0x4650
movq -0x50(%rbp), %rdi
callq 0x3030
movq -0x48(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0x17bef
movl -0x38(%rbp), %eax
decl %eax
movl %eax, %ecx
movq %rcx, -0x60(%rbp)
subl $0xa, %eax
ja 0x17d1d
movq -0x60(%rbp), %rax
leaq 0x24f21(%rip), %rcx     # 0x3cba8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x8(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x1d560
jmp 0x17d37
movq -0x8(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x1d7e0
jmp 0x17d37
movq -0x8(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x1d940
jmp 0x17d37
movq -0x8(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x1dad0
jmp 0x17d37
movq -0x8(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x1dba0
jmp 0x17d37
movq -0x8(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x1dc30
jmp 0x17d37
movq -0x8(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x1de60
jmp 0x17d37
movq -0x8(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x1df30
jmp 0x17d37
movq -0x8(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x1e0e0
jmp 0x17d37
movq -0x8(%rbp), %rdi
movl -0x38(%rbp), %ecx
movl $0x1, %esi
leaq 0x26a93(%rip), %rdx     # 0x3e7c3
movb $0x0, %al
callq 0x4650
movq -0x48(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0x17bef
movq -0x8(%rbp), %rax
movl 0x88(%rax), %ecx
addl $0x1000, %ecx           # imm = 0x1000
subl $0x1, %ecx
andl $0xfffff000, %ecx       # imm = 0xFFFFF000
movq -0x8(%rbp), %rax
movl %ecx, 0x88(%rax)
movq -0x8(%rbp), %rax
movl 0x88(%rax), %ecx
addl $0x100000, %ecx         # imm = 0x100000
movl %ecx, 0x88(%rax)
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rax
movl 0x88(%rax), %ecx
movq -0x58(%rbp), %rax
movl %ecx, 0x4(%rax)
addq $0x60, %rsp
popq %rbp
retq