// File: /workspace/github_cpp_makefile_star2_O1/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 0x145cd
movq %rax, %r14
movq (%r12), %rdi
callq 0x4580
leaq -0x60(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x18b6(%rip), %rsi      # 0x15cd6
leaq 0x18c9(%rip), %rdx      # 0x15cf0
leaq -0x70(%rbp), %rdi
callq 0x5790
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 0x14478
movq %rdx, -0x870(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x860(%rbp)
jmp 0x14480
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 0x1847(%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 0x144d8
movq %rdx, -0x50(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x40(%rbp)
jmp 0x144de
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 0x14510
movq -0x860(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0x70(%rbp), %rdi
cmpq %r12, %rdi
je 0x14525
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 0x14583
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4760
jmp 0x14552
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 0x145bb
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), %r12
movq %r12, -0x10(%r12)
leaq 0x16bb(%rip), %rsi      # 0x15ca8
leaq 0x16e1(%rip), %rdx      # 0x15cd5
leaq -0x870(%rbp), %rdi
callq 0x5790
movb $0x1, %r15b
leaq 0x14e3(%rip), %rdx      # 0x15aed
leaq -0x870(%rbp), %rcx
movq %r14, %rdi
movl $0x1f4, %esi            # imm = 0x1F4
callq 0xe106
xorl %r15d, %r15d
leaq 0x8748(%rip), %rsi      # 0x1cd70
leaq -0x64bb(%rip), %rdx     # 0xe174
movq %r14, %rdi
callq 0x46a0
movq %rax, %rbx
movq -0x870(%rbp), %rdi
cmpq %r12, %rdi
je 0x1465d
movq -0x860(%rbp), %rsi
incq %rsi
callq 0x43e0
jmp 0x1465d
movq %rax, %rbx
movb $0x1, %r15b
testb %r15b, %r15b
je 0x146c3
movq %r14, %rdi
callq 0x42e0
jmp 0x146c3
movq %rax, %rbx
movq -0x870(%rbp), %rdi
cmpq %r13, %rdi
je 0x1468f
movq -0x860(%rbp), %rsi
incq %rsi
callq 0x43e0
jmp 0x1468f
movq %rax, %rbx
movq -0x70(%rbp), %rdi
cmpq %r12, %rdi
je 0x146c3
movq -0x60(%rbp), %rsi
jmp 0x146bb
movq %rax, %rbx
jmp 0x146c3
jmp 0x146a7
jmp 0x146a7
movq %rax, %rbx
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x146c3
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x43e0
movq %rbx, %rdi
callq 0x46d0