// File: 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
subq $0x8f0, %rsp            # imm = 0x8F0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x18(%rbp), %rdi
callq 0x5740
movl $0x1, %edi
callq 0x57d0
movl %eax, -0x1c(%rbp)
movl -0x14(%rbp), %edi
movl $0x1, %esi
callq 0x54f0
movl -0x14(%rbp), %edi
callq 0x59c0
xorl %eax, %eax
movl %eax, %esi
movq %rsi, %rdi
callq 0x52c0
movq %rax, -0x28(%rbp)
cmpq $0x0, %rax
jne 0x2223c
movb $0x1, -0x5d(%rbp)
movl $0x48, %edi
callq 0x5290
movq %rax, -0x8d0(%rbp)
leaq -0x49(%rbp), %rdi
movq %rdi, -0x8c8(%rbp)
callq 0x58f0
movq -0x8c8(%rbp), %rdx
leaq 0x1af0(%rip), %rsi      # 0x23c9b
leaq -0x48(%rbp), %rdi
callq 0x63e0
jmp 0x221b6
movq -0x8d0(%rbp), %rdi
leaq 0x18e4(%rip), %rdx      # 0x23aa8
movl $0x1f4, %esi            # imm = 0x1F4
leaq -0x48(%rbp), %rcx
callq 0x199d0
jmp 0x221d4
movq -0x8d0(%rbp), %rdi
movb $0x0, -0x5d(%rbp)
leaq 0xebaa(%rip), %rsi      # 0x30d90
leaq -0x876d(%rip), %rdx     # 0x19a80
callq 0x58a0
jmp 0x22420
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x2221a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x5a08
leaq -0x49(%rbp), %rdi
callq 0x55c0
testb $0x1, -0x5d(%rbp)
jne 0x2222b
jmp 0x22237
movq -0x8d0(%rbp), %rdi
callq 0x53f0
jmp 0x22417
movq -0x8(%rbp), %rdi
callq 0x5240
movq %rax, %rdi
callq 0x57b0
movq -0x28(%rbp), %rax
movq %rax, -0x8e0(%rbp)
leaq -0xc1(%rbp), %rdi
movq %rdi, -0x8d8(%rbp)
callq 0x58f0
movq -0x8d8(%rbp), %rdx
leaq 0x1a50(%rip), %rsi      # 0x23cc9
leaq -0xc0(%rbp), %rdi
callq 0x63e0
jmp 0x22287
movq -0x8e0(%rbp), %rsi
leaq -0xa0(%rbp), %rdi
leaq -0xc0(%rbp), %rdx
callq 0x6700
jmp 0x222a3
leaq 0x1a35(%rip), %rsi      # 0x23cdf
leaq -0x80(%rbp), %rdi
leaq -0xa0(%rbp), %rdx
callq 0x6700
jmp 0x222bc
leaq -0xa0(%rbp), %rdi
callq 0x5a08
leaq -0xc0(%rbp), %rdi
callq 0x5a08
leaq -0xc1(%rbp), %rdi
callq 0x55c0
leaq -0x80(%rbp), %rdi
callq 0x5240
movq %rax, %rdi
callq 0x5470
jmp 0x222f3
movq -0x28(%rbp), %rdi
callq 0x57b0
movl -0x1c(%rbp), %edi
movl $0x1, %esi
callq 0x54f0
movl -0x1c(%rbp), %edi
callq 0x59c0
jmp 0x22313
leaq -0x8c0(%rbp), %rdi
xorl %esi, %esi
movl $0x7ff, %edx            # imm = 0x7FF
callq 0x52e0
movl -0x18(%rbp), %edi
leaq -0x8c0(%rbp), %rsi
movl $0x7fe, %edx            # imm = 0x7FE
callq 0x5790
movq %rax, -0x8e8(%rbp)
jmp 0x22343
movq -0x8e8(%rbp), %rax
cmpq $0x0, %rax
jle 0x223e0
movq -0x10(%rbp), %rdi
leaq -0x8c0(%rbp), %rsi
callq 0x5520
jmp 0x22366
leaq -0x8c0(%rbp), %rdi
xorl %esi, %esi
movl $0x7ff, %edx            # imm = 0x7FF
callq 0x52e0
jmp 0x22326
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x223bb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x223af
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x5a08
leaq -0xc0(%rbp), %rdi
callq 0x5a08
leaq -0xc1(%rbp), %rdi
callq 0x55c0
jmp 0x22417
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x5a08
jmp 0x22417
movq -0x10(%rbp), %rdi
leaq -0x8c0(%rbp), %rsi
callq 0x5520
jmp 0x223f2
movl -0x18(%rbp), %edi
callq 0x59c0
jmp 0x223fc
movq -0x28(%rbp), %rdi
callq 0x5640
leaq -0x80(%rbp), %rdi
callq 0x5a08
addq $0x8f0, %rsp            # imm = 0x8F0
popq %rbp
retq
movq -0x58(%rbp), %rdi
callq 0x58e0