// File: /workspace/github_c_makefile_star2_O1/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 0x10f206
testb $0x1, %dl
je 0x10f1df
cmpw $0x7e, %r15w
je 0x10f2a7
movzwl %r15w, %eax
cmpl $0x5c, %eax
jne 0x10f1df
movl $0xa5, (%rbx)
jmp 0x10f2ad
cmpw $0x7f, %r15w
jbe 0x10f1fe
leal -0xa1(%r15), %eax
cmpw $0x3e, %ax
ja 0x10f2a3
addl $0xfec0, %r15d          # imm = 0xFEC0
movl %r15d, (%rbx)
jmp 0x10f2ad
movq %r8, %r14
movq %rdi, %r12
cmpq $0x0, 0x2e0(%rdi)
jne 0x10f260
movl $0x18000, %esi          # imm = 0x18000
movq %r12, %rdi
callq 0xe969b
movq %rax, %r13
leaq 0x3e0c3(%rip), %rsi     # 0x14d2f0
movl $0x44cb, %edx           # imm = 0x44CB
movl $0x18000, %r8d          # imm = 0x18000
movq %r12, %rdi
movq %rax, %rcx
callq 0x10f0a4
testl %eax, %eax
jne 0x10f258
leaq 0x42582(%rip), %rsi     # 0x1517d0
movq %r12, %rdi
xorl %eax, %eax
callq 0xea0a6
movq %r13, 0x2e0(%r12)
movq 0x2e0(%r12), %rax
testq %rax, %rax
je 0x10f2a3
movzwl %r15w, %ecx
movzbl (%rax,%rcx), %esi
shll $0x8, %esi
movzbl 0x8000(%rax,%rcx), %edx
orl %esi, %edx
movl %edx, (%rbx)
movzbl -0x8000(%rax,%rcx), %ecx
movl $0x1, %eax
testl %ecx, %ecx
je 0x10f2b2
cmpb $0x10, %cl
ja 0x10f2bc
shll $0x10, %ecx
orl %edx, %ecx
movl %ecx, (%rbx)
jmp 0x10f2b2
xorl %eax, %eax
jmp 0x10f2b2
movl $0x203e, (%rbx)         # imm = 0x203E
movl $0x1, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
addb $0x10, %cl
cmpb $0x4, %cl
ja 0x10f2d4
movzbl %cl, %eax
leaq 0x424ee(%rip), %rcx     # 0x1517bc
movl (%rcx,%rax,4), %eax
movl %eax, (%r14)
xorl %eax, %eax
cmpl $0x0, (%r14)
setne %al
addl %eax, %eax
jmp 0x10f2b2
nopl (%rax)