; LLVM IR from tangjz/acm-icpc :: hackerrank (closest-numbers)

; Unoptimized IR (clang -disable-llvm-passes):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

@n = external dso_local global i32, align 4
@a = external dso_local global [200001 x i32], align 16
@pos = external dso_local global i32, align 4
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [7 x i8], align 1

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  %5 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @n)
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #4
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %6

6:                                                ; preds = %16, %0
  %7 = load i32, ptr %2, align 4, !tbaa !5
  %8 = load i32, ptr @n, align 4, !tbaa !5
  %9 = icmp slt i32 %7, %8
  br i1 %9, label %11, label %10

10:                                               ; preds = %6
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #4
  br label %19

11:                                               ; preds = %6
  %12 = load i32, ptr %2, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds i32, ptr @a, i64 %13
  %15 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %14)
  br label %16

16:                                               ; preds = %11
  %17 = load i32, ptr %2, align 4, !tbaa !5
  %18 = add nsw i32 %17, 1
  store i32 %18, ptr %2, align 4, !tbaa !5
  br label %6, !llvm.loop !9

19:                                               ; preds = %10
  %20 = load i32, ptr @n, align 4, !tbaa !5
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds i32, ptr @a, i64 %21
  call void @_ZSt4sortIPiEvT_S1_(ptr noundef @a, ptr noundef %22)
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  store i32 1, ptr %3, align 4, !tbaa !5
  br label %23

23:                                               ; preds = %54, %19
  %24 = load i32, ptr %3, align 4, !tbaa !5
  %25 = add nsw i32 %24, 1
  %26 = load i32, ptr @n, align 4, !tbaa !5
  %27 = icmp slt i32 %25, %26
  br i1 %27, label %29, label %28

28:                                               ; preds = %23
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  br label %57

29:                                               ; preds = %23
  %30 = load i32, ptr @pos, align 4, !tbaa !5
  %31 = add nsw i32 %30, 1
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [200001 x i32], ptr @a, i64 0, i64 %32
  %34 = load i32, ptr %33, align 4, !tbaa !5
  %35 = load i32, ptr @pos, align 4, !tbaa !5
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [200001 x i32], ptr @a, i64 0, i64 %36
  %38 = load i32, ptr %37, align 4, !tbaa !5
  %39 = sub nsw i32 %34, %38
  %40 = load i32, ptr %3, align 4, !tbaa !5
  %41 = add nsw i32 %40, 1
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds [200001 x i32], ptr @a, i64 0, i64 %42
  %44 = load i32, ptr %43, align 4, !tbaa !5
  %45 = load i32, ptr %3, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [200001 x i32], ptr @a, i64 0, i64 %46
  %48 = load i32, ptr %47, align 4, !tbaa !5
  %49 = sub nsw i32 %44, %48
  %50 = icmp sgt i32 %39, %49
  br i1 %50, label %51, label %53

51:                                               ; preds = %29
  %52 = load i32, ptr %3, align 4, !tbaa !5
  store i32 %52, ptr @pos, align 4, !tbaa !5
  br label %53

53:                                               ; preds = %51, %29
  br label %54

54:                                               ; preds = %53
  %55 = load i32, ptr %3, align 4, !tbaa !5
  %56 = add nsw i32 %55, 1
  store i32 %56, ptr %3, align 4, !tbaa !5
  br label %23, !llvm.loop !11

57:                                               ; preds = %28
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %58

58:                                               ; preds = %98, %57
  %59 = load i32, ptr %4, align 4, !tbaa !5
  %60 = add nsw i32 %59, 1
  %61 = load i32, ptr @n, align 4, !tbaa !5
  %62 = icmp slt i32 %60, %61
  br i1 %62, label %64, label %63

63:                                               ; preds = %58
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  br label %101

64:                                               ; preds = %58
  %65 = load i32, ptr @pos, align 4, !tbaa !5
  %66 = add nsw i32 %65, 1
  %67 = sext i32 %66 to i64
  %68 = getelementptr inbounds [200001 x i32], ptr @a, i64 0, i64 %67
  %69 = load i32, ptr %68, align 4, !tbaa !5
  %70 = load i32, ptr @pos, align 4, !tbaa !5
  %71 = sext i32 %70 to i64
  %72 = getelementptr inbounds [200001 x i32], ptr @a, i64 0, i64 %71
  %73 = load i32, ptr %72, align 4, !tbaa !5
  %74 = sub nsw i32 %69, %73
  %75 = load i32, ptr %4, align 4, !tbaa !5
  %76 = add nsw i32 %75, 1
  %77 = sext i32 %76 to i64
  %78 = getelementptr inbounds [200001 x i32], ptr @a, i64 0, i64 %77
  %79 = load i32, ptr %78, align 4, !tbaa !5
  %80 = load i32, ptr %4, align 4, !tbaa !5
  %81 = sext i32 %80 to i64
  %82 = getelementptr inbounds [200001 x i32], ptr @a, i64 0, i64 %81
  %83 = load i32, ptr %82, align 4, !tbaa !5
  %84 = sub nsw i32 %79, %83
  %85 = icmp eq i32 %74, %84
  br i1 %85, label %86, label %97

