#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 double uint8_t;
typedef double int8_t;

/* Variables and functions */
scalar_t__ all_equal(double *, int);
int /*<<< orphan*/ round(double);
int sqrt(double);
uint8_t convolve_sobel(uint8_t *image_chunk, int8_t *kernel_x, int8_t *kernel_y,
                       int size) {
  double res = 0;
  double resx = 0;
  double resy = 0;
  int len = size * size;
  if (all_equal(image_chunk, len)) {
    return 0;
  }
  for (int i = 0; i < len; ++i) {
    resx += image_chunk[i] * kernel_x[i];
    resy += image_chunk[i] * kernel_y[i];
  }
  res = sqrt(resx * resx + resy * resy) / 8;
  return (uint8_t)round(res);
}


x86 assembly (clang, -O0):
<convolve_sobel>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x30(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x38(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    -0x24(%rbp),%eax
imul   -0x24(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x44(%rbp),%esi
call   1030 <all_equal@plt>
cmp    $0x0,%rax
je     118c <convolve_sobel+0x5c>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    124a <convolve_sobel+0x11a>
movl   $0x0,-0x48(%rbp)
mov    -0x48(%rbp),%eax
cmp    -0x44(%rbp),%eax
jge    1205 <convolve_sobel+0xd5>
mov    -0x10(%rbp),%rax
movslq -0x48(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x18(%rbp),%rax
movslq -0x48(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm2
movsd  -0x38(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x48(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x20(%rbp),%rax
movslq -0x48(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm2
movsd  -0x40(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
jmp    1193 <convolve_sobel+0x63>
movsd  -0x38(%rbp),%xmm0
movsd  -0x38(%rbp),%xmm2
movsd  -0x40(%rbp),%xmm1
mulsd  -0x40(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
call   1050 <sqrt@plt>
mov    $0x8,%ecx
cltd
idiv   %ecx
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
call   1040 <round@plt>
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<convolve_sobel>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%ebp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
imul   %ecx,%ebp
mov    %ebp,%esi
call   1030 <all_equal@plt>
xorpd  %xmm0,%xmm0
test   %rax,%rax
jne    1236 <convolve_sobel+0x106>
test   %ebp,%ebp
je     1173 <convolve_sobel+0x43>
mov    %ebp,%eax
cmp    $0x1,%ebp
jne    117c <convolve_sobel+0x4c>
xorpd  %xmm1,%xmm1
xor    %ecx,%ecx
test   $0x1,%al
jne    11e2 <convolve_sobel+0xb2>
jmp    11ff <convolve_sobel+0xcf>
xorpd  %xmm0,%xmm0
jmp    11ff <convolve_sobel+0xcf>
mov    %eax,%edx
and    $0x7ffffffe,%edx
xorpd  %xmm1,%xmm1
xor    %ecx,%ecx
nopw   0x0(%rax,%rax,1)
movsd  (%rbx,%rcx,8),%xmm3
movsd  0x8(%rbx,%rcx,8),%xmm4
unpcklpd %xmm3,%xmm3
movsd  (%r15,%rcx,8),%xmm2
movsd  0x8(%r15,%rcx,8),%xmm0
movhpd (%r14,%rcx,8),%xmm2
mulpd  %xmm3,%xmm2
addpd  %xmm1,%xmm2
unpcklpd %xmm4,%xmm4
movhpd 0x8(%r14,%rcx,8),%xmm0
mulpd  %xmm4,%xmm0
movapd %xmm0,%xmm1
addpd  %xmm2,%xmm1
add    $0x2,%rcx
cmp    %rcx,%rdx
jne    1190 <convolve_sobel+0x60>
addpd  %xmm2,%xmm0
test   $0x1,%al
je     11ff <convolve_sobel+0xcf>
movsd  (%rbx,%rcx,8),%xmm2
unpcklpd %xmm2,%xmm2
movsd  (%r15,%rcx,8),%xmm0
movhpd (%r14,%rcx,8),%xmm0
mulpd  %xmm2,%xmm0
addpd  %xmm1,%xmm0
movapd %xmm0,%xmm1
mulpd  %xmm0,%xmm1
unpckhpd %xmm1,%xmm1
mulsd  %xmm0,%xmm0
addsd  %xmm1,%xmm0
call   1050 <sqrt@plt>
lea    0x7(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
sar    $0x3,%ecx
xorps  %xmm0,%xmm0
cvtsi2sd %ecx,%xmm0
call   1040 <round@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define double @convolve_sobel(double* %0, double* %1, double* %2, i32 %3) {
%5 = alloca double
%6 = alloca double*
%7 = alloca double*
%8 = alloca double*
%9 = alloca i32
%10 = alloca double
%11 = alloca double
%12 = alloca double
%13 = alloca i32
%14 = alloca i32
store double* %0, double** %6
store double* %1, double** %7
store double* %2, double** %8
store i32 %3, i32* %9
store double 0.000000e+00, double* %10
store double 0.000000e+00, double* %11
store double 0.000000e+00, double* %12
%15 = load i32, i32* %9
%16 = load i32, i32* %9
%17 = mul nsw i32 %15, %16
store i32 %17, i32* %13
%18 = load double*, double** %6
%19 = load i32, i32* %13
%20 = call i64 @all_equal(double* %18, i32 %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %4
store double 0.000000e+00, double* %5
br label %69
23: ; preds = %4
store i32 0, i32* %14
br label %24
24: ; preds = %53, %23
%25 = load i32, i32* %14
%26 = load i32, i32* %13
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %56
28: ; preds = %24
%29 = load double*, double** %6
%30 = load i32, i32* %14
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds double, double* %29, i64 %31
%33 = load double, double* %32
%34 = load double*, double** %7
%35 = load i32, i32* %14
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds double, double* %34, i64 %36
%38 = load double, double* %37
%39 = load double, double* %11
%40 = call double @llvm.fmuladd.f64(double %33, double %38, double %39)
store double %40, double* %11
%41 = load double*, double** %6
%42 = load i32, i32* %14
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds double, double* %41, i64 %43
%45 = load double, double* %44
%46 = load double*, double** %8
%47 = load i32, i32* %14
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds double, double* %46, i64 %48
%50 = load double, double* %49
%51 = load double, double* %12
%52 = call double @llvm.fmuladd.f64(double %45, double %50, double %51)
store double %52, double* %12
br label %53
53: ; preds = %28
%54 = load i32, i32* %14
%55 = add nsw i32 %54, 1
store i32 %55, i32* %14
br label %24
56: ; preds = %24
%57 = load double, double* %11
%58 = load double, double* %11
%59 = load double, double* %12
%60 = load double, double* %12
%61 = fmul double %59, %60
%62 = call double @llvm.fmuladd.f64(double %57, double %58, double %61)
%63 = call i32 @sqrt(double %62)
%64 = sdiv i32 %63, 8
%65 = sitofp i32 %64 to double
store double %65, double* %10
%66 = load double, double* %10
%67 = call i32 @round(double %66)
%68 = sitofp i32 %67 to double
store double %68, double* %5
br label %69
69: ; preds = %56, %22
%70 = load double, double* %5
ret double %70
}

LLVM IR (-O2):
define double @convolve_sobel(double* %0, double* nocapture %1, double* nocapture %2, i32 %3) {
%5 = mul nsw i32 %3, %3
%6 = tail call i64 @all_equal(double* %0, i32 %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %76
8: ; preds = %4
%9 = icmp eq i32 %5, 0
br i1 %9, label %35, label %10
10: ; preds = %8
%11 = zext i32 %5 to i64
%12 = and i64 %11, 1
%13 = icmp eq i32 %5, 1
br i1 %13, label %18, label %14
14: ; preds = %10
%15 = and i64 %11, 2147483646
br label %46
16: ; preds = %46
%17 = call <2 x double> @llvm.fmuladd.v2f64(<2 x double> %69, <2 x double> %71, <2 x double> %60)
br label %18
18: ; preds = %16, %10
%19 = phi <2 x double> [ undef, %10 ], [ %17, %16 ]
%20 = phi i64 [ 0, %10 ], [ %73, %16 ]
%21 = phi <2 x double> [ zeroinitializer, %10 ], [ %72, %16 ]
%22 = icmp eq i64 %12, 0
br i1 %22, label %35, label %23
23: ; preds = %18
%24 = getelementptr inbounds double, double* %0, i64 %20
%25 = load double, double* %24
%26 = getelementptr inbounds double, double* %1, i64 %20
%27 = load double, double* %26
%28 = getelementptr inbounds double, double* %2, i64 %20
%29 = load double, double* %28
%30 = insertelement <2 x double> poison, double %25, i64 0
%31 = shufflevector <2 x double> %30, <2 x double> poison, <2 x i32> zeroinitializer
%32 = insertelement <2 x double> poison, double %27, i64 0
%33 = insertelement <2 x double> %32, double %29, i64 1
%34 = call <2 x double> @llvm.fmuladd.v2f64(<2 x double> %31, <2 x double> %33, <2 x double> %21)
br label %35
35: ; preds = %23, %18, %8
%36 = phi <2 x double> [ zeroinitializer, %8 ], [ %19, %18 ], [ %34, %23 ]
%37 = fmul <2 x double> %36, %36
%38 = extractelement <2 x double> %37, i64 1
%39 = extractelement <2 x double> %36, i64 0
%40 = tail call double @llvm.fmuladd.f64(double %39, double %39, double %38)
%41 = tail call i32 @sqrt(double %40)
%42 = sdiv i32 %41, 8
%43 = sitofp i32 %42 to double
%44 = tail call i32 @round(double %43)
%45 = sitofp i32 %44 to double
br label %76
46: ; preds = %46, %14
%47 = phi i64 [ 0, %14 ], [ %73, %46 ]
%48 = phi <2 x double> [ zeroinitializer, %14 ], [ %72, %46 ]
%49 = phi i64 [ 0, %14 ], [ %74, %46 ]
%50 = getelementptr inbounds double, double* %0, i64 %47
%51 = load double, double* %50
%52 = getelementptr inbounds double, double* %1, i64 %47
%53 = load double, double* %52
%54 = getelementptr inbounds double, double* %2, i64 %47
%55 = load double, double* %54
%56 = insertelement <2 x double> poison, double %51, i64 0
%57 = shufflevector <2 x double> %56, <2 x double> poison, <2 x i32> zeroinitializer
%58 = insertelement <2 x double> poison, double %53, i64 0
%59 = insertelement <2 x double> %58, double %55, i64 1
%60 = call <2 x double> @llvm.fmuladd.v2f64(<2 x double> %57, <2 x double> %59, <2 x double> %48)
%61 = or i64 %47, 1
%62 = getelementptr inbounds double, double* %0, i64 %61
%63 = load double, double* %62
%64 = getelementptr inbounds double, double* %1, i64 %61
%65 = load double, double* %64
%66 = getelementptr inbounds double, double* %2, i64 %61
%67 = load double, double* %66
%68 = insertelement <2 x double> poison, double %63, i64 0
%69 = shufflevector <2 x double> %68, <2 x double> poison, <2 x i32> zeroinitializer
%70 = insertelement <2 x double> poison, double %65, i64 0
%71 = insertelement <2 x double> %70, double %67, i64 1
%72 = call <2 x double> @llvm.fmuladd.v2f64(<2 x double> %69, <2 x double> %71, <2 x double> %60)
%73 = add nuw nsw i64 %47, 2
%74 = add i64 %49, 2
%75 = icmp eq i64 %74, %15
br i1 %75, label %16, label %46
76: ; preds = %4, %35
%77 = phi double [ %45, %35 ], [ 0.000000e+00, %4 ]
ret double %77
}