; LLVM IR from div-bargali/Data-Structures-and-Algorithms :: C++/Algorithms/Sorting-Algorithms/SelectionSort (selection_sort)

; 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.std::basic_istream" = type { ptr, i64, %"class.std::basic_ios" }
%"class.std::basic_ios" = type { %"class.std::ios_base", ptr, i8, i8, ptr, ptr, ptr, ptr }
%"class.std::ios_base" = type { ptr, i64, i64, i32, i32, i32, ptr, %"struct.std::ios_base::_Words", [8 x %"struct.std::ios_base::_Words"], i32, ptr, %"class.std::locale" }
%"struct.std::ios_base::_Words" = type { ptr, i64 }
%"class.std::locale" = type { ptr }
%"class.std::basic_ostream" = type { ptr, %"class.std::basic_ios" }

@_ZSt3cin = external global %"class.std::basic_istream", align 8
@_ZSt4cout = external global %"class.std::basic_ostream", align 8

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i64, align 8
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  %9 = call noundef nonnull align 8 dereferenceable(16) ptr @_ZNSirsERx(ptr noundef nonnull align 8 dereferenceable(16) @_ZSt3cin, ptr noundef nonnull align 8 dereferenceable(8) %2)
  %10 = load i64, ptr %2, align 8, !tbaa !5
  %11 = call ptr @llvm.stacksave.p0()
  store ptr %11, ptr %7, align 8
  %12 = alloca i64, i64 %10, align 16
  store i64 %10, ptr %8, align 8
  store i64 0, ptr %3, align 8, !tbaa !5
  br label %13

13:                                               ; preds = %21, %0
  %14 = load i64, ptr %3, align 8, !tbaa !5
  %15 = load i64, ptr %2, align 8, !tbaa !5
  %16 = icmp slt i64 %14, %15
  br i1 %16, label %17, label %24

17:                                               ; preds = %13
  %18 = load i64, ptr %3, align 8, !tbaa !5
  %19 = getelementptr inbounds i64, ptr %12, i64 %18
  %20 = call noundef nonnull align 8 dereferenceable(16) ptr @_ZNSirsERx(ptr noundef nonnull align 8 dereferenceable(16) @_ZSt3cin, ptr noundef nonnull align 8 dereferenceable(8) %19)
  br label %21

21:                                               ; preds = %17
  %22 = load i64, ptr %3, align 8, !tbaa !5
  %23 = add nsw i64 %22, 1
  store i64 %23, ptr %3, align 8, !tbaa !5
  br label %13, !llvm.loop !9

24:                                               ; preds = %13
  store i64 0, ptr %3, align 8, !tbaa !5
  br label %25

25:                                               ; preds = %64, %24
  %26 = load i64, ptr %3, align 8, !tbaa !5
  %27 = load i64, ptr %2, align 8, !tbaa !5
  %28 = sub nsw i64 %27, 1
  %29 = icmp slt i64 %26, %28
  br i1 %29, label %30, label %67

30:                                               ; preds = %25
  %31 = load i64, ptr %3, align 8, !tbaa !5
  store i64 %31, ptr %5, align 8, !tbaa !5
  %32 = load i64, ptr %3, align 8, !tbaa !5
  %33 = add nsw i64 %32, 1
  store i64 %33, ptr %4, align 8, !tbaa !5
  br label %34

34:                                               ; preds = %49, %30
  %35 = load i64, ptr %4, align 8, !tbaa !5
  %36 = load i64, ptr %2, align 8, !tbaa !5
  %37 = icmp slt i64 %35, %36
  br i1 %37, label %38, label %52

38:                                               ; preds = %34
  %39 = load i64, ptr %4, align 8, !tbaa !5
  %40 = getelementptr inbounds i64, ptr %12, i64 %39
  %41 = load i64, ptr %40, align 8, !tbaa !5
  %42 = load i64, ptr %5, align 8, !tbaa !5
  %43 = getelementptr inbounds i64, ptr %12, i64 %42
  %44 = load i64, ptr %43, align 8, !tbaa !5
  %45 = icmp slt i64 %41, %44
  br i1 %45, label %46, label %48

46:                                               ; preds = %38
  %47 = load i64, ptr %4, align 8, !tbaa !5
  store i64 %47, ptr %5, align 8, !tbaa !5
  br label %48

48:                                               ; preds = %46, %38
  br label %49

49:                                               ; preds = %48
  %50 = load i64, ptr %4, align 8, !tbaa !5
  %51 = add nsw i64 %50, 1
  store i64 %51, ptr %4, align 8, !tbaa !5
  br label %34, !llvm.loop !11

52:                                               ; preds = %34
  %53 = load i64, ptr %5, align 8, !tbaa !5
  %54 = getelementptr inbounds i64, ptr %12, i64 %53
  %55 = load i64, ptr %54, align 8, !tbaa !5
  store i64 %55, ptr %6, align 8, !tbaa !5
  %56 = load i64, ptr %3, align 8, !tbaa !5
  %57 = getelementptr inbounds i64, ptr %12, i64 %56
  %58 = load i64, ptr %57, align 8, !tbaa !5
  %59 = load i64, ptr %5, align 8, !tbaa !5
  %60 = getelementptr inbounds i64, ptr %12, i64 %59
  store i64 %58, ptr %60, align 8, !tbaa !5
  %61 = load i64, ptr %6, align 8, !tbaa !5
  %62 = load i64, ptr %3, align 8, !tbaa !5
  %63 = getelementptr inbounds i64, ptr %12, i64 %62
  store i64 %61, ptr %63, align 8, !tbaa !5
  br label %64

64:                                               ; preds = %52
  %65 = load i64, ptr %3, align 8, !tbaa !5
  %66 = add nsw i64 %65, 1
  store i64 %66, ptr %3, align 8, !tbaa !5
  br label %25, !llvm.loop !12

67:                                               ; preds = %25
  store i64 0, ptr %3, align 8, !tbaa !5
  br label %68

68:                                               ; preds = %78, %67
  %69 = load i64, ptr %3, align 8, !tbaa !5
  %70 = load i64, ptr %2, align 8, !tbaa !5
  %71 = icmp slt i64 %69, %70
  br i1 %71, label %72, label %81

72:                                               ; preds = %68
  %73 = load i64, ptr %3, align 8, !tbaa !5
  %74 = getelementptr inbounds i64, ptr %12, i64 %73
  %75 = load i64, ptr %74, align 8, !tbaa !5
  %76 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEx(ptr noundef nonnull align 8 dereferenceable(8) @_ZSt4cout, i64 noundef %75)
  %77 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_c(ptr noundef nonnull align 8 dereferenceable(8) %76, i8 noundef signext 32)
  br label %78

78:                                               ; preds = %72
  %79 = load i64, ptr %3, align 8, !tbaa !5
  %80 = add nsw i64 %79, 1
  store i64 %80, ptr %3, align 8, !tbaa !5
  br label %68, !llvm.loop !13

81:                                               ; preds = %68
  store i32 0, ptr %1, align 4
  %82 = load ptr, ptr %7, align 8
  call void @llvm.stackrestore.p0(ptr %82)
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #5
  %83 = load i32, ptr %1, align 4
  ret i32 %83
}

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

; Function Attrs: mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(16) ptr @_ZNSirsERx(ptr noundef nonnull align 8 dereferenceable(16), ptr noundef nonnull align 8 dereferenceable(8)) #2 align 2

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #3

; Function Attrs: inlinehint mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_c(ptr noundef nonnull align 8 dereferenceable(8), i8 noundef signext) #4

; Function Attrs: mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEx(ptr noundef nonnull align 8 dereferenceable(8), i64 noundef) #2 align 2

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #3

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

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 = { nocallback nofree nosync nounwind willreturn }
attributes #4 = { 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 #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 = !{!"long long", !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}
!13 = distinct !{!13, !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"

%"class.std::basic_istream" = type { ptr, i64, %"class.std::basic_ios" }
%"class.std::basic_ios" = type { %"class.std::ios_base", ptr, i8, i8, ptr, ptr, ptr, ptr }
%"class.std::ios_base" = type { ptr, i64, i64, i32, i32, i32, ptr, %"struct.std::ios_base::_Words", [8 x %"struct.std::ios_base::_Words"], i32, ptr, %"class.std::locale" }
%"struct.std::ios_base::_Words" = type { ptr, i64 }
%"class.std::locale" = type { ptr }
%"class.std::basic_ostream" = type { ptr, %"class.std::basic_ios" }

@_ZSt3cin = external global %"class.std::basic_istream", align 8
@_ZSt4cout = external global %"class.std::basic_ostream", align 8

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %1) #5
  %2 = call noundef nonnull align 8 dereferenceable(16) ptr @_ZNSirsERx(ptr noundef nonnull align 8 dereferenceable(16) @_ZSt3cin, ptr noundef nonnull align 8 dereferenceable(8) %1)
  %3 = load i64, ptr %1, align 8, !tbaa !5
  %4 = call ptr @llvm.stacksave.p0()
  %5 = alloca i64, i64 %3, align 16
  %6 = load i64, ptr %1, align 8, !tbaa !5
  %7 = icmp sgt i64 %6, 0
  br i1 %7, label %.lr.ph, label %._crit_edge34

.preheader24:                                     ; preds = %.lr.ph
  %8 = icmp sgt i64 %13, 1
  br i1 %8, label %.lr.ph31.preheader, label %.preheader

