// File: /workspace/github_c_makefile_star2_O1/aquaxis[P]synverll/src/parser.c
// Function: create_header_source
int create_header_source()
{
//    PARSER_TREE *now_parser_tree;
//    PARSER_TREE *parser_tree_process;
//    unsigned int now_level;
    char *token;
    char *line;
    int m = 0;
    FILE *fp;
    int size, start;
//    char *filename;
	char *const_extern = "extern \0";
	char *const_define = "#define \0";
	char *const_space = " \0";
	int i;
	char *const_extern_c = "__extern.c\0";
	char *const_extern_h = "__extern.h\0";

	char *const_grmem_func = "void __gr_mem(){\n";

    printf(" -> Create Header Source\n");

	token = calloc(STR_MAX, 1);

	/*
	 * グローバル変数用ソースコードの生成
	 */
	fp = fopen(const_extern_c, "wb+");
	fwrite(const_grmem_func, 1, strlen(const_grmem_func), fp);
    parser_tree_current = parser_tree_top;
    while(parser_tree_current != NULL){
        if(parser_tree_current->flag == PARSER_FLAG_LINE){
			size = strlen(parser_tree_current->line.body);
			fwrite(parser_tree_current->line.body, 1, size, fp);
			fwrite("\n\n", 1, 2, fp);
        }
        parser_tree_current = parser_tree_current->next_ptr;
    }
	fwrite("}\n", 1, 2, fp);
	fclose(fp);

	/*
	 * externヘッダファイルの生成
	 */
	fp = fopen(const_extern_h, "wb+");
    parser_tree_current = parser_tree_top;

	/*
	 * デファイン文
	 */
    while(parser_tree_current != NULL){
        if(parser_tree_current->flag == PARSER_FLAG_DEFINE){
			fwrite(const_define, 1, strlen(const_define), fp);
			size = strlen(parser_tree_current->define.name);
			fwrite(parser_tree_current->define.name, 1, size, fp);
			fwrite(const_space, 1, strlen(const_space), fp);
			size = strlen(parser_tree_current->define.value);
			fwrite(parser_tree_current->define.value, 1, size, fp);
			fwrite(const_space, 1, strlen(const_space), fp);
			fwrite("\n", 1, 1, fp);
		}
        parser_tree_current = parser_tree_current->next_ptr;
	}

	/*
	 * グローバル変数
	 */
    parser_tree_current = parser_tree_top;
    while(parser_tree_current != NULL){
        if(parser_tree_current->flag == PARSER_FLAG_LINE){
			// 予約語から
			m++;

			line = charalloc(parser_tree_current->line.body);

			// fopenのエラー処理が必要
			size = strlen(line);

			m = 0;
			// "="を見つけたら強制的に";\0"に変換する
			size = strlen(line);
			for(i = 0; i < size; i++){
				if(line[i] == '='){
					line[i] = ';';
					line[i+1] = '\0';
					break;
				}
			}

			// static宣言はextern宣言にして出力する
			// typedefはそのまま出力する
			// その他の宣言はextern化して出力する
			get_token(line, token);
			if(!strcmp(token, "static")){
				fwrite(const_extern, 1, strlen(const_extern), fp);
				start = strlen(const_space);
				size = strlen(line);
				fwrite(&line[start], 1, size-start, fp);
			}else if(!strcmp(token, "typedef")){
				fwrite(token, 1, strlen(token), fp);
				size = strlen(line);
				fwrite(line, 1, size, fp);
			}else{
				fwrite(const_extern, 1, strlen(const_extern), fp);
				fwrite(token, 1, strlen(token), fp);
				size = strlen(line);
				fwrite(line, 1, size, fp);
			}
			fwrite(const_space, 1, strlen(const_space), fp);
			fwrite("\n\n", 1, 2, fp);

			free(line);
        }
        parser_tree_current = parser_tree_current->next_ptr;
    }

	/*
	 * extern関数定義
	 */
    parser_tree_current = parser_tree_top;

    while(parser_tree_current != NULL){
        if(parser_tree_current->flag == PARSER_FLAG_PROCESS){
			// 予約語から
			m++;

			line = charalloc(parser_tree_current->line.body);
			// fopenのエラー処理が必要
			size = strlen(parser_tree_current->line.body);

			m = 0;
			// ")"を見つけたら強制的に";\0"に変換する
			size = strlen(line);
			for(i = 0; i < size; i++){
				if(line[i] == ')'){
					line[i+1] = ';';
					line[i+2] = '\0';
					break;
				}
			}

			// static宣言はextern宣言にして出力する
			// typedefはそのまま出力する
			// その他の宣言はextern化して出力する
			get_token(line, token);
			if(!strcmp(token, "static")){
				fwrite(const_extern, 1, strlen(const_extern), fp);
				start = strlen(const_space);
				size = strlen(line);
				fwrite(&line[start], 1, size-start, fp);
			}else{
				fwrite(const_extern, 1, strlen(const_extern), fp);
				fwrite(token, 1, strlen(token), fp);
				size = strlen(line);
				fwrite(line, 1, size, fp);
			}
			fwrite(const_space, 1, strlen(const_space), fp);
			fwrite("\n\n", 1, 2, fp);

			free(line);
        }
        parser_tree_current = parser_tree_current->next_ptr;
    }

	fclose(fp);

	free(token);

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
leaq 0xcacd(%rip), %rdi      # 0xe0cd
callq 0x1070
movl $0x400, %edi            # imm = 0x400
movl $0x1, %esi
callq 0x1100
movq %rax, %rbx
leaq 0xca3c(%rip), %rdi      # 0xe05a
leaq 0xca4d(%rip), %rsi      # 0xe072
callq 0x1170
movq %rax, %r14
leaq 0xca42(%rip), %rdi      # 0xe076
movl $0x1, %esi
movl $0x11, %edx
movq %rax, %rcx
callq 0x11d0
movq 0x12ad3(%rip), %rax     # 0x14120
movq %rax, 0x12ad4(%rip)     # 0x14128
testq %rax, %rax
je 0x16b4
leaq 0xe96d(%rip), %r15      # 0xffcd
cmpl $0x3d, 0x10(%rax)
jne 0x169d
movq 0x6c50(%rax), %r12
movq %r12, %rdi
callq 0x1090
movslq %eax, %rdx
movl $0x1, %esi
movq %r12, %rdi
movq %r14, %rcx
callq 0x11d0
movl $0x1, %esi
movl $0x2, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x11d0
movq 0x12a84(%rip), %rax     # 0x14128
movq 0x8(%rax), %rax
movq %rax, 0x12a79(%rip)     # 0x14128
testq %rax, %rax
jne 0x1660
leaq 0xc9cd(%rip), %rdi      # 0xe088
movl $0x1, %esi
movl $0x2, %edx
movq %r14, %rcx
callq 0x11d0
movq %r14, %rdi
callq 0x1080
leaq 0xc98a(%rip), %rdi      # 0xe066
leaq 0xc98f(%rip), %rsi      # 0xe072
callq 0x1170
movq %rax, %r14
movq 0x12a2e(%rip), %rax     # 0x14120
movq %rax, 0x12a2f(%rip)     # 0x14128
testq %rax, %rax
je 0x17bb
leaq 0xc947(%rip), %r15      # 0xe050
movl $0x420, %r13d           # imm = 0x420
cmpl $0x3, 0x10(%rax)
jne 0x17a0
movl $0x1, %esi
movl $0x8, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x11d0
movq 0x129f3(%rip), %r12     # 0x14128
addq $0x20, %r12
movq %r12, %rdi
callq 0x1090
movslq %eax, %rdx
movl $0x1, %esi
movq %r12, %rdi
movq %r14, %rcx
callq 0x11d0
movl $0x20, %edi
movq %r14, %rsi
callq 0x10e0
movq 0x129c0(%rip), %r12     # 0x14128
addq %r13, %r12
movq %r12, %rdi
callq 0x1090
movslq %eax, %rdx
movl $0x1, %esi
movq %r12, %rdi
movq %r14, %rcx
callq 0x11d0
movl $0x20, %edi
movq %r14, %rsi
callq 0x10e0
movl $0xa, %edi
movq %r14, %rsi
callq 0x10e0
movq 0x12981(%rip), %rax     # 0x14128
movq 0x8(%rax), %rax
movq %rax, 0x12976(%rip)     # 0x14128
testq %rax, %rax
jne 0x170f
movq 0x1295e(%rip), %rax     # 0x14120
movq %rax, 0x1295f(%rip)     # 0x14128
testq %rax, %rax
je 0x1928
leaq 0xc8b2(%rip), %r15      # 0xe08b
leaq 0xc867(%rip), %r12      # 0xe047
leaq 0xe7e6(%rip), %r13      # 0xffcd
cmpl $0x3d, 0x10(%rax)
jne 0x190d
movq 0x6c50(%rax), %rdi
callq 0x1395
movq %rax, %rbp
movq %rax, %rdi
callq 0x1090
testl %eax, %eax
jle 0x182b
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorl %ecx, %ecx
cmpb $0x3d, (%rbp,%rcx)
je 0x1824
incq %rcx
cmpq %rcx, %rax
jne 0x1813
jmp 0x182b
movw $0x3b, (%rbp,%rcx)
movq %rbp, %rdi
movq %rbx, %rsi
callq 0xc4cb
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1110
testl %eax, %eax
je 0x189d
movq %rbx, %rdi
leaq 0xc843(%rip), %rsi      # 0xe092
callq 0x1110
testl %eax, %eax
je 0x186d
movl $0x1, %esi
movl $0x7, %edx
movq %r12, %rdi
movq %r14, %rcx
callq 0x11d0
movq %rbx, %rdi
callq 0x1090
movl $0x1, %esi
movq %rbx, %rdi
movq %rax, %rdx
movq %r14, %rcx
callq 0x11d0
movq %rbp, %rdi
callq 0x1090
movslq %eax, %rdx
movl $0x1, %esi
movq %rbp, %rdi
jmp 0x18db
movl $0x1, %esi
movl $0x7, %edx
movq %r12, %rdi
movq %r14, %rcx
callq 0x11d0
movq %rbp, %rdi
callq 0x1090
movq %rbp, %rdi
incq %rdi
shlq $0x20, %rax
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
leaq (%rax,%rcx), %rdx
sarq $0x20, %rdx
movl $0x1, %esi
movq %r14, %rcx
callq 0x11d0
movl $0x20, %edi
movq %r14, %rsi
callq 0x10e0
movl $0x1, %esi
movl $0x2, %edx
movq %r13, %rdi
movq %r14, %rcx
callq 0x11d0
movq %rbp, %rdi
callq 0x1030
movq 0x12814(%rip), %rax     # 0x14128
movq 0x8(%rax), %rax
movq %rax, 0x12809(%rip)     # 0x14128
testq %rax, %rax
jne 0x17e7
movq 0x127f1(%rip), %rax     # 0x14120
movq %rax, 0x127f2(%rip)     # 0x14128
testq %rax, %rax
je 0x1a6e
leaq 0xc701(%rip), %r12      # 0xe047
leaq 0xe680(%rip), %r13      # 0xffcd
cmpl $0x19, 0x10(%rax)
jne 0x1a53
movq 0x6c50(%rax), %rdi
callq 0x1395
movq %rax, %rbp
movq %rax, %rdi
callq 0x1090
testl %eax, %eax
jle 0x1991
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorl %ecx, %ecx
cmpb $0x29, (%rbp,%rcx)
je 0x198a
incq %rcx
cmpq %rcx, %rax
jne 0x1979
jmp 0x1991
movw $0x3b, 0x1(%rbp,%rcx)
movq %rbp, %rdi
movq %rbx, %rsi
callq 0xc4cb
movq %rbx, %rdi
leaq 0xc6e5(%rip), %rsi      # 0xe08b
callq 0x1110
movl %eax, %r15d
movl $0x1, %esi
movl $0x7, %edx
movq %r12, %rdi
movq %r14, %rcx
callq 0x11d0
testl %r15d, %r15d
je 0x19f8
movq %rbx, %rdi
callq 0x1090
movl $0x1, %esi
movq %rbx, %rdi
movq %rax, %rdx
movq %r14, %rcx
callq 0x11d0
movq %rbp, %rdi
callq 0x1090
movslq %eax, %rdx
movl $0x1, %esi
movq %rbp, %rdi
jmp 0x1a21
movq %rbp, %rdi
callq 0x1090
movq %rbp, %rdi
incq %rdi
shlq $0x20, %rax
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
leaq (%rax,%rcx), %rdx
sarq $0x20, %rdx
movl $0x1, %esi
movq %r14, %rcx
callq 0x11d0
movl $0x20, %edi
movq %r14, %rsi
callq 0x10e0
movl $0x1, %esi
movl $0x2, %edx
movq %r13, %rdi
movq %r14, %rcx
callq 0x11d0
movq %rbp, %rdi
callq 0x1030
movq 0x126ce(%rip), %rax     # 0x14128
movq 0x8(%rax), %rax
movq %rax, 0x126c3(%rip)     # 0x14128
testq %rax, %rax
jne 0x194d
movq %r14, %rdi
callq 0x1080
movq %rbx, %rdi
callq 0x1030
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq