// File: /workspace/github_c_makefile_star2_O2/arkanis[P]process-map/map.c
// Function: LoadAddrRanges
void LoadAddrRanges(char* pid, AddrRange** ranges, size_t* rangeCount) {
	AddrRange* list = NULL;
	size_t count = 0;
	
	char* mapsPath = NULL;
	asprintf(&mapsPath, "/proc/%s/maps", pid);
	FILE* mapsFile = fopen(mapsPath, "rb");
	free(mapsPath);
	
	uint64_t prevRangeEnd = 0;
	while ( !feof(mapsFile) ) {
		uint64_t from, to, offset;
		char *perms = NULL, *pathname = NULL;
		int items_read = fscanf(mapsFile, "%lx-%lx%*[ ]%ms%*[ ]%lx%*[ ]%*s%*[ ]%*s%*[ ]%m[^\n]", &from, &to, &perms, &offset, &pathname);
		if (items_read < 3)
			continue;
		
		uint64_t gapSize = from - prevRangeEnd;
		if (gapSize > 0) {
			count++;
			list = realloc(list, sizeof(list[0]) * count);
			list[count - 1] = (AddrRange){ .startAddr = prevRangeEnd, .endAddr = from, .isGap = true };
		}
		
		count++;
		list = realloc(list, sizeof(list[0]) * count);
		list[count - 1] = (AddrRange){ .startAddr = from, .endAddr = to, .isGap = false, .perms = perms, .filePath = pathname, .fileOffset = offset };
		
		prevRangeEnd = to;
	}
	count++;
	list = realloc(list, sizeof(list[0]) * count);
	list[count - 1] = (AddrRange){ .startAddr = prevRangeEnd, .endAddr = 0xffffffffffffffff, .isGap = true };
	
	fclose(mapsFile);
	
	*ranges = list;
	*rangeCount = count;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdx, 0x28(%rsp)
movq %rsi, 0x20(%rsp)
movq %rdi, %rdx
leaq 0x40(%rsp), %rbx
andq $0x0, (%rbx)
leaq 0x49158(%rip), %rsi     # 0x59596
movq %rbx, %rdi
xorl %eax, %eax
callq 0x6510
movq (%rbx), %rdi
leaq 0x48f8b(%rip), %rsi     # 0x593dd
callq 0x61d0
movq %rax, %r15
movq (%rbx), %rdi
callq 0x62f0
leaq 0x10(%rsp), %r14
leaq 0x49136(%rip), %rbx     # 0x595a4
xorl %ebp, %ebp
xorl %r13d, %r13d
xorl %r12d, %r12d
movq %r15, %rdi
callq 0x6610
testl %eax, %eax
jne 0x1059a
andq $0x0, 0x18(%rsp)
andq $0x0, 0x10(%rsp)
movq %r14, (%rsp)
movq %r15, %rdi
movq %rbx, %rsi
leaq 0x8(%rsp), %rdx
leaq 0x38(%rsp), %rcx
leaq 0x18(%rsp), %r8
leaq 0x30(%rsp), %r9
xorl %eax, %eax
callq 0x60e0
cmpl $0x3, %eax
jl 0x10476
cmpq %rbp, 0x8(%rsp)
je 0x10506
imulq $0xa0, %r13, %rbx
incq %r13
movl $0xa0, %eax
leaq (%rbx,%rax), %rsi
movq %r12, %rdi
callq 0x6700
movq %rax, %r12
movq 0x8(%rsp), %rax
leaq (%r12,%rbx), %rdi
addq $0x11, %rdi
movq %rbp, -0x11(%rdi)
movq %rax, -0x9(%rdi)
movb $0x1, -0x1(%rdi)
movl $0x8f, %edx
xorl %esi, %esi
callq 0x6060
movl $0xa0, %eax
imulq $0xa0, %r13, %rbx
incq %r13
leaq (%rbx,%rax), %rsi
movq %r12, %rdi
callq 0x6700
movq %rax, %r12
movq 0x8(%rsp), %rax
movq 0x38(%rsp), %rbp
movq 0x18(%rsp), %rcx
movq %rax, (%r12,%rbx)
movq %rbp, 0x8(%r12,%rbx)
andq $0x0, 0x10(%r12,%rbx)
movq 0x10(%rsp), %rax
movq 0x30(%rsp), %rdx
movq %rcx, 0x18(%r12,%rbx)
movq %rax, 0x20(%r12,%rbx)
movq %rdx, 0x28(%r12,%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x90(%r12,%rbx)
movups %xmm0, 0x80(%r12,%rbx)
movups %xmm0, 0x70(%r12,%rbx)
movups %xmm0, 0x60(%r12,%rbx)
movups %xmm0, 0x50(%r12,%rbx)
movups %xmm0, 0x40(%r12,%rbx)
movups %xmm0, 0x30(%r12,%rbx)
leaq 0x4900f(%rip), %rbx     # 0x595a4
jmp 0x10476
imulq $0xa0, %r13, %r14
incq %r13
leaq 0xa0(%r14), %rsi
movq %r12, %rdi
callq 0x6700
movq %rax, %rbx
leaq (%rax,%r14), %rdi
addq $0x11, %rdi
movq %rbp, -0x11(%rdi)
orq $-0x1, -0x9(%rdi)
movb $0x1, -0x1(%rdi)
movl $0x8f, %edx
xorl %esi, %esi
callq 0x6060
movq %r15, %rdi
callq 0x6630
movq 0x20(%rsp), %rax
movq %rbx, (%rax)
movq 0x28(%rsp), %rax
movq %r13, (%rax)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq