#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 uint32_t;

/* Variables and functions */
void insert_point_double(uint32_t *closest_idx, double *closest_dist,
                         uint32_t pidx, double cur_dist, uint32_t k) {
  int i;
  for (i = k - 1; i > 0; i--) {
    if (closest_dist[i - 1] > cur_dist) {
      closest_dist[i] = closest_dist[i - 1];
      closest_idx[i] = closest_idx[i - 1];
    } else {
      break;
    }
  }
  closest_idx[i] = pidx;
  closest_dist[i] = cur_dist;
}


x86 assembly (clang, -O0):
<insert_point_double>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movsd  %xmm0,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x24(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jle    119e <insert_point_double+0x9e>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
ucomisd -0x20(%rbp),%xmm0
jbe    1186 <insert_point_double+0x86>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x8(%rbp),%rax
mov    -0x28(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x8(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
jmp    118b <insert_point_double+0x8b>
jmp    119e <insert_point_double+0x9e>
jmp    1190 <insert_point_double+0x90>
mov    -0x28(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x28(%rbp)
jmp    1120 <insert_point_double+0x20>
mov    -0x14(%rbp),%edx
mov    -0x8(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
movsd  -0x20(%rbp),%xmm0
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
pop    %rbp
ret

x86 assembly (clang, -O2):
<insert_point_double>:
lea    -0x1(%rcx),%r8d
cmp    $0x2,%ecx
jl     113d <insert_point_double+0x3d>
mov    %r8d,%r8d
add    $0xfffffffe,%ecx
nop
mov    %ecx,%ecx
movsd  (%rsi,%rcx,8),%xmm1
ucomisd %xmm0,%xmm1
jbe    113d <insert_point_double+0x3d>
movsd  %xmm1,(%rsi,%r8,8)
mov    (%rdi,%rcx,4),%eax
mov    %eax,(%rdi,%r8,4)
lea    -0x1(%r8),%rax
add    $0xffffffff,%ecx
cmp    $0x1,%r8
mov    %rax,%r8
jg     1110 <insert_point_double+0x10>
xor    %r8d,%r8d
movslq %r8d,%rax
mov    %edx,(%rdi,%rax,4)
movsd  %xmm0,(%rsi,%rax,8)
ret

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

LLVM IR (-O2):
define void @insert_point_double(i32* nocapture %0, double* nocapture %1, i32 %2, double %3, i32 %4) {
%6 = add i32 %4, -1
%7 = icmp sgt i32 %4, 1
br i1 %7, label %8, label %28
8: ; preds = %5
%9 = zext i32 %6 to i64
br label %10
10: ; preds = %8, %19
%11 = phi i64 [ %9, %8 ], [ %24, %19 ]
%12 = phi i32 [ %4, %8 ], [ %18, %19 ]
%13 = add nsw i32 %12, -2
%14 = zext i32 %13 to i64
%15 = getelementptr inbounds double, double* %1, i64 %14
%16 = load double, double* %15
%17 = fcmp ogt double %16, %3
%18 = trunc i64 %11 to i32
br i1 %17, label %19, label %26
19: ; preds = %10
%20 = getelementptr inbounds double, double* %1, i64 %11
store double %16, double* %20
%21 = getelementptr inbounds i32, i32* %0, i64 %14
%22 = load i32, i32* %21
%23 = getelementptr inbounds i32, i32* %0, i64 %11
store i32 %22, i32* %23
%24 = add nsw i64 %11, -1
%25 = icmp sgt i64 %11, 1
br i1 %25, label %10, label %28
26: ; preds = %10
%27 = trunc i64 %11 to i32
br label %28
28: ; preds = %26, %19, %5
%29 = phi i32 [ %6, %5 ], [ %27, %26 ], [ 0, %19 ]
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i32, i32* %0, i64 %30
store i32 %2, i32* %31
%32 = getelementptr inbounds double, double* %1, i64 %30
store double %3, double* %32
ret void
}