// File: ksherlock[P]wdc-utils/link.cpp
// Function: simplify()
void simplify() {
	for (auto &s : sections) {
		for (auto &e : s.expressions) {
			bool delta = false;

			// first check for undefined symbols.
			if (e.undefined) {
				e.undefined = false;
				for (auto &t : e.stack) {

					if (t.tag == OP_SYM) {
						const auto &ss = symbols[t.section];
						switch(ss.type & 0x0f) {
							case S_UND:
								e.undefined = true;
								break;
							case S_REL:
								t = expr{OP_LOC, ss.offset, (uint32_t)ss.section};
								delta = true;
								break;
							case S_ABS:
								t = expr{OP_VAL, (uint32_t)ss.offset};
								delta = true;
								break;
						}
					}
				}
			}
			if (e.stack.size() > 1) simplify_expression(e);
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
leaq 0x3b5ce(%rip), %rax     # 0x3f3e0
movq %rax, -0x8(%rbp)
leaq 0x3b5c3(%rip), %rdi     # 0x3f3e0
callq 0x9d70
movq %rax, -0x10(%rbp)
leaq 0x3b5b3(%rip), %rdi     # 0x3f3e0
callq 0x9da0
movq %rax, -0x18(%rbp)
leaq -0x10(%rbp), %rdi
leaq -0x18(%rbp), %rsi
callq 0x9dd0
testb $0x1, %al
jne 0x3e4c
jmp 0x4019
leaq -0x10(%rbp), %rdi
callq 0x9e10
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
addq $0x48, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
callq 0x9e30
movq %rax, -0x30(%rbp)
movq -0x28(%rbp), %rdi
callq 0x9e60
movq %rax, -0x38(%rbp)
leaq -0x30(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x9e90
testb $0x1, %al
jne 0x3e95
jmp 0x4009
leaq -0x30(%rbp), %rdi
callq 0x9ed0
movq %rax, -0x40(%rbp)
movb $0x0, -0x41(%rbp)
movq -0x40(%rbp), %rax
testb $0x1, 0xa(%rax)
je 0x3fdd
movq -0x40(%rbp), %rax
movb $0x0, 0xa(%rax)
movq -0x40(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
callq 0x9ef0
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rdi
callq 0x9f20
movq %rax, -0x60(%rbp)
leaq -0x58(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x9f50
testb $0x1, %al
jne 0x3ef8
jmp 0x3fdb
leaq -0x58(%rbp), %rdi
callq 0x9f90
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpl $0x1, (%rax)
jne 0x3fcb
movq -0x68(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, %esi
leaq 0x3b50e(%rip), %rdi     # 0x3f430
callq 0x9fb0
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movzbl 0x20(%rax), %eax
andl $0xf, %eax
movl %eax, -0x8c(%rbp)
je 0x3f5a
jmp 0x3f40
movl -0x8c(%rbp), %eax
subl $0x1, %eax
je 0x3f97
jmp 0x3f4d
movl -0x8c(%rbp), %eax
subl $0x2, %eax
je 0x3f64
jmp 0x3fc9
movq -0x40(%rbp), %rax
movb $0x1, 0xa(%rax)
jmp 0x3fc9
movq -0x70(%rbp), %rax
movl 0x24(%rax), %edx
movq -0x70(%rbp), %rax
movl 0x28(%rax), %ecx
leaq -0x7c(%rbp), %rdi
movl $0x3, %esi
callq 0x9fd0
movq -0x68(%rbp), %rax
movq -0x7c(%rbp), %rcx
movq %rcx, (%rax)
movl -0x74(%rbp), %ecx
movl %ecx, 0x8(%rax)
movb $0x1, -0x41(%rbp)
jmp 0x3fc9
movq -0x70(%rbp), %rax
movl 0x24(%rax), %edx
leaq -0x88(%rbp), %rdi
movl $0x2, %esi
xorl %ecx, %ecx
callq 0x9fd0
movq -0x68(%rbp), %rax
movq -0x88(%rbp), %rcx
movq %rcx, (%rax)
movl -0x80(%rbp), %ecx
movl %ecx, 0x8(%rax)
movb $0x1, -0x41(%rbp)
jmp 0x3fcb
jmp 0x3fcd
leaq -0x58(%rbp), %rdi
callq 0xa000
jmp 0x3ee2
jmp 0x3fdd
movq -0x40(%rbp), %rdi
addq $0x10, %rdi
callq 0xa020
cmpq $0x1, %rax
jbe 0x3ff9
movq -0x40(%rbp), %rdi
callq 0x22140
jmp 0x3ffb
leaq -0x30(%rbp), %rdi
callq 0xa050
jmp 0x3e7f
jmp 0x400b
leaq -0x10(%rbp), %rdi
callq 0xa070
jmp 0x3e36
addq $0x90, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)