// File: rofl0r[P]ascc/cs_prepro.cpp
// Function: pre_process_directive(char*, FMEM*)
void pre_process_directive(char*dirpt,FMEM*outfl) {
	char *p, *shal=dirpt+1;
	while(is_whitespace(*shal)) ++shal;
	p = shal;
	// seek to the end of the first word (eg. #define)
	while ((!is_whitespace(shal[0])) && (shal[0]!=0)) shal++;
	// extract the directive name
	int shalwas = shal[0];
	shal[0] = 0;
	char dname[150];
	snprintf(dname, sizeof dname, "%s", p);
	ags_strlwr(dname);
	shal[0] = shalwas;

	// skip to the next word on the line
	skip_whitespace(&shal);

	// write a blank line to the output so that line numbers are correct
	fmem_puts("",outfl);

	size_t l = strlen(dname);
	while(l && strchr(" \t\r\n", dname[l-1])) dname[--l] = 0;

	int did = directive_to_id(dname);
	if(skip_conditional_block) switch(did) {
	case di_include: case di_error: case di_warning:
	case di_define: case di_undef:
	case di_line: case di_pragma:
		return;
	default: break;
	}
	switch(did) {
	case di_error:
		cc_error("User error: %s", shal);
		return;
	case di_define: {
		char nambit[100];
		int nin=0;
		while ((!isspace(shal[0])) && (shal[0]!=0)) {
			nambit[nin]=shal[0];
			nin++;
			shal++;
		}
		nambit[nin]=0;
		skip_whitespace(&shal);
		macros.add(nambit,shal);
		} break;
	case di_undef: {
		char macroToCheck[100];
		get_next_word(shal, macroToCheck);
		if (macroToCheck[0] == 0) {
			cc_error("Expected: macro");
			return;
		}

		int idx = macros.find_name(macroToCheck);
		if (idx < 0) {
			cc_error("'%s' not defined", macroToCheck);
			return;
		}

		macros.remove(idx);
		} break;
	case di_if:
		if(all_levels_active()) {
			int wantDefined = evaluate_if_statement(shal);
			set_level(if_level + 1, wantDefined);
		} else
			set_level(if_level + 1, 0);
		break;
	case di_elif:
		if(prev_level_active() && if_level_satisfied < if_level) {
			int wantDefined = evaluate_if_statement(shal);
			if(wantDefined) {
				if_level_active = if_level;
				if_level_satisfied = if_level;
			}
		} else if(if_level_active == if_level) {
			--if_level_active;
		}
		break;
	case di_else:
		if(prev_level_active() && if_level_satisfied < if_level) {
			if(1) {
				if_level_active = if_level;
				if_level_satisfied = if_level;
			}
		} else if(if_level_active == if_level) {
			--if_level_active;
		}
		break;
	case di_ifdef:
	case di_ifndef:
	case di_ifver:
	case di_ifnver: {
		int isVersionCheck = (did == di_ifver || did == di_ifnver);
		int wantDefined, neg = (did == di_ifndef || did == di_ifnver);

		char macroToCheck[100];
		get_next_word(shal, macroToCheck, true);

		if (macroToCheck[0] == 0) {
			cc_error("Expected token after space");
			return;
		}

		if ((isVersionCheck) && (!is_digit(macroToCheck[0]))) {
			cc_error("Expected version number");
			return;
		}

		if (isVersionCheck)
			wantDefined = (strcmp(ccSoftwareVersion, macroToCheck) >= 0);
		else
			wantDefined = macros.find_name(macroToCheck) >= 0;

		if (neg) wantDefined = !wantDefined;

		if(all_levels_active()) {
			set_level(if_level + 1, wantDefined);
		} else {
			set_level(if_level + 1, 0);
		}
		} break;
	case di_endif:
		if (if_level < 1) {
			cc_error("#endif without #if");
			return;
		}
		set_level(if_level-1, -1);
		break;
	case di_sectionstart:
	case di_sectionend:
		// do nothing - markers for the editor, just remove them
		break;
	default:
		cc_error("unknown/unimplemented preprocessor directive '%s'",dname);
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x240, %rsp            # imm = 0x240
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movsbl (%rax), %edi
callq 0x26240
cmpl $0x0, %eax
je 0x2c79e
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x2c77f
movq -0x20(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x20(%rbp), %rax
movsbl (%rax), %edi
callq 0x26240
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x221(%rbp)
jne 0x2c7d4
movq -0x20(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
setne %al
movb %al, -0x221(%rbp)
movb -0x221(%rbp), %al
testb $0x1, %al
jne 0x2c7e0
jmp 0x2c7ee
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x2c7a6
movq -0x20(%rbp), %rax
movsbl (%rax), %eax
movl %eax, -0x24(%rbp)
movq -0x20(%rbp), %rax
movb $0x0, (%rax)
leaq -0xc0(%rbp), %rdi
movq -0x18(%rbp), %rcx
movl $0x96, %esi
leaq 0x6eb2(%rip), %rdx      # 0x336c8
movb $0x0, %al
callq 0x45f0
leaq -0xc0(%rbp), %rdi
callq 0x27960
movl -0x24(%rbp), %eax
movb %al, %cl
movq -0x20(%rbp), %rax
movb %cl, (%rax)
leaq -0x20(%rbp), %rdi
callq 0x26290
movq -0x10(%rbp), %rsi
leaq 0x7924(%rip), %rdi      # 0x3416c
callq 0x24f00
leaq -0xc0(%rbp), %rdi
callq 0x41d0
movq %rax, -0xc8(%rbp)
xorl %eax, %eax
cmpq $0x0, -0xc8(%rbp)
movb %al, -0x222(%rbp)
je 0x2c89e
movq -0xc8(%rbp), %rax
subq $0x1, %rax
movsbl -0xc0(%rbp,%rax), %esi
leaq 0x78dc(%rip), %rdi      # 0x34168
callq 0x4180
cmpq $0x0, %rax
setne %al
movb %al, -0x222(%rbp)
movb -0x222(%rbp), %al
testb $0x1, %al
jne 0x2c8aa
jmp 0x2c8c9
movq -0xc8(%rbp), %rax
movq %rax, %rcx
addq $-0x1, %rcx
movq %rcx, -0xc8(%rbp)
movb $0x0, -0xc1(%rbp,%rax)
jmp 0x2c860
leaq -0xc0(%rbp), %rdi
callq 0x2d000
movl %eax, -0xcc(%rbp)
movl 0x21d37(%rip), %eax     # 0x4e618
cmpl 0x21d35(%rip), %eax     # 0x4e61c
jle 0x2c915
movl -0xcc(%rbp), %eax
movl %eax, -0x228(%rbp)
subl $0x5, %eax
jb 0x2c90c
jmp 0x2c8fc
movl -0x228(%rbp), %eax
addl $-0xb, %eax
subl $0x1, %eax
ja 0x2c911
jmp 0x2c90c
jmp 0x2cff1
jmp 0x2c913
jmp 0x2c915
movl -0xcc(%rbp), %eax
decl %eax
movl %eax, %ecx
movq %rcx, -0x230(%rbp)
subl $0xf, %eax
ja 0x2cfdc
movq -0x230(%rbp), %rax
leaq 0x77eb(%rip), %rcx      # 0x34128
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x20(%rbp), %rsi
leaq 0x781c(%rip), %rdi      # 0x3416d
movb $0x0, %al
callq 0x1bac0
jmp 0x2cff1
movl $0x0, -0x134(%rbp)
movq -0x20(%rbp), %rax
movsbl (%rax), %edi
callq 0x40a0
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x231(%rbp)
jne 0x2c995
movq -0x20(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
setne %al
movb %al, -0x231(%rbp)
movb -0x231(%rbp), %al
testb $0x1, %al
jne 0x2c9a1
jmp 0x2c9d2
movq -0x20(%rbp), %rax
movb (%rax), %cl
movslq -0x134(%rbp), %rax
movb %cl, -0x130(%rbp,%rax)
movl -0x134(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x134(%rbp)
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x2c967
movslq -0x134(%rbp), %rax
movb $0x0, -0x130(%rbp,%rax)
leaq -0x20(%rbp), %rdi
callq 0x26290
leaq -0x130(%rbp), %rsi
movq -0x20(%rbp), %rdx
leaq 0x1be54(%rip), %rdi     # 0x48850
callq 0x2c290
jmp 0x2cff1
movq -0x20(%rbp), %rdi
leaq -0x1a0(%rbp), %rsi
xorl %edx, %edx
callq 0x2c4c0
movsbl -0x1a0(%rbp), %eax
cmpl $0x0, %eax
jne 0x2ca37
leaq 0x7751(%rip), %rdi      # 0x3417c
movb $0x0, %al
callq 0x1bac0
jmp 0x2cff1
leaq -0x1a0(%rbp), %rsi
leaq 0x1be0b(%rip), %rdi     # 0x48850
callq 0x2c3f0
movl %eax, -0x1a4(%rbp)
cmpl $0x0, -0x1a4(%rbp)
jge 0x2ca73
leaq -0x1a0(%rbp), %rsi
leaq 0x7725(%rip), %rdi      # 0x3418c
movb $0x0, %al
callq 0x1bac0
jmp 0x2cff1
movl -0x1a4(%rbp), %esi
leaq 0x1bdd0(%rip), %rdi     # 0x48850
callq 0x2c460
jmp 0x2cff1
movl 0x21b8c(%rip), %eax     # 0x4e61c
cmpl 0x21b82(%rip), %eax     # 0x4e618
jne 0x2cb77
movq -0x20(%rbp), %rdi
callq 0x2f2f0
movl %eax, -0x1a8(%rbp)
movl 0x21b6b(%rip), %eax     # 0x4e61c
movl 0x21b61(%rip), %ecx     # 0x4e618
addl $0x1, %ecx
cmpl %ecx, %eax
jle 0x2cacd
movl 0x21b54(%rip), %eax     # 0x4e618
addl $0x1, %eax
movl %eax, 0x21b4f(%rip)     # 0x4e61c
movl 0x21b4d(%rip), %eax     # 0x4e620
movl 0x21b3f(%rip), %ecx     # 0x4e618
addl $0x1, %ecx
cmpl %ecx, %eax
jle 0x2caef
movl 0x21b32(%rip), %eax     # 0x4e618
addl $0x1, %eax
movl %eax, 0x21b31(%rip)     # 0x4e620
cmpl $-0x1, -0x1a8(%rbp)
je 0x2cb66
cmpl $0x0, -0x1a8(%rbp)
je 0x2cb12
movl 0x21b11(%rip), %eax     # 0x4e618
addl $0x1, %eax
movl %eax, 0x21b0c(%rip)     # 0x4e61c
jmp 0x2cb39
movl 0x21b04(%rip), %eax     # 0x4e61c
movl 0x21afa(%rip), %ecx     # 0x4e618
addl $0x1, %ecx
cmpl %ecx, %eax
jne 0x2cb37
movl 0x21aed(%rip), %eax     # 0x4e618
addl $0x1, %eax
subl $0x1, %eax
movl %eax, 0x21ae5(%rip)     # 0x4e61c
jmp 0x2cb39
cmpl $0x0, -0x1a8(%rbp)
je 0x2cb64
movl 0x21ad4(%rip), %eax     # 0x4e61c
movl 0x21aca(%rip), %ecx     # 0x4e618
addl $0x1, %ecx
cmpl %ecx, %eax
jne 0x2cb64
movl 0x21abd(%rip), %eax     # 0x4e618
addl $0x1, %eax
movl %eax, 0x21abc(%rip)     # 0x4e620
jmp 0x2cb66
movl 0x21aac(%rip), %eax     # 0x4e618
addl $0x1, %eax
movl %eax, 0x21aa3(%rip)     # 0x4e618
jmp 0x2cbf3
jmp 0x2cb79
movl 0x21a9d(%rip), %eax     # 0x4e61c
movl 0x21a93(%rip), %ecx     # 0x4e618
addl $0x1, %ecx
cmpl %ecx, %eax
jle 0x2cb9b
movl 0x21a86(%rip), %eax     # 0x4e618
addl $0x1, %eax
movl %eax, 0x21a81(%rip)     # 0x4e61c
movl 0x21a7f(%rip), %eax     # 0x4e620
movl 0x21a71(%rip), %ecx     # 0x4e618
addl $0x1, %ecx
cmpl %ecx, %eax
jle 0x2cbbd
movl 0x21a64(%rip), %eax     # 0x4e618
addl $0x1, %eax
movl %eax, 0x21a63(%rip)     # 0x4e620
movl 0x21a59(%rip), %eax     # 0x4e61c
movl 0x21a4f(%rip), %ecx     # 0x4e618
addl $0x1, %ecx
cmpl %ecx, %eax
jne 0x2cbe2
movl 0x21a42(%rip), %eax     # 0x4e618
addl $0x1, %eax
subl $0x1, %eax
movl %eax, 0x21a3a(%rip)     # 0x4e61c
movl 0x21a30(%rip), %eax     # 0x4e618
addl $0x1, %eax
movl %eax, 0x21a27(%rip)     # 0x4e618
jmp 0x2cbf3
jmp 0x2cff1
movl 0x21a1e(%rip), %eax     # 0x4e61c
movl 0x21a14(%rip), %ecx     # 0x4e618
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0x2cc4b
movl 0x21a0f(%rip), %eax     # 0x4e620
cmpl 0x21a01(%rip), %eax     # 0x4e618
jge 0x2cc4b
movq -0x20(%rbp), %rdi
callq 0x2f2f0
movl %eax, -0x1ac(%rbp)
cmpl $0x0, -0x1ac(%rbp)
je 0x2cc49
movl 0x219e1(%rip), %eax     # 0x4e618
movl %eax, 0x219df(%rip)     # 0x4e61c
movl 0x219d5(%rip), %eax     # 0x4e618
movl %eax, 0x219d7(%rip)     # 0x4e620
jmp 0x2cc6a
movl 0x219cb(%rip), %eax     # 0x4e61c
cmpl 0x219c1(%rip), %eax     # 0x4e618
jne 0x2cc68
movl 0x219bd(%rip), %eax     # 0x4e61c
addl $-0x1, %eax
movl %eax, 0x219b4(%rip)     # 0x4e61c
jmp 0x2cc6a
jmp 0x2cff1
movl 0x219a7(%rip), %eax     # 0x4e61c
movl 0x2199d(%rip), %ecx     # 0x4e618
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0x2ccaa
movl 0x21998(%rip), %eax     # 0x4e620
cmpl 0x2198a(%rip), %eax     # 0x4e618
jge 0x2ccaa
movl 0x21982(%rip), %eax     # 0x4e618
movl %eax, 0x21980(%rip)     # 0x4e61c
movl 0x21976(%rip), %eax     # 0x4e618
movl %eax, 0x21978(%rip)     # 0x4e620
jmp 0x2ccc9
movl 0x2196c(%rip), %eax     # 0x4e61c
cmpl 0x21962(%rip), %eax     # 0x4e618
jne 0x2ccc7
movl 0x2195e(%rip), %eax     # 0x4e61c
addl $-0x1, %eax
movl %eax, 0x21955(%rip)     # 0x4e61c
jmp 0x2ccc9
jmp 0x2cff1
movb $0x1, %al
cmpl $0xf, -0xcc(%rbp)
movb %al, -0x232(%rbp)
je 0x2ccef
cmpl $0x10, -0xcc(%rbp)
sete %al
movb %al, -0x232(%rbp)
movb -0x232(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x1b0(%rbp)
movb $0x1, %al
cmpl $0x9, -0xcc(%rbp)
movb %al, -0x233(%rbp)
je 0x2cd21
cmpl $0x10, -0xcc(%rbp)
sete %al
movb %al, -0x233(%rbp)
movb -0x233(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x1b8(%rbp)
movq -0x20(%rbp), %rdi
leaq -0x220(%rbp), %rsi
movl $0x1, %edx
callq 0x2c4c0
movsbl -0x220(%rbp), %eax
cmpl $0x0, %eax
jne 0x2cd66
leaq 0x7443(%rip), %rdi      # 0x3419d
movb $0x0, %al
callq 0x1bac0
jmp 0x2cff1
cmpl $0x0, -0x1b0(%rbp)
je 0x2cd93
movsbl -0x220(%rbp), %edi
callq 0x262e0
cmpl $0x0, %eax
jne 0x2cd93
leaq 0x7431(%rip), %rdi      # 0x341b8
movb $0x0, %al
callq 0x1bac0
jmp 0x2cff1
cmpl $0x0, -0x1b0(%rbp)
je 0x2cdc5
leaq 0x156c5(%rip), %rax     # 0x42468
movq (%rax), %rdi
leaq -0x220(%rbp), %rsi
callq 0x4510
cmpl $0x0, %eax
setge %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x1b4(%rbp)
jmp 0x2cde9
leaq -0x220(%rbp), %rsi
leaq 0x1ba7d(%rip), %rdi     # 0x48850
callq 0x2c3f0
cmpl $0x0, %eax
setge %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x1b4(%rbp)
cmpl $0x0, -0x1b8(%rbp)
je 0x2ce09
cmpl $0x0, -0x1b4(%rbp)
setne %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x1b4(%rbp)
movl 0x2180d(%rip), %eax     # 0x4e61c
cmpl 0x21803(%rip), %eax     # 0x4e618
jne 0x2cee9
jmp 0x2ce1d
movl 0x217f9(%rip), %eax     # 0x4e61c
movl 0x217ef(%rip), %ecx     # 0x4e618
addl $0x1, %ecx
cmpl %ecx, %eax
jle 0x2ce3f
movl 0x217e2(%rip), %eax     # 0x4e618
addl $0x1, %eax
movl %eax, 0x217dd(%rip)     # 0x4e61c
movl 0x217db(%rip), %eax     # 0x4e620
movl 0x217cd(%rip), %ecx     # 0x4e618
addl $0x1, %ecx
cmpl %ecx, %eax
jle 0x2ce61
movl 0x217c0(%rip), %eax     # 0x4e618
addl $0x1, %eax
movl %eax, 0x217bf(%rip)     # 0x4e620
cmpl $-0x1, -0x1b4(%rbp)
je 0x2ced8
cmpl $0x0, -0x1b4(%rbp)
je 0x2ce84
movl 0x2179f(%rip), %eax     # 0x4e618
addl $0x1, %eax
movl %eax, 0x2179a(%rip)     # 0x4e61c
jmp 0x2ceab
movl 0x21792(%rip), %eax     # 0x4e61c
movl 0x21788(%rip), %ecx     # 0x4e618
addl $0x1, %ecx
cmpl %ecx, %eax
jne 0x2cea9
movl 0x2177b(%rip), %eax     # 0x4e618
addl $0x1, %eax
subl $0x1, %eax
movl %eax, 0x21773(%rip)     # 0x4e61c
jmp 0x2ceab
cmpl $0x0, -0x1b4(%rbp)
je 0x2ced6
movl 0x21762(%rip), %eax     # 0x4e61c
movl 0x21758(%rip), %ecx     # 0x4e618
addl $0x1, %ecx
cmpl %ecx, %eax
jne 0x2ced6
movl 0x2174b(%rip), %eax     # 0x4e618
addl $0x1, %eax
movl %eax, 0x2174a(%rip)     # 0x4e620
jmp 0x2ced8
movl 0x2173a(%rip), %eax     # 0x4e618
addl $0x1, %eax
movl %eax, 0x21731(%rip)     # 0x4e618
jmp 0x2cf65
jmp 0x2ceeb
movl 0x2172b(%rip), %eax     # 0x4e61c
movl 0x21721(%rip), %ecx     # 0x4e618
addl $0x1, %ecx
cmpl %ecx, %eax
jle 0x2cf0d
movl 0x21714(%rip), %eax     # 0x4e618
addl $0x1, %eax
movl %eax, 0x2170f(%rip)     # 0x4e61c
movl 0x2170d(%rip), %eax     # 0x4e620
movl 0x216ff(%rip), %ecx     # 0x4e618
addl $0x1, %ecx
cmpl %ecx, %eax
jle 0x2cf2f
movl 0x216f2(%rip), %eax     # 0x4e618
addl $0x1, %eax
movl %eax, 0x216f1(%rip)     # 0x4e620
movl 0x216e7(%rip), %eax     # 0x4e61c
movl 0x216dd(%rip), %ecx     # 0x4e618
addl $0x1, %ecx
cmpl %ecx, %eax
jne 0x2cf54
movl 0x216d0(%rip), %eax     # 0x4e618
addl $0x1, %eax
subl $0x1, %eax
movl %eax, 0x216c8(%rip)     # 0x4e61c
movl 0x216be(%rip), %eax     # 0x4e618
addl $0x1, %eax
movl %eax, 0x216b5(%rip)     # 0x4e618
jmp 0x2cf65
jmp 0x2cff1
cmpl $0x1, 0x216a7(%rip)     # 0x4e618
jge 0x2cf83
leaq 0x7256(%rip), %rdi      # 0x341d0
movb $0x0, %al
callq 0x1bac0
jmp 0x2cff1
jmp 0x2cf85
movl 0x21691(%rip), %eax     # 0x4e61c
movl 0x21687(%rip), %ecx     # 0x4e618
subl $0x1, %ecx
cmpl %ecx, %eax
jle 0x2cfa7
movl 0x2167a(%rip), %eax     # 0x4e618
subl $0x1, %eax
movl %eax, 0x21675(%rip)     # 0x4e61c
movl 0x21673(%rip), %eax     # 0x4e620
movl 0x21665(%rip), %ecx     # 0x4e618
subl $0x1, %ecx
cmpl %ecx, %eax
jle 0x2cfc9
movl 0x21658(%rip), %eax     # 0x4e618
subl $0x1, %eax
movl %eax, 0x21657(%rip)     # 0x4e620
movl 0x21649(%rip), %eax     # 0x4e618
subl $0x1, %eax
movl %eax, 0x21640(%rip)     # 0x4e618
jmp 0x2cff1
jmp 0x2cff1
leaq -0xc0(%rbp), %rsi
leaq 0x71f9(%rip), %rdi      # 0x341e3
movb $0x0, %al
callq 0x1bac0
addq $0x240, %rsp            # imm = 0x240
popq %rbp
retq
nopw (%rax,%rax)