// File: /workspace/github_c_makefile_star2_O1/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, -0x40(%rbp)
movq %rdi, %rbx
movl 0x10(%r8), %r8d
cmpq %r8, 0x170(%rdi)
jbe 0x10622
movq %rsi, %r14
movq 0x168(%rbx), %rax
leaq (%r8,%r8,4), %rcx
cmpl $0x0, 0x14(%rax,%rcx,8)
movq %rsi, -0x60(%rbp)
jle 0x1043d
leaq (%rax,%rcx,8), %rax
movq %rax, -0x38(%rbp)
xorl %r12d, %r12d
movq -0x38(%rbp), %r15
movl $0x1, %edi
movl $0x60, %esi
callq 0x32f0
movq %rax, %r13
xorl %ecx, %ecx
movq %rcx, 0x20(%rax)
movabsq $-0xffffffeb, %rax      # imm = 0xFFFFFFFF00000015
movq %rax, 0x28(%r13)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, 0x30(%r13)
movq %r14, 0x10(%r13)
movq %rcx, (%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 0xf5a0
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 -0x60(%rbp), %r14
movq %rax, 0x2c(%r13)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x11f77
incq %r12
movslq 0x14(%r15), %rax
cmpq %rax, %r12
jl 0x1038b
movl -0x30(%rbp), %edx
cmpl $0x1000, %edx           # imm = 0x1000
jg 0x1063f
movslq %edx, %rax
movq %rsp, %rcx
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x48(%rbp)
movq %rcx, %rsp
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x58(%rbp)
movq %rcx, %rsp
movl 0x198(%rbx), %r12d
movl %edx, %eax
movq %rax, -0x50(%rbp)
testl %edx, %edx
jle 0x10524
xorl %r14d, %r14d
movq -0x40(%rbp), %rsi
movq (%rsi), %rax
movq 0x8(%rsi), %rdx
xorl %r15d, %r15d
xorl %ecx, %ecx
movb $-0x1, %r8b
cmpq %rdx, %rax
jae 0x104ae
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 0x1049d
movq %rbx, %rdi
callq 0xf23d
movl %eax, %r13d
movq -0x58(%rbp), %rax
movl %r15d, (%rax,%r14,4)
movq -0x48(%rbp), %rax
movl %r13d, (%rax,%r14,4)
testl %r15d, %r15d
jle 0x10511
movq %rbx, %rdi
callq 0xf5a0
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 0x104e6
movq %r14, %rax
incq %rax
movq %rax, %r14
cmpq -0x50(%rbp), %rax
jne 0x1048d
movq %rbx, -0x68(%rbp)
cmpl $0x0, -0x30(%rbp)
jle 0x10613
xorl %ecx, %ecx
movq -0x68(%rbp), %rbx
movq -0x58(%rbp), %rax
movl (%rax,%rcx,4), %r14d
testl %r14d, %r14d
movq %rcx, -0x30(%rbp)
jle 0x10602
movq -0x48(%rbp), %rax
movl (%rax,%rcx,4), %eax
movq %rax, -0x40(%rbp)
shlq $0x20, %rax
movq %rax, -0x38(%rbp)
movl $0x1, %edi
movl $0x60, %esi
callq 0x32f0
movq %rax, %r15
movl %r12d, %r13d
xorl %r12d, %r12d
movq %r12, 0x20(%rax)
movabsq $-0xffffffeb, %rax      # imm = 0xFFFFFFFF00000015
movq %rax, 0x28(%r15)
movl $0xffffffff, 0x30(%r15) # imm = 0xFFFFFFFF
movq -0x60(%rbp), %rcx
movq %rcx, 0x10(%r15)
movq %r12, (%r15)
movq 0x18(%rcx), %rax
movq %rax, 0x8(%r15)
movq %r15, (%rax)
movq %r15, 0x18(%rcx)
movq %rbx, %rdi
callq 0xf5a0
movq 0x180(%rbx), %rcx
cltq
movq (%rcx,%rax,8), %rcx
movl $0x3, (%rcx)
movq -0x40(%rbp), %rdx
movl %edx, 0x24(%rcx)
movq %r12, 0x30(%rcx)
movl %r13d, %r12d
movl %eax, %eax
orq -0x38(%rbp), %rax
movq %rax, 0x58(%r15)
movl %r13d, 0x2c(%r15)
incl %r12d
movl 0x5c(%r15), %eax
movl %eax, 0x30(%r15)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x11f77
decl %r14d
jne 0x10560
movq -0x30(%rbp), %rcx
incq %rcx
cmpq -0x50(%rbp), %rcx
jne 0x10538
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x153e3(%rip), %rsi     # 0x25a0c
leaq 0x15415(%rip), %rcx     # 0x25a45
movq %rbx, %rdi
movl $0x7c, %edx
xorl %eax, %eax
callq 0xca73
callq 0x3060