86:                                               ; preds = %64
  %87 = load i32, ptr %4, align 4, !tbaa !5
  %88 = sext i32 %87 to i64
  %89 = getelementptr inbounds [200001 x i32], ptr @a, i64 0, i64 %88
  %90 = load i32, ptr %89, align 4, !tbaa !5
  %91 = load i32, ptr %4, align 4, !tbaa !5
  %92 = add nsw i32 %91, 1
  %93 = sext i32 %92 to i64
  %94 = getelementptr inbounds [200001 x i32], ptr @a, i64 0, i64 %93
  %95 = load i32, ptr %94, align 4, !tbaa !5
  %96 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %90, i32 noundef %95)
  br label %97

97:                                               ; preds = %86, %64
  br label %98

98:                                               ; preds = %97
  %99 = load i32, ptr %4, align 4, !tbaa !5
  %100 = add nsw i32 %99, 1
  store i32 %100, ptr %4, align 4, !tbaa !5
  br label %58, !llvm.loop !12

101:                                              ; preds = %63
  %102 = call i32 @putchar(i32 noundef 10)
  ret i32 0
}

declare i32 @__isoc99_scanf(ptr noundef, ...) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #2

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_ZSt4sortIPiEvT_S1_(ptr noundef, ptr noundef) #3

declare i32 @printf(ptr noundef, ...) #1

; Function Attrs: inlinehint mustprogress uwtable
declare i32 @putchar(i32 noundef) #3

attributes #0 = { mustprogress norecurse uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { inlinehint mustprogress uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #4 = { nounwind }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !10}


; Optimized IR (opt -passes='default<O3>'):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

@n = external dso_local global i32, align 4
@a = external dso_local global [200001 x i32], align 16
@pos = external dso_local local_unnamed_addr global i32, align 4
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [7 x i8], align 1

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @n)
  %2 = load i32, ptr @n, align 4, !tbaa !5
  %3 = icmp sgt i32 %2, 0
  br i1 %3, label %.lr.ph, label %.._crit_edge_crit_edge

.._crit_edge_crit_edge:                           ; preds = %0
  %.pre48 = sext i32 %2 to i64
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph, %.._crit_edge_crit_edge
  %.pre-phi = phi i64 [ %.pre48, %.._crit_edge_crit_edge ], [ %11, %.lr.ph ]
  %4 = getelementptr inbounds i32, ptr @a, i64 %.pre-phi
  tail call void @_ZSt4sortIPiEvT_S1_(ptr noundef nonnull @a, ptr noundef nonnull %4)
  %5 = load i32, ptr @n, align 4, !tbaa !5
  %pos.promoted = load i32, ptr @pos, align 4, !tbaa !5
  %6 = icmp sgt i32 %5, 2
  br i1 %6, label %.lr.ph25.preheader, label %.preheader

.lr.ph25.preheader:                               ; preds = %._crit_edge
  %7 = add nsw i32 %5, -1
  %wide.trip.count = zext nneg i32 %7 to i64
  br label %.lr.ph25

.lr.ph:                                           ; preds = %0, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %0 ]
  %8 = getelementptr inbounds i32, ptr @a, i64 %indvars.iv
  %9 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %8)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %10 = load i32, ptr @n, align 4, !tbaa !5
  %11 = sext i32 %10 to i64
  %12 = icmp slt i64 %indvars.iv.next, %11
  br i1 %12, label %.lr.ph, label %._crit_edge, !llvm.loop !9

.preheader:                                       ; preds = %30, %._crit_edge
  %.pre45 = phi i32 [ %pos.promoted, %._crit_edge ], [ %.01420, %30 ]
  %13 = icmp sgt i32 %5, 1
  br i1 %13, label %.lr.ph27, label %._crit_edge28

