#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 */

/* Variables and functions */
int /*<<< orphan*/ collect_component(unsigned char *, unsigned char *, int,
                                     int const, int const);
int /*<<< orphan*/ downsample_component(unsigned char *, int const, int const,
                                        int, int, int, int, int *, int *);
int /*<<< orphan*/ free(unsigned char *);
unsigned char *malloc(int const);
int /*<<< orphan*/ pack_component_array(unsigned char *, int const, int, int);
int /*<<< orphan*/ rewrite_component(unsigned char *, unsigned char *, int,
                                     int const, int);
void jp2_downsample_image_components(unsigned char *bitmap, const int width,
                                     const int height, const int bpp,
                                     const int *csampling,
                                     int *csampling_effective, int *cwidth,
                                     int *cheight) {
  unsigned char *carray;
  int compstep;
  int out_width, out_height;
  int *csampling_base;
  int i;
  carray = malloc(width * height);
  for (compstep = 0; compstep < bpp; compstep++) {
    for (i = 0; i < 4; i++)
      csampling_effective[(compstep * 4) + i] = csampling[(compstep * 4) + i];
    csampling_base = csampling_effective + (compstep * 4);
    if ((csampling_base[0] >= width) || (csampling_base[0] < 0))
      csampling_base[0] = 0;
    if ((csampling_base[1] >= height) || (csampling_base[1] < 0))
      csampling_base[1] = 0;
    if (csampling_base[2] < 1)
      csampling_base[2] = 1;
    if (csampling_base[3] < 1)
      csampling_base[3] = 1;
    collect_component(carray, bitmap, compstep, bpp, width * height);
    downsample_component(carray, width, height, csampling_base[0],
                         csampling_base[1], csampling_base[2],
                         csampling_base[3], &out_width, &out_height);
    pack_component_array(carray, width, out_width, out_height);
    rewrite_component(bitmap, carray, compstep, bpp, out_width * out_height);
    cwidth[compstep] = out_width;
    cheight[compstep] = out_height;
  }
  free(carray);
}


