// File: rofl0r[P]ascc/eval_if.c
// Function: evaluate_condition
static int evaluate_condition(struct tokenizer *t, int *result) {
	int ret, backslash_seen = 0;
	struct token curr;
	char buf[1024], *bufp = buf;
	*bufp = 0;
#if 0
	ret = tokenizer_next(t, &curr);
	if(!ret) return ret;
	if(!is_whitespace_token(&curr)) {
		error("expected whitespace after if/elif");
		return 0;
	}
#endif
	// first pass: expand all macros and defined(foo)
	while(1) {
		ret = tokenizer_next(t, &curr);
		if(!ret) return ret;
		if(curr.type == TT_EOF) break;
		if(curr.type == TT_IDENTIFIER) {
			if(!strcmp(t->buf, "defined")) {
				ret = tokenizer_next(t, &curr);
				if(!ret || !(curr.type == TT_SEP && curr.value == '(')) {
					error("expected ( after defined");
					return ret;
				}
				ret = tokenizer_next(t, &curr);
				if(!ret || curr.type != TT_IDENTIFIER) {
					error("expected identifier after defined(");
					return ret;
				}
				char *ou = defined_macro(t->buf) ? "1" : "0";
				strcat(bufp, ou);
				ret = tokenizer_next(t, &curr);
				if(!ret || !(curr.type == TT_SEP && curr.value == ')')) {
					error("expected ) after defined(ID");
					return ret;
				}
			} else
				expand_macro(t->buf, bufp);
			bufp += strlen(bufp);
		} else if(curr.type == TT_SEP) {
			if(curr.value == '\\')
				backslash_seen = 1;
			else {
				if(curr.value == '\n') {
					if(!backslash_seen) break;
				} else {
					bufp = emit_token(bufp, &curr, t->buf);
				}
				backslash_seen = 0;
			}
		} else {
			bufp = emit_token(bufp, &curr, t->buf);
		}
	}
	if(strlen(buf) == 0) {
		error("#(el)if with no expression");
		return 0;
	}
#ifdef DEBUG
	dprintf(2, "evaluating condition %s\n", bufp);
#endif
	// second pass: evaluate expanded contents
	struct tokenizer t2;
	FILE *f = tokenizer_from_string(&t2, buf);
	ret = do_eval(&t2, result);
	fclose(f);
	return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x15d0, %rsp           # imm = 0x15D0
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl $0x0, -0x20(%rbp)
leaq -0x430(%rbp), %rax
movq %rax, -0x438(%rbp)
movq -0x438(%rbp), %rax
movb $0x0, (%rax)
movq -0x10(%rbp), %rdi
leaq -0x30(%rbp), %rsi
xorl %edx, %edx
callq 0x2f670
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jne 0x2f425
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x2f65a
cmpl $0xe, -0x30(%rbp)
jne 0x2f430
jmp 0x2f5f4
cmpl $0x1, -0x30(%rbp)
jne 0x2f57b
movq -0x10(%rbp), %rdi
addq $0x120, %rdi            # imm = 0x120
leaq 0x5300(%rip), %rsi      # 0x3474c
callq 0x4510
cmpl $0x0, %eax
jne 0x2f548
movq -0x10(%rbp), %rdi
leaq -0x30(%rbp), %rsi
xorl %edx, %edx
callq 0x2f670
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x2f47e
cmpl $0x9, -0x30(%rbp)
jne 0x2f47e
cmpl $0x28, -0x24(%rbp)
je 0x2f495
leaq 0x52b6(%rip), %rdi      # 0x3473b
callq 0x2c740
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x2f65a
movq -0x10(%rbp), %rdi
leaq -0x30(%rbp), %rsi
xorl %edx, %edx
callq 0x2f670
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x2f4b3
cmpl $0x1, -0x30(%rbp)
je 0x2f4ca
leaq 0x529a(%rip), %rdi      # 0x34754
callq 0x2c740
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x2f65a
movq -0x10(%rbp), %rdi
addq $0x120, %rdi            # imm = 0x120
callq 0x2c710
movl %eax, %edx
leaq 0x5137(%rip), %rax      # 0x3461a
leaq 0x3b76(%rip), %rcx      # 0x33060
cmpl $0x0, %edx
cmovneq %rcx, %rax
movq %rax, -0x440(%rbp)
movq -0x438(%rbp), %rdi
movq -0x440(%rbp), %rsi
callq 0x4690
movq -0x10(%rbp), %rdi
leaq -0x30(%rbp), %rsi
xorl %edx, %edx
callq 0x2f670
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x2f52f
cmpl $0x9, -0x30(%rbp)
jne 0x2f52f
cmpl $0x29, -0x24(%rbp)
je 0x2f546
leaq 0x5241(%rip), %rdi      # 0x34777
callq 0x2c740
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x2f65a
jmp 0x2f55f
movq -0x10(%rbp), %rdi
addq $0x120, %rdi            # imm = 0x120
movq -0x438(%rbp), %rsi
callq 0x2c6b0
movq -0x438(%rbp), %rdi
callq 0x41d0
addq -0x438(%rbp), %rax
movq %rax, -0x438(%rbp)
jmp 0x2f5ef
cmpl $0x9, -0x30(%rbp)
jne 0x2f5cb
cmpl $0x5c, -0x24(%rbp)
jne 0x2f590
movl $0x1, -0x20(%rbp)
jmp 0x2f5c9
cmpl $0xa, -0x24(%rbp)
jne 0x2f5a0
cmpl $0x0, -0x20(%rbp)
jne 0x2f59e
jmp 0x2f5f4
jmp 0x2f5c2
movq -0x438(%rbp), %rdi
movq -0x10(%rbp), %rdx
addq $0x120, %rdx            # imm = 0x120
leaq -0x30(%rbp), %rsi
callq 0x2f280
movq %rax, -0x438(%rbp)
movl $0x0, -0x20(%rbp)
jmp 0x2f5ed
movq -0x438(%rbp), %rdi
movq -0x10(%rbp), %rdx
addq $0x120, %rdx            # imm = 0x120
leaq -0x30(%rbp), %rsi
callq 0x2f280
movq %rax, -0x438(%rbp)
jmp 0x2f5ef
jmp 0x2f402
leaq -0x430(%rbp), %rdi
callq 0x41d0
cmpq $0x0, %rax
jne 0x2f61b
leaq 0x5186(%rip), %rdi      # 0x34793
callq 0x2c740
movl $0x0, -0x4(%rbp)
jmp 0x2f65a
leaq -0x430(%rbp), %rsi
leaq -0x15c8(%rbp), %rdi
callq 0x2f370
movq %rax, -0x15d0(%rbp)
movq -0x18(%rbp), %rsi
leaq -0x15c8(%rbp), %rdi
callq 0x2f710
movl %eax, -0x1c(%rbp)
movq -0x15d0(%rbp), %rdi
callq 0x4370
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x15d0, %rsp           # imm = 0x15D0
popq %rbp
retq
nopw %cs:(%rax,%rax)