#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 */
double DCT_cosine(int, int);
int sqrt(int);
double spatial_to_frequency(int block_matrix[8][8], int u, int v) {
  int x, y;
  double F, cosine_x_result, cosine_y_result, Cu, Cv;
  F = 0;
  Cu = 1;
  Cv = 1;
  if (u == 0) {
    Cu = (1 / (sqrt(2)));
  }
  if (v == 0) {
    Cv = (1 / (sqrt(2)));
  }
  for (x = 0; x < 8; x++) {
    for (y = 0; y < 8; y++) {
      cosine_x_result = DCT_cosine(x, u);
      cosine_y_result = DCT_cosine(y, v);
      F += block_matrix[x][y] * cosine_x_result * cosine_y_result;
    }
  }
  F = (Cu / 2) * (Cv / 2) * F;
  return F;
}


x86 assembly (clang, -O0):
<spatial_to_frequency>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  0xebe(%rip),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  0xeb1(%rip),%xmm0
movsd  %xmm0,-0x40(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    117b <spatial_to_frequency+0x5b>
mov    $0x2,%edi
call   1030 <sqrt@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cltd
idiv   %ecx
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x38(%rbp)
cmpl   $0x0,-0x10(%rbp)
jne    11a2 <spatial_to_frequency+0x82>
mov    $0x2,%edi
call   1030 <sqrt@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cltd
idiv   %ecx
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x40(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x8,-0x14(%rbp)
jge    1239 <spatial_to_frequency+0x119>
movl   $0x0,-0x18(%rbp)
cmpl   $0x8,-0x18(%rbp)
jge    1226 <spatial_to_frequency+0x106>
mov    -0x14(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1040 <DCT_cosine@plt>
movsd  %xmm0,-0x28(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%esi
call   1040 <DCT_cosine@plt>
movsd  %xmm0,-0x30(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
shl    $0x5,%rcx
add    %rcx,%rax
movslq -0x18(%rbp),%rcx
cvtsi2sdl (%rax,%rcx,4),%xmm0
mulsd  -0x28(%rbp),%xmm0
movsd  -0x30(%rbp),%xmm2
movsd  -0x20(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11ba <spatial_to_frequency+0x9a>
jmp    122b <spatial_to_frequency+0x10b>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11a9 <spatial_to_frequency+0x89>
movsd  -0x38(%rbp),%xmm0
movsd  0xdc2(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  -0x40(%rbp),%xmm1
movsd  0xdb1(%rip),%xmm2
divsd  %xmm2,%xmm1
mulsd  %xmm1,%xmm0
mulsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spatial_to_frequency>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %edx,%r14d
mov    %esi,%r15d
mov    %rdi,%rbx
movsd  0xec5(%rip),%xmm0
movsd  %xmm0,0x18(%rsp)
test   %esi,%esi
jne    1169 <spatial_to_frequency+0x49>
mov    $0x2,%edi
call   1030 <sqrt@plt>
lea    0x1(%rax),%ecx
xor    %edx,%edx
cmp    $0x3,%ecx
cmovb  %eax,%edx
xorps  %xmm0,%xmm0
cvtsi2sd %edx,%xmm0
mulsd  0xe97(%rip),%xmm0
movsd  %xmm0,0x20(%rsp)
test   %r14d,%r14d
jne    119e <spatial_to_frequency+0x7e>
mov    $0x2,%edi
call   1030 <sqrt@plt>
lea    0x1(%rax),%ecx
xor    %edx,%edx
cmp    $0x3,%ecx
cmovb  %eax,%edx
xorps  %xmm0,%xmm0
cvtsi2sd %edx,%xmm0
mulsd  0xe68(%rip),%xmm0
movsd  %xmm0,0x18(%rsp)
add    $0x1c,%rbx
xorpd  %xmm1,%xmm1
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
movsd  %xmm1,0x10(%rsp)
mov    %ebp,%edi
mov    %r15d,%esi
call   1040 <DCT_cosine@plt>
movsd  %xmm0,0x8(%rsp)
xor    %edi,%edi
mov    %r14d,%esi
call   1040 <DCT_cosine@plt>
xorps  %xmm1,%xmm1
cvtsi2sdl -0x1c(%rbx),%xmm1
mulsd  0x8(%rsp),%xmm1
mulsd  %xmm0,%xmm1
addsd  0x10(%rsp),%xmm1
movsd  %xmm1,0x10(%rsp)
mov    %ebp,%edi
mov    %r15d,%esi
call   1040 <DCT_cosine@plt>
movsd  %xmm0,0x8(%rsp)
mov    $0x1,%edi
mov    %r14d,%esi
call   1040 <DCT_cosine@plt>
xorps  %xmm1,%xmm1
cvtsi2sdl -0x18(%rbx),%xmm1
mulsd  0x8(%rsp),%xmm1
mulsd  %xmm0,%xmm1
addsd  0x10(%rsp),%xmm1
movsd  %xmm1,0x10(%rsp)
mov    %ebp,%edi
mov    %r15d,%esi
call   1040 <DCT_cosine@plt>
movsd  %xmm0,0x8(%rsp)
mov    $0x2,%edi
mov    %r14d,%esi
call   1040 <DCT_cosine@plt>
xorps  %xmm1,%xmm1
cvtsi2sdl -0x14(%rbx),%xmm1
mulsd  0x8(%rsp),%xmm1
mulsd  %xmm0,%xmm1
addsd  0x10(%rsp),%xmm1
movsd  %xmm1,0x10(%rsp)
mov    %ebp,%edi
mov    %r15d,%esi
call   1040 <DCT_cosine@plt>
movsd  %xmm0,0x8(%rsp)
mov    $0x3,%edi
mov    %r14d,%esi
call   1040 <DCT_cosine@plt>
xorps  %xmm1,%xmm1
cvtsi2sdl -0x10(%rbx),%xmm1
mulsd  0x8(%rsp),%xmm1
mulsd  %xmm0,%xmm1
addsd  0x10(%rsp),%xmm1
movsd  %xmm1,0x10(%rsp)
mov    %ebp,%edi
mov    %r15d,%esi
call   1040 <DCT_cosine@plt>
movsd  %xmm0,0x8(%rsp)
mov    $0x4,%edi
mov    %r14d,%esi
call   1040 <DCT_cosine@plt>
xorps  %xmm1,%xmm1
cvtsi2sdl -0xc(%rbx),%xmm1
mulsd  0x8(%rsp),%xmm1
mulsd  %xmm0,%xmm1
addsd  0x10(%rsp),%xmm1
movsd  %xmm1,0x10(%rsp)
mov    %ebp,%edi
mov    %r15d,%esi
call   1040 <DCT_cosine@plt>
movsd  %xmm0,0x8(%rsp)
mov    $0x5,%edi
mov    %r14d,%esi
call   1040 <DCT_cosine@plt>
xorps  %xmm1,%xmm1
cvtsi2sdl -0x8(%rbx),%xmm1
mulsd  0x8(%rsp),%xmm1
mulsd  %xmm0,%xmm1
addsd  0x10(%rsp),%xmm1
movsd  %xmm1,0x10(%rsp)
mov    %ebp,%edi
mov    %r15d,%esi
call   1040 <DCT_cosine@plt>
movsd  %xmm0,0x8(%rsp)
mov    $0x6,%edi
mov    %r14d,%esi
call   1040 <DCT_cosine@plt>
xorps  %xmm1,%xmm1
cvtsi2sdl -0x4(%rbx),%xmm1
mulsd  0x8(%rsp),%xmm1
mulsd  %xmm0,%xmm1
addsd  0x10(%rsp),%xmm1
movsd  %xmm1,0x10(%rsp)
mov    %ebp,%edi
mov    %r15d,%esi
call   1040 <DCT_cosine@plt>
movsd  %xmm0,0x8(%rsp)
mov    $0x7,%edi
mov    %r14d,%esi
call   1040 <DCT_cosine@plt>
xorps  %xmm1,%xmm1
cvtsi2sdl (%rbx),%xmm1
mulsd  0x8(%rsp),%xmm1
mulsd  %xmm0,%xmm1
addsd  0x10(%rsp),%xmm1
add    $0x1,%rbp
add    $0x20,%rbx
cmp    $0x8,%rbp
jne    11b0 <spatial_to_frequency+0x90>
movsd  0x20(%rsp),%xmm0
mulsd  0x18(%rsp),%xmm0
mulsd  %xmm1,%xmm0
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define double @spatial_to_frequency([8 x i32]* nocapture %0, i32 %1, i32 %2) {
%4 = icmp eq i32 %1, 0
br i1 %4, label %5, label %12
5: ; preds = %3
%6 = tail call i32 @sqrt(i32 2)
%7 = add i32 %6, 1
%8 = icmp ult i32 %7, 3
%9 = select i1 %8, i32 %6, i32 0
%10 = sitofp i32 %9 to double
%11 = fmul double %10, 5.000000e-01
br label %12
12: ; preds = %5, %3
%13 = phi double [ %11, %5 ], [ 5.000000e-01, %3 ]
%14 = icmp eq i32 %2, 0
br i1 %14, label %15, label %22
15: ; preds = %12
%16 = tail call i32 @sqrt(i32 2)
%17 = add i32 %16, 1
%18 = icmp ult i32 %17, 3
%19 = select i1 %18, i32 %16, i32 0
%20 = sitofp i32 %19 to double
%21 = fmul double %20, 5.000000e-01
br label %22
22: ; preds = %15, %12
%23 = phi double [ %21, %15 ], [ 5.000000e-01, %12 ]
br label %24
24: ; preds = %22, %24
%25 = phi i64 [ 0, %22 ], [ %84, %24 ]
%26 = phi double [ 0.000000e+00, %22 ], [ %83, %24 ]
%27 = trunc i64 %25 to i32
%28 = tail call double @DCT_cosine(i32 %27, i32 %1)
%29 = tail call double @DCT_cosine(i32 0, i32 %2)
%30 = getelementptr inbounds [8 x i32], [8 x i32]* %0, i64 %25, i64 0
%31 = load i32, i32* %30
%32 = sitofp i32 %31 to double
%33 = fmul double %28, %32
%34 = tail call double @llvm.fmuladd.f64(double %33, double %29, double %26)
%35 = tail call double @DCT_cosine(i32 %27, i32 %1)
%36 = tail call double @DCT_cosine(i32 1, i32 %2)
%37 = getelementptr inbounds [8 x i32], [8 x i32]* %0, i64 %25, i64 1
%38 = load i32, i32* %37
%39 = sitofp i32 %38 to double
%40 = fmul double %35, %39
%41 = tail call double @llvm.fmuladd.f64(double %40, double %36, double %34)
%42 = tail call double @DCT_cosine(i32 %27, i32 %1)
%43 = tail call double @DCT_cosine(i32 2, i32 %2)
%44 = getelementptr inbounds [8 x i32], [8 x i32]* %0, i64 %25, i64 2
%45 = load i32, i32* %44
%46 = sitofp i32 %45 to double
%47 = fmul double %42, %46
%48 = tail call double @llvm.fmuladd.f64(double %47, double %43, double %41)
%49 = tail call double @DCT_cosine(i32 %27, i32 %1)
%50 = tail call double @DCT_cosine(i32 3, i32 %2)
%51 = getelementptr inbounds [8 x i32], [8 x i32]* %0, i64 %25, i64 3
%52 = load i32, i32* %51
%53 = sitofp i32 %52 to double
%54 = fmul double %49, %53
%55 = tail call double @llvm.fmuladd.f64(double %54, double %50, double %48)
%56 = tail call double @DCT_cosine(i32 %27, i32 %1)
%57 = tail call double @DCT_cosine(i32 4, i32 %2)
%58 = getelementptr inbounds [8 x i32], [8 x i32]* %0, i64 %25, i64 4
%59 = load i32, i32* %58
%60 = sitofp i32 %59 to double
%61 = fmul double %56, %60
%62 = tail call double @llvm.fmuladd.f64(double %61, double %57, double %55)
%63 = tail call double @DCT_cosine(i32 %27, i32 %1)
%64 = tail call double @DCT_cosine(i32 5, i32 %2)
%65 = getelementptr inbounds [8 x i32], [8 x i32]* %0, i64 %25, i64 5
%66 = load i32, i32* %65
%67 = sitofp i32 %66 to double
%68 = fmul double %63, %67
%69 = tail call double @llvm.fmuladd.f64(double %68, double %64, double %62)
%70 = tail call double @DCT_cosine(i32 %27, i32 %1)
%71 = tail call double @DCT_cosine(i32 6, i32 %2)
%72 = getelementptr inbounds [8 x i32], [8 x i32]* %0, i64 %25, i64 6
%73 = load i32, i32* %72
%74 = sitofp i32 %73 to double
%75 = fmul double %70, %74
%76 = tail call double @llvm.fmuladd.f64(double %75, double %71, double %69)
%77 = tail call double @DCT_cosine(i32 %27, i32 %1)
%78 = tail call double @DCT_cosine(i32 7, i32 %2)
%79 = getelementptr inbounds [8 x i32], [8 x i32]* %0, i64 %25, i64 7
%80 = load i32, i32* %79
%81 = sitofp i32 %80 to double
%82 = fmul double %77, %81
%83 = tail call double @llvm.fmuladd.f64(double %82, double %78, double %76)
%84 = add nuw nsw i64 %25, 1
%85 = icmp eq i64 %84, 8
br i1 %85, label %86, label %24
86: ; preds = %24
%87 = fmul double %13, %23
%88 = fmul double %87, %83
ret double %88
}