// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x280, %rsp            # imm = 0x280
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x8(%rbp), %rdx
leaq -0x30(%rbp), %rdi
leaq 0x77ac8(%rip), %rsi     # 0x8c326
movb $0x0, %al
callq 0x64d0
movq -0x30(%rbp), %rdi
leaq 0x778fd(%rip), %rsi     # 0x8c16d
callq 0x61e0
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rdi
callq 0x62e0
movq $0x0, -0x40(%rbp)
movq -0x38(%rbp), %rdi
callq 0x65d0
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x148a4
jmp 0x14a33
movq $0x0, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
movq -0x38(%rbp), %rdi
leaq 0x77a75(%rip), %rsi     # 0x8c334
leaq -0x48(%rbp), %rdx
leaq -0x50(%rbp), %rcx
leaq -0x60(%rbp), %r8
leaq -0x58(%rbp), %r9
leaq -0x68(%rbp), %rax
movq %rax, (%rsp)
movb $0x0, %al
callq 0x60e0
movl %eax, -0x6c(%rbp)
cmpl $0x3, -0x6c(%rbp)
jge 0x148e9
jmp 0x1488a
movq -0x48(%rbp), %rax
subq -0x40(%rbp), %rax
movq %rax, -0x78(%rbp)
cmpq $0x0, -0x78(%rbp)
jbe 0x14986
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rdi
imulq $0xa0, -0x28(%rbp), %rsi
callq 0x66c0
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq -0x28(%rbp), %rcx
subq $0x1, %rcx
imulq $0xa0, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x260(%rbp)
leaq -0x118(%rbp), %rdi
xorl %esi, %esi
movl $0xa0, %edx
callq 0x6060
movq -0x260(%rbp), %rdi
movq -0x40(%rbp), %rax
movq %rax, -0x118(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x110(%rbp)
movb $0x1, -0x108(%rbp)
leaq -0x118(%rbp), %rsi
movl $0xa0, %edx
callq 0x6790
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rdi
imulq $0xa0, -0x28(%rbp), %rsi
callq 0x66c0
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq -0x28(%rbp), %rcx
subq $0x1, %rcx
imulq $0xa0, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x268(%rbp)
leaq -0x1b8(%rbp), %rdi
xorl %esi, %esi
movl $0xa0, %edx
callq 0x6060
movq -0x268(%rbp), %rdi
movq -0x48(%rbp), %rax
movq %rax, -0x1b8(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x1b0(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x1a0(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x198(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0x190(%rbp)
leaq -0x1b8(%rbp), %rsi
movl $0xa0, %edx
callq 0x6790
movq -0x50(%rbp), %rax
movq %rax, -0x40(%rbp)
jmp 0x1488a
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rdi
imulq $0xa0, -0x28(%rbp), %rsi
callq 0x66c0
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq -0x28(%rbp), %rcx
subq $0x1, %rcx
imulq $0xa0, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x270(%rbp)
leaq -0x258(%rbp), %rdi
xorl %esi, %esi
movl $0xa0, %edx
callq 0x6060
movq -0x270(%rbp), %rdi
movq -0x40(%rbp), %rax
movq %rax, -0x258(%rbp)
movq $-0x1, -0x250(%rbp)
movb $0x1, -0x248(%rbp)
leaq -0x258(%rbp), %rsi
movl $0xa0, %edx
callq 0x6790
movq -0x38(%rbp), %rdi
callq 0x65f0
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x28(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
addq $0x280, %rsp            # imm = 0x280
popq %rbp
retq
nopw %cs:(%rax,%rax)