// File: /workspace/github_c_makefile_star2_O0/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 %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movw %si, %ax
movq %rdi, -0x10(%rbp)
movw %ax, -0x12(%rbp)
movl %edx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x20(%rbp), %rax
movl $0x0, (%rax)
movq -0x28(%rbp), %rax
movl $0x0, (%rax)
movzwl -0x12(%rbp), %eax
cmpl $0x8000, %eax           # imm = 0x8000
jge 0x19a8b9
movl -0x18(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x19a85c
movzwl -0x12(%rbp), %eax
cmpl $0x5c, %eax
jne 0x19a83b
movq -0x20(%rbp), %rax
movl $0xa5, (%rax)
movl $0x1, -0x4(%rbp)
jmp 0x19a9e6
movzwl -0x12(%rbp), %eax
cmpl $0x7e, %eax
jne 0x19a85a
movq -0x20(%rbp), %rax
movl $0x203e, (%rax)         # imm = 0x203E
movl $0x1, -0x4(%rbp)
jmp 0x19a9e6
jmp 0x19a85c
movzwl -0x12(%rbp), %eax
cmpl $0x7f, %eax
jg 0x19a87b
movzwl -0x12(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movl $0x1, -0x4(%rbp)
jmp 0x19a9e6
movzwl -0x12(%rbp), %eax
cmpl $0xa1, %eax
jl 0x19a8ad
movzwl -0x12(%rbp), %eax
cmpl $0xdf, %eax
jg 0x19a8ad
movzwl -0x12(%rbp), %ecx
addl $0xfec0, %ecx           # imm = 0xFEC0
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movl $0x1, -0x4(%rbp)
jmp 0x19a9e6
movl $0x0, -0x4(%rbp)
jmp 0x19a9e6
movq -0x10(%rbp), %rax
cmpq $0x0, 0x2e0(%rax)
jne 0x19a8d0
movq -0x10(%rbp), %rdi
callq 0x19a9f0
movq -0x10(%rbp), %rax
movq 0x2e0(%rax), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x19a8f2
movl $0x0, -0x4(%rbp)
jmp 0x19a9e6
movq -0x30(%rbp), %rax
movzwl -0x12(%rbp), %ecx
movzbl (%rax,%rcx), %ecx
shll $0x8, %ecx
movq -0x30(%rbp), %rax
movzwl -0x12(%rbp), %edx
addl $0x8000, %edx           # imm = 0x8000
movslq %edx, %rdx
movzbl (%rax,%rdx), %eax
orl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movq -0x30(%rbp), %rax
movzwl -0x12(%rbp), %ecx
subl $0x8000, %ecx           # imm = 0x8000
movslq %ecx, %rcx
movb (%rax,%rcx), %al
movb %al, -0x31(%rbp)
movzbl -0x31(%rbp), %eax
cmpl $0x0, %eax
jne 0x19a94a
movl $0x1, -0x4(%rbp)
jmp 0x19a9e6
movzbl -0x31(%rbp), %eax
cmpl $0x10, %eax
jg 0x19a96b
movzbl -0x31(%rbp), %ecx
shll $0x10, %ecx
movq -0x20(%rbp), %rax
orl (%rax), %ecx
movl %ecx, (%rax)
movl $0x1, -0x4(%rbp)
jmp 0x19a9e6
movzbl -0x31(%rbp), %eax
addl $0xffffff10, %eax       # imm = 0xFFFFFF10
movl %eax, %ecx
movq %rcx, -0x40(%rbp)
subl $0x4, %eax
ja 0x19a9cd
movq -0x40(%rbp), %rax
leaq 0x3eb66(%rip), %rcx     # 0x1d94f0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x28(%rbp), %rax
movl $0x2e5, (%rax)          # imm = 0x2E5
jmp 0x19a9cd
movq -0x28(%rbp), %rax
movl $0x2e9, (%rax)          # imm = 0x2E9
jmp 0x19a9cd
movq -0x28(%rbp), %rax
movl $0x300, (%rax)          # imm = 0x300
jmp 0x19a9cd
movq -0x28(%rbp), %rax
movl $0x301, (%rax)          # imm = 0x301
jmp 0x19a9cd
movq -0x28(%rbp), %rax
movl $0x309a, (%rax)         # imm = 0x309A
movq -0x28(%rbp), %rax
cmpl $0x0, (%rax)
je 0x19a9df
movl $0x2, -0x4(%rbp)
jmp 0x19a9e6
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nop