x86 assembly (clang, -O0):
<jp2_downsample_image_components>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    0x18(%rbp),%rax
mov    0x10(%rbp),%rax
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8,-0x20(%rbp)
mov    %r9,-0x28(%rbp)
mov    -0xc(%rbp),%edi
imul   -0x10(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    133d <jp2_downsample_image_components+0x1dd>
movl   $0x0,-0x4c(%rbp)
cmpl   $0x4,-0x4c(%rbp)
jge    11ed <jp2_downsample_image_components+0x8d>
mov    -0x20(%rbp),%rax
mov    -0x34(%rbp),%ecx
shl    $0x2,%ecx
add    -0x4c(%rbp),%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x28(%rbp),%rax
mov    -0x34(%rbp),%ecx
shl    $0x2,%ecx
add    -0x4c(%rbp),%ecx
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    11af <jp2_downsample_image_components+0x4f>
mov    -0x28(%rbp),%rax
mov    -0x34(%rbp),%ecx
shl    $0x2,%ecx
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    (%rax),%eax
cmp    -0xc(%rbp),%eax
jge    1221 <jp2_downsample_image_components+0xc1>
mov    -0x48(%rbp),%rax
cmpl   $0x0,(%rax)
jge    122b <jp2_downsample_image_components+0xcb>
mov    -0x48(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x48(%rbp),%rax
mov    0x4(%rax),%eax
cmp    -0x10(%rbp),%eax
jge    1249 <jp2_downsample_image_components+0xe9>
mov    -0x48(%rbp),%rax
cmpl   $0x0,0x4(%rax)
jge    1254 <jp2_downsample_image_components+0xf4>
mov    -0x48(%rbp),%rax
movl   $0x0,0x4(%rax)
mov    -0x48(%rbp),%rax
cmpl   $0x1,0x8(%rax)
jge    126d <jp2_downsample_image_components+0x10d>
mov    -0x48(%rbp),%rax
movl   $0x1,0x8(%rax)
mov    -0x48(%rbp),%rax
cmpl   $0x1,0xc(%rax)
jge    1286 <jp2_downsample_image_components+0x126>
mov    -0x48(%rbp),%rax
movl   $0x1,0xc(%rax)
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x34(%rbp),%edx
mov    -0x14(%rbp),%ecx
mov    -0xc(%rbp),%r8d
imul   -0x10(%rbp),%r8d
call   1040 <collect_component@plt>
mov    -0x30(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0x48(%rbp),%rax
mov    (%rax),%ecx
mov    -0x48(%rbp),%rax
mov    0x4(%rax),%r8d
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%r9d
mov    -0x48(%rbp),%rax
mov    0xc(%rax),%r11d
lea    -0x38(%rbp),%r10
lea    -0x3c(%rbp),%rax
mov    %r11d,(%rsp)
mov    %r10,0x8(%rsp)
mov    %rax,0x10(%rsp)
call   1050 <downsample_component@plt>
mov    -0x30(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x38(%rbp),%edx
mov    -0x3c(%rbp),%ecx
call   1060 <pack_component_array@plt>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x34(%rbp),%edx
mov    -0x14(%rbp),%ecx
mov    -0x38(%rbp),%r8d
imul   -0x3c(%rbp),%r8d
call   1080 <rewrite_component@plt>
mov    -0x38(%rbp),%edx
mov    0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x3c(%rbp),%edx
mov    0x18(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    119c <jp2_downsample_image_components+0x3c>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<jp2_downsample_image_components>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %r9,%rbx
mov    %r8,0x30(%rsp)
mov    %ecx,%r12d
mov    %esi,%ebp
mov    %rdi,0x20(%rsp)
mov    %edx,0x10(%rsp)
mov    %edx,%edi
imul   %esi,%edi
mov    %edi,0x14(%rsp)
call   1070 <malloc@plt>
mov    %rax,0x18(%rsp)
mov    %r12d,0xc(%rsp)
test   %r12d,%r12d
jle    1312 <jp2_downsample_image_components+0x1b2>
mov    0xc(%rsp),%eax
mov    %rax,0x28(%rsp)
mov    $0xc,%r13d
xor    %r12d,%r12d
mov    0x18(%rsp),%r14
mov    0x28(%rsp),%r15
jmp    127b <jp2_downsample_image_components+0x11b>
cs nopw 0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    0x20(%rsp),%rsi
mov    %r12d,%edx
mov    %r15d,%ecx
mov    0x14(%rsp),%r8d
call   1040 <collect_component@plt>
mov    -0xc(%rbx,%r13,1),%ecx
mov    -0x8(%rbx,%r13,1),%r8d
mov    -0x4(%rbx,%r13,1),%r9d
mov    (%rbx,%r13,1),%r10d
sub    $0x8,%rsp
mov    %r14,%rdi
mov    %ebp,%esi
mov    0x18(%rsp),%edx
lea    0xc(%rsp),%rax
push   %rax
lea    0x18(%rsp),%rax
push   %rax
push   %r10
call   1050 <downsample_component@plt>
add    $0x20,%rsp
mov    0x8(%rsp),%edx
mov    0x4(%rsp),%ecx
mov    %r14,%rdi
mov    %ebp,%esi
call   1060 <pack_component_array@plt>
mov    0x4(%rsp),%r8d
imul   0x8(%rsp),%r8d
mov    0x20(%rsp),%rdi
mov    %r14,%rsi
mov    %r12d,%edx
mov    0xc(%rsp),%ecx
call   1080 <rewrite_component@plt>
mov    0x8(%rsp),%eax
mov    0x70(%rsp),%rcx
mov    %eax,(%rcx,%r12,4)
mov    0x4(%rsp),%eax
mov    0x78(%rsp),%rcx
mov    %eax,(%rcx,%r12,4)
add    $0x1,%r12
add    $0x10,%r13
cmp    %r12,%r15
je     1312 <jp2_downsample_image_components+0x1b2>
mov    0x30(%rsp),%rdx
mov    -0xc(%rdx,%r13,1),%eax
mov    %eax,-0xc(%rbx,%r13,1)
mov    -0x8(%rdx,%r13,1),%ecx
mov    %ecx,-0x8(%rbx,%r13,1)
mov    -0x4(%rdx,%r13,1),%ecx
mov    %ecx,-0x4(%rbx,%r13,1)
mov    (%rdx,%r13,1),%ecx
mov    %ecx,(%rbx,%r13,1)
cmp    %ebp,%eax
jge    12ae <jp2_downsample_image_components+0x14e>
test   %eax,%eax
jns    12b7 <jp2_downsample_image_components+0x157>
movl   $0x0,-0xc(%rbx,%r13,1)
mov    -0x8(%rbx,%r13,1),%eax
cmp    0x10(%rsp),%eax
jge    12e0 <jp2_downsample_image_components+0x180>
test   %eax,%eax
js     12e0 <jp2_downsample_image_components+0x180>
cmpl   $0x0,-0x4(%rbx,%r13,1)
jle    12f1 <jp2_downsample_image_components+0x191>
cmpl   $0x0,(%rbx,%r13,1)
jg     11d0 <jp2_downsample_image_components+0x70>
jmp    1305 <jp2_downsample_image_components+0x1a5>
nopl   0x0(%rax,%rax,1)
movl   $0x0,-0x8(%rbx,%r13,1)
cmpl   $0x0,-0x4(%rbx,%r13,1)
jg     12ce <jp2_downsample_image_components+0x16e>
movl   $0x1,-0x4(%rbx,%r13,1)
cmpl   $0x0,(%rbx,%r13,1)
jg     11d0 <jp2_downsample_image_components+0x70>
movl   $0x1,(%rbx,%r13,1)
jmp    11d0 <jp2_downsample_image_components+0x70>
mov    0x18(%rsp),%rdi
call   1030 <free@plt>
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define void @jp2_downsample_image_components(i8* %0, i32 %1, i32 %2, i32 %3, i32* nocapture %4, i32* nocapture %5, i32* nocapture writeonly %6, i32* nocapture writeonly %7) {
%9 = alloca i32
%10 = alloca i32
%11 = bitcast i32* %9 to i8*
%12 = bitcast i32* %10 to i8*
%13 = mul nsw i32 %2, %1
%14 = tail call i8* @malloc(i32 %13)
%15 = icmp sgt i32 %3, 0
br i1 %15, label %16, label %79
16: ; preds = %8
%17 = zext i32 %3 to i64
br label %18
18: ; preds = %16, %58
%19 = phi i64 [ 0, %16 ], [ %77, %58 ]
%20 = shl nsw i64 %19, 2
%21 = getelementptr inbounds i32, i32* %4, i64 %20
%22 = load i32, i32* %21
%23 = getelementptr inbounds i32, i32* %5, i64 %20
store i32 %22, i32* %23
%24 = or i64 %20, 1
%25 = getelementptr inbounds i32, i32* %4, i64 %24
%26 = load i32, i32* %25
%27 = getelementptr inbounds i32, i32* %5, i64 %24
store i32 %26, i32* %27
%28 = or i64 %20, 2
%29 = getelementptr inbounds i32, i32* %4, i64 %28
%30 = load i32, i32* %29
%31 = getelementptr inbounds i32, i32* %5, i64 %28
store i32 %30, i32* %31
%32 = or i64 %20, 3
%33 = getelementptr inbounds i32, i32* %4, i64 %32
%34 = load i32, i32* %33
%35 = getelementptr inbounds i32, i32* %5, i64 %32
store i32 %34, i32* %35
%36 = load i32, i32* %23
%37 = icmp sge i32 %36, %1
%38 = icmp slt i32 %36, 0
%39 = or i1 %37, %38
br i1 %39, label %40, label %41
40: ; preds = %18
store i32 0, i32* %23
br label %41
41: ; preds = %18, %40
%42 = getelementptr inbounds i32, i32* %23, i64 1
%43 = load i32, i32* %42
%44 = icmp sge i32 %43, %2
%45 = icmp slt i32 %43, 0
%46 = or i1 %44, %45
br i1 %46, label %47, label %48
47: ; preds = %41
store i32 0, i32* %42
br label %48
48: ; preds = %41, %47
%49 = getelementptr inbounds i32, i32* %23, i64 2
%50 = load i32, i32* %49
%51 = icmp slt i32 %50, 1
br i1 %51, label %52, label %53
52: ; preds = %48
store i32 1, i32* %49
br label %53
53: ; preds = %52, %48
%54 = getelementptr inbounds i32, i32* %23, i64 3
%55 = load i32, i32* %54
%56 = icmp slt i32 %55, 1
br i1 %56, label %57, label %58
57: ; preds = %53
store i32 1, i32* %54
br label %58
58: ; preds = %57, %53
%59 = trunc i64 %19 to i32
%60 = call i32 @collect_component(i8* %14, i8* %0, i32 %59, i32 %3, i32 %13)
%61 = load i32, i32* %23
%62 = load i32, i32* %42
%63 = load i32, i32* %49
%64 = load i32, i32* %54
%65 = call i32 @downsample_component(i8* %14, i32 %1, i32 %2, i32 %61, i32 %62, i32 %63, i32 %64, i32* %9, i32* %10)
%66 = load i32, i32* %9
%67 = load i32, i32* %10
%68 = call i32 @pack_component_array(i8* %14, i32 %1, i32 %66, i32 %67)
%69 = load i32, i32* %9
%70 = load i32, i32* %10
%71 = mul nsw i32 %70, %69
%72 = call i32 @rewrite_component(i8* %0, i8* %14, i32 %59, i32 %3, i32 %71)
%73 = load i32, i32* %9
%74 = getelementptr inbounds i32, i32* %6, i64 %19
store i32 %73, i32* %74
%75 = load i32, i32* %10
%76 = getelementptr inbounds i32, i32* %7, i64 %19
store i32 %75, i32* %76
%77 = add nuw nsw i64 %19, 1
%78 = icmp eq i64 %77, %17
br i1 %78, label %79, label %18
79: ; preds = %58, %8
%80 = call i32 @free(i8* %14)
ret void
}