.lr.ph25:                                         ; preds = %.lr.ph25.preheader, %30
  %indvars.iv33 = phi i64 [ 1, %.lr.ph25.preheader ], [ %indvars.iv.next34, %30 ]
  %indvars.iv31 = phi i64 [ 2, %.lr.ph25.preheader ], [ %indvars.iv.next32, %30 ]
  %.0142122 = phi i32 [ %pos.promoted, %.lr.ph25.preheader ], [ %.01420, %30 ]
  %14 = add nsw i32 %.0142122, 1
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds [200001 x i32], ptr @a, i64 0, i64 %15
  %17 = load i32, ptr %16, align 4, !tbaa !5
  %18 = sext i32 %.0142122 to i64
  %19 = getelementptr inbounds [200001 x i32], ptr @a, i64 0, i64 %18
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = sub nsw i32 %17, %20
  %22 = getelementptr inbounds [200001 x i32], ptr @a, i64 0, i64 %indvars.iv31
  %23 = load i32, ptr %22, align 4, !tbaa !5
  %24 = getelementptr inbounds [200001 x i32], ptr @a, i64 0, i64 %indvars.iv33
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %26 = sub nsw i32 %23, %25
  %27 = icmp sgt i32 %21, %26
  br i1 %27, label %28, label %30

28:                                               ; preds = %.lr.ph25
  %29 = trunc nuw nsw i64 %indvars.iv33 to i32
  store i32 %29, ptr @pos, align 4, !tbaa !5
  br label %30

30:                                               ; preds = %.lr.ph25, %28
  %.01420 = phi i32 [ %.0142122, %.lr.ph25 ], [ %29, %28 ]
  %indvars.iv.next32 = add nuw nsw i64 %indvars.iv31, 1
  %indvars.iv.next34 = add nuw nsw i64 %indvars.iv33, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next34, %wide.trip.count
  br i1 %exitcond.not, label %.preheader, label %.lr.ph25, !llvm.loop !11

._crit_edge28:                                    ; preds = %50, %.preheader
  %31 = tail call i32 @putchar(i32 noundef 10)
  ret i32 0

.lr.ph27:                                         ; preds = %.preheader, %50
  %32 = phi i32 [ %51, %50 ], [ %5, %.preheader ]
  %33 = phi i32 [ %52, %50 ], [ %.pre45, %.preheader ]
  %indvars.iv40 = phi i64 [ %indvars.iv.next41, %50 ], [ 0, %.preheader ]
  %indvars.iv38 = phi i64 [ %indvars.iv.next39, %50 ], [ 1, %.preheader ]
  %34 = add nsw i32 %33, 1
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [200001 x i32], ptr @a, i64 0, i64 %35
  %37 = load i32, ptr %36, align 4, !tbaa !5
  %38 = sext i32 %33 to i64
  %39 = getelementptr inbounds [200001 x i32], ptr @a, i64 0, i64 %38
  %40 = load i32, ptr %39, align 4, !tbaa !5
  %41 = sub nsw i32 %37, %40
  %42 = getelementptr inbounds [200001 x i32], ptr @a, i64 0, i64 %indvars.iv38
  %43 = load i32, ptr %42, align 4, !tbaa !5
  %44 = getelementptr inbounds [200001 x i32], ptr @a, i64 0, i64 %indvars.iv40
  %45 = load i32, ptr %44, align 4, !tbaa !5
  %46 = sub nsw i32 %43, %45
  %47 = icmp eq i32 %41, %46
  br i1 %47, label %48, label %50

48:                                               ; preds = %.lr.ph27
  %49 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %45, i32 noundef %43)
  %.pre = load i32, ptr @pos, align 4, !tbaa !5
  %.pre46 = load i32, ptr @n, align 4, !tbaa !5
  br label %50

50:                                               ; preds = %.lr.ph27, %48
  %51 = phi i32 [ %32, %.lr.ph27 ], [ %.pre46, %48 ]
  %52 = phi i32 [ %33, %.lr.ph27 ], [ %.pre, %48 ]
  %indvars.iv.next39 = add nuw nsw i64 %indvars.iv38, 1
  %53 = sext i32 %51 to i64
  %54 = icmp slt i64 %indvars.iv.next39, %53
  %indvars.iv.next41 = add nuw nsw i64 %indvars.iv40, 1
  br i1 %54, label %.lr.ph27, label %._crit_edge28, !llvm.loop !12
}

; Function Attrs: nofree nounwind
declare noundef i32 @__isoc99_scanf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_ZSt4sortIPiEvT_S1_(ptr noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

; Function Attrs: inlinehint mustprogress nofree nounwind uwtable
declare noundef i32 @putchar(i32 noundef) local_unnamed_addr #3

attributes #0 = { mustprogress norecurse uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { nofree nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #2 = { inlinehint mustprogress uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #3 = { inlinehint mustprogress nofree nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !10}


; Effective O3 passes: BDCEPass,CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass