// File: /workspace/github_c_makefile_star2_O0/aquaxis[P]synverll/src/parser_ir_memory.c
// Function: get_memory_size
int get_memory_size(char *label, char *ptr, char *rslt_ptr)
{
	MEMORY_TREE *now_memory_tree;
	int size = 0;
	int total = 0;
	int base_adrs = 0;
	char *buf = NULL;
	char *token = NULL;
	char *buf_ptr = NULL;
	char *ptr_cnt = NULL;
	char *temp = NULL;
	char *str = NULL;

	now_memory_tree = memory_tree_top;
    while(now_memory_tree != NULL){
		if(!strcmp(now_memory_tree->label, label)) break;
        now_memory_tree = now_memory_tree->next_ptr;
    }
	if(now_memory_tree == NULL){
		printf("[ABORT] get_memory_size(): unknown memory = \"%s\"\n", label);
		exit(0);
	}

	token		= calloc(STR_MAX,1);
	temp		= calloc(STR_MAX,1);
	buf			= calloc(strlen(now_memory_tree->type)+1,1);
	strcpy(buf, now_memory_tree->type);

	if(ptr != NULL){
		if(strlen(ptr) > 0){
			buf_ptr = calloc(strlen(ptr)+1,1);
			strcpy(buf_ptr, ptr);
		}
	}

	size = 0;
	total = 0;
	do{
		/*
		 * ptrからアドレスポインタを取得する
		 */
		if(buf_ptr != NULL){
			// Type
			get_irtype_llvm(buf_ptr, token);
			// Name
			get_token_llvm(buf_ptr, token);
			str = regalloc(token);
			ptr_cnt = charalloc(str);
			free(str);
			// ,
			get_token_llvm(buf_ptr, token);
		}

		get_irtype_llvm(buf, token);
		if(strlen(token) == 0) break;

		if(token[0] == '['){
			size = __get_array_size(token, buf_ptr, temp);
		}else if(token[0] == '{'){
			size = __get_struct_size(token, buf_ptr, temp);
		}else if(!strcmp(token, "i8")){
			size = 1;
		}else if(!strcmp(token, "i16")){
			size = 2;
		}else if(!strcmp(token, "i32")){
			size = 4;
		}
		total += size;

		if(ptr_cnt != NULL){
			if(rslt_ptr != NULL){
				if(strlen(temp) > 0){
					sprintf(temp, "%s + (%d * %s)", temp, size, ptr_cnt);
				}else{
					sprintf(temp, "(%d * %s)", size, ptr_cnt);
				}
			}
			free(ptr_cnt);
		}

	}while(strlen(buf) > 0);
	if(token != NULL) free(token);
	if(buf != NULL) free(buf);

	if(rslt_ptr != NULL){
		if(label[0] == '@'){
			// グローバルメモリ
			base_adrs = get_adrs_memmap("GLOBAL", label);
			size = get_size_memmap("GLOBAL", label);
		}else if(label[0] == '%'){
			// ローカルメモリ
			base_adrs = get_adrs_memmap(module_name, label);
			size = get_size_memmap(module_name, label);
		}

		if(size > 0){
			sprintf(rslt_ptr, "__gm_base + %d + %s", base_adrs, temp);
		}else{
			buf = regalloc(label);
			sprintf(rslt_ptr, "%s + %s", buf, temp);
			free(buf);
		}
	}

	return total;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl $0x0, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq 0xf0c8(%rip), %rax      # 0x18548
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x94b1
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x8(%rbp), %rsi
callq 0x10d0
cmpl $0x0, %eax
jne 0x94a3
jmp 0x94b1
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x20(%rbp)
jmp 0x9484
cmpq $0x0, -0x20(%rbp)
jne 0x94d1
movq -0x8(%rbp), %rsi
leaq 0xa4c2(%rip), %rdi      # 0x13985
movb $0x0, %al
callq 0x1090
xorl %edi, %edi
callq 0x1170
movl $0x400, %edi            # imm = 0x400
movl $0x1, %esi
callq 0x10c0
movq %rax, -0x40(%rbp)
movl $0x400, %edi            # imm = 0x400
movl $0x1, %esi
callq 0x10c0
movq %rax, -0x58(%rbp)
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x1070
movq %rax, %rdi
addq $0x1, %rdi
movl $0x1, %esi
callq 0x10c0
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rsi
callq 0x1050
cmpq $0x0, -0x10(%rbp)
je 0x956d
movq -0x10(%rbp), %rdi
callq 0x1070
cmpq $0x0, %rax
jbe 0x956b
movq -0x10(%rbp), %rdi
callq 0x1070
movq %rax, %rdi
addq $0x1, %rdi
movl $0x1, %esi
callq 0x10c0
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x1050
jmp 0x956d
movl $0x0, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x95cc
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x25b0
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x11ca0
movq -0x40(%rbp), %rdi
callq 0xaa30
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rdi
callq 0x13b0
movq %rax, -0x50(%rbp)
movq -0x60(%rbp), %rdi
callq 0x1030
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x11ca0
movq -0x38(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x25b0
movq -0x40(%rbp), %rdi
callq 0x1070
cmpq $0x0, %rax
jne 0x95ed
jmp 0x9712
movq -0x40(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x5b, %eax
jne 0x9612
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x58(%rbp), %rdx
callq 0x91c0
movl %eax, -0x24(%rbp)
jmp 0x9694
movq -0x40(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x7b, %eax
jne 0x9634
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x58(%rbp), %rdx
callq 0x8f70
movl %eax, -0x24(%rbp)
jmp 0x9692
movq -0x40(%rbp), %rdi
leaq 0x99cd(%rip), %rsi      # 0x1300c
callq 0x10d0
cmpl $0x0, %eax
jne 0x9652
movl $0x1, -0x24(%rbp)
jmp 0x9690
movq -0x40(%rbp), %rdi
leaq 0x99b2(%rip), %rsi      # 0x1300f
callq 0x10d0
cmpl $0x0, %eax
jne 0x9670
movl $0x2, -0x24(%rbp)
jmp 0x968e
movq -0x40(%rbp), %rdi
leaq 0xac1e(%rip), %rsi      # 0x14299
callq 0x10d0
cmpl $0x0, %eax
jne 0x968c
movl $0x4, -0x24(%rbp)
jmp 0x968e
jmp 0x9690
jmp 0x9692
jmp 0x9694
movl -0x24(%rbp), %eax
addl -0x28(%rbp), %eax
movl %eax, -0x28(%rbp)
cmpq $0x0, -0x50(%rbp)
je 0x96fd
cmpq $0x0, -0x18(%rbp)
je 0x96f4
movq -0x58(%rbp), %rdi
callq 0x1070
cmpq $0x0, %rax
jbe 0x96d9
movq -0x58(%rbp), %rdi
movq -0x58(%rbp), %rdx
movl -0x24(%rbp), %ecx
movq -0x50(%rbp), %r8
leaq 0xa2a6(%rip), %rsi      # 0x13976
movb $0x0, %al
callq 0x1160
jmp 0x96f2
movq -0x58(%rbp), %rdi
movl -0x24(%rbp), %edx
movq -0x50(%rbp), %rcx
leaq 0xa290(%rip), %rsi      # 0x1397b
movb $0x0, %al
callq 0x1160
jmp 0x96f4
movq -0x50(%rbp), %rdi
callq 0x1030
jmp 0x96ff
movq -0x38(%rbp), %rdi
callq 0x1070
cmpq $0x0, %rax
ja 0x957b
cmpq $0x0, -0x40(%rbp)
je 0x9722
movq -0x40(%rbp), %rdi
callq 0x1030
cmpq $0x0, -0x38(%rbp)
je 0x9732
movq -0x38(%rbp), %rdi
callq 0x1030
cmpq $0x0, -0x18(%rbp)
je 0x97fe
movq -0x8(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x40, %eax
jne 0x9771
movq -0x8(%rbp), %rsi
leaq 0xa03c(%rip), %rdi      # 0x13790
callq 0x7980
movl %eax, -0x2c(%rbp)
movq -0x8(%rbp), %rsi
leaq 0xa029(%rip), %rdi      # 0x13790
callq 0x7a10
movl %eax, -0x24(%rbp)
jmp 0x97ab
movq -0x8(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x25, %eax
jne 0x97a9
leaq 0xe9a4(%rip), %rax      # 0x18128
movq (%rax), %rdi
movq -0x8(%rbp), %rsi
callq 0x7980
movl %eax, -0x2c(%rbp)
leaq 0xe98e(%rip), %rax      # 0x18128
movq (%rax), %rdi
movq -0x8(%rbp), %rsi
callq 0x7a10
movl %eax, -0x24(%rbp)
jmp 0x97ab
cmpl $0x0, -0x24(%rbp)
jle 0x97cc
movq -0x18(%rbp), %rdi
movl -0x2c(%rbp), %edx
movq -0x58(%rbp), %rcx
leaq 0xa1f4(%rip), %rsi      # 0x139b7
movb $0x0, %al
callq 0x1160
jmp 0x97fc
movq -0x8(%rbp), %rdi
callq 0xaa30
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rdi
movq -0x38(%rbp), %rdx
movq -0x58(%rbp), %rcx
leaq 0xa1df(%rip), %rsi      # 0x139cb
movb $0x0, %al
callq 0x1160
movq -0x38(%rbp), %rdi
callq 0x1030
jmp 0x97fe
movl -0x28(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw (%rax,%rax)