; LLVM IR from ritikbanger/Hacktoberfest2022-DSA :: 04. Arrays/3sum_closest.ll (Z15threeSumClosestRSt6vectorIiSaIiEEi)

; 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"

%"class.__gnu_cxx::__normal_iterator" = type { ptr }

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z15threeSumClosestRSt6vectorIiSaIiEEi(ptr noundef nonnull align 8 dereferenceable(24) %0, i32 noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca %"class.__gnu_cxx::__normal_iterator", align 8
  %7 = alloca %"class.__gnu_cxx::__normal_iterator", align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  %17 = call ptr @_ZNSt6vectorIiSaIiEE5beginEv(ptr noundef nonnull align 8 dereferenceable(24) %16) #5
  %18 = getelementptr inbounds %"class.__gnu_cxx::__normal_iterator", ptr %6, i32 0, i32 0
  store ptr %17, ptr %18, align 8
  %19 = load ptr, ptr %4, align 8, !tbaa !5
  %20 = call ptr @_ZNSt6vectorIiSaIiEE3endEv(ptr noundef nonnull align 8 dereferenceable(24) %19) #5
  %21 = getelementptr inbounds %"class.__gnu_cxx::__normal_iterator", ptr %7, i32 0, i32 0
  store ptr %20, ptr %21, align 8
  %22 = getelementptr inbounds %"class.__gnu_cxx::__normal_iterator", ptr %6, i32 0, i32 0
  %23 = load ptr, ptr %22, align 8
  %24 = getelementptr inbounds %"class.__gnu_cxx::__normal_iterator", ptr %7, i32 0, i32 0
  %25 = load ptr, ptr %24, align 8
  call void @_ZSt4sortIN9__gnu_cxx17__normal_iteratorIPiSt6vectorIiSaIiEEEEEvT_S7_(ptr %23, ptr %25)
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #5
  %26 = load ptr, ptr %4, align 8, !tbaa !5
  %27 = call noundef i64 @_ZNKSt6vectorIiSaIiEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(24) %26) #5
  %28 = trunc i64 %27 to i32
  store i32 %28, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #5
  store i32 2147483647, ptr %10, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #5
  store i32 0, ptr %11, align 4, !tbaa !9
  br label %29

29:                                               ; preds = %101, %2
  %30 = load i32, ptr %11, align 4, !tbaa !9
  %31 = load i32, ptr %8, align 4, !tbaa !9
  %32 = icmp slt i32 %30, %31
  br i1 %32, label %34, label %33

33:                                               ; preds = %29
  store i32 2, ptr %12, align 4
  br label %104

34:                                               ; preds = %29
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #5
  %35 = load i32, ptr %11, align 4, !tbaa !9
  %36 = add nsw i32 %35, 1
  store i32 %36, ptr %13, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #5
  %37 = load i32, ptr %8, align 4, !tbaa !9
  %38 = sub nsw i32 %37, 1
  store i32 %38, ptr %14, align 4, !tbaa !9
  br label %39

39:                                               ; preds = %96, %34
  %40 = load i32, ptr %14, align 4, !tbaa !9
  %41 = load i32, ptr %13, align 4, !tbaa !9
  %42 = icmp sgt i32 %40, %41
  br i1 %42, label %43, label %97

43:                                               ; preds = %39
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #5
  %44 = load ptr, ptr %4, align 8, !tbaa !5
  %45 = load i32, ptr %13, align 4, !tbaa !9
  %46 = sext i32 %45 to i64
  %47 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) %44, i64 noundef %46) #5
  %48 = load i32, ptr %47, align 4, !tbaa !9
  %49 = load ptr, ptr %4, align 8, !tbaa !5
  %50 = load i32, ptr %11, align 4, !tbaa !9
  %51 = sext i32 %50 to i64
  %52 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) %49, i64 noundef %51) #5
  %53 = load i32, ptr %52, align 4, !tbaa !9
  %54 = add nsw i32 %48, %53
  %55 = load ptr, ptr %4, align 8, !tbaa !5
  %56 = load i32, ptr %14, align 4, !tbaa !9
  %57 = sext i32 %56 to i64
  %58 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) %55, i64 noundef %57) #5
  %59 = load i32, ptr %58, align 4, !tbaa !9
  %60 = add nsw i32 %54, %59
  store i32 %60, ptr %15, align 4, !tbaa !9
  %61 = load i32, ptr %10, align 4, !tbaa !9
  %62 = load i32, ptr %15, align 4, !tbaa !9
  %63 = load i32, ptr %5, align 4, !tbaa !9
  %64 = sub nsw i32 %62, %63
  %65 = call i32 @llvm.abs.i32(i32 %64, i1 true)
  %66 = icmp sgt i32 %61, %65
  br i1 %66, label %67, label %73

67:                                               ; preds = %43
  %68 = load i32, ptr %15, align 4, !tbaa !9
  %69 = load i32, ptr %5, align 4, !tbaa !9
  %70 = sub nsw i32 %68, %69
  %71 = call i32 @llvm.abs.i32(i32 %70, i1 true)
  store i32 %71, ptr %10, align 4, !tbaa !9
  %72 = load i32, ptr %15, align 4, !tbaa !9
  store i32 %72, ptr %9, align 4, !tbaa !9
  br label %73

73:                                               ; preds = %67, %43
  %74 = load i32, ptr %15, align 4, !tbaa !9
  %75 = load i32, ptr %5, align 4, !tbaa !9
  %76 = icmp eq i32 %74, %75
  br i1 %76, label %77, label %79

77:                                               ; preds = %73
  %78 = load i32, ptr %15, align 4, !tbaa !9
  store i32 %78, ptr %3, align 4
  store i32 1, ptr %12, align 4
  br label %94

79:                                               ; preds = %73
  %80 = load i32, ptr %15, align 4, !tbaa !9
  %81 = load i32, ptr %5, align 4, !tbaa !9
  %82 = icmp sgt i32 %80, %81
  br i1 %82, label %83, label %86

83:                                               ; preds = %79
  %84 = load i32, ptr %14, align 4, !tbaa !9
  %85 = add nsw i32 %84, -1
  store i32 %85, ptr %14, align 4, !tbaa !9
  br label %86

86:                                               ; preds = %83, %79
  %87 = load i32, ptr %15, align 4, !tbaa !9
  %88 = load i32, ptr %5, align 4, !tbaa !9
  %89 = icmp slt i32 %87, %88
  br i1 %89, label %90, label %93

90:                                               ; preds = %86
  %91 = load i32, ptr %13, align 4, !tbaa !9
  %92 = add nsw i32 %91, 1
  store i32 %92, ptr %13, align 4, !tbaa !9
  br label %93

93:                                               ; preds = %90, %86
  store i32 0, ptr %12, align 4
  br label %94

94:                                               ; preds = %93, %77
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #5
  %95 = load i32, ptr %12, align 4
  switch i32 %95, label %98 [
    i32 0, label %96
  ]

96:                                               ; preds = %94
  br label %39, !llvm.loop !11

97:                                               ; preds = %39
  store i32 0, ptr %12, align 4
  br label %98

98:                                               ; preds = %97, %94
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #5
  %99 = load i32, ptr %12, align 4
  switch i32 %99, label %104 [
    i32 0, label %100
  ]

100:                                              ; preds = %98
  br label %101

101:                                              ; preds = %100
  %102 = load i32, ptr %11, align 4, !tbaa !9
  %103 = add nsw i32 %102, 1
  store i32 %103, ptr %11, align 4, !tbaa !9
  br label %29, !llvm.loop !13

104:                                              ; preds = %98, %33
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #5
  %105 = load i32, ptr %12, align 4
  switch i32 %105, label %108 [
    i32 2, label %106
  ]

106:                                              ; preds = %104
  %107 = load i32, ptr %9, align 4, !tbaa !9
  store i32 %107, ptr %3, align 4
  store i32 1, ptr %12, align 4
  br label %108

108:                                              ; preds = %106, %104
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #5
  %109 = load i32, ptr %3, align 4
  ret i32 %109
}

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_ZSt4sortIN9__gnu_cxx17__normal_iteratorIPiSt6vectorIiSaIiEEEEEvT_S7_(ptr, ptr) #1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local ptr @_ZNSt6vectorIiSaIiEE5beginEv(ptr noundef nonnull align 8 dereferenceable(24)) #2 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local ptr @_ZNSt6vectorIiSaIiEE3endEv(ptr noundef nonnull align 8 dereferenceable(24)) #2 align 2

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZNKSt6vectorIiSaIiEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(24)) #2 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24), i64 noundef) #2 align 2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.abs.i32(i32, i1 immarg) #4

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

attributes #0 = { 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 #1 = { 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 #2 = { mustprogress 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" }
attributes #3 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #5 = { 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


; 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"

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z15threeSumClosestRSt6vectorIiSaIiEEi(ptr noundef nonnull align 8 dereferenceable(24) %0, i32 noundef %1) local_unnamed_addr #0 {
.lr.ph88:
  %2 = tail call ptr @_ZNSt6vectorIiSaIiEE5beginEv(ptr noundef nonnull align 8 dereferenceable(24) %0) #6
  %3 = tail call ptr @_ZNSt6vectorIiSaIiEE3endEv(ptr noundef nonnull align 8 dereferenceable(24) %0) #6
  tail call void @_ZSt4sortIN9__gnu_cxx17__normal_iteratorIPiSt6vectorIiSaIiEEEEEvT_S7_(ptr %2, ptr %3)
  %4 = tail call noundef i64 @_ZNKSt6vectorIiSaIiEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(24) %0) #6
  %5 = trunc i64 %4 to i32
  %.not6384 = icmp sgt i32 %5, 0
  tail call void @llvm.assume(i1 %.not6384)
  %6 = add nsw i32 %5, -1
  %7 = zext nneg i32 %6 to i64
  %wide.trip.count = and i64 %4, 2147483647
  br label %8

