; LLVM IR from ritikbanger/Hacktoberfest2022-DSA :: 04. Arrays (count triplets with sum smaller than x)

; 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 i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca ptr, align 8
  %10 = 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
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  %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) %3)
  %12 = call noundef nonnull align 8 dereferenceable(16) ptr @_ZNSirsERx(ptr noundef nonnull align 8 dereferenceable(16) %11, ptr noundef nonnull align 8 dereferenceable(8) %7)
  %13 = load i64, ptr %3, align 8, !tbaa !5
  %14 = call ptr @llvm.stacksave.p0()
  store ptr %14, ptr %9, align 8
  %15 = alloca i64, i64 %13, align 16
  store i64 %13, ptr %10, align 8
  store i64 0, ptr %2, align 8, !tbaa !5
  br label %16

16:                                               ; preds = %24, %0
  %17 = load i64, ptr %2, align 8, !tbaa !5
  %18 = load i64, ptr %3, align 8, !tbaa !5
  %19 = icmp slt i64 %17, %18
  br i1 %19, label %20, label %27

20:                                               ; preds = %16
  %21 = load i64, ptr %2, align 8, !tbaa !5
  %22 = getelementptr inbounds i64, ptr %15, i64 %21
  %23 = call noundef nonnull align 8 dereferenceable(16) ptr @_ZNSirsERx(ptr noundef nonnull align 8 dereferenceable(16) @_ZSt3cin, ptr noundef nonnull align 8 dereferenceable(8) %22)
  br label %24

24:                                               ; preds = %20
  %25 = load i64, ptr %2, align 8, !tbaa !5
  %26 = add nsw i64 %25, 1
  store i64 %26, ptr %2, align 8, !tbaa !5
  br label %16, !llvm.loop !9

27:                                               ; preds = %16
  %28 = load i64, ptr %3, align 8, !tbaa !5
  %29 = getelementptr inbounds i64, ptr %15, i64 %28
  call void @_ZSt4sortIPxEvT_S1_(ptr noundef %15, ptr noundef %29)
  store i64 0, ptr %2, align 8, !tbaa !5
  br label %30

30:                                               ; preds = %72, %27
  %31 = load i64, ptr %2, align 8, !tbaa !5
  %32 = load i64, ptr %3, align 8, !tbaa !5
  %33 = sub nsw i64 %32, 2
  %34 = icmp slt i64 %31, %33
  br i1 %34, label %35, label %75

35:                                               ; preds = %30
  %36 = load i64, ptr %2, align 8, !tbaa !5
  %37 = add nsw i64 %36, 1
  store i64 %37, ptr %4, align 8, !tbaa !5
  %38 = load i64, ptr %3, align 8, !tbaa !5
  %39 = sub nsw i64 %38, 1
  store i64 %39, ptr %5, align 8, !tbaa !5
  br label %40

40:                                               ; preds = %70, %35
  %41 = load i64, ptr %4, align 8, !tbaa !5
  %42 = load i64, ptr %5, align 8, !tbaa !5
  %43 = icmp slt i64 %41, %42
  br i1 %43, label %44, label %71

44:                                               ; preds = %40
  %45 = load i64, ptr %2, align 8, !tbaa !5
  %46 = getelementptr inbounds i64, ptr %15, i64 %45
  %47 = load i64, ptr %46, align 8, !tbaa !5
  %48 = load i64, ptr %4, align 8, !tbaa !5
  %49 = getelementptr inbounds i64, ptr %15, i64 %48
  %50 = load i64, ptr %49, align 8, !tbaa !5
  %51 = add nsw i64 %47, %50
  %52 = load i64, ptr %5, align 8, !tbaa !5
  %53 = getelementptr inbounds i64, ptr %15, i64 %52
  %54 = load i64, ptr %53, align 8, !tbaa !5
  %55 = add nsw i64 %51, %54
  store i64 %55, ptr %6, align 8, !tbaa !5
  %56 = load i64, ptr %6, align 8, !tbaa !5
  %57 = load i64, ptr %7, align 8, !tbaa !5
  %58 = icmp sge i64 %56, %57
  br i1 %58, label %59, label %62

59:                                               ; preds = %44
  %60 = load i64, ptr %5, align 8, !tbaa !5
  %61 = add nsw i64 %60, -1
  store i64 %61, ptr %5, align 8, !tbaa !5
  br label %70

62:                                               ; preds = %44
  %63 = load i64, ptr %5, align 8, !tbaa !5
  %64 = load i64, ptr %4, align 8, !tbaa !5
  %65 = sub nsw i64 %63, %64
  %66 = load i64, ptr %8, align 8, !tbaa !5
  %67 = add nsw i64 %66, %65
  store i64 %67, ptr %8, align 8, !tbaa !5
  %68 = load i64, ptr %4, align 8, !tbaa !5
  %69 = add nsw i64 %68, 1
  store i64 %69, ptr %4, align 8, !tbaa !5
  br label %70

70:                                               ; preds = %62, %59
  br label %40, !llvm.loop !11

71:                                               ; preds = %40
  br label %72

72:                                               ; preds = %71
  %73 = load i64, ptr %2, align 8, !tbaa !5
  %74 = add nsw i64 %73, 1
  store i64 %74, ptr %2, align 8, !tbaa !5
  br label %30, !llvm.loop !12

75:                                               ; preds = %30
  %76 = load i64, ptr %8, align 8, !tbaa !5
  %77 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEx(ptr noundef nonnull align 8 dereferenceable(8) @_ZSt4cout, i64 noundef %76)
  store i32 0, ptr %1, align 4
  %78 = load ptr, ptr %9, align 8
  call void @llvm.stackrestore.p0(ptr %78)
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #5
  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
  %79 = load i32, ptr %1, align 4
  ret i32 %79
}

; 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 dso_local void @_ZSt4sortIPxEvT_S1_(ptr noundef, ptr noundef) #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}


; 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
  %2 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %1) #6
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2) #6
  %3 = 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)
  %4 = call noundef nonnull align 8 dereferenceable(16) ptr @_ZNSirsERx(ptr noundef nonnull align 8 dereferenceable(16) %3, ptr noundef nonnull align 8 dereferenceable(8) %2)
  %5 = load i64, ptr %1, align 8, !tbaa !5
  %6 = call ptr @llvm.stacksave.p0()
  %7 = alloca i64, i64 %5, align 16
  %8 = load i64, ptr %1, align 8, !tbaa !5
  %9 = icmp sgt i64 %8, 0
  br i1 %9, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %0, %.lr.ph
  %.026 = phi i64 [ %12, %.lr.ph ], [ 0, %0 ]
  %10 = getelementptr inbounds i64, ptr %7, i64 %.026
  %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 %.026, 1
  %13 = load i64, ptr %1, align 8, !tbaa !5
  %14 = icmp slt i64 %12, %13
  br i1 %14, label %.lr.ph, label %._crit_edge, !llvm.loop !9

._crit_edge:                                      ; preds = %.lr.ph, %0
  %.lcssa = phi i64 [ %8, %0 ], [ %13, %.lr.ph ]
  %15 = getelementptr inbounds i64, ptr %7, i64 %.lcssa
  call void @_ZSt4sortIPxEvT_S1_(ptr noundef nonnull %7, ptr noundef nonnull %15)
  %16 = load i64, ptr %1, align 8, !tbaa !5
  %17 = icmp sgt i64 %16, 2
  call void @llvm.assume(i1 %17)
  %18 = add nsw i64 %16, -1
  %19 = load i64, ptr %2, align 8
  %20 = add nsw i64 %16, -3
  br label %.lr.ph31

.loopexit:                                        ; preds = %24
  %exitcond.not = icmp eq i64 %.134, %20
  br i1 %exitcond.not, label %._crit_edge37, label %.lr.ph31, !llvm.loop !11

.lr.ph31:                                         ; preds = %.loopexit, %._crit_edge
  %.134 = phi i64 [ 0, %._crit_edge ], [ %21, %.loopexit ]
  %.01933 = phi i64 [ undef, %._crit_edge ], [ %.2, %.loopexit ]
  %21 = add nuw nsw i64 %.134, 1
  %22 = getelementptr inbounds i64, ptr %7, i64 %.134
  %23 = load i64, ptr %22, align 8, !tbaa !5
  br label %24

24:                                               ; preds = %.lr.ph31, %24
  %.12029 = phi i64 [ %.01933, %.lr.ph31 ], [ %.2, %24 ]
  %.02128 = phi i64 [ %21, %.lr.ph31 ], [ %.122, %24 ]
  %.02327 = phi i64 [ %18, %.lr.ph31 ], [ %.124, %24 ]
  %25 = getelementptr inbounds i64, ptr %7, i64 %.02128
  %26 = load i64, ptr %25, align 8, !tbaa !5
  %27 = add nsw i64 %26, %23
  %28 = getelementptr inbounds i64, ptr %7, i64 %.02327
  %29 = load i64, ptr %28, align 8, !tbaa !5
  %30 = add nsw i64 %27, %29
  %.not = icmp sge i64 %30, %19
  %31 = sub nsw i64 %.02327, %.02128
  %32 = sext i1 %.not to i64
  %.124 = add nsw i64 %.02327, %32
  %not..not = xor i1 %.not, true
  %33 = zext i1 %not..not to i64
  %.122 = add nuw nsw i64 %.02128, %33
  %34 = select i1 %.not, i64 0, i64 %31
  %.2 = add nsw i64 %34, %.12029
  %35 = icmp slt i64 %.122, %.124
  br i1 %35, label %24, label %.loopexit, !llvm.loop !12

._crit_edge37:                                    ; preds = %.loopexit
  %36 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEx(ptr noundef nonnull align 8 dereferenceable(8) @_ZSt4cout, i64 noundef %.2)
  call void @llvm.stackrestore.p0(ptr %6)
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %1) #6
  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 dso_local void @_ZSt4sortIPxEvT_S1_(ptr noundef, ptr noundef) 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

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

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 = { 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 = !{!"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}


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