// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x40, %rsp
movl %edi, -0x8(%rbp)
leaq -0x8(%rbp), %rdi
callq 0x320030
movw 0x4(%rax), %ax
movw %ax, -0xa(%rbp)
leaq -0x8(%rbp), %rdi
callq 0x320030
movl 0x1c(%rax), %eax
movl %eax, -0x10(%rbp)
leaq 0x6f2e(%rip), %rax      # 0x3269d0
movq (%rax), %rax
movl $0x0, 0x20(%rax)
movswl -0xa(%rbp), %eax
cmpl $0x3, %eax
je 0x31fac2
movswl -0xa(%rbp), %eax
cmpl $0xd, %eax
jne 0x31fb4b
movl $0x80068260, %edi       # imm = 0x80068260
callq 0x31bba0
cmpl $0x0, %eax
je 0x31fb0e
leaq -0x8(%rbp), %rdi
callq 0x320030
movl (%rax), %eax
movl %eax, -0x14(%rbp)
movl -0x14(%rbp), %edi
callq 0x31fa20
cmpl $0x0, %eax
jne 0x31fb0e
movl -0x8(%rbp), %eax
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %edx
movl $0x80013148, %edi       # imm = 0x80013148
movl $0x1, %esi
movb $0x0, %al
callq 0x31dad0
movl %eax, -0x4(%rbp)
jmp 0x31ff5c
leaq -0x8(%rbp), %rdi
callq 0x320030
movw $0x4, 0x4(%rax)
leaq -0x8(%rbp), %rdi
callq 0x320030
movl (%rax), %eax
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %edx
movl $0x8001254c, %edi       # imm = 0x8001254C
movl $0x1, %esi
movb $0x0, %al
callq 0x31dad0
movl $0x0, -0x4(%rbp)
jmp 0x31ff5c
movl $0x80067838, %edi       # imm = 0x80067838
callq 0x31bba0
movl %eax, %ecx
leaq 0x6e72(%rip), %rax      # 0x3269d0
movq (%rax), %rax
movl %ecx, 0x8(%rax)
leaq 0x6e65(%rip), %rax      # 0x3269d0
movq (%rax), %rax
movl 0x8(%rax), %edi
addl $0x41c, %edi            # imm = 0x41C
callq 0x31bba0
movl %eax, %ecx
leaq 0x6e4b(%rip), %rax      # 0x3269d0
movq (%rax), %rax
movl %ecx, 0x8(%rax)
leaq -0x8(%rbp), %rdi
callq 0x320030
movl 0x14(%rax), %ecx
sarl %ecx
leaq 0x6e30(%rip), %rax      # 0x3269d0
movq (%rax), %rax
movl %ecx, 0xc(%rax)
leaq 0x6e23(%rip), %rax      # 0x3269d0
movq (%rax), %rax
movl 0xc(%rax), %ecx
leaq 0x6e16(%rip), %rax      # 0x3269d0
movq (%rax), %rdx
xorl %eax, %eax
cmpl 0x8(%rdx), %ecx
movb %al, -0x2d(%rbp)
jge 0x31fbde
movl $0x80068260, %edi       # imm = 0x80068260
callq 0x31bba0
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
xorb $-0x1, %al
movb %al, -0x2d(%rbp)
movb -0x2d(%rbp), %al
andb $0x1, %al
movzbl %al, %ecx
leaq 0x6de3(%rip), %rax      # 0x3269d0
movq (%rax), %rax
movl %ecx, 0x18(%rax)
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x10(%rbp), %eax
jge 0x31ff3f
movswl -0xa(%rbp), %eax
cmpl $0x2, %eax
je 0x31fc14
jmp 0x31ff23
leaq 0x6db5(%rip), %rax      # 0x3269d0
movq (%rax), %rax
movl 0x18(%rax), %eax
movw %ax, -0x30(%rbp)
leaq -0x8(%rbp), %rdi
callq 0x320030
movw -0x30(%rbp), %cx
movw %cx, 0xa(%rax)
leaq 0x6d93(%rip), %rax      # 0x3269d0
movq (%rax), %rax
cmpl $0x0, 0x18(%rax)
je 0x31fd4e
movl $0x80067838, %edi       # imm = 0x80067838
callq 0x31bba0
movl %eax, %ecx
leaq 0x6d73(%rip), %rax      # 0x3269d0
movq (%rax), %rax
movl %ecx, 0xc(%rax)
leaq -0x8(%rbp), %rdi
callq 0x320030
movl 0x14(%rax), %ecx
sarl %ecx
leaq 0x6d58(%rip), %rax      # 0x3269d0
movq (%rax), %rax
movl %ecx, 0x8(%rax)
leaq 0x6d4b(%rip), %rax      # 0x3269d0
movq (%rax), %rax
movl 0x8(%rax), %ecx
shll $0x2, %ecx
movl %ecx, 0x8(%rax)
leaq 0x6d38(%rip), %rax      # 0x3269d0
movq (%rax), %rax
movl 0xc(%rax), %ecx
leaq 0x6d2b(%rip), %rax      # 0x3269d0
movq (%rax), %rax
addl 0x8(%rax), %ecx
movl %ecx, 0x8(%rax)
leaq 0x6d1b(%rip), %rax      # 0x3269d0
movq (%rax), %rax
movl 0x8(%rax), %edi
addl $0x420, %edi            # imm = 0x420
callq 0x31bba0
movl %eax, %ecx
leaq 0x6d01(%rip), %rax      # 0x3269d0
movq (%rax), %rax
movl %ecx, 0x8(%rax)
leaq 0x6cf4(%rip), %rax      # 0x3269d0
movq (%rax), %rax
movl 0x20(%rax), %ecx
addl $0x20, %ecx
leaq 0x6ce4(%rip), %rax      # 0x3269d0
movq (%rax), %rax
movl %ecx, 0xc(%rax)
leaq 0x6cd7(%rip), %rax      # 0x3269d0
movq (%rax), %rax
movl 0x8(%rax), %ecx
andl $0x3f, %ecx
movl %ecx, 0x8(%rax)
leaq 0x6cc4(%rip), %rax      # 0x3269d0
movq (%rax), %rax
movl 0xc(%rax), %ecx
leaq 0x6cb7(%rip), %rax      # 0x3269d0
movq (%rax), %rax
subl 0x8(%rax), %ecx
leaq 0x6caa(%rip), %rax      # 0x3269d0
movq (%rax), %rax
movl %ecx, 0x20(%rax)
leaq 0x6c9d(%rip), %rax      # 0x3269d0
movq (%rax), %rax
movl 0x20(%rax), %eax
movw %ax, -0x32(%rbp)
leaq -0x8(%rbp), %rdi
callq 0x320030
movw -0x32(%rbp), %cx
movw %cx, 0x8(%rax)
leaq -0x8(%rbp), %rdi
callq 0x320030
movswl 0x4(%rax), %eax
cmpl $0x2, %eax
jne 0x31fd74
leaq -0x8(%rbp), %rdi
callq 0x320030
movw $0x3, 0x4(%rax)
jmp 0x31ff21
leaq -0x8(%rbp), %rdi
callq 0x320030
movw $0xd, 0x4(%rax)
leaq 0x6c46(%rip), %rax      # 0x3269d0
movq (%rax), %rax
cmpl $0x0, 0x18(%rax)
je 0x31fee3
movl $0x80067838, %edi       # imm = 0x80067838
callq 0x31bba0
movl %eax, %ecx
leaq 0x6c26(%rip), %rax      # 0x3269d0
movq (%rax), %rax
movl %ecx, 0xc(%rax)
leaq -0x8(%rbp), %rdi
callq 0x320030
movl 0x14(%rax), %ecx
sarl %ecx
leaq 0x6c0b(%rip), %rax      # 0x3269d0
movq (%rax), %rax
movl %ecx, 0x8(%rax)
leaq 0x6bfe(%rip), %rax      # 0x3269d0
movq (%rax), %rax
movl 0x8(%rax), %ecx
shll $0x2, %ecx
movl %ecx, 0x8(%rax)
leaq 0x6beb(%rip), %rax      # 0x3269d0
movq (%rax), %rax
movl 0xc(%rax), %ecx
leaq 0x6bde(%rip), %rax      # 0x3269d0
movq (%rax), %rax
addl 0x8(%rax), %ecx
movl %ecx, 0x8(%rax)
leaq 0x6bce(%rip), %rax      # 0x3269d0
movq (%rax), %rax
movl 0x8(%rax), %edi
addl $0x420, %edi            # imm = 0x420
callq 0x31bba0
movl %eax, %ecx
leaq 0x6bb4(%rip), %rax      # 0x3269d0
movq (%rax), %rax
movl %ecx, 0x8(%rax)
leaq -0x8(%rbp), %rdi
callq 0x320030
movswl 0x8(%rax), %ecx
leaq 0x6b9a(%rip), %rax      # 0x3269d0
movq (%rax), %rax
movl %ecx, 0x10(%rax)
leaq -0x8(%rbp), %rdi
callq 0x320030
movq %rax, %rdi
callq 0x31fff0
movl %eax, %ecx
leaq 0x6b7a(%rip), %rax      # 0x3269d0
movq (%rax), %rax
movl %ecx, 0xc(%rax)
leaq 0x6b6d(%rip), %rax      # 0x3269d0
movq (%rax), %rax
movl 0x8(%rax), %ecx
andl $0x3f, %ecx
movl %ecx, 0x8(%rax)
leaq 0x6b5a(%rip), %rax      # 0x3269d0
movq (%rax), %rax
movl 0x10(%rax), %ecx
leaq 0x6b4d(%rip), %rax      # 0x3269d0
movq (%rax), %rax
addl 0x8(%rax), %ecx
movl %ecx, 0x8(%rax)
leaq 0x6b3d(%rip), %rax      # 0x3269d0
movq (%rax), %rax
movl 0x8(%rax), %ecx
shll $0xb, %ecx
movl %ecx, 0x8(%rax)
leaq 0x6b2a(%rip), %rax      # 0x3269d0
movq (%rax), %rax
movl 0x8(%rax), %ecx
leaq 0x6b1d(%rip), %rax      # 0x3269d0
movq (%rax), %rax
addl 0xc(%rax), %ecx
movl %ecx, 0xc(%rax)
leaq 0x6b0d(%rip), %rax      # 0x3269d0
movq (%rax), %rax
movl 0xc(%rax), %esi
leaq -0x24(%rbp), %rdi
callq 0x320050
leaq -0x8(%rbp), %rdi
callq 0x320030
movl -0x24(%rbp), %ecx
movl %ecx, 0x10(%rax)
jmp 0x31ff07
leaq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x320070
movq %rax, -0x40(%rbp)
leaq -0x8(%rbp), %rdi
callq 0x320030
movq -0x40(%rbp), %rcx
movl (%rcx), %ecx
movl %ecx, 0x10(%rax)
leaq -0x8(%rbp), %rdi
callq 0x320030
movq %rax, %rdi
addq $0x10, %rdi
callq 0x31ffd0
movw $0x8765, (%rax)         # imm = 0x8765
jmp 0x31ff23
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
leaq -0x8(%rbp), %rdi
xorl %esi, %esi
callq 0x3200a0
movl %eax, -0x28(%rbp)
jmp 0x31fbfa
leaq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x3200d0
movl %eax, -0x2c(%rbp)
leaq -0x2c(%rbp), %rdi
callq 0x320110
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)