// File: /workspace/github_c_makefile_star2_O1/sisshiki1969[P]monocc/src/codegen.c
// Function: emit_global
void emit_global(Type *type, Node *init) {
  if (!init) {
    // char str[];  => incomplete type
    if (is_array(type) && type->array_size == 0)
      error_at_token(init->token, "Incomplete type is not allowed.");
    fprintf(output, "\t.zero %d\n", sizeof_type(type));
    return;
  }
  long i = 0;
  Vector *vec;
  MemberInfo *member;
  int offset = 0;
  switch (type->ty) {
    case CHAR:
      fprintf(output, "\t.byte %ld\n", eval_const(init)->num_val);
      return;
    case SHORT:
    case USHORT:
      fprintf(output, "\t.word %ld\n", eval_const(init)->num_val);
      return;
    case INT:
    case UINT:
      fprintf(output, "\t.long %ld\n", eval_const(init)->num_val);
      return;
    case LONG:
    case ULONG:
      fprintf(output, "\t.quad %ld\n", eval_const(init)->num_val);
      return;

    case ARRAY:
      if (type->ptr_to->ty == CHAR) {
        if (init->kind == ND_STR) {
          // char str[] = "sample";
          int str_size = 1;
          fprintf(output, "\t.string \"");
          for (Token *t = init->token; t; t = t->next) {
            fprintf(output, "%.*s", t->len, t->str);
            str_size += t->len;
          }
          fprintf(output, "\"\n");
          int ary_size = type->array_size;
          if (ary_size > str_size)
            fprintf(output, "\t.zero %d\n", ary_size - str_size);
          return;
        }
      }
      if (init->kind != ND_INIT)
        error_at_node(init, "Unsupported initializer.");
      vec = init->nodes;
      type = type->ptr_to;
      for (int i = 0; i < vec_len(vec); i++) {
        emit_global(type, vec->data[i]);
      }
      return;

    case STRUCT:
      if (init->kind != ND_INIT)
        error_at_node(init, "Unsupported initializer.");
      vec = init->nodes;
      member = type->member;
      for (int i = 0; i < vec_len(vec); i++) {
        if (member->offset > offset) {
          fprintf(output, "\t.zero %d\n", member->offset - offset);
          offset = member->offset;
        }
        emit_global(member->type, vec->data[i]);
        offset += sizeof_type(member->type);
        member = member->next;
      }
      if (sizeof_type(type) > offset) {
        fprintf(output, "\t.zero %d\n", sizeof_type(type) - offset);
      }
      return;

    case PTR:
      if (type->ptr_to->ty == CHAR && init->kind == ND_STR) {
        // char *str = "sample";
        fprintf(output, "\t.quad .LS%06d\n", init->str_id);
        return;
      }
      if (init->kind == ND_ADDR && init->lhs->kind == ND_GVAR) {
        fprintf(output, "\t.quad %.*s\n", init->lhs->token->len,
                init->lhs->token->str);
        return;
      }
      if (init->kind == ND_CAST && init->lhs->kind == ND_NUM) {
        fprintf(output, "\t.quad %ld\n", init->lhs->num_val);
        return;
      }
      error_at_node(init, "Unsupported initializer.");
      return;

    default:
      error_at_node(init, "Unsupported initializer.");
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
testq %rsi, %rsi
je 0x3bd5
movq %rsi, %r14
movl (%rbx), %eax
decl %eax
cmpl $0xb, %eax
ja 0x3f15
leaq 0x8572(%rip), %rcx      # 0xc11c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0xf666(%rip), %rax      # 0x13220
movq (%rax), %rbx
movq %r14, %rdi
callq 0x3ae0
movq 0x20(%rax), %rdx
leaq 0x8e99(%rip), %rsi      # 0xca69
jmp 0x3c8b
movq %rbx, %rdi
callq 0xaea7
testb %al, %al
je 0x3bfd
cmpl $0x0, 0x10(%rbx)
jne 0x3bfd
movq 0x50, %rdi
leaq 0x8e3c(%rip), %rsi      # 0xca32
xorl %eax, %eax
callq 0x52f9
leaq 0xf61c(%rip), %rax      # 0x13220
movq (%rax), %r14
movq %rbx, %rdi
callq 0xafa3
leaq 0x8e3c(%rip), %rsi      # 0xca52
movq %r14, %rdi
movl %eax, %edx
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x10f0
leaq 0xf5e9(%rip), %rax      # 0x13220
movq (%rax), %rbx
movq %r14, %rdi
callq 0x3ae0
movq 0x20(%rax), %rdx
leaq 0x8e28(%rip), %rsi      # 0xca75
jmp 0x3c8b
leaq 0xf5ca(%rip), %rax      # 0x13220
movq (%rax), %rbx
movq %r14, %rdi
callq 0x3ae0
movq 0x20(%rax), %rdx
leaq 0x8e15(%rip), %rsi      # 0xca81
jmp 0x3c8b
leaq 0xf5ab(%rip), %rax      # 0x13220
movq (%rax), %rbx
movq %r14, %rdi
callq 0x3ae0
movq 0x20(%rax), %rdx
leaq 0x8dd2(%rip), %rsi      # 0xca5d
movq %rbx, %rdi
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x10f0
movq 0x8(%rbx), %rax
cmpl $0x1, (%rax)
jne 0x3e13
cmpl $0x2, (%r14)
jne 0x3e13
leaq 0xf55f(%rip), %rax      # 0x13220
movq (%rax), %rdi
movl 0x28(%r14), %edx
leaq 0x8dce(%rip), %rsi      # 0xca9d
jmp 0x3c1b
cmpl $0x3, (%r14)
je 0x3ceb
leaq 0x9acd(%rip), %rsi      # 0xd7ae
movq %r14, %rdi
xorl %eax, %eax
callq 0x552e
movq 0x48(%r14), %r14
movq 0x20(%rbx), %r12
movq %r14, %rdi
callq 0x3f62
testl %eax, %eax
jle 0x3eb7
xorl %r13d, %r13d
xorl %r15d, %r15d
movl 0x18(%r12), %edx
subl %r15d, %edx
jle 0x3d30
leaq 0xf506(%rip), %rax      # 0x13220
movq (%rax), %rdi
leaq 0x8d2e(%rip), %rsi      # 0xca52
xorl %eax, %eax
callq 0x10f0
movl 0x18(%r12), %r15d
movq 0x10(%r12), %rdi
movq (%r14), %rax
movq (%rax,%r13,8), %rsi
callq 0x3b80
movq 0x10(%r12), %rdi
callq 0xafa3
movl %eax, %ebp
addl %r15d, %ebp
incq %r13
movq (%r12), %r12
movq %r14, %rdi
callq 0x3f62
cltq
movl %ebp, %r15d
cmpq %rax, %r13
jl 0x3d09
jmp 0x3eb9
movq 0x8(%rbx), %rax
cmpl $0x1, (%rax)
jne 0x3e66
cmpl $0x2, (%r14)
jne 0x3e66
leaq 0xf494(%rip), %r15      # 0x13220
movq (%r15), %rcx
leaq 0x8cf7(%rip), %rdi      # 0xca8d
movl $0xa, %esi
movl $0x1, %edx
callq 0x1190
movq 0x50(%r14), %r12
movl $0x1, %ebp
testq %r12, %r12
je 0x3de0
leaq 0x8cde(%rip), %r14      # 0xca98
movq (%r15), %rdi
movl 0x28(%r12), %edx
movq 0x20(%r12), %rcx
movq %r14, %rsi
xorl %eax, %eax
callq 0x10f0
addl 0x28(%r12), %ebp
movq 0x8(%r12), %r12
testq %r12, %r12
jne 0x3dba
movq (%r15), %rcx
leaq 0x934c(%rip), %rdi      # 0xd136
movl $0x2, %esi
movl $0x1, %edx
callq 0x1190
movl 0x10(%rbx), %edx
subl %ebp, %edx
jle 0x3ede
movq (%r15), %rdi
leaq 0x8c44(%rip), %rsi      # 0xca52
jmp 0x3c1b
movl (%r14), %eax
cmpl $0x1c, %eax
je 0x3ef2
cmpl $0x16, %eax
jne 0x3f15
movq 0x8(%r14), %rcx
cmpl $0x19, (%rcx)
jne 0x3eed
leaq 0xf3e4(%rip), %rax      # 0x13220
movq (%rax), %rdi
movq 0x50(%rcx), %rax
movl 0x28(%rax), %edx
movq 0x20(%rax), %rcx
leaq 0x8c5c(%rip), %rsi      # 0xcaad
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x10f0
cmpl $0x3, (%r14)
je 0x3e7d
leaq 0x993b(%rip), %rsi      # 0xd7ae
movq %r14, %rdi
xorl %eax, %eax
callq 0x552e
movq 0x48(%r14), %r14
movq 0x8(%rbx), %rbx
movq %r14, %rdi
callq 0x3f62
testl %eax, %eax
jle 0x3ede
xorl %r15d, %r15d
movq (%r14), %rax
movq (%rax,%r15,8), %rsi
movq %rbx, %rdi
callq 0x3b80
incq %r15
movq %r14, %rdi
callq 0x3f62
cltq
cmpq %rax, %r15
jl 0x3e94
jmp 0x3ede
xorl %ebp, %ebp
movq %rbx, %rdi
callq 0xafa3
cmpl %ebp, %eax
jle 0x3ede
leaq 0xf354(%rip), %rax      # 0x13220
movq (%rax), %r14
movq %rbx, %rdi
callq 0xafa3
subl %ebp, %eax
jmp 0x3c0f
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x1c, %eax
jne 0x3f15
movq 0x8(%r14), %rax
cmpl $0x1, (%rax)
jne 0x3f15
leaq 0xf31e(%rip), %rcx      # 0x13220
movq (%rcx), %rdi
movq 0x20(%rax), %rdx
leaq 0x8b71(%rip), %rsi      # 0xca81
jmp 0x3c8e
leaq 0x9892(%rip), %rsi      # 0xd7ae
movq %r14, %rdi
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x552e