// File: /workspace/github_c_makefile_star2_O3/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 $0x48, %rsp
movl %ecx, -0x30(%rbp)
movq %rdx, -0x38(%rbp)
movq %rdi, %rbx
movl %r8d, %eax
cmpq %rax, 0x170(%rdi)
jbe 0x10586
movq %rsi, %r14
movq 0x168(%rbx), %rcx
leaq (%rax,%rax,4), %rax
cmpl $0x0, 0x14(%rcx,%rax,8)
movq %rsi, -0x58(%rbp)
jle 0x103a9
leaq (%rcx,%rax,8), %rax
movq %rax, -0x40(%rbp)
xorl %r12d, %r12d
movq -0x40(%rbp), %r15
movl $0x1, %edi
movl $0x60, %esi
callq 0x32f0
movq %rax, %r13
movabsq $-0xffffffeb, %rax      # imm = 0xFFFFFFFF00000015
movq %rax, 0x28(%r13)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, 0x30(%r13)
movq %r14, 0x10(%r13)
movq 0x18(%r14), %rax
movq %rax, 0x8(%r13)
movq %r13, (%rax)
movq %r13, 0x18(%r14)
movl 0x19c(%rbx), %eax
addl %r12d, %eax
movl %eax, 0x58(%r13)
movq 0x8(%r15), %rax
movl (%rax,%r12,4), %r14d
movl %r14d, 0x5c(%r13)
movq %rbx, %rdi
callq 0xf50d
movq 0x180(%rbx), %rcx
cltq
movq (%rcx,%rax,8), %rcx
movl $0x4, (%rcx)
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl %edx, 0x38(%rcx)
movl %r14d, 0x24(%rcx)
shlq $0x20, %r14
movl %eax, %eax
orq %r14, %rax
movq -0x58(%rbp), %r14
movq %rax, 0x2c(%r13)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x11d54
incq %r12
movslq 0x14(%r15), %rax
cmpq %rax, %r12
jl 0x10301
movq %rbx, -0x48(%rbp)
movl -0x30(%rbp), %edx
cmpl $0x1000, %edx           # imm = 0x1000
jg 0x105a3
movslq %edx, %rax
movq %rsp, %rcx
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x50(%rbp)
movq %rcx, %rsp
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x68(%rbp)
movq %rcx, %rsp
testl %edx, %edx
movq -0x38(%rbp), %rsi
jle 0x10577
movq -0x48(%rbp), %rbx
movl 0x198(%rbx), %r12d
movl %edx, %eax
movq %rax, -0x60(%rbp)
xorl %r14d, %r14d
movq (%rsi), %rax
movq 0x8(%rsi), %rdx
xorl %r15d, %r15d
xorl %ecx, %ecx
movb $-0x1, %r8b
cmpq %rdx, %rax
jae 0x10422
movb (%rax), %r8b
incq %rax
movq %rax, (%rsi)
movl %r8d, %edi
andb $0x7f, %dil
movzbl %dil, %edi
shll %cl, %edi
orl %edi, %r15d
addl $0x7, %ecx
testb %r8b, %r8b
js 0x10411
movq %rbx, %rdi
callq 0xf1ba
movl %eax, %r13d
movq -0x68(%rbp), %rax
movl %r15d, (%rax,%r14,4)
movq -0x50(%rbp), %rax
movl %r13d, (%rax,%r14,4)
testl %r15d, %r15d
jle 0x10485
movq %rbx, %rdi
callq 0xf50d
movq 0x180(%rbx), %rcx
cltq
movq (%rcx,%rax,8), %rax
movl $0x4, (%rax)
movl $0xffffffff, 0x38(%rax) # imm = 0xFFFFFFFF
movl %r13d, 0x24(%rax)
decl %r15d
jne 0x1045a
movq %r14, %rax
incq %rax
movq %rax, %r14
cmpq -0x60(%rbp), %rax
movq -0x38(%rbp), %rsi
jne 0x10405
cmpl $0x0, -0x30(%rbp)
jle 0x10577
xorl %ecx, %ecx
movq -0x58(%rbp), %rbx
movq -0x48(%rbp), %r14
movq -0x68(%rbp), %rax
movl (%rax,%rcx,4), %r13d
testl %r13d, %r13d
movq %rcx, -0x30(%rbp)
jle 0x10566
movq -0x50(%rbp), %rax
movl (%rax,%rcx,4), %eax
movq %rax, -0x38(%rbp)
shlq $0x20, %rax
movq %rax, -0x40(%rbp)
movl $0x1, %edi
movl $0x60, %esi
callq 0x32f0
movq %rax, %r15
movabsq $-0xffffffeb, %rax      # imm = 0xFFFFFFFF00000015
movq %rax, 0x28(%r15)
movl $0xffffffff, 0x30(%r15) # imm = 0xFFFFFFFF
movq %rbx, 0x10(%r15)
movq 0x18(%rbx), %rax
movq %rax, 0x8(%r15)
movq %r15, (%rax)
movq %r15, 0x18(%rbx)
movq %r14, %rdi
callq 0xf50d
movq 0x180(%r14), %rcx
cltq
movq (%rcx,%rax,8), %rcx
movl $0x3, (%rcx)
movq -0x38(%rbp), %rdx
movl %edx, 0x24(%rcx)
movq $0x0, 0x30(%rcx)
movl %eax, %eax
orq -0x40(%rbp), %rax
movq %rax, 0x58(%r15)
movl %r12d, 0x2c(%r15)
incl %r12d
movl %edx, 0x30(%r15)
movq %r14, %rdi
movq %r15, %rsi
callq 0x11d54
decl %r13d
jne 0x104d8
movq -0x30(%rbp), %rcx
incq %rcx
cmpq -0x60(%rbp), %rcx
jne 0x104b0
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1552f(%rip), %rsi     # 0x25abc
leaq 0x15561(%rip), %rcx     # 0x25af5
movq %rbx, %rdi
movl $0x7c, %edx
xorl %eax, %eax
callq 0xca98
callq 0x3060