// File: /workspace/github_c_makefile_star2_O2/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 0xbb35(%rip), %rdi      # 0xd0c7
callq 0x1070
pushq $0x1
popq %r14
movl $0x400, %edi            # imm = 0x400
movq %r14, %rsi
callq 0x10f0
movq %rax, %rbx
leaq 0xbaa2(%rip), %rdi      # 0xd054
leaq 0xbab3(%rip), %rsi      # 0xd06c
callq 0x1160
movq %rax, %r12
leaq 0xbaa8(%rip), %rdi      # 0xd070
pushq $0x11
popq %rdx
movq %r14, %rsi
movq %rax, %rcx
callq 0x11c0
leaq 0x11b33(%rip), %rax     # 0x13110
leaq 0xd9bd(%rip), %r15      # 0xefa1
pushq $0x2
popq %r13
movq (%rax), %rax
movq %rax, 0x11b26(%rip)     # 0x13118
testq %rax, %rax
je 0x163b
cmpl $0x3d, 0x10(%rax)
jne 0x1635
movq 0x6c50(%rax), %rbp
movq %rbp, %rdi
callq 0x1090
movslq %eax, %rdx
movq %rbp, %rdi
movq %r14, %rsi
movq %r12, %rcx
callq 0x11c0
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %r12, %rcx
callq 0x11c0
movq 0x11ae3(%rip), %rax     # 0x13118
addq $0x8, %rax
jmp 0x15e8
leaq 0xba40(%rip), %rdi      # 0xd082
pushq $0x1
popq %r15
pushq $0x2
popq %rdx
movq %r15, %rsi
movq %r12, %rcx
callq 0x11c0
movq %r12, %rdi
callq 0x1080
leaq 0xb9fd(%rip), %rdi      # 0xd060
leaq 0xba02(%rip), %rsi      # 0xd06c
callq 0x1160
movq %rax, %r14
leaq 0x11a97(%rip), %rax     # 0x13110
pushq $0x20
popq %rbp
pushq $0xa
popq %r12
movq (%rax), %rax
movq %rax, 0x11a8e(%rip)     # 0x13118
testq %rax, %rax
je 0x172d
cmpl $0x3, 0x10(%rax)
jne 0x1724
leaq 0xb9a6(%rip), %rdi      # 0xd04a
movq %r15, %rsi
pushq $0x8
popq %rdx
movq %r14, %rcx
callq 0x11c0
movq 0x11a5f(%rip), %r13     # 0x13118
addq $0x20, %r13
movq %r13, %rdi
callq 0x1090
movslq %eax, %rdx
movq %r13, %rdi
movq %r15, %rsi
movq %r14, %rcx
callq 0x11c0
movl %ebp, %edi
movq %r14, %rsi
callq 0x10d0
movq 0x11a31(%rip), %r13     # 0x13118
movl $0x420, %eax            # imm = 0x420
addq %rax, %r13
movq %r13, %rdi
callq 0x1090
movslq %eax, %rdx
movq %r13, %rdi
movq %r15, %rsi
movq %r14, %rcx
callq 0x11c0
movl %ebp, %edi
movq %r14, %rsi
callq 0x10d0
movl %r12d, %edi
movq %r14, %rsi
callq 0x10d0
movq 0x119f4(%rip), %rax     # 0x13118
addq $0x8, %rax
jmp 0x1680
leaq 0x119dc(%rip), %rax     # 0x13110
pushq $0x1
popq %r13
leaq 0xd862(%rip), %rbp      # 0xefa1
pushq $0x2
popq %r15
movq (%rax), %rax
movq %rax, 0x119cb(%rip)     # 0x13118
testq %rax, %rax
je 0x1885
cmpl $0x3d, 0x10(%rax)
jne 0x187c
movq 0x6c50(%rax), %rdi
callq 0x1372
movq %rax, %r12
movq %rax, %rdi
callq 0x1090
testl %eax, %eax
movl $0x0, %ecx
cmovlel %ecx, %eax
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x179e
leaq 0x1(%rcx), %rdx
cmpb $0x3d, (%r12,%rcx)
movq %rdx, %rcx
jne 0x1783
movw $0x3b, -0x1(%r12,%rdx)
movq %r12, %rdi
movq %rbx, %rsi
callq 0xbe02
movq %rbx, %rdi
leaq 0xb8d2(%rip), %rsi      # 0xd085
callq 0x1100
testl %eax, %eax
je 0x180d
movq %rbx, %rdi
leaq 0xb8c6(%rip), %rsi      # 0xd08c
callq 0x1100
testl %eax, %eax
je 0x17e4
leaq 0xb86b(%rip), %rdi      # 0xd041
movq %r13, %rsi
pushq $0x7
popq %rdx
movq %r14, %rcx
callq 0x11c0
movq %rbx, %rdi
callq 0x1090
movq %rbx, %rdi
movq %r13, %rsi
movq %rax, %rdx
movq %r14, %rcx
callq 0x11c0
movq %r12, %rdi
callq 0x1090
movslq %eax, %rdx
movq %r12, %rdi
jmp 0x1846
leaq 0xb82d(%rip), %rdi      # 0xd041
movq %r13, %rsi
pushq $0x7
popq %rdx
movq %r14, %rcx
callq 0x11c0
movq %r12, %rdi
callq 0x1090
movq %r12, %rdi
incq %rdi
shlq $0x20, %rax
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
leaq (%rax,%rcx), %rdx
sarq $0x20, %rdx
movq %r13, %rsi
movq %r14, %rcx
callq 0x11c0
pushq $0x20
popq %rdi
movq %r14, %rsi
callq 0x10d0
movq %rbp, %rdi
movq %r13, %rsi
movq %r15, %rdx
movq %r14, %rcx
callq 0x11c0
movq %r12, %rdi
callq 0x1030
movq 0x1189c(%rip), %rax     # 0x13118
addq $0x8, %rax
jmp 0x1743
leaq 0x11884(%rip), %rax     # 0x13110
pushq $0x1
popq %r13
pushq $0x2
popq %rbp
movq (%rax), %rax
movq %rax, 0x1187b(%rip)     # 0x13118
testq %rax, %rax
je 0x19b4
cmpl $0x19, 0x10(%rax)
jne 0x19ab
movq 0x6c50(%rax), %rdi
callq 0x1372
movq %rax, %r15
movq %rax, %rdi
callq 0x1090
testl %eax, %eax
movl $0x0, %ecx
cmovlel %ecx, %eax
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x18ed
leaq 0x1(%rcx), %rdx
cmpb $0x29, (%r15,%rcx)
movq %rdx, %rcx
jne 0x18d3
movw $0x3b, (%r15,%rdx)
movq %r15, %rdi
movq %rbx, %rsi
callq 0xbe02
movq %rbx, %rdi
leaq 0xb783(%rip), %rsi      # 0xd085
callq 0x1100
movl %eax, %r12d
leaq 0xb730(%rip), %rdi      # 0xd041
movq %r13, %rsi
pushq $0x7
popq %rdx
movq %r14, %rcx
callq 0x11c0
testl %r12d, %r12d
je 0x194d
movq %rbx, %rdi
callq 0x1090
movq %rbx, %rdi
movq %r13, %rsi
movq %rax, %rdx
movq %r14, %rcx
callq 0x11c0
movq %r15, %rdi
callq 0x1090
movslq %eax, %rdx
movq %r15, %rdi
jmp 0x1971
movq %r15, %rdi
callq 0x1090
movq %r15, %rdi
incq %rdi
shlq $0x20, %rax
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
leaq (%rax,%rcx), %rdx
sarq $0x20, %rdx
movq %r13, %rsi
movq %r14, %rcx
callq 0x11c0
pushq $0x20
popq %rdi
movq %r14, %rsi
callq 0x10d0
leaq 0xd613(%rip), %rdi      # 0xefa1
movq %r13, %rsi
movq %rbp, %rdx
movq %r14, %rcx
callq 0x11c0
movq %r15, %rdi
callq 0x1030
movq 0x1176d(%rip), %rax     # 0x13118
addq $0x8, %rax
jmp 0x1893
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