// File: /workspace/github_cpp_makefile_star2_O3/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 $0x848, %rsp            # imm = 0x848
movq %rsi, %rbx
movq %rdi, %r12
leaq -0x30(%rbp), %r14
movq %r14, %rdi
callq 0x4530
movl $0x1, %edi
callq 0x45b0
movl %eax, %r15d
movl 0x4(%r14), %edi
movl $0x1, %esi
callq 0x4390
movl 0x4(%r14), %edi
callq 0x4780
xorl %edi, %edi
xorl %esi, %esi
callq 0x4210
testq %rax, %rax
je 0x1447d
movq %rax, %r14
movq (%r12), %rdi
callq 0x4580
leaq -0x60(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x1a06(%rip), %rsi      # 0x15cd6
leaq 0x1a19(%rip), %rdx      # 0x15cf0
leaq -0x70(%rbp), %rdi
callq 0x5796
movq %r14, %rdi
callq 0x41e0
leaq -0x70(%rbp), %rdi
xorl %esi, %esi
xorl %edx, %edx
movq %r14, %rcx
movq %rax, %r8
callq 0x4440
leaq -0x860(%rbp), %r13
movq %r13, -0x10(%r13)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x14328
movq %rdx, -0x870(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x860(%rbp)
jmp 0x14330
movups (%rcx), %xmm0
movups %xmm0, (%r13)
movq 0x8(%rax), %rdx
leaq -0x870(%rbp), %rdi
movq %rdx, 0x8(%rdi)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x1997(%rip), %rcx      # 0x15cec
movl $0x4, %r8d
xorl %esi, %esi
xorl %edx, %edx
callq 0x4440
leaq -0x40(%rbp), %rsi
movq %rsi, -0x10(%rsi)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x14388
movq %rdx, -0x50(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x40(%rbp)
jmp 0x1438e
movups (%rcx), %xmm0
movups %xmm0, (%rsi)
movq 0x8(%rax), %rdx
movq %rdx, -0x48(%rbp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq -0x870(%rbp), %rdi
cmpq %r13, %rdi
je 0x143c0
movq -0x860(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0x70(%rbp), %rdi
cmpq %r12, %rdi
je 0x143d5
movq -0x60(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0x50(%rbp), %rdi
callq 0x4340
movq %r14, %rdi
callq 0x4580
movl %r15d, %edi
movl $0x1, %esi
callq 0x4390
movl %r15d, %edi
callq 0x4780
leaq -0x870(%rbp), %r15
movl $0x7ff, %edx            # imm = 0x7FF
movq %r15, %rdi
xorl %esi, %esi
callq 0x4250
movl -0x30(%rbp), %edi
movl $0x7fe, %edx            # imm = 0x7FE
movq %r15, %rsi
callq 0x4560
testq %rax, %rax
jle 0x14433
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4760
jmp 0x14402
leaq -0x870(%rbp), %rsi
movq %rbx, %rdi
callq 0x4760
movl -0x30(%rbp), %edi
callq 0x4780
movq %r14, %rdi
callq 0x44a0
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x1446b
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x43e0
addq $0x848, %rsp            # imm = 0x848
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x48, %edi
callq 0x41f0
movq %rax, %r14
leaq -0x860(%rbp), %r15
movq %r15, -0x10(%r15)
leaq 0x180c(%rip), %rsi      # 0x15ca8
leaq 0x1832(%rip), %rdx      # 0x15cd5
leaq -0x870(%rbp), %rdi
callq 0x5796
movb $0x1, %r12b
leaq 0x1634(%rip), %rdx      # 0x15aed
leaq -0x870(%rbp), %rcx
movq %r14, %rdi
movl $0x1f4, %esi            # imm = 0x1F4
callq 0xe0d4
xorl %r12d, %r12d
leaq 0x8899(%rip), %rsi      # 0x1cd70
leaq -0x639c(%rip), %rdx     # 0xe142
movq %r14, %rdi
callq 0x46a0
movq %rax, %rbx
movq -0x870(%rbp), %rdi
cmpq %r15, %rdi
je 0x14504
movq -0x860(%rbp), %rsi
incq %rsi
callq 0x43e0
testb %r12b, %r12b
jne 0x1450e
jmp 0x1456f
movq %rax, %rbx
movq %r14, %rdi
callq 0x42e0
jmp 0x1456f
movq %rax, %rbx
movq -0x870(%rbp), %rdi
cmpq %r13, %rdi
je 0x1453b
movq -0x860(%rbp), %rsi
incq %rsi
callq 0x43e0
jmp 0x1453b
movq %rax, %rbx
movq -0x70(%rbp), %rdi
cmpq %r12, %rdi
je 0x1456f
movq -0x60(%rbp), %rsi
jmp 0x14567
movq %rax, %rbx
jmp 0x1456f
jmp 0x14553
jmp 0x14553
movq %rax, %rbx
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x1456f
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x43e0
movq %rbx, %rdi
callq 0x46d0