// File: mbourand[P]webserv/srcs/Utils/Compress.cpp
// Function: compress_gzip(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, unsigned int)
std::string compress_gzip(const std::string &str, unsigned int compressionlevel)
{
	z_stream zs;
	ft::memset(&zs, 0, sizeof(zs));
	if (deflateInit2(&zs, compressionlevel, Z_DEFLATED, 31, 9, Z_DEFAULT_STRATEGY) != Z_OK)
		throw(std::runtime_error("deflateInit2() 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 $0x80f0, %rsp           # imm = 0x80F0
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
movl $0x8, %edx
movl $0x1f, %ecx
movl $0x9, %r8d
xorl %r9d, %r9d
leaq 0x48034(%rip), %rax     # 0x6acb2
movq %rax, (%rsp)
movl $0x70, 0x8(%rsp)
callq 0x90c0
cmpl $0x0, %eax
je 0x22cf6
movl $0x10, %edi
callq 0x9340
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x80c0(%rbp)
leaq 0x48007(%rip), %rsi     # 0x6acb9
callq 0x9290
jmp 0x22cb9
movq -0x80c0(%rbp), %rdi
movq 0x6c329(%rip), %rsi     # 0x8eff0
movq 0x6c2ca(%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 0x22e7c
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 0x22d50
movq -0x80b8(%rbp), %rdi
movl -0x80c4(%rbp), %eax
movl %eax, -0x98(%rbp)
callq 0x9480
cmpq -0x60(%rbp), %rax
jae 0x22dbd
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 0x22da4
jmp 0x22dbd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x90(%rbp)
movl %eax, -0x94(%rbp)
jmp 0x22e70
jmp 0x22dbf
cmpl $0x0, -0x98(%rbp)
je 0x22d25
leaq -0x88(%rbp), %rdi
callq 0x9420
jmp 0x22dda
cmpl $0x1, -0x98(%rbp)
je 0x22e44
movl $0x10, %edi
callq 0x9340
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x80d8(%rbp)
leaq 0x47ecf(%rip), %rsi     # 0x6acd0
callq 0x9290
jmp 0x22e08
movq -0x80d8(%rbp), %rdi
movq 0x6c1da(%rip), %rsi     # 0x8eff0
movq 0x6c17b(%rip), %rdx     # 0x8ef98
callq 0x9aa0
jmp 0x22e88
movq -0x80d8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x90(%rbp)
movl %eax, -0x94(%rbp)
callq 0x94c0
jmp 0x22e70
movb $0x1, -0x80a1(%rbp)
testb $0x1, -0x80a1(%rbp)
jne 0x22e60
movq -0x80b8(%rbp), %rdi
callq 0x9c98
movq -0x80b0(%rbp), %rax
addq $0x80f0, %rsp           # imm = 0x80F0
popq %rbp
retq
movq -0x80b8(%rbp), %rdi
callq 0x9c98
movq -0x90(%rbp), %rdi
callq 0x9af0
nopl (%rax,%rax)