// File: /workspace/github_c_makefile_star2_O3/chgzm[P]minic/generator.c
// Function: process_declaration
static void process_declaration(const DeclarationNode* node) {
    Type* type = NULL;
    for (int i = 0; i < node->decl_specifier_nodes->size; ++i) {
        const DeclSpecifierNode* decl_specifier_node = node->decl_specifier_nodes->elements[i];
        const TypeSpecifierNode* type_specifier_node = decl_specifier_node->type_specifier_node;
        if (type_specifier_node == NULL) {
            continue;
        }

        type = process_type_specifier_in_local(type_specifier_node);
        break;
    }

    // add local-variable to list
    for (int j = 0; j < node->init_declarator_nodes->size; ++j) {
        LocalVar* lv = malloc(sizeof(LocalVar));
        lv->type     = type;

        const InitDeclaratorNode* init_declarator_node = node->init_declarator_nodes->elements[j];
        const DeclaratorNode* declarator_node  = init_declarator_node->declarator_node;

        const PointerNode* pointer_node = declarator_node->pointer_node;
        if (pointer_node != NULL) {
            lv->type->ptr_count = pointer_node->count;
            lv->type->size      = 8;
        } else {
            lv->type->size = lv->type->type_size;
        }

        const DirectDeclaratorNode* direct_declarator_node = declarator_node->direct_declarator_node;
        const ConditionalExprNode*  conditional_expr_node  = direct_declarator_node->conditional_expr_node;
        if (conditional_expr_node == NULL) {
            lv->type->array_size = 0;
            current_offset += lv->type->size;
        } else {
            lv->type->array_size = get_array_size_from_constant_expr(conditional_expr_node);
            current_offset += (lv->type->array_size * lv->type->size);

        }
        lv->offset = align_offset(current_offset, lv->type->size);

        const DirectDeclaratorNode* ident_node = get_identifier_direct_declarator(direct_declarator_node);
        lv->name = strdup(ident_node->identifier);
        vector_push_back(localvar_list, lv);

        if (init_declarator_node->initializer_node != NULL) {
            const InitializerNode* initializer_node = init_declarator_node->initializer_node;
            printf("  lea rax, [rbp-%d]\n", lv->offset);
            printf("  push rax\n");

            if (initializer_node->assign_expr_node != NULL) {
                process_assign_expr(initializer_node->assign_expr_node);
            }

            printf("  pop rdi\n");
            printf("  pop rax\n");
            if (lv->type->size == 1) {
                printf("  mov [rax], dil\n");
            } else {
                printf("  mov [rax], rdi\n");
            }
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movq (%rdi), %rcx
movslq 0x8(%rcx), %rax
testq %rax, %rax
jle 0x3117
movq (%rcx), %rcx
xorl %r14d, %r14d
xorl %edx, %edx
movq (%rcx,%rdx,8), %rsi
movq (%rsi), %rdi
testq %rdi, %rdi
jne 0x311c
incq %rdx
cmpq %rdx, %rax
jne 0x3101
jmp 0x3124
xorl %r14d, %r14d
jmp 0x3124
callq 0x3001
movq %rax, %r14
movq 0x8(%rbx), %rbp
cmpl $0x0, 0x8(%rbp)
jle 0x3295
leaq 0x672a(%rip), %r13      # 0x9863
xorl %r12d, %r12d
movl $0x18, %edi
callq 0x1100
movq %rax, %r15
movq %r14, (%rax)
movq (%rbp), %rax
movq (%rax,%r12,8), %rbp
movq (%rbp), %rax
movq (%rax), %rcx
testq %rcx, %rcx
je 0x316d
movl (%rcx), %ecx
movl %ecx, 0x10(%r14)
movl $0x8, %ecx
jmp 0x3171
movl 0x4(%r14), %ecx
movl %ecx, 0x8(%r14)
movq 0x8(%rax), %rax
movq 0x18(%rax), %rdx
testq %rdx, %rdx
je 0x31bf
movq (%rdx), %rdx
movq (%rdx), %rdx
movq (%rdx), %rdx
movq (%rdx), %rdx
movq (%rdx), %rdx
movq (%rdx), %rdx
movq 0x8(%rdx), %rdx
movq 0x8(%rdx), %rdx
movq (%rdx), %rdx
movq 0x8(%rdx), %rdx
movq 0x8(%rdx), %rdx
movq (%rdx), %rdx
movq (%rdx), %rdx
movq (%rdx), %rdx
movq (%rdx), %rdx
movl 0x4(%rdx), %edx
movl %edx, 0xc(%r14)
imull %ecx, %edx
jmp 0x31c9
movl $0x0, 0xc(%r14)
movl %ecx, %edx
addl 0xaf79(%rip), %edx      # 0xe148
movl %edx, 0xaf73(%rip)      # 0xe148
cmpl $0x1, %ecx
je 0x31ef
leal 0x7(%rdx), %ecx
testl %edx, %edx
cmovnsl %edx, %ecx
andl $-0x8, %ecx
leal 0x8(%rcx), %esi
cmpl %ecx, %edx
cmovel %edx, %esi
movl %esi, %edx
movl %edx, 0x14(%r15)
movq %rax, %rcx
movq 0x10(%rax), %rax
testq %rax, %rax
jne 0x31f3
movq (%rcx), %rdi
callq 0x1190
movq %rax, 0x8(%r15)
movq 0xaf2e(%rip), %rdi      # 0xe140
movq %r15, %rsi
callq 0x876b
movq 0x8(%rbp), %rbp
testq %rbp, %rbp
je 0x3281
movl 0x14(%r15), %esi
leaq 0x63f6(%rip), %rdi      # 0x9624
xorl %eax, %eax
callq 0x10a0
leaq 0x66f9(%rip), %rdi      # 0x9935
callq 0x1070
movq (%rbp), %rdi
testq %rdi, %rdi
je 0x324f
callq 0x3647
leaq 0x66d5(%rip), %rdi      # 0x992b
callq 0x1070
leaq 0x66ed(%rip), %rdi      # 0x994f
callq 0x1070
movq (%r15), %rax
cmpl $0x1, 0x8(%rax)
movq %r13, %rdi
leaq 0x64c1(%rip), %rax      # 0x9739
cmoveq %rax, %rdi
callq 0x1070
incq %r12
movq 0x8(%rbx), %rbp
movslq 0x8(%rbp), %rax
cmpq %rax, %r12
jl 0x313c
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq