// File: /workspace/github_c_makefile_star2_O1/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
movq $0x0, (%rbx)
leaq 0x55e3e(%rip), %rsi     # 0x6848e
xorl %r13d, %r13d
movq %rbx, %rdi
xorl %eax, %eax
callq 0x6500
movq (%rbx), %rdi
leaq 0x55c6e(%rip), %rsi     # 0x682d5
callq 0x61d0
movq %rax, %r15
movq (%rbx), %rdi
callq 0x62e0
movq %r15, %rdi
callq 0x6600
movl $0x0, %r14d
movl $0x0, %ebp
testl %eax, %eax
jne 0x127ce
xorl %r12d, %r12d
leaq 0x10(%rsp), %rbx
xorl %ebp, %ebp
xorl %r14d, %r14d
xorl %r13d, %r13d
movq %r12, 0x18(%rsp)
movq %r12, 0x10(%rsp)
movq %rbx, (%rsp)
movq %r15, %rdi
leaq 0x55de2(%rip), %rsi     # 0x6849c
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 0x127be
cmpq %r13, 0x8(%rsp)
je 0x1272f
leaq 0x1(%r14), %r12
shlq $0x5, %r14
leaq (%r14,%r14,4), %rsi
addq $0xa0, %rsi
movq %rbp, %rdi
callq 0x66f0
movq %rax, %rbp
leaq (%r14,%r14,4), %rax
movq 0x8(%rsp), %rcx
leaq (%rax,%rbp), %rdi
addq $0x11, %rdi
movq %r13, -0x11(%rdi)
movq %rcx, -0x9(%rdi)
movb $0x1, -0x1(%rdi)
movl $0x8f, %edx
xorl %esi, %esi
callq 0x6060
movq %r12, %r14
leaq 0x1(%r14), %r12
shlq $0x5, %r14
leaq (%r14,%r14,4), %rsi
addq $0xa0, %rsi
movq %rbp, %rdi
callq 0x66f0
movq %rax, %rbp
leaq (%r14,%r14,4), %rax
movq 0x8(%rsp), %rcx
movq 0x38(%rsp), %r13
movq 0x18(%rsp), %rdx
movq 0x10(%rsp), %rsi
movq 0x30(%rsp), %rdi
movq %rcx, (%rbp,%rax)
movq %r13, 0x8(%rbp,%rax)
movq $0x0, 0x10(%rbp,%rax)
movq %rdx, 0x18(%rbp,%rax)
movq %rsi, 0x20(%rbp,%rax)
movq %rdi, 0x28(%rbp,%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x90(%rbp,%rax)
movups %xmm0, 0x80(%rbp,%rax)
movups %xmm0, 0x70(%rbp,%rax)
movups %xmm0, 0x60(%rbp,%rax)
movups %xmm0, 0x50(%rbp,%rax)
movups %xmm0, 0x40(%rbp,%rax)
movups %xmm0, 0x30(%rbp,%rax)
movq %r12, %r14
xorl %r12d, %r12d
movq %r15, %rdi
callq 0x6600
testl %eax, %eax
je 0x126a2
leaq 0x1(%r14), %r12
shlq $0x5, %r14
leaq (%r14,%r14,4), %rsi
addq $0xa0, %rsi
movq %rbp, %rdi
callq 0x66f0
movq %rax, %rbx
leaq (%r14,%r14,4), %rax
leaq (%rbx,%rax), %rdi
addq $0x11, %rdi
movq %r13, -0x11(%rdi)
movq $-0x1, -0x9(%rdi)
movb $0x1, -0x1(%rdi)
movl $0x8f, %edx
xorl %esi, %esi
callq 0x6060
movq %r15, %rdi
callq 0x6620
movq 0x20(%rsp), %rax
movq %rbx, (%rax)
movq 0x28(%rsp), %rax
movq %r12, (%rax)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq