// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/src/deark-util2.c
// Function: de_cp932_lookup
int de_cp932_lookup(deark *c, u16 n, UI flags, de_rune *pr1, de_rune *pr2)
{
	u8 *tbl;
	u8 plane_code;

	*pr1 = 0;
	*pr2 = 0;

	if(n<32768) {
		if(flags & 0x1) {
			// This is probably(??) what we want to do most of the time, but
			// until all of Deark's path-separator-detection code is reviewed,
			// it's too risky to turn backslashes into yen signs.
			if(n==0x5c) {
				*pr1 = 0x00a5; // YEN SIGN
				return 1;
			}
			if(n==0x7e) {
				*pr1 = 0x203e; // OVERLINE
				return 1;
			}
		}
		if(n<=0x7f) {
			// TODO: Figure out what DOS does with codes below 0x20. Should we
			// treat them as graphics characters sometimes?
			*pr1 = (de_rune)n;
			return 1;
		}
		if(n>=0xa1 && n<=0xdf) {
			*pr1 = (de_rune)n + (0xff61-0xa1);
			return 1;
		}
		return 0;
	}

	if(!c->persistent_item[DE_PERSISTENT_ITEM_CP932_TBL]) {
		make_cp932_table(c);
	}

	tbl = (u8*)c->persistent_item[DE_PERSISTENT_ITEM_CP932_TBL];
	if(!tbl) {
		return 0;
	}

	*pr1 = ((de_rune)tbl[n]<<8)|tbl[n+32768];

	plane_code = tbl[n-32768];
	if(plane_code==0) return 1;
	if(plane_code<=0x10) {
		*pr1 |= ((de_rune)plane_code<<16);
		return 1;
	}

	// Some hacky special handling of the case where a Shift JIS code
	// maps to *two* Unicode codepoints.
	// E.g. 0x8663 -> U+00E6 U+0300
	switch(plane_code) {
	case 0xf0: *pr2 = 0x02e5; break;
	case 0xf1: *pr2 = 0x02e9; break;
	case 0xf2: *pr2 = 0x0300; break;
	case 0xf3: *pr2 = 0x0301; break;
	case 0xf4: *pr2 = 0x309a; break;
	}
	if(*pr2 != 0) return 2;
	return 0;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rcx, %rbx
movl %esi, %r15d
andl $0x0, (%rcx)
andl $0x0, (%r8)
testw %r15w, %r15w
js 0x101e94
testb $0x1, %dl
je 0x101e6d
cmpw $0x7e, %r15w
je 0x101f31
movzwl %r15w, %eax
cmpl $0x5c, %eax
jne 0x101e6d
movl $0xa5, (%rbx)
jmp 0x101f37
cmpw $0x7f, %r15w
jbe 0x101e8c
leal -0xa1(%r15), %eax
cmpw $0x3e, %ax
ja 0x101f2d
addl $0xfec0, %r15d          # imm = 0xFEC0
movl %r15d, (%rbx)
jmp 0x101f37
movq %r8, %r14
movq %rdi, %r12
movq 0x2e0(%rdi), %r13
testq %r13, %r13
jne 0x101ef5
movl $0x18000, %esi          # imm = 0x18000
movq %r12, %rdi
callq 0xdecaf
movq %rax, %r13
leaq 0x3e563(%rip), %rsi     # 0x140420
movl $0x44cb, %edx           # imm = 0x44CB
movl $0x18000, %r8d          # imm = 0x18000
movq %r12, %rdi
movq %rax, %rcx
callq 0x101d34
testl %eax, %eax
jne 0x101ee8
leaq 0x42a0d(%rip), %rsi     # 0x1448eb
movq %r12, %rdi
xorl %eax, %eax
callq 0xdf4f0
movq %r13, 0x2e0(%r12)
testq %r13, %r13
je 0x101f2d
movzwl %r15w, %eax
movzbl (%r13,%rax), %ecx
shll $0x8, %ecx
movzbl 0x8000(%r13,%rax), %edx
orl %ecx, %edx
movl %edx, (%rbx)
movzbl -0x8000(%r13,%rax), %ecx
pushq $0x1
popq %rax
testl %ecx, %ecx
je 0x101f3a
cmpb $0x10, %cl
ja 0x101f44
shll $0x10, %ecx
orl %edx, %ecx
movl %ecx, (%rbx)
jmp 0x101f3a
xorl %eax, %eax
jmp 0x101f3a
movl $0x203e, (%rbx)         # imm = 0x203E
pushq $0x1
popq %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
addl $0xffffff10, %ecx       # imm = 0xFFFFFF10
cmpl $0x4, %ecx
ja 0x101f8e
leaq 0x3e4aa(%rip), %rax     # 0x140400
movslq (%rax,%rcx,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movl $0x2e5, (%r14)          # imm = 0x2E5
jmp 0x101f8a
movl $0x309a, (%r14)         # imm = 0x309A
jmp 0x101f8a
movl $0x300, (%r14)          # imm = 0x300
jmp 0x101f8a
movl $0x301, (%r14)          # imm = 0x301
jmp 0x101f8a
movl $0x2e9, (%r14)          # imm = 0x2E9
pushq $0x2
jmp 0x101f39
xorl %eax, %eax
cmpl $0x0, (%r14)
setne %al
addl %eax, %eax
jmp 0x101f3a
nop