#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef unsigned long long uLong;
typedef int /*<<< orphan*/ uInt;
typedef int /*<<< orphan*/ Bytef;
typedef int /*<<< orphan*/ Byte;

/* Variables and functions */
int /*<<< orphan*/ *Z_NULL;
int Z_OK;
scalar_t__ calloc(int /*<<< orphan*/, int);
scalar_t__ ceil(double);
int compress(int /*<<< orphan*/ *, unsigned long long *,
             int /*<<< orphan*/ const *, unsigned int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdout;
char *buffer_zlib_compress(char *buf, unsigned long long buflen,
                           unsigned long long *comprlen) {
  int err;
  Byte *compr;
  uLong comprLen;
  comprLen = (uLong)ceil((double)1.001 * (double)(unsigned int)buflen) + 12;
  if ((compr = (Byte *)calloc((uInt)comprLen, 1)) == Z_NULL) {
    printf("out of memory\n");
    fflush(stdout);
    return NULL;
  }
  if ((err = compress(compr, &comprLen, (const Bytef *)buf,
                      (unsigned int)buflen)) != Z_OK) {
    printf("Error: %d in buffer_compress\n", err);
    return NULL;
  }
  *comprlen = comprLen;
  return (char *)compr;
}


x86 assembly (clang, -O0):
<buffer_zlib_compress>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%eax
cvtsi2sd %rax,%xmm1
movsd  0xe8c(%rip),%xmm0
mulsd  %xmm1,%xmm0
call   1040 <ceil@plt>
add    $0xc,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %eax,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x30(%rbp)
mov    0x2e38(%rip),%rcx
cmp    (%rcx),%rax
jne    11d2 <buffer_zlib_compress+0x82>
lea    0xe58(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
movq   $0x0,-0x8(%rbp)
jmp    122c <buffer_zlib_compress+0xdc>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rax
mov    %eax,%ecx
lea    -0x38(%rbp),%rsi
call   1030 <compress@plt>
mov    %eax,-0x24(%rbp)
mov    0x2dfd(%rip),%rcx
cmp    (%rcx),%eax
je     1219 <buffer_zlib_compress+0xc9>
mov    -0x24(%rbp),%esi
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    122c <buffer_zlib_compress+0xdc>
mov    -0x38(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<buffer_zlib_compress>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
mov    %r15d,%eax
cvtsi2sd %rax,%xmm0
mulsd  0xe7f(%rip),%xmm0
call   1050 <ceil@plt>
lea    0xc(%rax),%rdi
mov    %rdi,(%rsp)
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,%rbx
mov    0x2e36(%rip),%rax
cmp    %rbx,(%rax)
je     11cc <buffer_zlib_compress+0x6c>
mov    %rsp,%rsi
mov    %rbx,%rdi
mov    %r12,%rdx
mov    %r15d,%ecx
call   1030 <compress@plt>
mov    0x2e31(%rip),%rcx
cmp    (%rcx),%eax
jne    11ea <buffer_zlib_compress+0x8a>
mov    (%rsp),%rax
mov    %rax,(%r14)
jmp    11fc <buffer_zlib_compress+0x9c>
lea    0xe53(%rip),%rdi
call   1040 <puts@plt>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
xor    %ebx,%ebx
jmp    11fc <buffer_zlib_compress+0x9c>
lea    0xe17(%rip),%rdi
xor    %ebx,%ebx
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@Z_NULL = global i32* null
@.str = [15 x i8] c"out of memory\0A\00"
@stdout = global i32 0
@Z_OK = global i32 0
@.str.1 = [30 x i8] c"Error: %d in buffer_compress\0A\00"
define i8* @buffer_zlib_compress(i8* %0, i64 %1, i64* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64*
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i64
store i8* %0, i8** %5
store i64 %1, i64* %6
store i64* %2, i64** %7
%11 = load i64, i64* %6
%12 = trunc i64 %11 to i32
%13 = uitofp i32 %12 to double
%14 = fmul double 1.001000e+00, %13
%15 = call i64 @ceil(double %14)
%16 = add i64 %15, 12
store i64 %16, i64* %10
%17 = load i64, i64* %10
%18 = trunc i64 %17 to i32
%19 = call i64 @calloc(i32 %18, i32 1)
%20 = inttoptr i64 %19 to i32*
store i32* %20, i32** %9
%21 = load i32*, i32** @Z_NULL
%22 = icmp eq i32* %20, %21
br i1 %22, label %23, label %27
23: ; preds = %3
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%25 = load i32, i32* @stdout
%26 = call i32 @fflush(i32 %25)
store i8* null, i8** %4
br label %44
27: ; preds = %3
%28 = load i32*, i32** %9
%29 = load i8*, i8** %5
%30 = bitcast i8* %29 to i32*
%31 = load i64, i64* %6
%32 = trunc i64 %31 to i32
%33 = call i32 @compress(i32* %28, i64* %10, i32* %30, i32 %32)
store i32 %33, i32* %8
%34 = load i32, i32* @Z_OK
%35 = icmp ne i32 %33, %34
br i1 %35, label %36, label %39
36: ; preds = %27
%37 = load i32, i32* %8
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %37)
store i8* null, i8** %4
br label %44
39: ; preds = %27
%40 = load i64, i64* %10
%41 = load i64*, i64** %7
store i64 %40, i64* %41
%42 = load i32*, i32** %9
%43 = bitcast i32* %42 to i8*
store i8* %43, i8** %4
br label %44
44: ; preds = %39, %36, %23
%45 = load i8*, i8** %4
ret i8* %45
}

LLVM IR (-O2):
@Z_NULL = global i32* null
@stdout = global i32 0
@Z_OK = global i32 0
@.str.1 = [30 x i8] c"Error: %d in buffer_compress\0A\00"
@str = [14 x i8] c"out of memory\00"
define i8* @buffer_zlib_compress(i8* %0, i64 %1, i64* nocapture writeonly %2) {
%4 = alloca i64
%5 = bitcast i64* %4 to i8*
%6 = trunc i64 %1 to i32
%7 = uitofp i32 %6 to double
%8 = fmul double %7, 1.001000e+00
%9 = tail call i64 @ceil(double %8)
%10 = add i64 %9, 12
store i64 %10, i64* %4
%11 = trunc i64 %10 to i32
%12 = tail call i64 @calloc(i32 %11, i32 1)
%13 = inttoptr i64 %12 to i32*
%14 = load i32*, i32** @Z_NULL
%15 = icmp eq i32* %14, %13
br i1 %15, label %16, label %20
16: ; preds = %3
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%18 = load i32, i32* @stdout
%19 = tail call i32 @fflush(i32 %18)
br label %30
20: ; preds = %3
%21 = bitcast i8* %0 to i32*
%22 = call i32 @compress(i32* %13, i64* %4, i32* %21, i32 %6)
%23 = load i32, i32* @Z_OK
%24 = icmp eq i32 %22, %23
br i1 %24, label %27, label %25
25: ; preds = %20
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %22)
br label %30
27: ; preds = %20
%28 = load i64, i64* %4
store i64 %28, i64* %2
%29 = inttoptr i64 %12 to i8*
br label %30
30: ; preds = %27, %25, %16
%31 = phi i8* [ null, %16 ], [ null, %25 ], [ %29, %27 ]
ret i8* %31
}