// File: /workspace/github_c_makefile_star2_O2/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 0x1360a2
movzwl 0x4(%rax), %ebp
movl %ebx, %edi
callq 0x1360a2
movl 0x1c(%rax), %r14d
leaq 0x23324(%rip), %r15     # 0x15a350
movq (%r15), %rax
andl $0x0, 0x20(%rax)
cmpl $0xd, %ebp
je 0x13703d
cmpl $0x3, %ebp
jne 0x13708d
movl $0x80068260, %edi       # imm = 0x80068260
callq 0x1356f5
testl %eax, %eax
je 0x137061
movl %ebx, %edi
callq 0x1360a2
movl (%rax), %edi
callq 0x136fd9
testl %eax, %eax
je 0x137270
movl %ebx, %edi
callq 0x1360a2
movw $0x4, 0x4(%rax)
movl %ebx, %edi
callq 0x1360a2
movl (%rax), %edx
xorl %ebx, %ebx
pushq $0x1
popq %rsi
movl $0x8001254c, %edi       # imm = 0x8001254C
xorl %eax, %eax
callq 0x136157
jmp 0x137265
movl $0x80067838, %edi       # imm = 0x80067838
callq 0x1356f5
movq (%r15), %rcx
movl %eax, 0x8(%rcx)
leal 0x41c(%rax), %edi
callq 0x1356f5
movq (%r15), %rcx
movl %eax, 0x8(%rcx)
movl %ebx, %edi
callq 0x1360a2
movl 0x14(%rax), %edx
sarl %edx
movq (%r15), %rax
movl %edx, 0xc(%rax)
xorl %r12d, %r12d
movl $0x0, %ecx
cmpl 0x8(%rax), %edx
jge 0x1370e1
movl $0x80068260, %edi       # imm = 0x80068260
callq 0x1356f5
xorl %ecx, %ecx
testl %eax, %eax
setne %cl
movq (%r15), %rax
movl %ecx, 0x18(%rax)
testl %r14d, %r14d
cmovgl %r14d, %r12d
subl $0x1, %r12d
jb 0x137262
cmpw $0x2, %bp
jne 0x13725a
movq (%r15), %rax
movzwl 0x18(%rax), %r14d
movl %ebx, %edi
callq 0x1360a2
movw %r14w, 0xa(%rax)
movq (%r15), %rax
cmpl $0x0, 0x18(%rax)
je 0x13717b
movl $0x80067838, %edi       # imm = 0x80067838
callq 0x1356f5
movq (%r15), %rcx
movl %eax, 0xc(%rcx)
movl %ebx, %edi
callq 0x1360a2
movl 0x14(%rax), %eax
movq (%r15), %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 0x1356f5
movq (%r15), %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 0x1360a2
movw %r14w, 0x8(%rax)
movl %ebx, %edi
callq 0x1360a2
movzwl 0x4(%rax), %r14d
movl %ebx, %edi
callq 0x1360a2
cmpw $0x2, %r14w
jne 0x1371a0
movw $0x3, 0x4(%rax)
jmp 0x13725a
movw $0xd, 0x4(%rax)
movq (%r15), %rax
cmpl $0x0, 0x18(%rax)
je 0x13722d
movl $0x80067838, %edi       # imm = 0x80067838
callq 0x1356f5
movq (%r15), %rcx
movl %eax, 0xc(%rcx)
movl %ebx, %edi
callq 0x1360a2
movl 0x14(%rax), %eax
movq (%r15), %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 0x1356f5
movq (%r15), %rcx
movl %eax, 0x8(%rcx)
movl %ebx, %edi
callq 0x1360a2
movswl 0x8(%rax), %eax
movq (%r15), %rcx
movl %eax, 0x10(%rcx)
movl %ebx, %edi
callq 0x1360a2
movq (%r15), %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 0x1360a2
movl %r14d, 0x10(%rax)
jmp 0x137245
leal 0x2c(%rbx), %edi
callq 0x1360a2
movq %rax, %r14
movl %ebx, %edi
callq 0x1360a2
movl (%r14), %ecx
movl %ecx, 0x10(%rax)
movl %ebx, %edi
callq 0x1360a2
leaq 0x10(%rax), %rdi
callq 0x13728a
movw $0x8765, (%rax)         # imm = 0x8765
addl $-0x2c, %ebx
jmp 0x1370eb
addl $0x2c, %ebx
movl %ebx, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
pushq $0x1
popq %rsi
movl $0x80013148, %edi       # imm = 0x80013148
movl %ebx, %edx
xorl %eax, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x136157
nop