#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 int uint32_t;
typedef int /*<<< orphan*/ uint16_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int __simplePNG__adler32(int *, size_t);
size_t __simplePNG__deflate_block_count(size_t);
size_t __simplePNG__store_deflate_blocks(int *, int *, size_t);
int /*<<< orphan*/ __simplePNG__write_chunk(int /*<<< orphan*/ *, int *, int *,
                                            size_t);
int /*<<< orphan*/ free(int *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ memcpy(int *, int const *, int);
void __simplePNG_write_IDAT(FILE *f, uint32_t width, uint32_t height,
                            uint8_t const *rgb_image) {
  uint8_t chunk_type[4];
  uint32_t *u32_ptr;
  size_t img_size = width * height * 3;
  size_t img_size_with_filter = img_size + height;
  size_t zlib_meta_size = sizeof(uint8_t) * 2 + sizeof(uint32_t);
  size_t num_deflate_blocks =
      __simplePNG__deflate_block_count(img_size_with_filter);
  size_t deflate_meta_per_block = (sizeof(uint8_t) + sizeof(uint16_t) * 2);
  size_t deflate_size =
      img_size_with_filter + deflate_meta_per_block * num_deflate_blocks;
  size_t idat_size = zlib_meta_size + deflate_size;
  uint8_t *img_with_filter = (uint8_t *)malloc(img_size_with_filter);
  for (size_t i = 0; i < height; i++) {
    size_t scanline_offset = i * width * 3 + i;
    img_with_filter[scanline_offset] = 0x00;
    memcpy(img_with_filter + scanline_offset + 1, rgb_image + (i * width * 3),
           width * 3);
  }
  uint8_t *idat_chunk = (uint8_t *)malloc(idat_size);
  chunk_type[0] = 'I';
  chunk_type[1] = 'D';
  chunk_type[2] = 'A';
  chunk_type[3] = 'T';
  uint8_t zlib_compression = 0x08;
  uint8_t zlib_window = 0x00;
  uint8_t zlib_cmf = zlib_window << 4 | zlib_compression;
  uint8_t zlib_dict = 0x00;
  uint8_t zlib_flevel = 0x00;
  uint8_t zlib_fcheck = 0x1d;
  uint8_t zlib_flg = zlib_dict | zlib_flevel | zlib_fcheck;
  idat_chunk[0] = zlib_cmf;
  idat_chunk[1] = zlib_flg;
  size_t idat_offset = 2;
  size_t deflate_bytes = __simplePNG__store_deflate_blocks(
      idat_chunk + idat_offset, img_with_filter, img_size_with_filter);
  uint32_t zlib_adler =
      __simplePNG__adler32(img_with_filter, img_size_with_filter);
  u32_ptr = (uint32_t *)(idat_chunk + idat_offset + deflate_size);
  u32_ptr[0] = zlib_adler;
  __simplePNG__write_chunk(f, chunk_type, idat_chunk, idat_size);
  free(img_with_filter);
  free(idat_chunk);
}


x86 assembly (clang, -O0):
<__simplePNG_write_IDAT>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0xc(%rbp),%eax
imul   -0x10(%rbp),%eax
imul   $0x3,%eax,%eax
cltq
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
movslq -0x10(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
movq   $0xc,-0x50(%rbp)
mov    -0x48(%rbp),%rdi
call   1060 <__simplePNG__deflate_block_count@plt>
mov    %rax,-0x58(%rbp)
movq   $0xc,-0x60(%rbp)
mov    -0x48(%rbp),%rax
mov    -0x60(%rbp),%rcx
imul   -0x58(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x68(%rbp)
mov    -0x50(%rbp),%rax
add    -0x68(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x48(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x78(%rbp)
movq   $0x0,-0x80(%rbp)
mov    -0x80(%rbp),%rax
movslq -0x10(%rbp),%rcx
cmp    %rcx,%rax
jae    1283 <__simplePNG_write_IDAT+0x113>
mov    -0x80(%rbp),%rax
movslq -0xc(%rbp),%rcx
imul   %rcx,%rax
imul   $0x3,%rax,%rax
add    -0x80(%rbp),%rax
mov    %rax,-0x88(%rbp)
mov    -0x78(%rbp),%rax
mov    -0x88(%rbp),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x78(%rbp),%rdi
mov    -0x88(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
add    $0x4,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x80(%rbp),%rax
movslq -0xc(%rbp),%rcx
imul   %rcx,%rax
imul   $0x3,%rax,%rax
shl    $0x2,%rax
add    %rax,%rsi
imul   $0x3,-0xc(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x80(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x80(%rbp)
jmp    11fa <__simplePNG_write_IDAT+0x8a>
mov    -0x70(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x90(%rbp)
movl   $0x49,-0x30(%rbp)
movl   $0x44,-0x2c(%rbp)
movl   $0x41,-0x28(%rbp)
movl   $0x54,-0x24(%rbp)
movl   $0x8,-0x94(%rbp)
movl   $0x0,-0x98(%rbp)
mov    -0x98(%rbp),%eax
shl    $0x4,%eax
or     -0x94(%rbp),%eax
mov    %eax,-0x9c(%rbp)
movl   $0x0,-0xa0(%rbp)
movl   $0x0,-0xa4(%rbp)
movl   $0x1d,-0xa8(%rbp)
mov    -0xa0(%rbp),%eax
or     -0xa4(%rbp),%eax
or     -0xa8(%rbp),%eax
mov    %eax,-0xac(%rbp)
mov    -0x9c(%rbp),%ecx
mov    -0x90(%rbp),%rax
mov    %ecx,(%rax)
mov    -0xac(%rbp),%ecx
mov    -0x90(%rbp),%rax
mov    %ecx,0x4(%rax)
movq   $0x2,-0xb8(%rbp)
mov    -0x90(%rbp),%rdi
mov    -0xb8(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x78(%rbp),%rsi
mov    -0x48(%rbp),%rdx
call   1040 <__simplePNG__store_deflate_blocks@plt>
mov    %rax,-0xc0(%rbp)
mov    -0x78(%rbp),%rdi
mov    -0x48(%rbp),%rsi
call   1090 <__simplePNG__adler32@plt>
mov    %eax,-0xc4(%rbp)
mov    -0x90(%rbp),%rax
mov    -0xb8(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    -0x68(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0xc4(%rbp),%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    -0x90(%rbp),%rdx
mov    -0x70(%rbp),%rcx
call   1080 <__simplePNG__write_chunk@plt>
mov    -0x78(%rbp),%rdi
call   1030 <free@plt>
mov    -0x90(%rbp),%rdi
call   1030 <free@plt>
add    $0xd0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__simplePNG_write_IDAT>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rcx,%rbx
mov    %esi,%r12d
mov    %rdi,0x28(%rsp)
lea    (%r12,%r12,2),%ebp
mov    %ebp,%eax
imul   %edx,%eax
movslq %eax,%r15
movslq %edx,%r14
add    %r14,%r15
mov    %r15,%rdi
call   1060 <__simplePNG__deflate_block_count@plt>
lea    (%rax,%rax,2),%rax
lea    (%r15,%rax,4),%rcx
mov    %rcx,0x10(%rsp)
lea    (%r15,%rax,4),%rax
add    $0xc,%rax
mov    %rax,0x18(%rsp)
mov    %r15,0x20(%rsp)
mov    %r15,%rdi
call   1070 <malloc@plt>
mov    %rax,0x8(%rsp)
test   %r14d,%r14d
je     1222 <__simplePNG_write_IDAT+0xb2>
movslq %r12d,%rax
cmp    $0x2,%r14
mov    $0x1,%r15d
cmovae %r14,%r15
mov    0x8(%rsp),%r12
add    $0x4,%r12
shl    $0x2,%rax
lea    (%rax,%rax,2),%r14
lea    (%rax,%rax,2),%r13
add    $0x4,%r13
xchg   %ax,%ax
movl   $0x0,-0x4(%r12)
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1050 <memcpy@plt>
add    %r13,%r12
add    %r14,%rbx
add    $0xffffffffffffffff,%r15
jne    1200 <__simplePNG_write_IDAT+0x90>
mov    0x18(%rsp),%r14
mov    %r14,%rdi
call   1070 <malloc@plt>
mov    %rax,%rbx
movaps 0xdc7(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movabs $0x1d00000008,%rax
mov    %rax,(%rbx)
lea    0x8(%rbx),%rdi
mov    0x8(%rsp),%r15
mov    %r15,%rsi
mov    0x20(%rsp),%rbp
mov    %rbp,%rdx
call   1040 <__simplePNG__store_deflate_blocks@plt>
mov    %r15,%rdi
mov    %rbp,%rsi
call   1090 <__simplePNG__adler32@plt>
mov    0x10(%rsp),%rcx
mov    %eax,0x8(%rbx,%rcx,4)
lea    0x30(%rsp),%rsi
mov    0x28(%rsp),%rdi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1080 <__simplePNG__write_chunk@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @__simplePNG_write_IDAT(i32* %0, i32 %1, i32 %2, i32* %3) {
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca [4 x i32]
%10 = alloca i32*
%11 = alloca i64
%12 = alloca i64
%13 = alloca i64
%14 = alloca i64
%15 = alloca i64
%16 = alloca i64
%17 = alloca i64
%18 = alloca i32*
%19 = alloca i64
%20 = alloca i64
%21 = alloca i32*
%22 = alloca i32
%23 = alloca i32
%24 = alloca i32
%25 = alloca i32
%26 = alloca i32
%27 = alloca i32
%28 = alloca i32
%29 = alloca i64
%30 = alloca i64
%31 = alloca i32
store i32* %0, i32** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32* %3, i32** %8
%32 = load i32, i32* %6
%33 = load i32, i32* %7
%34 = mul nsw i32 %32, %33
%35 = mul nsw i32 %34, 3
%36 = sext i32 %35 to i64
store i64 %36, i64* %11
%37 = load i64, i64* %11
%38 = load i32, i32* %7
%39 = sext i32 %38 to i64
%40 = add i64 %37, %39
store i64 %40, i64* %12
store i64 12, i64* %13
%41 = load i64, i64* %12
%42 = call i64 @__simplePNG__deflate_block_count(i64 %41)
store i64 %42, i64* %14
store i64 12, i64* %15
%43 = load i64, i64* %12
%44 = load i64, i64* %15
%45 = load i64, i64* %14
%46 = mul i64 %44, %45
%47 = add i64 %43, %46
store i64 %47, i64* %16
%48 = load i64, i64* %13
%49 = load i64, i64* %16
%50 = add i64 %48, %49
store i64 %50, i64* %17
%51 = load i64, i64* %12
%52 = call i64 @malloc(i64 %51)
%53 = inttoptr i64 %52 to i32*
store i32* %53, i32** %18
store i64 0, i64* %19
br label %54
54: ; preds = %84, %4
%55 = load i64, i64* %19
%56 = load i32, i32* %7
%57 = sext i32 %56 to i64
%58 = icmp ult i64 %55, %57
br i1 %58, label %59, label %87
59: ; preds = %54
%60 = load i64, i64* %19
%61 = load i32, i32* %6
%62 = sext i32 %61 to i64
%63 = mul i64 %60, %62
%64 = mul i64 %63, 3
%65 = load i64, i64* %19
%66 = add i64 %64, %65
store i64 %66, i64* %20
%67 = load i32*, i32** %18
%68 = load i64, i64* %20
%69 = getelementptr inbounds i32, i32* %67, i64 %68
store i32 0, i32* %69
%70 = load i32*, i32** %18
%71 = load i64, i64* %20
%72 = getelementptr inbounds i32, i32* %70, i64 %71
%73 = getelementptr inbounds i32, i32* %72, i64 1
%74 = load i32*, i32** %8
%75 = load i64, i64* %19
%76 = load i32, i32* %6
%77 = sext i32 %76 to i64
%78 = mul i64 %75, %77
%79 = mul i64 %78, 3
%80 = getelementptr inbounds i32, i32* %74, i64 %79
%81 = load i32, i32* %6
%82 = mul nsw i32 %81, 3
%83 = call i32 @memcpy(i32* %73, i32* %80, i32 %82)
br label %84
84: ; preds = %59
%85 = load i64, i64* %19
%86 = add i64 %85, 1
store i64 %86, i64* %19
br label %54
87: ; preds = %54
%88 = load i64, i64* %17
%89 = call i64 @malloc(i64 %88)
%90 = inttoptr i64 %89 to i32*
store i32* %90, i32** %21
%91 = getelementptr inbounds [4 x i32], [4 x i32]* %9, i64 0, i64 0
store i32 73, i32* %91
%92 = getelementptr inbounds [4 x i32], [4 x i32]* %9, i64 0, i64 1
store i32 68, i32* %92
%93 = getelementptr inbounds [4 x i32], [4 x i32]* %9, i64 0, i64 2
store i32 65, i32* %93
%94 = getelementptr inbounds [4 x i32], [4 x i32]* %9, i64 0, i64 3
store i32 84, i32* %94
store i32 8, i32* %22
store i32 0, i32* %23
%95 = load i32, i32* %23
%96 = shl i32 %95, 4
%97 = load i32, i32* %22
%98 = or i32 %96, %97
store i32 %98, i32* %24
store i32 0, i32* %25
store i32 0, i32* %26
store i32 29, i32* %27
%99 = load i32, i32* %25
%100 = load i32, i32* %26
%101 = or i32 %99, %100
%102 = load i32, i32* %27
%103 = or i32 %101, %102
store i32 %103, i32* %28
%104 = load i32, i32* %24
%105 = load i32*, i32** %21
%106 = getelementptr inbounds i32, i32* %105, i64 0
store i32 %104, i32* %106
%107 = load i32, i32* %28
%108 = load i32*, i32** %21
%109 = getelementptr inbounds i32, i32* %108, i64 1
store i32 %107, i32* %109
store i64 2, i64* %29
%110 = load i32*, i32** %21
%111 = load i64, i64* %29
%112 = getelementptr inbounds i32, i32* %110, i64 %111
%113 = load i32*, i32** %18
%114 = load i64, i64* %12
%115 = call i64 @__simplePNG__store_deflate_blocks(i32* %112, i32* %113, i64 %114)
store i64 %115, i64* %30
%116 = load i32*, i32** %18
%117 = load i64, i64* %12
%118 = call i32 @__simplePNG__adler32(i32* %116, i64 %117)
store i32 %118, i32* %31
%119 = load i32*, i32** %21
%120 = load i64, i64* %29
%121 = getelementptr inbounds i32, i32* %119, i64 %120
%122 = load i64, i64* %16
%123 = getelementptr inbounds i32, i32* %121, i64 %122
store i32* %123, i32** %10
%124 = load i32, i32* %31
%125 = load i32*, i32** %10
%126 = getelementptr inbounds i32, i32* %125, i64 0
store i32 %124, i32* %126
%127 = load i32*, i32** %5
%128 = getelementptr inbounds [4 x i32], [4 x i32]* %9, i64 0, i64 0
%129 = load i32*, i32** %21
%130 = load i64, i64* %17
%131 = call i32 @__simplePNG__write_chunk(i32* %127, i32* %128, i32* %129, i64 %130)
%132 = load i32*, i32** %18
%133 = call i32 @free(i32* %132)
%134 = load i32*, i32** %21
%135 = call i32 @free(i32* %134)
ret void
}

LLVM IR (-O2):
define void @__simplePNG_write_IDAT(i32* %0, i32 %1, i32 %2, i32* %3) {
%5 = alloca <4 x i32>
%6 = bitcast <4 x i32>* %5 to i8*
%7 = mul i32 %1, 3
%8 = mul i32 %7, %2
%9 = sext i32 %8 to i64
%10 = sext i32 %2 to i64
%11 = add nsw i64 %9, %10
%12 = tail call i64 @__simplePNG__deflate_block_count(i64 %11)
%13 = mul i64 %12, 12
%14 = add i64 %13, %11
%15 = add i64 %14, 12
%16 = tail call i64 @malloc(i64 %11)
%17 = inttoptr i64 %16 to i32*
%18 = icmp eq i32 %2, 0
br i1 %18, label %23, label %19
19: ; preds = %4
%20 = sext i32 %1 to i64
%21 = mul nsw i64 %20, 3
%22 = call i64 @llvm.umax.i64(i64 %10, i64 1)
br label %35
23: ; preds = %35, %4
%24 = tail call i64 @malloc(i64 %15)
%25 = inttoptr i64 %24 to i32*
%26 = getelementptr inbounds <4 x i32>, <4 x i32>* %5, i64 0, i64 0
store <4 x i32> <i32 73, i32 68, i32 65, i32 84>, <4 x i32>* %5
store i32 8, i32* %25
%27 = getelementptr inbounds i32, i32* %25, i64 1
store i32 29, i32* %27
%28 = getelementptr inbounds i32, i32* %25, i64 2
%29 = tail call i64 @__simplePNG__store_deflate_blocks(i32* %28, i32* %17, i64 %11)
%30 = tail call i32 @__simplePNG__adler32(i32* %17, i64 %11)
%31 = getelementptr inbounds i32, i32* %28, i64 %14
store i32 %30, i32* %31
%32 = call i32 @__simplePNG__write_chunk(i32* %0, i32* %26, i32* %25, i64 %15)
%33 = call i32 @free(i32* %17)
%34 = call i32 @free(i32* %25)
ret void
35: ; preds = %19, %35
%36 = phi i64 [ 0, %19 ], [ %43, %35 ]
%37 = mul i64 %21, %36
%38 = add i64 %37, %36
%39 = getelementptr inbounds i32, i32* %17, i64 %38
store i32 0, i32* %39
%40 = getelementptr inbounds i32, i32* %39, i64 1
%41 = getelementptr inbounds i32, i32* %3, i64 %37
%42 = tail call i32 @memcpy(i32* %40, i32* %41, i32 %7)
%43 = add nuw i64 %36, 1
%44 = icmp eq i64 %43, %22
br i1 %44, label %23, label %35
}