// File: /workspace/github_c_makefile_star2_O1/ughman[P]c2c/src/ns.cc
// Function: NS_13164
uint32_t NS_13164(emuptr<struct nspageinfo> nspageinfo)
{
	int16_t status = nspageinfo->status;
	int32_t unk_28 = nspageinfo->unk_28;
	T0 = 0;
	if (status == 0x3 || status == 0xD)
	{
		if (EMU_ReadU32(0x80068260) && !NS_VerifyChecksum(nspageinfo->page))
		{
			return EMU_Invoke(0x80013148,1,nspageinfo);
		}
		nspageinfo->status = 0x4;
		EMU_Invoke(0x8001254C,1,nspageinfo->page);
		return 0;
	}
	V0 = EMU_ReadU32(0x80067838);
	V0 = EMU_ReadU32(V0 + 1052); //+ 0x41C
	V1 = nspageinfo->pagenum >> 1;
	A2 = ((int32_t)V1 < (int32_t)V0) && !!EMU_ReadU32(0x80068260);
	for (int32_t i = 0;i < unk_28;i++,nspageinfo--)
	{
		if (status != 0x2)
			continue;
		nspageinfo->off_10 = A2;
		if (A2)
		{
			V1 = EMU_ReadU32(0x80067838);
			V0 = nspageinfo->pagenum >> 1;
			V0 <<= 2;
			V0 += V1;
			V0 = EMU_ReadU32(V0 + 1056); //+ 0x420
			V1 = T0 + 32;
			V0 &= 0x3F;
			T0 = V1 - V0;
			nspageinfo->unk_8 = T0;
		}
		if (nspageinfo->status == 0x2)
		{
			nspageinfo->status = 0x3;
		}
		else
		{
			nspageinfo->status = 0xD;
			if (A2)
			{
				V1 = EMU_ReadU32(0x80067838);
				V0 = nspageinfo->pagenum >> 1;
				V0 <<= 2;
				V0 += V1;
				V0 = EMU_ReadU32(V0 + 1056); //+ 0x420
				A0 = nspageinfo->unk_8;
				V1 = (uint32_t)nspageinfo->page;
				V0 &= 0x3F;
				V0 += A0;
				V0 <<= 11;
				V1 += V0;
				nspageinfo->nextpage = (emuptr<struct nspage>)V1;
			}
			else
			{
				nspageinfo->nextpage = nspageinfo[1].page;
			}
			nspageinfo->nextpage->magic = 0x8765;
		}
	}
	// BUG :: The game's original implementation of this function
	// returned an undefined value if the loop body was not executed
	// at least once (register $A3)
	return (uint32_t)(nspageinfo + 1);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movl %edi, %ebx
callq 0x162c24
movzwl 0x4(%rax), %ebp
movl %ebx, %edi
callq 0x162c24
movl 0x1c(%rax), %r15d
leaq 0x775a(%rip), %r12      # 0x16b350
movq (%r12), %rax
movl $0x0, 0x20(%rax)
cmpl $0xd, %ebp
je 0x163c0b
cmpl $0x3, %ebp
jne 0x163c77
movl $0x80068260, %edi       # imm = 0x80068260
callq 0x1621d5
testl %eax, %eax
je 0x163c49
movl %ebx, %edi
callq 0x162c24
movl (%rax), %ebp
movl %ebp, %edi
callq 0x162c24
movl 0xc(%rax), %r14d
movl $0x800125f8, %edi       # imm = 0x800125F8
movl $0x1, %esi
movl %ebp, %edx
xorl %eax, %eax
callq 0x162cdf
cmpl %eax, %r14d
jne 0x163e62
movl %ebx, %edi
callq 0x162c24
movw $0x4, 0x4(%rax)
movl %ebx, %edi
callq 0x162c24
movl (%rax), %edx
xorl %ebx, %ebx
movl $0x8001254c, %edi       # imm = 0x8001254C
movl $0x1, %esi
xorl %eax, %eax
callq 0x162cdf
jmp 0x163e57
movl $0x80067838, %edi       # imm = 0x80067838
callq 0x1621d5
movq (%r12), %rcx
movl %eax, 0x8(%rcx)
leal 0x41c(%rax), %edi
callq 0x1621d5
movq (%r12), %rcx
movl %eax, 0x8(%rcx)
movl %ebx, %edi
callq 0x162c24
movl 0x14(%rax), %eax
sarl %eax
movq (%r12), %rdx
movl %eax, 0xc(%rdx)
xorl %ecx, %ecx
cmpl 0x8(%rdx), %eax
jge 0x163cc5
movl $0x80068260, %edi       # imm = 0x80068260
callq 0x1621d5
xorl %ecx, %ecx
testl %eax, %eax
setne %cl
movq (%r12), %rax
movl %ecx, 0x18(%rax)
testl %r15d, %r15d
jle 0x163e54
cmpw $0x2, %bp
jne 0x163e48
movq (%r12), %rax
movzwl 0x18(%rax), %r14d
movl %ebx, %edi
callq 0x162c24
movw %r14w, 0xa(%rax)
movq (%r12), %rax
cmpl $0x0, 0x18(%rax)
je 0x163d60
movl $0x80067838, %edi       # imm = 0x80067838
callq 0x1621d5
movq (%r12), %rcx
movl %eax, 0xc(%rcx)
movl %ebx, %edi
callq 0x162c24
movl 0x14(%rax), %eax
movq (%r12), %rcx
andl $-0x2, %eax
movl 0xc(%rcx), %edx
leal (%rdx,%rax,2), %esi
movl %esi, 0x8(%rcx)
leal (%rdx,%rax,2), %edi
addl $0x420, %edi            # imm = 0x420
callq 0x1621d5
movq (%r12), %rcx
movl 0x20(%rcx), %r14d
addl $0x20, %r14d
movl %r14d, 0xc(%rcx)
andl $0x3f, %eax
movl %eax, 0x8(%rcx)
subl %eax, %r14d
movl %r14d, 0x20(%rcx)
movl %ebx, %edi
callq 0x162c24
movw %r14w, 0x8(%rax)
movl %ebx, %edi
callq 0x162c24
movzwl 0x4(%rax), %r14d
movl %ebx, %edi
callq 0x162c24
cmpw $0x2, %r14w
jne 0x163d85
movw $0x3, 0x4(%rax)
jmp 0x163e48
movw $0xd, 0x4(%rax)
movq (%r12), %rax
cmpl $0x0, 0x18(%rax)
je 0x163e1c
movl $0x80067838, %edi       # imm = 0x80067838
callq 0x1621d5
movq (%r12), %rcx
movl %eax, 0xc(%rcx)
movl %ebx, %edi
callq 0x162c24
movl 0x14(%rax), %eax
movq (%r12), %rcx
andl $-0x2, %eax
movl 0xc(%rcx), %edx
leal (%rdx,%rax,2), %esi
movl %esi, 0x8(%rcx)
leal (%rdx,%rax,2), %edi
addl $0x420, %edi            # imm = 0x420
callq 0x1621d5
movq (%r12), %rcx
movl %eax, 0x8(%rcx)
movl %ebx, %edi
callq 0x162c24
movswl 0x8(%rax), %eax
movq (%r12), %rcx
movl %eax, 0x10(%rcx)
movl %ebx, %edi
callq 0x162c24
movq (%r12), %rcx
movl 0x8(%rcx), %edx
andl $0x3f, %edx
addl 0x10(%rcx), %edx
shll $0xb, %edx
movl (%rax), %r14d
addl %edx, %r14d
movl %edx, 0x8(%rcx)
movl %r14d, 0xc(%rcx)
movl %ebx, %edi
callq 0x162c24
movl %r14d, 0x10(%rax)
jmp 0x163e34
leal 0x2c(%rbx), %edi
callq 0x162c24
movq %rax, %r14
movl %ebx, %edi
callq 0x162c24
movl (%r14), %ecx
movl %ecx, 0x10(%rax)
movl %ebx, %edi
callq 0x162c24
movl 0x10(%rax), %edi
callq 0x162c24
movw $0x8765, (%rax)         # imm = 0x8765
addl $-0x2c, %ebx
decl %r15d
jne 0x163cd5
addl $0x2c, %ebx
movl %ebx, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movl $0x80013148, %edi       # imm = 0x80013148
movl $0x1, %esi
movl %ebx, %edx
xorl %eax, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x162cdf
nopl (%rax)