// File: /workspace/github_cpp_makefile_star2_O2/llefranc[P]42_webserv/srcs/utils/autoindex.cpp
// Function: autoIndexDisplayer(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*)
void autoIndexDisplayer(std::string const &uri, std::string* displayList)
{
	int fd[2];
	
	pipe(fd);
	int coutSave = dup(STDOUT_FILENO);
	dup2(fd[1], STDOUT_FILENO);
	close(fd[1]);
	
	char *path;
	if (!(path = getcwd(NULL, 0)))
		throw StatusLine(500, REASON_500, "getcwd function in autoIndexDisplayer failed\n");
	chdir(uri.c_str());

	std::string exec_path = "php " + (path + std::string("/srcs/utils/autoindex.php ")) ;
	std::system(exec_path.c_str());
	chdir(path);
	
	// Restoring STDOUT and closing the last fd[1], so read will get EOF signal
	dup2(coutSave, STDOUT_FILENO);
	close(coutSave);

	char buf[CGI_BUFFER_SIZE + 1] = {0};

	while (read(fd[0], buf, CGI_BUFFER_SIZE) > 0){	
		*displayList += buf;
		memset(buf, 0, CGI_BUFFER_SIZE + 1);
	}

	*displayList += buf;

	close(fd[0]);
	free(path);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x858, %rsp            # imm = 0x858
movq %rsi, %rbx
movq %rdi, %r12
leaq -0x34(%rbp), %r14
movq %r14, %rdi
callq 0x4570
pushq $0x1
popq %r13
movl %r13d, %edi
callq 0x45e0
movl %eax, %r15d
movl 0x4(%r14), %edi
movl %r13d, %esi
callq 0x43f0
movl 0x4(%r14), %edi
callq 0x47b0
xorl %edi, %edi
xorl %esi, %esi
callq 0x4250
testq %rax, %rax
je 0x107c9
movq %rax, %r14
movq (%r12), %rdi
callq 0x45d0
leaq 0x159d(%rip), %rsi      # 0x11c84
leaq -0x78(%rbp), %rdi
leaq -0x29(%rbp), %rdx
callq 0x4ec8
leaq -0x880(%rbp), %rdi
leaq -0x78(%rbp), %rdx
movq %r14, %rsi
callq 0x4f60
leaq 0x158c(%rip), %rsi      # 0x11c9a
leaq -0x58(%rbp), %rdi
leaq -0x880(%rbp), %rdx
callq 0x4f60
leaq -0x880(%rbp), %rdi
callq 0x47d8
leaq -0x78(%rbp), %rdi
callq 0x47d8
movq -0x58(%rbp), %rdi
callq 0x4390
movq %r14, %rdi
callq 0x45d0
pushq $0x1
popq %rsi
movl %r15d, %edi
callq 0x43f0
movl %r15d, %edi
callq 0x47b0
leaq -0x880(%rbp), %r15
movl $0x7ff, %edx            # imm = 0x7FF
movq %r15, %rdi
xorl %esi, %esi
callq 0x4280
movl -0x34(%rbp), %edi
movl $0x7fe, %edx            # imm = 0x7FE
movq %r15, %rsi
callq 0x45b0
testq %rax, %rax
jle 0x1078f
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4770
jmp 0x1075e
leaq -0x880(%rbp), %rsi
movq %rbx, %rdi
callq 0x4770
movl -0x34(%rbp), %edi
callq 0x47b0
movq %r14, %rdi
callq 0x44c0
leaq -0x58(%rbp), %rdi
callq 0x47d8
addq $0x858, %rsp            # imm = 0x858
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x48
popq %rdi
callq 0x4220
movq %rax, %r14
leaq 0x147b(%rip), %rsi      # 0x11c56
leaq -0x880(%rbp), %rdi
leaq -0x58(%rbp), %rdx
callq 0x4ec8
movb $0x1, %r15b
leaq 0x12a6(%rip), %rdx      # 0x11a9b
leaq -0x880(%rbp), %rcx
movq %r14, %rdi
movl $0x1f4, %esi            # imm = 0x1F4
callq 0xc540
xorl %r15d, %r15d
leaq 0x857d(%rip), %rsi      # 0x18d90
leaq -0x4284(%rip), %rdx     # 0xc596
movq %r14, %rdi
callq 0x46b0
movq %rax, %rbx
leaq -0x880(%rbp), %rdi
callq 0x47d8
testb %r15b, %r15b
jne 0x1083b
jmp 0x10874
movq %rax, %rbx
movq %r14, %rdi
callq 0x4330
jmp 0x10874
movq %rax, %rbx
leaq -0x880(%rbp), %rdi
callq 0x47d8
jmp 0x10859
movq %rax, %rbx
leaq -0x78(%rbp), %rdi
jmp 0x1086f
movq %rax, %rbx
jmp 0x10874
jmp 0x10868
jmp 0x10868
movq %rax, %rbx
leaq -0x58(%rbp), %rdi
callq 0x47d8
movq %rbx, %rdi
callq 0x46d0