// File: /workspace/github_c_makefile_star2_O1/cogbee[P]vmir-latest/src/vmir.c
// Function: initialize_global
static void
initialize_global(ir_unit_t *iu, void *addr,
                  int dstty_index, const ir_value_t *c)
{
  int x, size;
  ir_valuetype_t *ivt;
  const ir_type_t *dstty = type_get(iu, dstty_index);

  switch(dstty->it_code) {
  case IR_TYPE_INT1:
  case IR_TYPE_INT8:
    mem_wr8(addr, value_get_const32(iu, c), iu);
    break;
  case IR_TYPE_INT16:
    mem_wr16(addr, value_get_const32(iu, c), iu);
    break;
  case IR_TYPE_INT32:
  case IR_TYPE_FLOAT:
    mem_wr32(addr, value_get_const32(iu, c), iu);
    break;
  case IR_TYPE_INT64:
  case IR_TYPE_DOUBLE:
    mem_wr64(addr, value_get_const64(iu, c), iu);
    break;

  case IR_TYPE_POINTER:
    switch(c->iv_class) {
    case IR_VC_GLOBALVAR:
    case IR_VC_CONSTANT:
      mem_wr32(addr, value_get_const32(iu, c), iu);
      break;
    case IR_VC_FUNCTION:
      mem_wr32(addr, value_function_addr(c), iu);
      break;

    default:
      parser_error(iu, "Unable to initialize pointer from value class %d",
                   c->iv_class);
    }
    break;

  case IR_TYPE_ARRAY:
    size = type_sizeof(iu, dstty_index);
    switch(c->iv_class) {
    case IR_VC_DATA:
      memcpy(addr, c->iv_data, size);
      break;
    case IR_VC_ZERO_INITIALIZER:
      memset(addr, 0, size);
      break;
    case IR_VC_AGGREGATE:
      // Iterate over all elements
      x = dstty->it_array.num_elements;
      assert(c->iv_num_values == x);
      ivt = c->iv_data;
      for(int i = 0; i < x; i++) {
        ir_value_t *subvalue = value_get(iu, ivt[i].value);
        initialize_global(iu, addr, dstty->it_array.element_type, subvalue);
        addr += type_sizeof(iu, dstty->it_array.element_type);
      }

      break;
    default:
      parser_error(iu, "Unable to initialize array from value class %d",
                   c->iv_class);
    }
    break;

  case IR_TYPE_STRUCT:
    size = type_sizeof(iu, dstty_index);
    switch(c->iv_class) {
    case IR_VC_ZERO_INITIALIZER:
      memset(addr, 0, size);
      break;
    case IR_VC_AGGREGATE:
      // Iterate over all elements
      x = dstty->it_struct.num_elements;
      assert(c->iv_num_values == x);
      ivt = c->iv_data;
      for(int i = 0; i < x; i++) {
        ir_value_t *subvalue = value_get(iu, ivt[i].value);
        initialize_global(iu, addr + dstty->it_struct.elements[i].offset,
                          dstty->it_struct.elements[i].type, subvalue);
      }
      break;
    default:
      parser_error(iu, "Unable to initialize struct from value class %d",
                   c->iv_class);
    }
    break;

  default:
    parser_error(iu, "Unable to initialize global 0x%x (%s) from %s",
                 addr, type_str(iu, dstty),
                 type_str_index(iu, c->iv_type));
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %edx, %r8d
movq %rdi, %r14
movl %edx, %eax
cmpq %rax, 0x170(%rdi)
jbe 0x216db
movq %rcx, %r12
movq %rsi, %rbx
movq 0x168(%r14), %rcx
leaq (%rax,%rax,4), %rax
leaq (%rcx,%rax,8), %r15
movl (%r15), %eax
cmpq $0xb, %rax
ja 0x216f8
leaq 0x33ef(%rip), %rcx      # 0x248ec
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %r14, %rdi
movq %r12, %rsi
callq 0x16a3f
movb %al, (%rbx)
jmp 0x216b4
movq %r14, %rdi
movq %r12, %rsi
callq 0x1d4d9
movq %rax, (%rbx)
jmp 0x216b4
movq %r14, %rdi
movl %r8d, %esi
callq 0xf67c
movl (%r12), %r8d
cmpl $0xb, %r8d
je 0x215cb
cmpl $0x9, %r8d
je 0x21659
cmpl $0x7, %r8d
jne 0x21771
movq 0x18(%r12), %rsi
movslq %eax, %rdx
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3360
movl (%r12), %r8d
leal -0x2(%r8), %eax
cmpl $0x2, %eax
jae 0x21642
movq %r14, %rdi
movq %r12, %rsi
callq 0x16a3f
movl %eax, (%rbx)
jmp 0x216b4
movq %r14, %rdi
movq %r12, %rsi
callq 0x16a3f
movw %ax, (%rbx)
jmp 0x216b4
movq %r14, %rdi
movl %r8d, %esi
callq 0xf67c
movl (%r12), %r8d
cmpl $0x9, %r8d
je 0x215e6
cmpl $0xb, %r8d
jne 0x2173a
movslq %eax, %rdx
movq %rbx, %rdi
xorl %esi, %esi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3210
movl 0x18(%r15), %r13d
cmpl %r13d, 0x30(%r12)
jne 0x21752
testl %r13d, %r13d
jle 0x216b4
movq 0x18(%r12), %r12
xorl %ebp, %ebp
movl (%r12,%rbp,8), %r8d
cmpq %r8, 0x188(%r14)
jbe 0x216c3
movq 0x180(%r14), %rax
movq (%rax,%r8,8), %rcx
movq 0x8(%r15), %rax
movslq 0x4(%rax,%rbp,8), %rsi
addq %rbx, %rsi
movl (%rax,%rbp,8), %edx
movq %r14, %rdi
callq 0x214b4
incq %rbp
cmpq %rbp, %r13
jne 0x21605
jmp 0x216b4
cmpl $0x1, %r8d
jne 0x2178c
movq 0x30(%r12), %rax
movl 0x34(%rax), %eax
jmp 0x21592
movl 0x8(%r15), %r13d
cmpl %r13d, 0x30(%r12)
jne 0x217a7
testl %r13d, %r13d
jle 0x216b4
movq 0x18(%r12), %r12
xorl %ebp, %ebp
movl (%r12,%rbp,8), %r8d
cmpq %r8, 0x188(%r14)
jbe 0x216c3
movq 0x180(%r14), %rax
movq (%rax,%r8,8), %rcx
movl 0xc(%r15), %edx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x214b4
movl 0xc(%r15), %esi
movq %r14, %rdi
callq 0xf67c
movl %eax, %eax
addq %rax, %rbx
incq %rbp
cmpq %rbp, %r13
jne 0x21674
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3df6(%rip), %rsi      # 0x254c0
leaq 0x45a3(%rip), %rcx      # 0x25c74
movq %r14, %rdi
movl $0x143, %edx            # imm = 0x143
jmp 0x216f1
leaq 0x432a(%rip), %rsi      # 0x25a0c
leaq 0x435c(%rip), %rcx      # 0x25a45
movq %r14, %rdi
movl $0x7c, %edx
xorl %eax, %eax
callq 0xca73
movq %r14, %rdi
movq %r15, %rsi
callq 0x4112
movq %rax, %r15
movl 0x24(%r12), %esi
movq %r14, %rdi
callq 0x12335
movq %rax, (%rsp)
leaq 0x3827(%rip), %rsi      # 0x24f45
leaq 0x6a21(%rip), %rcx      # 0x28146
movq %r14, %rdi
movl $0x2c2, %edx            # imm = 0x2C2
movq %rbx, %r8
movq %r15, %r9
xorl %eax, %eax
callq 0xca73
leaq 0x3804(%rip), %rsi      # 0x24f45
leaq 0x69ce(%rip), %rcx      # 0x28116
movq %r14, %rdi
movl $0x2bb, %edx            # imm = 0x2BB
jmp 0x216f1
leaq 0x6933(%rip), %rdi      # 0x2808c
leaq 0x37e5(%rip), %rsi      # 0x24f45
leaq 0x693b(%rip), %rcx      # 0x280a2
movl $0x2b1, %edx            # imm = 0x2B1
callq 0x31e0
leaq 0x37cd(%rip), %rsi      # 0x24f45
leaq 0x6968(%rip), %rcx      # 0x280e7
movq %r14, %rdi
movl $0x2a4, %edx            # imm = 0x2A4
jmp 0x216f1
leaq 0x37b2(%rip), %rsi      # 0x24f45
leaq 0x68c1(%rip), %rcx      # 0x2805b
movq %r14, %rdi
movl $0x289, %edx            # imm = 0x289
jmp 0x216d9
leaq 0x68de(%rip), %rdi      # 0x2808c
leaq 0x3790(%rip), %rsi      # 0x24f45
leaq 0x68e6(%rip), %rcx      # 0x280a2
movl $0x299, %edx            # imm = 0x299
callq 0x31e0