// File: /workspace/github_c_makefile_star2_O0/cogbee[P]vmir-latest/src/vmir_wasm_parser.c
// Function: init_local_vars
static void
init_local_vars(ir_unit_t *iu, ir_bb_t *ib, wasm_bytestream_t *wbs,
                const int num_locals, ir_function_t *f)
{
  ir_type_t *it = type_get(iu, f->if_type);

  // We need to copy all function arguments to local var area
  // as wasm assume those are writable

  for(int i = 0; i < it->it_function.num_parameters; i++) {
    ir_instr_move_t *move = instr_add(ib, sizeof(ir_instr_move_t), IR_IC_MOVE);
    move->value.value = iu->iu_first_func_value + i;
    move->value.type = it->it_function.parameters[i];
    move->super.ii_ret = value_alloc_temporary(iu, move->value.type);

    value_bind_return_value(iu, &move->super);
  }

  if(num_locals > 4096) {
    abort();
  }

  int *local_types = alloca(sizeof(int) * num_locals);
  int *local_nums  = alloca(sizeof(int) * num_locals);

  int local_init_index = iu->iu_next_value;

  for(int i = 0; i < num_locals; i++) {
    const int num = wbs_get_vu32(wbs);
    const int type = wasm_parse_value_type(iu, wbs);
    local_nums[i]  = num;
    local_types[i] = type;
    for(int j = 0; j < num; j++) {
      value_alloc_temporary(iu, type);
    }
  }

  // Do another pass where we create constant types for initializing

  for(int i = 0; i < num_locals; i++) {
    const int num = local_nums[i];
    const int type = local_types[i];
    for(int j = 0; j < num; j++) {
      ir_instr_move_t *move =
        instr_add(ib, sizeof(ir_instr_move_t), IR_IC_MOVE);
      move->value = value_create_zero(iu, type);
      move->super.ii_ret.value = local_init_index++;
      move->super.ii_ret.type = move->value.type;

      value_bind_return_value(iu, &move->super);
    }
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq %r8, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movl 0x10(%rax), %esi
callq 0x1ee20
movq %rax, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x30(%rbp), %rcx
cmpl 0x14(%rcx), %eax
jge 0x1ff76
movq -0x10(%rbp), %rdi
movl $0x60, %esi
movl $0x15, %edx
callq 0x20c10
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movl 0x19c(%rax), %ecx
addl -0x34(%rbp), %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x58(%rax)
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x34(%rbp), %rcx
movl (%rax,%rcx,4), %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x5c(%rax)
movq -0x40(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rax
movl 0x5c(%rax), %esi
callq 0x20c80
movq %rax, %rcx
movq -0xa0(%rbp), %rax
movq %rcx, -0x48(%rbp)
movq -0x48(%rbp), %rcx
movq %rcx, 0x2c(%rax)
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x20cf0
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x1fed9
cmpl $0x1000, -0x1c(%rbp)    # imm = 0x1000
jle 0x1ff84
callq 0x3050
movslq -0x1c(%rbp), %rax
leaq 0xf(,%rax,4), %rcx
andq $-0x10, %rcx
movq %rsp, %rax
subq %rcx, %rax
movq %rax, %rsp
movq %rax, -0x50(%rbp)
movslq -0x1c(%rbp), %rax
leaq 0xf(,%rax,4), %rcx
andq $-0x10, %rcx
movq %rsp, %rax
subq %rcx, %rax
movq %rax, %rsp
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rax
movl 0x198(%rax), %eax
movl %eax, -0x5c(%rbp)
movl $0x0, -0x60(%rbp)
movl -0x60(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x20049
movq -0x18(%rbp), %rdi
callq 0x1d450
movl %eax, -0x64(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x1e260
movl %eax, -0x68(%rbp)
movl -0x64(%rbp), %edx
movq -0x58(%rbp), %rax
movslq -0x60(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x68(%rbp), %edx
movq -0x50(%rbp), %rax
movslq -0x60(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x64(%rbp), %eax
jge 0x2003c
movq -0x8(%rbp), %rdi
movl -0x68(%rbp), %esi
callq 0x20c80
movq %rax, -0x74(%rbp)
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x20019
jmp 0x2003e
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x1ffd2
movl $0x0, -0x78(%rbp)
movl -0x78(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x2013e
movq -0x58(%rbp), %rax
movslq -0x78(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x7c(%rbp)
movq -0x50(%rbp), %rax
movslq -0x78(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x80(%rbp)
movl $0x0, -0x84(%rbp)
movl -0x84(%rbp), %eax
cmpl -0x7c(%rbp), %eax
jge 0x2012e
movq -0x10(%rbp), %rdi
movl $0x60, %esi
movl $0x15, %edx
callq 0x20c10
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x8(%rbp), %rdi
movl -0x80(%rbp), %esi
callq 0x20d30
movq %rax, %rcx
movq -0xa8(%rbp), %rax
movq %rcx, -0x98(%rbp)
movq -0x98(%rbp), %rcx
movq %rcx, 0x58(%rax)
movl -0x5c(%rbp), %ecx
movl %ecx, %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
movq -0x90(%rbp), %rax
movl %ecx, 0x2c(%rax)
movq -0x90(%rbp), %rax
movl 0x5c(%rax), %ecx
movq -0x90(%rbp), %rax
movl %ecx, 0x30(%rax)
movq -0x8(%rbp), %rdi
movq -0x90(%rbp), %rsi
callq 0x20cf0
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0x20082
jmp 0x20130
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x20050
movq %rbp, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)