.lr.ph31.preheader:                               ; preds = %.preheader24
  %9 = add nsw i64 %13, -2
  br label %.lr.ph28.preheader

.lr.ph:                                           ; preds = %0, %.lr.ph
  %.025 = phi i64 [ %12, %.lr.ph ], [ 0, %0 ]
  %10 = getelementptr inbounds i64, ptr %5, i64 %.025
  %11 = call noundef nonnull align 8 dereferenceable(16) ptr @_ZNSirsERx(ptr noundef nonnull align 8 dereferenceable(16) @_ZSt3cin, ptr noundef nonnull align 8 dereferenceable(8) %10)
  %12 = add nuw nsw i64 %.025, 1
  %13 = load i64, ptr %1, align 8, !tbaa !5
  %14 = icmp slt i64 %12, %13
  br i1 %14, label %.lr.ph, label %.preheader24, !llvm.loop !9

.preheader:                                       ; preds = %._crit_edge, %.preheader24
  %15 = icmp sgt i64 %13, 0
  br i1 %15, label %.lr.ph33, label %._crit_edge34

.lr.ph28.preheader:                               ; preds = %._crit_edge, %.lr.ph31.preheader
  %.130 = phi i64 [ %16, %._crit_edge ], [ 0, %.lr.ph31.preheader ]
  %16 = add nuw nsw i64 %.130, 1
  br label %.lr.ph28

.lr.ph28:                                         ; preds = %.lr.ph28.preheader, %.lr.ph28
  %.02127 = phi i64 [ %22, %.lr.ph28 ], [ %16, %.lr.ph28.preheader ]
  %.02226 = phi i64 [ %spec.select, %.lr.ph28 ], [ %.130, %.lr.ph28.preheader ]
  %17 = getelementptr inbounds i64, ptr %5, i64 %.02127
  %18 = load i64, ptr %17, align 8, !tbaa !5
  %19 = getelementptr inbounds i64, ptr %5, i64 %.02226
  %20 = load i64, ptr %19, align 8, !tbaa !5
  %21 = icmp slt i64 %18, %20
  %spec.select = select i1 %21, i64 %.02127, i64 %.02226
  %22 = add nuw nsw i64 %.02127, 1
  %23 = icmp slt i64 %22, %13
  br i1 %23, label %.lr.ph28, label %._crit_edge, !llvm.loop !11

._crit_edge:                                      ; preds = %.lr.ph28
  %24 = getelementptr inbounds i64, ptr %5, i64 %spec.select
  %25 = load i64, ptr %24, align 8, !tbaa !5
  %26 = getelementptr inbounds i64, ptr %5, i64 %.130
  %27 = load i64, ptr %26, align 8, !tbaa !5
  store i64 %27, ptr %24, align 8, !tbaa !5
  store i64 %25, ptr %26, align 8, !tbaa !5
  %exitcond.not = icmp eq i64 %.130, %9
  br i1 %exitcond.not, label %.preheader, label %.lr.ph28.preheader, !llvm.loop !12

.lr.ph33:                                         ; preds = %.preheader, %.lr.ph33
  %.232 = phi i64 [ %32, %.lr.ph33 ], [ 0, %.preheader ]
  %28 = getelementptr inbounds i64, ptr %5, i64 %.232
  %29 = load i64, ptr %28, align 8, !tbaa !5
  %30 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEx(ptr noundef nonnull align 8 dereferenceable(8) @_ZSt4cout, i64 noundef %29)
  %31 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_c(ptr noundef nonnull align 8 dereferenceable(8) %30, i8 noundef signext 32)
  %32 = add nuw nsw i64 %.232, 1
  %33 = load i64, ptr %1, align 8, !tbaa !5
  %34 = icmp slt i64 %32, %33
  br i1 %34, label %.lr.ph33, label %._crit_edge34, !llvm.loop !13

._crit_edge34:                                    ; preds = %.lr.ph33, %0, %.preheader
  call void @llvm.stackrestore.p0(ptr %4)
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %1) #5
  ret i32 0
}

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

; Function Attrs: mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(16) ptr @_ZNSirsERx(ptr noundef nonnull align 8 dereferenceable(16), ptr noundef nonnull align 8 dereferenceable(8)) local_unnamed_addr #2 align 2

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #3

; Function Attrs: inlinehint mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_c(ptr noundef nonnull align 8 dereferenceable(8), i8 noundef signext) local_unnamed_addr #4

; Function Attrs: mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEx(ptr noundef nonnull align 8 dereferenceable(8), i64 noundef) local_unnamed_addr #2 align 2

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #3

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

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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 = { mustprogress nocallback nofree nosync nounwind willreturn }
attributes #4 = { 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 #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 = !{!"long long", !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}
!13 = distinct !{!13, !10}


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