// File: /workspace/github_c_makefile_star2_O3/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 0xcaff(%rip), %rdi      # 0xe0c7
callq 0x1070
movl $0x400, %edi            # imm = 0x400
movl $0x1, %esi
callq 0x10f0
movq %rax, %rbx
leaq 0xca6e(%rip), %rdi      # 0xe054
leaq 0xca7f(%rip), %rsi      # 0xe06c
callq 0x1160
movq %rax, %r14
leaq 0xca74(%rip), %rdi      # 0xe070
movl $0x1, %esi
movl $0x11, %edx
movq %rax, %rcx
callq 0x11c0
movq 0x12afb(%rip), %rax     # 0x14110
movq %rax, 0x12afc(%rip)     # 0x14118
testq %rax, %rax
je 0x167c
leaq 0xe999(%rip), %r15      # 0xffc1
cmpl $0x3d, 0x10(%rax)
jne 0x166c
movq 0x6c50(%rax), %r12
movq %r12, %rdi
callq 0x1090
movslq %eax, %rdx
movl $0x1, %esi
movq %r12, %rdi
movq %r14, %rcx
callq 0x11c0
movl $0x1, %esi
movl $0x2, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x11c0
movq 0x12aac(%rip), %rax     # 0x14118
movq 0x8(%rax), %rax
movq %rax, 0x12aa1(%rip)     # 0x14118
testq %rax, %rax
jne 0x1628
leaq 0xc9ff(%rip), %rdi      # 0xe082
movl $0x1, %esi
movl $0x2, %edx
movq %r14, %rcx
callq 0x11c0
movq %r14, %rdi
callq 0x1080
leaq 0xc9bc(%rip), %rdi      # 0xe060
leaq 0xc9c1(%rip), %rsi      # 0xe06c
callq 0x1160
movq %rax, %r14
movq 0x12a56(%rip), %rax     # 0x14110
movq %rax, 0x12a57(%rip)     # 0x14118
testq %rax, %rax
je 0x1a38
leaq 0xc979(%rip), %r15      # 0xe04a
movl $0x420, %r13d           # imm = 0x420
cmpl $0x3, 0x10(%rax)
jne 0x176f
movl $0x1, %esi
movl $0x8, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x11c0
movq 0x12a1b(%rip), %r12     # 0x14118
addq $0x20, %r12
movq %r12, %rdi
callq 0x1090
movslq %eax, %rdx
movl $0x1, %esi
movq %r12, %rdi
movq %r14, %rcx
callq 0x11c0
movl $0x20, %edi
movq %r14, %rsi
callq 0x10d0
movq 0x129e8(%rip), %r12     # 0x14118
addq %r13, %r12
movq %r12, %rdi
callq 0x1090
movslq %eax, %rdx
movl $0x1, %esi
movq %r12, %rdi
movq %r14, %rcx
callq 0x11c0
movl $0x20, %edi
movq %r14, %rsi
callq 0x10d0
movl $0xa, %edi
movq %r14, %rsi
callq 0x10d0
movq 0x129a9(%rip), %rax     # 0x14118
movq 0x8(%rax), %rax
movq %rax, 0x1299e(%rip)     # 0x14118
testq %rax, %rax
jne 0x16d7
movq 0x12986(%rip), %rax     # 0x14110
movq %rax, 0x12987(%rip)     # 0x14118
testq %rax, %rax
je 0x1a38
leaq 0xc8e4(%rip), %r15      # 0xe085
leaq 0xc899(%rip), %r12      # 0xe041
leaq 0xe812(%rip), %r13      # 0xffc1
cmpl $0x3d, 0x10(%rax)
jne 0x18dc
movq 0x6c50(%rax), %rdi
callq 0x137c
movq %rax, %rbp
movq %rax, %rdi
callq 0x1090
testl %eax, %eax
jle 0x17f3
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorl %ecx, %ecx
cmpb $0x3d, (%rbp,%rcx)
je 0x17ec
incq %rcx
cmpq %rcx, %rax
jne 0x17db
jmp 0x17f3
movw $0x3b, (%rbp,%rcx)
movq %rbp, %rdi
movq %rbx, %rsi
callq 0xc3af
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1100
testl %eax, %eax
je 0x1865
movq %rbx, %rdi
leaq 0xc875(%rip), %rsi      # 0xe08c
callq 0x1100
testl %eax, %eax
je 0x1835
movl $0x1, %esi
movl $0x7, %edx
movq %r12, %rdi
movq %r14, %rcx
callq 0x11c0
movq %rbx, %rdi
callq 0x1090
movl $0x1, %esi
movq %rbx, %rdi
movq %rax, %rdx
movq %r14, %rcx
callq 0x11c0
movq %rbp, %rdi
callq 0x1090
movslq %eax, %rdx
movl $0x1, %esi
movq %rbp, %rdi
jmp 0x18a3
movl $0x1, %esi
movl $0x7, %edx
movq %r12, %rdi
movq %r14, %rcx
callq 0x11c0
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 0x11c0
movl $0x20, %edi
movq %r14, %rsi
callq 0x10d0
movl $0x1, %esi
movl $0x2, %edx
movq %r13, %rdi
movq %r14, %rcx
callq 0x11c0
movq %rbp, %rdi
callq 0x1030
movq 0x1283c(%rip), %rax     # 0x14118
movq 0x8(%rax), %rax
movq %rax, 0x12831(%rip)     # 0x14118
testq %rax, %rax
jne 0x17af
movq 0x12819(%rip), %rax     # 0x14110
movq %rax, 0x1281a(%rip)     # 0x14118
testq %rax, %rax
je 0x1a43
leaq 0xc733(%rip), %r12      # 0xe041
leaq 0xe6ac(%rip), %r13      # 0xffc1
cmpl $0x19, 0x10(%rax)
jne 0x1a22
movq 0x6c50(%rax), %rdi
callq 0x137c
movq %rax, %rbp
movq %rax, %rdi
callq 0x1090
testl %eax, %eax
jle 0x1959
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorl %ecx, %ecx
cmpb $0x29, (%rbp,%rcx)
je 0x1952
incq %rcx
cmpq %rcx, %rax
jne 0x1941
jmp 0x1959
movw $0x3b, 0x1(%rbp,%rcx)
movq %rbp, %rdi
movq %rbx, %rsi
callq 0xc3af
movq %rbx, %rdi
leaq 0xc717(%rip), %rsi      # 0xe085
callq 0x1100
movl %eax, %r15d
movl $0x1, %esi
movl $0x7, %edx
movq %r12, %rdi
movq %r14, %rcx
callq 0x11c0
testl %r15d, %r15d
je 0x19c0
movq %rbx, %rdi
callq 0x1090
movl $0x1, %esi
movq %rbx, %rdi
movq %rax, %rdx
movq %r14, %rcx
callq 0x11c0
movq %rbp, %rdi
callq 0x1090
movslq %eax, %rdx
movl $0x1, %esi
movq %rbp, %rdi
jmp 0x19e9
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 0x11c0
movl $0x20, %edi
movq %r14, %rsi
callq 0x10d0
movl $0x1, %esi
movl $0x2, %edx
movq %r13, %rdi
movq %r14, %rcx
callq 0x11c0
movq %rbp, %rdi
callq 0x1030
movq 0x126f6(%rip), %rax     # 0x14118
movq 0x8(%rax), %rax
movq %rax, 0x126eb(%rip)     # 0x14118
testq %rax, %rax
jne 0x1915
jmp 0x1a43
movq $0x0, 0x126d5(%rip) # 0x14118
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