// File: /workspace/github_c_makefile_star2_O1/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), %rax
cmpl $0x0, 0x8(%rax)
jle 0x30d8
xorl %r12d, %r12d
xorl %r14d, %r14d
movq (%rax), %rax
movq (%rax,%r12,8), %rax
movq (%rax), %r15
testq %r15, %r15
je 0x30c2
movq %r15, %rdi
callq 0x2faa
movq %rax, %r14
testq %r15, %r15
jne 0x30db
incq %r12
movq (%rbx), %rax
movslq 0x8(%rax), %rcx
cmpq %rcx, %r12
jl 0x30a8
jmp 0x30db
xorl %r14d, %r14d
movq 0x8(%rbx), %rbp
cmpl $0x0, 0x8(%rbp)
jle 0x3255
leaq 0x7773(%rip), %r13      # 0xa863
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 0x3127
movl (%rcx), %ecx
movl %ecx, 0x10(%r14)
movl $0x8, 0x8(%r14)
jmp 0x312f
movl 0x4(%r14), %ecx
movl %ecx, 0x8(%r14)
movq 0x8(%rax), %rax
movq 0x18(%rax), %rcx
testq %rcx, %rcx
je 0x317b
movq (%rcx), %rcx
movq (%rcx), %rcx
movq (%rcx), %rcx
movq (%rcx), %rcx
movq (%rcx), %rcx
movq (%rcx), %rcx
movq 0x8(%rcx), %rcx
movq 0x8(%rcx), %rcx
movq (%rcx), %rcx
movq 0x8(%rcx), %rcx
movq 0x8(%rcx), %rcx
movq (%rcx), %rcx
movq (%rcx), %rcx
movq (%rcx), %rcx
movq (%rcx), %rcx
movl 0x4(%rcx), %ecx
movl %ecx, 0xc(%r14)
imull 0x8(%r14), %ecx
jmp 0x3187
movl $0x0, 0xc(%r14)
movl 0x8(%r14), %ecx
addl 0xbfbb(%rip), %ecx      # 0xf148
movl %ecx, 0xbfb5(%rip)      # 0xf148
cmpl $0x1, 0x8(%r14)
je 0x31af
leal 0x7(%rcx), %edx
testl %ecx, %ecx
cmovnsl %ecx, %edx
andl $-0x8, %edx
leal 0x8(%rdx), %esi
cmpl %edx, %ecx
cmovel %ecx, %esi
movl %esi, %ecx
movl %ecx, 0x14(%r15)
movq %rax, %rcx
movq 0x10(%rax), %rax
testq %rax, %rax
jne 0x31b3
movq (%rcx), %rdi
callq 0x1190
movq %rax, 0x8(%r15)
movq 0xbf6e(%rip), %rdi      # 0xf140
movq %r15, %rsi
callq 0x8b3b
movq 0x8(%rbp), %rbp
testq %rbp, %rbp
je 0x3241
movl 0x14(%r15), %esi
leaq 0x7436(%rip), %rdi      # 0xa624
xorl %eax, %eax
callq 0x10a0
leaq 0x7739(%rip), %rdi      # 0xa935
callq 0x1070
movq (%rbp), %rdi
testq %rdi, %rdi
je 0x320f
callq 0x3501
leaq 0x7715(%rip), %rdi      # 0xa92b
callq 0x1070
leaq 0x772d(%rip), %rdi      # 0xa94f
callq 0x1070
movq (%r15), %rax
cmpl $0x1, 0x8(%rax)
movq %r13, %rdi
leaq 0x7501(%rip), %rax      # 0xa739
cmoveq %rax, %rdi
callq 0x1070
incq %r12
movq 0x8(%rbx), %rbp
movslq 0x8(%rbp), %rax
cmpq %rax, %r12
jl 0x30f3
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq