#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 int uint8_t;
typedef size_t uint16_t;
typedef int /*<<< orphan*/ def_len;

/* Variables and functions */
size_t __simplePNG__deflate_block_count(size_t);
size_t __simplePNG__final_deflate_block_size(size_t);
int /*<<< orphan*/ __simplePNG_to_lendian(size_t *, int);
int /*<<< orphan*/ memcpy(int *, int const *, size_t);
size_t __simplePNG__store_deflate_blocks(uint8_t *dst, const uint8_t *src,
                                         size_t size) {
  size_t dst_offset = 0;
  size_t img_offset = 0;
  size_t num_deflate_blocks = __simplePNG__deflate_block_count(size);
  size_t def_final_block_size = __simplePNG__final_deflate_block_size(size);
  uint16_t *u16_ptr;
  for (size_t i = 0; i < num_deflate_blocks; i++) {
    uint8_t def_final = 0x00;
    uint8_t is_last_block = i + 1 == num_deflate_blocks;
    if (is_last_block)
      def_final = 0x01;
    uint8_t def_compression = 0x00;
    uint8_t def_header = def_final | def_compression;
    size_t block_size = 0xffff;
    if (is_last_block)
      block_size = def_final_block_size;
    uint16_t def_len = block_size;
    __simplePNG_to_lendian(&def_len, sizeof(def_len));
    uint16_t def_nlen = ~def_len;
    dst[dst_offset] = def_header;
    dst_offset++;
    u16_ptr = (uint16_t *)(dst + dst_offset);
    u16_ptr[0] = def_len;
    u16_ptr[1] = def_nlen;
    dst_offset += sizeof(uint16_t) * 2;
    memcpy(dst + dst_offset, src + img_offset, block_size);
    img_offset += block_size;
    dst_offset += block_size;
  }
  return dst_offset;
}


x86 assembly (clang, -O0):
<__simplePNG__store_deflate_blocks>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <__simplePNG__deflate_block_count@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <__simplePNG__final_deflate_block_size@plt>
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x48(%rbp),%rax
cmp    -0x30(%rbp),%rax
jae    12af <__simplePNG__store_deflate_blocks+0x16f>
movl   $0x0,-0x4c(%rbp)
mov    -0x48(%rbp),%rax
add    $0x1,%rax
cmp    -0x30(%rbp),%rax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x50(%rbp)
cmpl   $0x0,-0x50(%rbp)
je     11c3 <__simplePNG__store_deflate_blocks+0x83>
movl   $0x1,-0x4c(%rbp)
movl   $0x0,-0x54(%rbp)
mov    -0x4c(%rbp),%eax
or     -0x54(%rbp),%eax
mov    %eax,-0x58(%rbp)
movq   $0xffff,-0x60(%rbp)
cmpl   $0x0,-0x50(%rbp)
je     11ed <__simplePNG__store_deflate_blocks+0xad>
mov    -0x38(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x60(%rbp),%rax
mov    %rax,-0x68(%rbp)
lea    -0x68(%rbp),%rdi
mov    $0x8,%esi
call   1060 <__simplePNG_to_lendian@plt>
mov    -0x68(%rbp),%rax
xor    $0xffffffffffffffff,%rax
mov    %rax,-0x70(%rbp)
mov    -0x58(%rbp),%edx
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x68(%rbp),%rcx
mov    -0x40(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x70(%rbp),%rcx
mov    -0x40(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x20(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x60(%rbp),%rdx
call   1030 <memcpy@plt>
mov    -0x60(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x60(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x48(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
jmp    1186 <__simplePNG__store_deflate_blocks+0x46>
mov    -0x20(%rbp),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__simplePNG__store_deflate_blocks>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbx
mov    %rsi,%r13
mov    %rdi,%r15
mov    %rdx,%rdi
call   1040 <__simplePNG__deflate_block_count@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1050 <__simplePNG__final_deflate_block_size@plt>
xor    %ebx,%ebx
test   %r12,%r12
je     11ef <__simplePNG__store_deflate_blocks+0xaf>
xor    %r14d,%r14d
mov    %r13,0x10(%rsp)
mov    %rax,0x8(%rsp)
xchg   %ax,%ax
xor    %r13d,%r13d
cmp    $0x1,%r12
sete   %r13b
mov    $0xffff,%ebp
cmove  %rax,%rbp
mov    %rbp,(%rsp)
mov    %rsp,%rdi
mov    $0x8,%esi
call   1060 <__simplePNG_to_lendian@plt>
mov    (%rsp),%rax
mov    %r13d,(%r15,%rbx,4)
mov    0x10(%rsp),%r13
mov    %rax,0x4(%r15,%rbx,4)
not    %rax
mov    %rax,0xc(%r15,%rbx,4)
lea    (%r15,%rbx,4),%rdi
add    $0x44,%rdi
lea    0x0(,%r14,4),%rsi
add    %r13,%rsi
mov    %rbp,%rdx
call   1030 <memcpy@plt>
mov    0x8(%rsp),%rax
add    %rbp,%r14
add    %rbp,%rbx
add    $0x11,%rbx
add    $0xffffffffffffffff,%r12
jne    1180 <__simplePNG__store_deflate_blocks+0x40>
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i64 @__simplePNG__store_deflate_blocks(i32* %0, i32* %1, i64 %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64*
%12 = alloca i64
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i64
%18 = alloca i64
%19 = alloca i64
store i32* %0, i32** %4
store i32* %1, i32** %5
store i64 %2, i64* %6
store i64 0, i64* %7
store i64 0, i64* %8
%20 = load i64, i64* %6
%21 = call i64 @__simplePNG__deflate_block_count(i64 %20)
store i64 %21, i64* %9
%22 = load i64, i64* %6
%23 = call i64 @__simplePNG__final_deflate_block_size(i64 %22)
store i64 %23, i64* %10
store i64 0, i64* %12
br label %24
24: ; preds = %82, %3
%25 = load i64, i64* %12
%26 = load i64, i64* %9
%27 = icmp ult i64 %25, %26
br i1 %27, label %28, label %85
28: ; preds = %24
store i32 0, i32* %13
%29 = load i64, i64* %12
%30 = add i64 %29, 1
%31 = load i64, i64* %9
%32 = icmp eq i64 %30, %31
%33 = zext i1 %32 to i32
store i32 %33, i32* %14
%34 = load i32, i32* %14
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %28
store i32 1, i32* %13
br label %37
37: ; preds = %36, %28
store i32 0, i32* %15
%38 = load i32, i32* %13
%39 = load i32, i32* %15
%40 = or i32 %38, %39
store i32 %40, i32* %16
store i64 65535, i64* %17
%41 = load i32, i32* %14
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %37
%44 = load i64, i64* %10
store i64 %44, i64* %17
br label %45
45: ; preds = %43, %37
%46 = load i64, i64* %17
store i64 %46, i64* %18
%47 = call i32 @__simplePNG_to_lendian(i64* %18, i32 8)
%48 = load i64, i64* %18
%49 = xor i64 %48, -1
store i64 %49, i64* %19
%50 = load i32, i32* %16
%51 = load i32*, i32** %4
%52 = load i64, i64* %7
%53 = getelementptr inbounds i32, i32* %51, i64 %52
store i32 %50, i32* %53
%54 = load i64, i64* %7
%55 = add i64 %54, 1
store i64 %55, i64* %7
%56 = load i32*, i32** %4
%57 = load i64, i64* %7
%58 = getelementptr inbounds i32, i32* %56, i64 %57
%59 = bitcast i32* %58 to i64*
store i64* %59, i64** %11
%60 = load i64, i64* %18
%61 = load i64*, i64** %11
%62 = getelementptr inbounds i64, i64* %61, i64 0
store i64 %60, i64* %62
%63 = load i64, i64* %19
%64 = load i64*, i64** %11
%65 = getelementptr inbounds i64, i64* %64, i64 1
store i64 %63, i64* %65
%66 = load i64, i64* %7
%67 = add i64 %66, 16
store i64 %67, i64* %7
%68 = load i32*, i32** %4
%69 = load i64, i64* %7
%70 = getelementptr inbounds i32, i32* %68, i64 %69
%71 = load i32*, i32** %5
%72 = load i64, i64* %8
%73 = getelementptr inbounds i32, i32* %71, i64 %72
%74 = load i64, i64* %17
%75 = call i32 @memcpy(i32* %70, i32* %73, i64 %74)
%76 = load i64, i64* %17
%77 = load i64, i64* %8
%78 = add i64 %77, %76
store i64 %78, i64* %8
%79 = load i64, i64* %17
%80 = load i64, i64* %7
%81 = add i64 %80, %79
store i64 %81, i64* %7
br label %82
82: ; preds = %45
%83 = load i64, i64* %12
%84 = add i64 %83, 1
store i64 %84, i64* %12
br label %24
85: ; preds = %24
%86 = load i64, i64* %7
ret i64 %86
}

LLVM IR (-O2):
define i64 @__simplePNG__store_deflate_blocks(i32* %0, i32* %1, i64 %2) {
%4 = alloca i64
%5 = tail call i64 @__simplePNG__deflate_block_count(i64 %2)
%6 = tail call i64 @__simplePNG__final_deflate_block_size(i64 %2)
%7 = icmp eq i64 %5, 0
br i1 %7, label %10, label %8
8: ; preds = %3
%9 = bitcast i64* %4 to i8*
br label %12
10: ; preds = %12, %3
%11 = phi i64 [ 0, %3 ], [ %34, %12 ]
ret i64 %11
12: ; preds = %8, %12
%13 = phi i64 [ 0, %8 ], [ %34, %12 ]
%14 = phi i64 [ 0, %8 ], [ %33, %12 ]
%15 = phi i64 [ 0, %8 ], [ %16, %12 ]
%16 = add nuw i64 %15, 1
%17 = icmp eq i64 %16, %5
%18 = zext i1 %17 to i32
%19 = select i1 %17, i64 %6, i64 65535
store i64 %19, i64* %4
%20 = call i32 @__simplePNG_to_lendian(i64* %4, i32 8)
%21 = load i64, i64* %4
%22 = xor i64 %21, -1
%23 = getelementptr inbounds i32, i32* %0, i64 %13
store i32 %18, i32* %23
%24 = add i64 %13, 1
%25 = getelementptr inbounds i32, i32* %0, i64 %24
%26 = bitcast i32* %25 to i64*
store i64 %21, i64* %26
%27 = getelementptr inbounds i32, i32* %25, i64 2
%28 = bitcast i32* %27 to i64*
store i64 %22, i64* %28
%29 = add i64 %13, 17
%30 = getelementptr inbounds i32, i32* %0, i64 %29
%31 = getelementptr inbounds i32, i32* %1, i64 %14
%32 = call i32 @memcpy(i32* %30, i32* %31, i64 %19)
%33 = add i64 %19, %14
%34 = add i64 %29, %19
%35 = icmp eq i64 %16, %5
br i1 %35, label %10, label %12
}