#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;
typedef int /*<<< orphan*/ int8_t;

/* Variables and functions */
int /*<<< orphan*/ convolve_sobel(int /*<<< orphan*/ *, 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_sobel(uint8_t *data, int8_t *kernel_x, int8_t *kernel_y,
                              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_sobel(chunk, kernel_x, kernel_y, size);
      index++;
      free(chunk);
    }
  }
  return processed;
}


x86 assembly (clang, -O0):
<convolve_image_sobel>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
mov    %r9d,-0x24(%rbp)
mov    -0x20(%rbp),%edi
imul   -0x1c(%rbp),%edi
call   1040 <malloc@plt>
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    120e <convolve_image_sobel+0xce>
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    11fb <convolve_image_sobel+0xbb>
mov    -0x8(%rbp),%rdi
mov    -0x38(%rbp),%esi
mov    -0x3c(%rbp),%edx
mov    -0x1c(%rbp),%ecx
mov    -0x20(%rbp),%r8d
mov    -0x24(%rbp),%r9d
call   1060 <get_matrix_at@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x24(%rbp),%ecx
call   1050 <convolve_sobel@plt>
mov    %eax,%edx
mov    -0x30(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    1190 <convolve_image_sobel+0x50>
jmp    1200 <convolve_image_sobel+0xc0>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    117d <convolve_image_sobel+0x3d>
mov    -0x30(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<convolve_image_sobel>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %r9d,0xc(%rsp)
mov    %ecx,%r13d
mov    %rdx,%rbp
mov    %rsi,%r12
mov    %rdi,%rbx
mov    %r8d,0x4(%rsp)
mov    %r8d,%edi
imul   %ecx,%edi
call   1040 <malloc@plt>
mov    %rax,0x10(%rsp)
test   %r13d,%r13d
jle    1236 <convolve_image_sobel+0xf6>
mov    0x4(%rsp),%r14d
xor    %eax,%eax
xor    %r15d,%r15d
mov    %rbp,0x28(%rsp)
mov    %r12,0x20(%rsp)
mov    %r13d,0x8(%rsp)
jmp    11ad <convolve_image_sobel+0x6d>
nopl   0x0(%rax,%rax,1)
add    $0x1,%r15d
cmp    %r13d,%r15d
je     1236 <convolve_image_sobel+0xf6>
cmpl   $0x0,0x4(%rsp)
jle    11a0 <convolve_image_sobel+0x60>
mov    %rax,0x18(%rsp)
cltq
mov    0x10(%rsp),%rcx
lea    (%rcx,%rax,4),%rax
mov    %rax,0x30(%rsp)
xor    %r12d,%r12d
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r15d,%ebp
mov    %r15d,%esi
mov    %r12d,%edx
mov    %r13d,%ecx
mov    %r14d,%r8d
mov    0xc(%rsp),%r15d
mov    %r15d,%r9d
call   1060 <get_matrix_at@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    0x20(%rsp),%rsi
mov    0x28(%rsp),%rdx
mov    %r15d,%ecx
call   1050 <convolve_sobel@plt>
mov    0x30(%rsp),%rcx
mov    %eax,(%rcx,%r12,4)
mov    %r13,%rdi
mov    0x8(%rsp),%r13d
call   1030 <free@plt>
mov    %ebp,%r15d
add    $0x1,%r12
cmp    %r12d,%r14d
jne    11d0 <convolve_image_sobel+0x90>
mov    0x18(%rsp),%rax
add    %r12d,%eax
jmp    11a0 <convolve_image_sobel+0x60>
mov    0x10(%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_sobel(i32* %0, i32* %1, i32* %2, i32 %3, i32 %4, i32 %5) {
%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
%16 = alloca i32
%17 = alloca i32*
store i32* %0, i32** %7
store i32* %1, i32** %8
store i32* %2, i32** %9
store i32 %3, i32* %10
store i32 %4, i32* %11
store i32 %5, i32* %12
%18 = load i32, i32* %11
%19 = load i32, i32* %10
%20 = mul nsw i32 %18, %19
%21 = call i32* @malloc(i32 %20)
store i32* %21, i32** %13
store i32 0, i32* %14
store i32 0, i32* %15
br label %22
22: ; preds = %56, %6
%23 = load i32, i32* %15
%24 = load i32, i32* %10
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %59
26: ; preds = %22
store i32 0, i32* %16
br label %27
27: ; preds = %52, %26
%28 = load i32, i32* %16
%29 = load i32, i32* %11
%30 = icmp slt i32 %28, %29
br i1 %30, label %31, label %55
31: ; preds = %27
%32 = load i32*, i32** %7
%33 = load i32, i32* %15
%34 = load i32, i32* %16
%35 = load i32, i32* %10
%36 = load i32, i32* %11
%37 = load i32, i32* %12
%38 = call i32* @get_matrix_at(i32* %32, i32 %33, i32 %34, i32 %35, i32 %36, i32 %37)
store i32* %38, i32** %17
%39 = load i32*, i32** %17
%40 = load i32*, i32** %8
%41 = load i32*, i32** %9
%42 = load i32, i32* %12
%43 = call i32 @convolve_sobel(i32* %39, i32* %40, i32* %41, i32 %42)
%44 = load i32*, i32** %13
%45 = load i32, i32* %14
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i32, i32* %44, i64 %46
store i32 %43, i32* %47
%48 = load i32, i32* %14
%49 = add nsw i32 %48, 1
store i32 %49, i32* %14
%50 = load i32*, i32** %17
%51 = call i32 @free(i32* %50)
br label %52
52: ; preds = %31
%53 = load i32, i32* %16
%54 = add nsw i32 %53, 1
store i32 %54, i32* %16
br label %27
55: ; preds = %27
br label %56
56: ; preds = %55
%57 = load i32, i32* %15
%58 = add nsw i32 %57, 1
store i32 %58, i32* %15
br label %22
59: ; preds = %22
%60 = load i32*, i32** %13
ret i32* %60
}

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