#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 mat_Nint(double);
int /*<<< orphan*/ mat_multiply_matrix_vector_d3(double *, double **, double *);
double mat_norm_squared_d3(double *);
int perm_argsort(int *, int const *, double *, void *, int const);
int argsort_by_lattice_point_distance(int *perm, double lattice[3][3],
                                      double (*positions)[3], const int *types,
                                      double *distance_temp, void *argsort_work,
                                      const int size) {
  double diff[3];
  int i, k;
  double x;
  for (i = 0; i < size; i++) {
    for (k = 0; k < 3; k++) {
      x = positions[i][k];
      diff[k] = x - mat_Nint(x);
    }
    mat_multiply_matrix_vector_d3(diff, lattice, diff);
    distance_temp[i] = mat_norm_squared_d3(diff);
  }
  return perm_argsort(perm, types, distance_temp, argsort_work, size);
}


x86 assembly (clang, -O0):
<argsort_by_lattice_point_distance>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    0x10(%rbp),%eax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
movl   $0x0,-0x54(%rbp)
mov    -0x54(%rbp),%eax
cmp    0x10(%rbp),%eax
jge    1211 <argsort_by_lattice_point_distance+0xd1>
movl   $0x0,-0x58(%rbp)
cmpl   $0x3,-0x58(%rbp)
jge    11dc <argsort_by_lattice_point_distance+0x9c>
mov    -0x18(%rbp),%rax
movslq -0x54(%rbp),%rcx
imul   $0x18,%rcx,%rcx
add    %rcx,%rax
movslq -0x58(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x60(%rbp)
movsd  -0x60(%rbp),%xmm0
movsd  %xmm0,-0x68(%rbp)
movsd  -0x60(%rbp),%xmm0
call   1060 <mat_Nint@plt>
movaps %xmm0,%xmm1
movsd  -0x68(%rbp),%xmm0
subsd  %xmm1,%xmm0
movslq -0x58(%rbp),%rax
movsd  %xmm0,-0x50(%rbp,%rax,8)
mov    -0x58(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x58(%rbp)
jmp    117d <argsort_by_lattice_point_distance+0x3d>
lea    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x50(%rbp),%rdx
call   1050 <mat_multiply_matrix_vector_d3@plt>
lea    -0x50(%rbp),%rdi
call   1040 <mat_norm_squared_d3@plt>
mov    -0x28(%rbp),%rax
movslq -0x54(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    116a <argsort_by_lattice_point_distance+0x2a>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    0x10(%rbp),%r8d
call   1030 <perm_argsort@plt>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<argsort_by_lattice_point_distance>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %r9,0x18(%rsp)
mov    %r8,%rbx
mov    %rcx,0x10(%rsp)
mov    %rdi,0x8(%rsp)
cmpl   $0x0,0x70(%rsp)
jle    1217 <argsort_by_lattice_point_distance+0xd7>
mov    %rdx,%rbp
mov    %rsi,%r12
mov    0x70(%rsp),%r15d
add    $0x10,%rbp
xor    %r13d,%r13d
lea    0x20(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,(%rsp)
call   1060 <mat_Nint@plt>
movsd  (%rsp),%xmm1
subsd  %xmm0,%xmm1
movsd  %xmm1,0x20(%rsp)
movsd  -0x8(%rbp),%xmm0
movsd  %xmm0,(%rsp)
call   1060 <mat_Nint@plt>
movsd  (%rsp),%xmm1
subsd  %xmm0,%xmm1
movsd  %xmm1,0x28(%rsp)
movsd  0x0(%rbp),%xmm0
movsd  %xmm0,(%rsp)
call   1060 <mat_Nint@plt>
movsd  (%rsp),%xmm1
subsd  %xmm0,%xmm1
movsd  %xmm1,0x30(%rsp)
mov    %r14,%rdi
mov    %r12,%rsi
mov    %r14,%rdx
call   1050 <mat_multiply_matrix_vector_d3@plt>
mov    %r14,%rdi
call   1040 <mat_norm_squared_d3@plt>
movsd  %xmm0,(%rbx,%r13,8)
add    $0x1,%r13
add    $0x18,%rbp
cmp    %r13,%r15
jne    1190 <argsort_by_lattice_point_distance+0x50>
mov    0x8(%rsp),%rdi
mov    0x10(%rsp),%rsi
mov    %rbx,%rdx
mov    0x18(%rsp),%rcx
mov    0x70(%rsp),%r8d
call   1030 <perm_argsort@plt>
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @argsort_by_lattice_point_distance(i32* %0, [3 x double]* %1, [3 x double]* %2, i32* %3, double* %4, i8* %5, i32 %6) {
%8 = alloca i32*
%9 = alloca [3 x double]*
%10 = alloca [3 x double]*
%11 = alloca i32*
%12 = alloca double*
%13 = alloca i8*
%14 = alloca i32
%15 = alloca [3 x double]
%16 = alloca i32
%17 = alloca i32
%18 = alloca double
store i32* %0, i32** %8
store [3 x double]* %1, [3 x double]** %9
store [3 x double]* %2, [3 x double]** %10
store i32* %3, i32** %11
store double* %4, double** %12
store i8* %5, i8** %13
store i32 %6, i32* %14
store i32 0, i32* %16
br label %19
19: ; preds = %58, %7
%20 = load i32, i32* %16
%21 = load i32, i32* %14
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %61
23: ; preds = %19
store i32 0, i32* %17
br label %24
24: ; preds = %43, %23
%25 = load i32, i32* %17
%26 = icmp slt i32 %25, 3
br i1 %26, label %27, label %46
27: ; preds = %24
%28 = load [3 x double]*, [3 x double]** %10
%29 = load i32, i32* %16
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [3 x double], [3 x double]* %28, i64 %30
%32 = load i32, i32* %17
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [3 x double], [3 x double]* %31, i64 0, i64 %33
%35 = load double, double* %34
store double %35, double* %18
%36 = load double, double* %18
%37 = load double, double* %18
%38 = call double @mat_Nint(double %37)
%39 = fsub double %36, %38
%40 = load i32, i32* %17
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds [3 x double], [3 x double]* %15, i64 0, i64 %41
store double %39, double* %42
br label %43
43: ; preds = %27
%44 = load i32, i32* %17
%45 = add nsw i32 %44, 1
store i32 %45, i32* %17
br label %24
46: ; preds = %24
%47 = getelementptr inbounds [3 x double], [3 x double]* %15, i64 0, i64 0
%48 = load [3 x double]*, [3 x double]** %9
%49 = bitcast [3 x double]* %48 to double**
%50 = getelementptr inbounds [3 x double], [3 x double]* %15, i64 0, i64 0
%51 = call i32 @mat_multiply_matrix_vector_d3(double* %47, double** %49, double* %50)
%52 = getelementptr inbounds [3 x double], [3 x double]* %15, i64 0, i64 0
%53 = call double @mat_norm_squared_d3(double* %52)
%54 = load double*, double** %12
%55 = load i32, i32* %16
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds double, double* %54, i64 %56
store double %53, double* %57
br label %58
58: ; preds = %46
%59 = load i32, i32* %16
%60 = add nsw i32 %59, 1
store i32 %60, i32* %16
br label %19
61: ; preds = %19
%62 = load i32*, i32** %8
%63 = load i32*, i32** %11
%64 = load double*, double** %12
%65 = load i8*, i8** %13
%66 = load i32, i32* %14
%67 = call i32 @perm_argsort(i32* %62, i32* %63, double* %64, i8* %65, i32 %66)
ret i32 %67
}

LLVM IR (-O2):
define i32 @argsort_by_lattice_point_distance(i32* %0, [3 x double]* %1, [3 x double]* nocapture %2, i32* %3, double* %4, i8* %5, i32 %6) {
%8 = alloca [3 x double]
%9 = bitcast [3 x double]* %8 to i8*
%10 = icmp sgt i32 %6, 0
br i1 %10, label %11, label %36
11: ; preds = %7
%12 = getelementptr inbounds [3 x double], [3 x double]* %8, i64 0, i64 0
%13 = bitcast [3 x double]* %1 to double**
%14 = zext i32 %6 to i64
%15 = getelementptr inbounds [3 x double], [3 x double]* %8, i64 0, i64 1
%16 = getelementptr inbounds [3 x double], [3 x double]* %8, i64 0, i64 2
br label %17
17: ; preds = %11, %17
%18 = phi i64 [ 0, %11 ], [ %34, %17 ]
%19 = getelementptr inbounds [3 x double], [3 x double]* %2, i64 %18, i64 0
%20 = load double, double* %19
%21 = call double @mat_Nint(double %20)
%22 = fsub double %20, %21
store double %22, double* %12
%23 = getelementptr inbounds [3 x double], [3 x double]* %2, i64 %18, i64 1
%24 = load double, double* %23
%25 = call double @mat_Nint(double %24)
%26 = fsub double %24, %25
store double %26, double* %15
%27 = getelementptr inbounds [3 x double], [3 x double]* %2, i64 %18, i64 2
%28 = load double, double* %27
%29 = call double @mat_Nint(double %28)
%30 = fsub double %28, %29
store double %30, double* %16
%31 = call i32 @mat_multiply_matrix_vector_d3(double* %12, double** %13, double* %12)
%32 = call double @mat_norm_squared_d3(double* %12)
%33 = getelementptr inbounds double, double* %4, i64 %18
store double %32, double* %33
%34 = add nuw nsw i64 %18, 1
%35 = icmp eq i64 %34, %14
br i1 %35, label %36, label %17
36: ; preds = %17, %7
%37 = call i32 @perm_argsort(i32* %0, i32* %3, double* %4, i8* %5, i32 %6)
ret i32 %37
}