#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 /*<<< orphan*/ uint8_t;

/* Variables and functions */
int /*<<< orphan*/ convolve(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *get_matrix_at(int /*<<< orphan*/ *, int, int, int, int,
                                  int);
int /*<<< orphan*/ *malloc(int);
uint8_t *convolve_image(uint8_t *data, uint8_t *kernel, int height, int width,
                        int size) {
  uint8_t *processed = malloc(width * height);
  int index = 0;
  for (int i = 0; i < height; ++i) {
    for (int j = 0; j < width; ++j) {
      uint8_t *chunk = get_matrix_at(data, i, j, height, width, size);
      processed[index] = convolve(chunk, kernel, size);
      index++;
      free(chunk);
    }
  }
  return processed;
}


x86 assembly (clang, -O0):
<convolve_image>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
imul   -0x14(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1205 <convolve_image+0xc5>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    11f2 <convolve_image+0xb2>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%esi
mov    -0x34(%rbp),%edx
mov    -0x14(%rbp),%ecx
mov    -0x18(%rbp),%r8d
mov    -0x1c(%rbp),%r9d
call   1060 <get_matrix_at@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <convolve@plt>
mov    %eax,%edx
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    118b <convolve_image+0x4b>
jmp    11f7 <convolve_image+0xb7>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    1178 <convolve_image+0x38>
mov    -0x28(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<convolve_image>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %r8d,0x14(%rsp)
mov    %edx,%ebx
mov    %rsi,%r15
mov    %rdi,%r12
mov    %ecx,0xc(%rsp)
mov    %ecx,%edi
imul   %edx,%edi
call   1050 <malloc@plt>
mov    %rax,0x18(%rsp)
test   %ebx,%ebx
jle    1220 <convolve_image+0xe0>
mov    %ebx,%r13d
mov    0xc(%rsp),%r14d
xor    %eax,%eax
xor    %ebp,%ebp
mov    %ebx,0x10(%rsp)
mov    %r15,0x28(%rsp)
jmp    119c <convolve_image+0x5c>
nopl   (%rax)
add    $0x1,%ebp
cmp    %r13d,%ebp
je     1220 <convolve_image+0xe0>
cmpl   $0x0,0xc(%rsp)
jle    1190 <convolve_image+0x50>
mov    %rax,0x20(%rsp)
cltq
mov    0x18(%rsp),%rcx
lea    (%rcx,%rax,4),%rax
mov    %rax,0x30(%rsp)
xor    %r15d,%r15d
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    %ebp,%esi
mov    %r15d,%edx
mov    %r13d,%ecx
mov    %r14d,%r8d
mov    0x14(%rsp),%r13d
mov    %r13d,%r9d
call   1060 <get_matrix_at@plt>
mov    %r12,%rbx
mov    %rax,%r12
mov    %rax,%rdi
mov    0x28(%rsp),%rsi
mov    %r13d,%edx
call   1040 <convolve@plt>
mov    0x30(%rsp),%rcx
mov    %eax,(%rcx,%r15,4)
mov    %r12,%rdi
mov    %rbx,%r12
mov    0x10(%rsp),%r13d
call   1030 <free@plt>
add    $0x1,%r15
cmp    %r15d,%r14d
jne    11c0 <convolve_image+0x80>
mov    0x20(%rsp),%rax
add    %r15d,%eax
jmp    1190 <convolve_image+0x50>
mov    0x18(%rsp),%rax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32* @convolve_image(i32* %0, i32* %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32*
store i32* %0, i32** %6
store i32* %1, i32** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 %4, i32* %10
%16 = load i32, i32* %9
%17 = load i32, i32* %8
%18 = mul nsw i32 %16, %17
%19 = call i32* @malloc(i32 %18)
store i32* %19, i32** %11
store i32 0, i32* %12
store i32 0, i32* %13
br label %20
20: ; preds = %53, %5
%21 = load i32, i32* %13
%22 = load i32, i32* %8
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %56
24: ; preds = %20
store i32 0, i32* %14
br label %25
25: ; preds = %49, %24
%26 = load i32, i32* %14
%27 = load i32, i32* %9
%28 = icmp slt i32 %26, %27
br i1 %28, label %29, label %52
29: ; preds = %25
%30 = load i32*, i32** %6
%31 = load i32, i32* %13
%32 = load i32, i32* %14
%33 = load i32, i32* %8
%34 = load i32, i32* %9
%35 = load i32, i32* %10
%36 = call i32* @get_matrix_at(i32* %30, i32 %31, i32 %32, i32 %33, i32 %34, i32 %35)
store i32* %36, i32** %15
%37 = load i32*, i32** %15
%38 = load i32*, i32** %7
%39 = load i32, i32* %10
%40 = call i32 @convolve(i32* %37, i32* %38, i32 %39)
%41 = load i32*, i32** %11
%42 = load i32, i32* %12
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i32, i32* %41, i64 %43
store i32 %40, i32* %44
%45 = load i32, i32* %12
%46 = add nsw i32 %45, 1
store i32 %46, i32* %12
%47 = load i32*, i32** %15
%48 = call i32 @free(i32* %47)
br label %49
49: ; preds = %29
%50 = load i32, i32* %14
%51 = add nsw i32 %50, 1
store i32 %51, i32* %14
br label %25
52: ; preds = %25
br label %53
53: ; preds = %52
%54 = load i32, i32* %13
%55 = add nsw i32 %54, 1
store i32 %55, i32* %13
br label %20
56: ; preds = %20
%57 = load i32*, i32** %11
ret i32* %57
}

LLVM IR (-O2):
define noalias i32* @convolve_image(i32* %0, i32* %1, i32 %2, i32 %3, i32 %4) {
%6 = mul nsw i32 %3, %2
%7 = tail call i32* @malloc(i32 %6)
%8 = icmp sgt i32 %2, 0
br i1 %8, label %9, label %16
9: ; preds = %5
%10 = icmp sgt i32 %3, 0
br label %11
11: ; preds = %9, %19
%12 = phi i32 [ 0, %9 ], [ %20, %19 ]
%13 = phi i32 [ 0, %9 ], [ %21, %19 ]
br i1 %10, label %14, label %19
14: ; preds = %11
%15 = sext i32 %12 to i64
br label %23
16: ; preds = %19, %5
ret i32* %7
17: ; preds = %23
%18 = trunc i64 %29 to i32
br label %19
19: ; preds = %17, %11
%20 = phi i32 [ %12, %11 ], [ %18, %17 ]
%21 = add nuw nsw i32 %13, 1
%22 = icmp eq i32 %21, %2
br i1 %22, label %16, label %11
23: ; preds = %14, %23
%24 = phi i64 [ %15, %14 ], [ %29, %23 ]
%25 = phi i32 [ 0, %14 ], [ %31, %23 ]
%26 = tail call i32* @get_matrix_at(i32* %0, i32 %13, i32 %25, i32 %2, i32 %3, i32 %4)
%27 = tail call i32 @convolve(i32* %26, i32* %1, i32 %4)
%28 = getelementptr inbounds i32, i32* %7, i64 %24
store i32 %27, i32* %28
%29 = add nsw i64 %24, 1
%30 = tail call i32 @free(i32* %26)
%31 = add nuw nsw i32 %25, 1
%32 = icmp eq i32 %31, %3
br i1 %32, label %17, label %23
}