// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %ecx, %r12d
movq %rdx, -0x30(%rbp)
movq %rsi, -0x50(%rbp)
movq %rdi, %r15
movl 0x10(%r8), %esi
callq 0xe719
movq %rax, %r13
xorl %r14d, %r14d
movslq 0x14(%r13), %rax
cmpq %rax, %r14
jge 0xf168
movq -0x50(%rbp), %rdi
pushq $0x60
popq %rsi
pushq $0x15
popq %rdx
callq 0x101c5
movq %rax, %rbx
movl 0x19c(%r15), %eax
addl %r14d, %eax
movl %eax, 0x58(%rbx)
movq 0x8(%r13), %rax
movl (%rax,%r14,4), %esi
movl %esi, 0x5c(%rbx)
movq %r15, %rdi
callq 0x101ec
movq %rax, 0x2c(%rbx)
movq %r15, %rdi
movq %rbx, %rsi
callq 0x10225
incq %r14
jmp 0xf119
movq %r12, %rcx
cmpl $0x1000, %ecx           # imm = 0x1000
jg 0xf28f
movslq %ecx, %rax
movq %rsp, %rdx
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %rdx
movq %rdx, -0x40(%rbp)
movq %rdx, %rsp
movq %rsp, %rdx
subq %rax, %rdx
movq %rdx, -0x38(%rbp)
movq %rdx, %rsp
movl 0x198(%r15), %r12d
xorl %eax, %eax
testl %ecx, %ecx
cmovlel %eax, %ecx
xorl %r14d, %r14d
movq %rcx, -0x48(%rbp)
cmpq %rcx, %r14
je 0xf20b
movq -0x30(%rbp), %r13
movq %r13, %rdi
callq 0xe21c
movl %eax, %ebx
movq %r15, %rdi
movq %r13, %rsi
callq 0xe2cb
movl %eax, %r13d
movq -0x38(%rbp), %rax
movl %ebx, (%rax,%r14,4)
movq -0x40(%rbp), %rax
movl %r13d, (%rax,%r14,4)
testl %ebx, %ebx
movl $0x0, %eax
cmovlel %eax, %ebx
subl $0x1, %ebx
jb 0xf202
movq %r15, %rdi
movl %r13d, %esi
callq 0x101ec
jmp 0xf1f0
incq %r14
movq -0x48(%rbp), %rcx
jmp 0xf1b5
xorl %edx, %edx
cmpq %rcx, %rdx
je 0xf280
movq -0x38(%rbp), %rax
movl (%rax,%rdx,4), %r13d
movq -0x40(%rbp), %rax
movq %rdx, -0x30(%rbp)
movl (%rax,%rdx,4), %ebx
testl %r13d, %r13d
movl $0x0, %eax
cmovlel %eax, %r13d
subl $0x1, %r13d
jb 0xf273
movq -0x50(%rbp), %rdi
pushq $0x60
popq %rsi
pushq $0x15
popq %rdx
callq 0x101c5
movq %rax, %r14
movq %r15, %rdi
movl %ebx, %esi
callq 0x1023d
movq %rax, 0x58(%r14)
movl %r12d, 0x2c(%r14)
incl %r12d
shrq $0x20, %rax
movl %eax, 0x30(%r14)
movq %r15, %rdi
movq %r14, %rsi
callq 0x10225
jmp 0xf231
movq -0x30(%rbp), %rdx
incq %rdx
movq -0x48(%rbp), %rcx
jmp 0xf20d
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x3060