// File: mbourand[P]webserv/srcs/Utils/Compress.cpp
// Function: compress_deflate(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, unsigned int)
std::string compress_deflate(const std::string &str, unsigned int compressionlevel)
{
	z_stream zs;
	ft::memset(&zs, 0, sizeof(zs));
	if (deflateInit(&zs, compressionlevel) != Z_OK)
		throw(std::runtime_error("deflateInit() failed."));

	zs.next_in = (Bytef*)str.data();
	zs.avail_in = str.size();
	int ret;
	char outbuffer[32768];
	std::string outstring;

	do {
		zs.next_out = reinterpret_cast<Bytef*>(outbuffer);
		zs.avail_out = sizeof(outbuffer);
		ret = deflate(&zs, Z_FINISH);
		if (outstring.size() < zs.total_out) {
			outstring.append(outbuffer,
			zs.total_out - outstring.size());
		}
	} while (ret == Z_OK);
	deflateEnd(&zs);

	if (ret != Z_STREAM_END)
		throw(std::runtime_error("Exception during zlib compression."));
	return (outstring);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80e0, %rsp           # imm = 0x80E0
movq %rdi, -0x80b8(%rbp)
movq %rdi, %rax
movq %rax, -0x80b0(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
leaq -0x88(%rbp), %rdi
xorl %esi, %esi
movl $0x70, %edx
callq 0x328b0
movl -0x14(%rbp), %esi
leaq -0x88(%rbp), %rdi
leaq 0x47dd7(%rip), %rdx     # 0x6acb2
movl $0x70, %ecx
callq 0x9040
cmpl $0x0, %eax
je 0x22f4c
movl $0x10, %edi
callq 0x9340
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x80c0(%rbp)
leaq 0x47deb(%rip), %rsi     # 0x6acf3
callq 0x9290
jmp 0x22f0f
movq -0x80c0(%rbp), %rdi
movq 0x6c0d3(%rip), %rsi     # 0x8eff0
movq 0x6c074(%rip), %rdx     # 0x8ef98
callq 0x9aa0
movq -0x80c0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x90(%rbp)
movl %eax, -0x94(%rbp)
callq 0x94c0
jmp 0x230d2
movq -0x10(%rbp), %rdi
callq 0x9850
movq %rax, -0x88(%rbp)
movq -0x10(%rbp), %rdi
callq 0x9480
movq -0x80b8(%rbp), %rdi
movl %eax, -0x80(%rbp)
movb $0x0, -0x80a1(%rbp)
callq 0x98c0
leaq -0x80a0(%rbp), %rax
movq %rax, -0x70(%rbp)
movl $0x8000, -0x68(%rbp)    # imm = 0x8000
leaq -0x88(%rbp), %rdi
movl $0x4, %esi
callq 0x9320
movl %eax, -0x80c4(%rbp)
jmp 0x22fa6
movq -0x80b8(%rbp), %rdi
movl -0x80c4(%rbp), %eax
movl %eax, -0x98(%rbp)
callq 0x9480
cmpq -0x60(%rbp), %rax
jae 0x23013
movq -0x80b8(%rbp), %rdi
movq -0x60(%rbp), %rax
movq %rax, -0x80d0(%rbp)
callq 0x9480
movq -0x80d0(%rbp), %rdx
movq -0x80b8(%rbp), %rdi
subq %rax, %rdx
leaq -0x80a0(%rbp), %rsi
callq 0x90f0
jmp 0x22ffa
jmp 0x23013
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x90(%rbp)
movl %eax, -0x94(%rbp)
jmp 0x230c6
jmp 0x23015
cmpl $0x0, -0x98(%rbp)
je 0x22f7b
leaq -0x88(%rbp), %rdi
callq 0x9420
jmp 0x23030
cmpl $0x1, -0x98(%rbp)
je 0x2309a
movl $0x10, %edi
callq 0x9340
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x80d8(%rbp)
leaq 0x47c79(%rip), %rsi     # 0x6acd0
callq 0x9290
jmp 0x2305e
movq -0x80d8(%rbp), %rdi
movq 0x6bf84(%rip), %rsi     # 0x8eff0
movq 0x6bf25(%rip), %rdx     # 0x8ef98
callq 0x9aa0
jmp 0x230de
movq -0x80d8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x90(%rbp)
movl %eax, -0x94(%rbp)
callq 0x94c0
jmp 0x230c6
movb $0x1, -0x80a1(%rbp)
testb $0x1, -0x80a1(%rbp)
jne 0x230b6
movq -0x80b8(%rbp), %rdi
callq 0x9c98
movq -0x80b0(%rbp), %rax
addq $0x80e0, %rsp           # imm = 0x80E0
popq %rbp
retq
movq -0x80b8(%rbp), %rdi
callq 0x9c98
movq -0x90(%rbp), %rdi
callq 0x9af0
nop