.loopexit:                                        ; preds = %21, %8
  %.153.lcssa = phi i32 [ %.05285, %8 ], [ %spec.select, %21 ]
  %.149.lcssa = phi i32 [ %.04886, %8 ], [ %spec.select64, %21 ]
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %.thread70, label %8

8:                                                ; preds = %.lr.ph88, %.loopexit
  %indvars.iv = phi i64 [ 0, %.lr.ph88 ], [ %indvars.iv.next, %.loopexit ]
  %.04886 = phi i32 [ 2147483647, %.lr.ph88 ], [ %.149.lcssa, %.loopexit ]
  %.05285 = phi i32 [ undef, %.lr.ph88 ], [ %.153.lcssa, %.loopexit ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %.not6278 = icmp ult i64 %indvars.iv.next, %7
  br i1 %.not6278, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %8
  %9 = trunc nuw nsw i64 %indvars.iv.next to i32
  br label %10

10:                                               ; preds = %.lr.ph, %21
  %.03882 = phi i32 [ %6, %.lr.ph ], [ %spec.select65, %21 ]
  %.04181 = phi i32 [ %9, %.lr.ph ], [ %.142, %21 ]
  %.14980 = phi i32 [ %.04886, %.lr.ph ], [ %spec.select64, %21 ]
  %.15379 = phi i32 [ %.05285, %.lr.ph ], [ %spec.select, %21 ]
  %11 = zext nneg i32 %.04181 to i64
  %12 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) %0, i64 noundef %11) #6
  %13 = load i32, ptr %12, align 4, !tbaa !5
  %14 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) %0, i64 noundef %indvars.iv) #6
  %15 = load i32, ptr %14, align 4, !tbaa !5
  %16 = add nsw i32 %15, %13
  %17 = sext i32 %.03882 to i64
  %18 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) %0, i64 noundef %17) #6
  %19 = load i32, ptr %18, align 4, !tbaa !5
  %20 = add nsw i32 %16, %19
  %.not = icmp eq i32 %20, %1
  br i1 %.not, label %.thread70, label %21

21:                                               ; preds = %10
  %22 = sub nsw i32 %20, %1
  %23 = tail call i32 @llvm.abs.i32(i32 %22, i1 true)
  %24 = icmp sgt i32 %.14980, %23
  %spec.select = select i1 %24, i32 %20, i32 %.15379
  %spec.select64 = tail call i32 @llvm.smin.i32(i32 %.14980, i32 %23)
  %25 = icmp sgt i32 %20, %1
  %26 = sext i1 %25 to i32
  %spec.select65 = add nsw i32 %.03882, %26
  %27 = icmp slt i32 %20, %1
  %28 = zext i1 %27 to i32
  %.142 = add nuw nsw i32 %.04181, %28
  %.not62 = icmp sgt i32 %spec.select65, %.142
  br i1 %.not62, label %10, label %.loopexit

.thread70:                                        ; preds = %.loopexit, %10
  %spec.select66 = phi i32 [ %1, %10 ], [ %.153.lcssa, %.loopexit ]
  ret i32 %spec.select66
}

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_ZSt4sortIN9__gnu_cxx17__normal_iteratorIPiSt6vectorIiSaIiEEEEEvT_S7_(ptr, ptr) local_unnamed_addr #1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local ptr @_ZNSt6vectorIiSaIiEE5beginEv(ptr noundef nonnull align 8 dereferenceable(24)) local_unnamed_addr #2 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local ptr @_ZNSt6vectorIiSaIiEE3endEv(ptr noundef nonnull align 8 dereferenceable(24)) local_unnamed_addr #2 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZNKSt6vectorIiSaIiEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(24)) local_unnamed_addr #2 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24), i64 noundef) local_unnamed_addr #2 align 2

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.abs.i32(i32, i1 immarg) #3

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smin.i32(i32, i32) #4

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(inaccessiblemem: write)
declare void @llvm.assume(i1 noundef) #5

attributes #0 = { 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 #1 = { 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 #2 = { mustprogress 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" }
attributes #3 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #5 = { nocallback nofree nosync nounwind willreturn memory(inaccessiblemem: write) }
attributes #6 = { 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"}


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