// File: /workspace/github_c_makefile_star2_O3/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
xorl %eax, %eax
movl %eax, (%rcx)
movl %eax, (%r8)
testw %r15w, %r15w
js 0x10f17e
testb $0x1, %dl
je 0x10f157
cmpw $0x7e, %r15w
je 0x10f21d
movzwl %r15w, %eax
cmpl $0x5c, %eax
jne 0x10f157
movl $0xa5, (%rbx)
jmp 0x10f223
cmpw $0x7f, %r15w
jbe 0x10f176
leal -0xa1(%r15), %eax
cmpw $0x3e, %ax
ja 0x10f219
addl $0xfec0, %r15d          # imm = 0xFEC0
movl %r15d, (%rbx)
jmp 0x10f223
movq %r8, %r14
movq %rdi, %r12
movq 0x2e0(%rdi), %r13
testq %r13, %r13
jne 0x10f1df
movl $0x18000, %esi          # imm = 0x18000
movq %r12, %rdi
callq 0xe8b2b
movq %rax, %r13
leaq 0x3e789(%rip), %rsi     # 0x14d930
movl $0x44cb, %edx           # imm = 0x44CB
movl $0x18000, %r8d          # imm = 0x18000
movq %r12, %rdi
movq %rax, %rcx
callq 0x10f01c
testl %eax, %eax
jne 0x10f1d2
leaq 0x42c33(%rip), %rsi     # 0x151dfb
movq %r12, %rdi
xorl %eax, %eax
callq 0xe9544
movq %r13, 0x2e0(%r12)
testq %r13, %r13
je 0x10f219
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
movl $0x1, %eax
testl %ecx, %ecx
je 0x10f228
cmpb $0x10, %cl
ja 0x10f232
shll $0x10, %ecx
orl %edx, %ecx
movl %ecx, (%rbx)
jmp 0x10f228
xorl %eax, %eax
jmp 0x10f228
movl $0x203e, (%rbx)         # imm = 0x203E
movl $0x1, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
addl $0xffffff10, %ecx       # imm = 0xFFFFFF10
cmpl $0x4, %ecx
ja 0x10f27f
leaq 0x3e6cc(%rip), %rax     # 0x14d910
movslq (%rax,%rcx,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movl $0x2e5, (%r14)          # imm = 0x2E5
jmp 0x10f278
movl $0x309a, (%r14)         # imm = 0x309A
jmp 0x10f278
movl $0x300, (%r14)          # imm = 0x300
jmp 0x10f278
movl $0x301, (%r14)          # imm = 0x301
jmp 0x10f278
movl $0x2e9, (%r14)          # imm = 0x2E9
movl $0x2, %eax
jmp 0x10f228
xorl %eax, %eax
cmpl $0x0, (%r14)
setne %al
addl %eax, %eax
jmp 0x10f228