#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 fabs(double);
double inner_prod(double const *, double const *, int);
int find_nearest(const double *data, const double *codebook, int nb_entries,
                 int n, double *sign, double *err) {
  double best_dist;
  double best_sign;
  int best_id;
  int i;
  best_dist = -1;
  best_id = 0;
  best_sign = 1;
  for (i = 0; i < nb_entries; i++) {
    double dist;
    dist = inner_prod(data, &codebook[i * n], n);
    if (fabs(dist) > best_dist) {
      best_dist = fabs(dist);
      best_sign = dist > 0 ? 1 : -1;
      best_id = i;
    }
  }
  if (sign)
    *sign = best_sign;
  if (err)
    *err = 2 - 2 * best_dist;
  return best_id;
}


x86 assembly (clang, -O0):
<find_nearest>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
mov    %r9,-0x28(%rbp)
movsd  0xed2(%rip),%xmm0
movsd  %xmm0,-0x30(%rbp)
movl   $0x0,-0x3c(%rbp)
movsd  0xeb6(%rip),%xmm0
movsd  %xmm0,-0x38(%rbp)
movl   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11f2 <find_nearest+0xe2>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x40(%rbp),%eax
imul   -0x18(%rbp),%eax
cltq
shl    $0x3,%rax
add    %rax,%rsi
mov    -0x18(%rbp),%edx
call   1030 <inner_prod@plt>
movsd  %xmm0,-0x48(%rbp)
movsd  -0x48(%rbp),%xmm0
movaps 0xe8d(%rip),%xmm1
pand   %xmm1,%xmm0
ucomisd -0x30(%rbp),%xmm0
jbe    11df <find_nearest+0xcf>
movsd  -0x48(%rbp),%xmm0
movaps 0xe72(%rip),%xmm1
pand   %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x48(%rbp),%xmm0
xorps  %xmm1,%xmm1
mov    $0xffffffff,%eax
mov    $0x1,%ecx
ucomisd %xmm1,%xmm0
cmova  %ecx,%eax
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x40(%rbp),%eax
mov    %eax,-0x3c(%rbp)
jmp    11e4 <find_nearest+0xd4>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    1156 <find_nearest+0x46>
cmpq   $0x0,-0x20(%rbp)
je     120a <find_nearest+0xfa>
movsd  -0x38(%rbp),%xmm0
mov    -0x20(%rbp),%rax
movsd  %xmm0,(%rax)
cmpq   $0x0,-0x28(%rbp)
je     1232 <find_nearest+0x122>
movsd  -0x30(%rbp),%xmm1
addsd  %xmm1,%xmm1
movsd  0xdea(%rip),%xmm0
subsd  %xmm1,%xmm0
mov    -0x28(%rbp),%rax
movsd  %xmm0,(%rax)
mov    -0x3c(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_nearest>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r9,0x10(%rsp)
mov    %r8,0x8(%rsp)
mov    %rdi,0x18(%rsp)
test   %edx,%edx
jle    11c9 <find_nearest+0xb9>
mov    %rsi,%rbx
movslq %ecx,%r14
mov    %edx,%r15d
shl    $0x3,%r14
movsd  0xebe(%rip),%xmm0
movsd  %xmm0,(%rsp)
xor    %r12d,%r12d
movsd  0xea6(%rip),%xmm1
xor    %ebp,%ebp
jmp    117b <find_nearest+0x6b>
xchg   %ax,%ax
cmova  %r12d,%ebp
maxsd  %xmm2,%xmm0
add    $0x1,%r12
add    %r14,%rbx
movapd %xmm0,%xmm1
cmp    %r12,%r15
mov    %r13d,%ecx
je     11e0 <find_nearest+0xd0>
movsd  %xmm1,0x20(%rsp)
mov    0x18(%rsp),%rdi
mov    %rbx,%rsi
mov    %ecx,%r13d
mov    %ecx,%edx
call   1030 <inner_prod@plt>
movsd  0x20(%rsp),%xmm2
xor    %eax,%eax
ucomisd 0xe75(%rip),%xmm0
seta   %cl
andpd  0xe72(%rip),%xmm0
ucomisd %xmm2,%xmm0
jbe    1160 <find_nearest+0x50>
mov    %cl,%al
lea    0xe73(%rip),%rcx
movsd  (%rcx,%rax,8),%xmm1
movsd  %xmm1,(%rsp)
jmp    1160 <find_nearest+0x50>
movsd  0xe2f(%rip),%xmm0
xor    %ebp,%ebp
movsd  0xe2d(%rip),%xmm1
movsd  %xmm1,(%rsp)
mov    0x8(%rsp),%rax
test   %rax,%rax
je     11f3 <find_nearest+0xe3>
movsd  (%rsp),%xmm1
movsd  %xmm1,(%rax)
mov    0x10(%rsp),%rax
test   %rax,%rax
je     1211 <find_nearest+0x101>
addsd  %xmm0,%xmm0
movsd  0xe07(%rip),%xmm1
subsd  %xmm0,%xmm1
movsd  %xmm1,(%rax)
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @find_nearest(double* %0, double* %1, i32 %2, i32 %3, double* writeonly %4, double* writeonly %5) {
%7 = icmp sgt i32 %2, 0
br i1 %7, label %8, label %29
8: ; preds = %6
%9 = sext i32 %3 to i64
%10 = zext i32 %2 to i64
br label %11
11: ; preds = %8, %11
%12 = phi i64 [ 0, %8 ], [ %27, %11 ]
%13 = phi double [ -1.000000e+00, %8 ], [ %26, %11 ]
%14 = phi i32 [ 0, %8 ], [ %25, %11 ]
%15 = phi double [ 1.000000e+00, %8 ], [ %23, %11 ]
%16 = mul nsw i64 %12, %9
%17 = getelementptr inbounds double, double* %1, i64 %16
%18 = tail call double @inner_prod(double* %0, double* %17, i32 %3)
%19 = tail call double @llvm.fabs.f64(double %18)
%20 = fcmp ogt double %19, %13
%21 = fcmp ogt double %18, 0.000000e+00
%22 = select i1 %21, double 1.000000e+00, double -1.000000e+00
%23 = select i1 %20, double %22, double %15
%24 = trunc i64 %12 to i32
%25 = select i1 %20, i32 %24, i32 %14
%26 = select i1 %20, double %19, double %13
%27 = add nuw nsw i64 %12, 1
%28 = icmp eq i64 %27, %10
br i1 %28, label %29, label %11
29: ; preds = %11, %6
%30 = phi double [ 1.000000e+00, %6 ], [ %23, %11 ]
%31 = phi i32 [ 0, %6 ], [ %25, %11 ]
%32 = phi double [ -1.000000e+00, %6 ], [ %26, %11 ]
%33 = icmp eq double* %4, null
br i1 %33, label %35, label %34
34: ; preds = %29
store double %30, double* %4
br label %35
35: ; preds = %34, %29
%36 = icmp eq double* %5, null
br i1 %36, label %39, label %37
37: ; preds = %35
%38 = tail call double @llvm.fmuladd.f64(double %32, double -2.000000e+00, double 2.000000e+00)
store double %38, double* %5
br label %39
39: ; preds = %37, %35
ret i32 %31
}