#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 */
int /*<<< orphan*/ free(float *);
float *get_1d_mem_float(int);
float getdist(float, float, float, float);
int find_near(float grid_lat, float grid_lon, float *sta_lat, float *sta_lon,
              int n_sta) {
  float min_dist;
  int i, iflag;
  float *dist;
  dist = get_1d_mem_float(n_sta);
  min_dist = 999999;
  for (i = 0; i < n_sta; i++) {
    dist[i] = getdist(grid_lat, grid_lon, sta_lat[i], sta_lon[i]);
    if (dist[i] < min_dist) {
      min_dist = dist[i];
      iflag = i;
    }
  }
  free(dist);
  return iflag;
}


x86 assembly (clang, -O0):
<find_near>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movss  %xmm0,-0x4(%rbp)
movss  %xmm1,-0x8(%rbp)
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <get_1d_mem_float@plt>
mov    %rax,-0x30(%rbp)
movss  0xe9f(%rip),%xmm0
movss  %xmm0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11f5 <find_near+0xc5>
movss  -0x4(%rbp),%xmm0
movss  -0x8(%rbp),%xmm1
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
movss  (%rax,%rcx,4),%xmm2
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
movss  (%rax,%rcx,4),%xmm3
call   1050 <getdist@plt>
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
movss  %xmm0,(%rax,%rcx,4)
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
movss  (%rax,%rcx,4),%xmm1
movss  -0x20(%rbp),%xmm0
ucomiss %xmm1,%xmm0
jbe    11e2 <find_near+0xb2>
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
movss  (%rax,%rcx,4),%xmm0
movss  %xmm0,-0x20(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
jmp    11e7 <find_near+0xb7>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    116d <find_near+0x3d>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_near>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%r15
movss  %xmm1,0x10(%rsp)
movss  %xmm0,0xc(%rsp)
mov    %edx,%edi
call   1040 <get_1d_mem_float@plt>
mov    %rax,%r12
test   %ebp,%ebp
jle    11b4 <find_near+0x84>
mov    %ebp,%r13d
movss  0xe95(%rip),%xmm0
xor    %ebx,%ebx
nopl   (%rax)
movss  %xmm0,0x14(%rsp)
movss  (%r15,%rbx,4),%xmm2
movss  (%r14,%rbx,4),%xmm3
movss  0xc(%rsp),%xmm0
movss  0x10(%rsp),%xmm1
call   1050 <getdist@plt>
movss  0x14(%rsp),%xmm1
ucomiss %xmm0,%xmm1
cmova  %ebx,%ebp
movss  %xmm0,(%r12,%rbx,4)
minss  %xmm1,%xmm0
add    $0x1,%rbx
cmp    %rbx,%r13
jne    1170 <find_near+0x40>
jmp    11b4 <find_near+0x84>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @find_near(float %0, float %1, float* nocapture %2, float* nocapture %3, i32 %4) {
%6 = tail call float* @get_1d_mem_float(i32 %4)
%7 = icmp sgt i32 %4, 0
br i1 %7, label %8, label %26
8: ; preds = %5
%9 = zext i32 %4 to i64
br label %10
10: ; preds = %8, %10
%11 = phi i64 [ 0, %8 ], [ %24, %10 ]
%12 = phi float [ 9.999990e+05, %8 ], [ %23, %10 ]
%13 = phi i32 [ undef, %8 ], [ %22, %10 ]
%14 = getelementptr inbounds float, float* %2, i64 %11
%15 = load float, float* %14
%16 = getelementptr inbounds float, float* %3, i64 %11
%17 = load float, float* %16
%18 = tail call float @getdist(float %0, float %1, float %15, float %17)
%19 = getelementptr inbounds float, float* %6, i64 %11
store float %18, float* %19
%20 = fcmp olt float %18, %12
%21 = trunc i64 %11 to i32
%22 = select i1 %20, i32 %21, i32 %13
%23 = select i1 %20, float %18, float %12
%24 = add nuw nsw i64 %11, 1
%25 = icmp eq i64 %24, %9
br i1 %25, label %26, label %10
26: ; preds = %10, %5
%27 = phi i32 [ undef, %5 ], [ %22, %10 ]
%28 = tail call i32 @free(float* %6)
ret i32 %27
}