; LLVM IR from wuzhouhui/leetcode :: 15_three_sum (threeSum)

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

@res = external hidden global ptr, align 8
@p = external hidden global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local ptr @threeSum(ptr noundef %0, i32 noundef %1, ptr noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !5
  %12 = load i32, ptr %6, align 4, !tbaa !9
  %13 = icmp slt i32 %12, 3
  br i1 %13, label %14, label %16

14:                                               ; preds = %3
  %15 = load ptr, ptr %7, align 8, !tbaa !5
  store i32 0, ptr %15, align 4, !tbaa !9
  store ptr null, ptr %4, align 8
  br label %81

16:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  store ptr null, ptr @res, align 8, !tbaa !5
  store i32 0, ptr @p, align 4, !tbaa !9
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  %18 = load i32, ptr %6, align 4, !tbaa !9
  %19 = sext i32 %18 to i64
  call void @qsort(ptr noundef %17, i64 noundef %19, i64 noundef 4, ptr noundef @intcmp)
  store i32 0, ptr %8, align 4, !tbaa !9
  %20 = load i32, ptr %6, align 4, !tbaa !9
  %21 = sub nsw i32 %20, 1
  store i32 %21, ptr %9, align 4, !tbaa !9
  br label %22

22:                                               ; preds = %72, %16
  %23 = load i32, ptr %9, align 4, !tbaa !9
  %24 = load i32, ptr %8, align 4, !tbaa !9
  %25 = sub nsw i32 %23, %24
  %26 = icmp sge i32 %25, 2
  br i1 %26, label %27, label %77

27:                                               ; preds = %22
  %28 = load i32, ptr %8, align 4, !tbaa !9
  %29 = add nsw i32 %28, 1
  store i32 %29, ptr %10, align 4, !tbaa !9
  br label %30

30:                                               ; preds = %71, %27
  %31 = load i32, ptr %10, align 4, !tbaa !9
  %32 = load i32, ptr %9, align 4, !tbaa !9
  %33 = icmp slt i32 %31, %32
  br i1 %33, label %34, label %72

34:                                               ; preds = %30
  %35 = load ptr, ptr %5, align 8, !tbaa !5
  %36 = load i32, ptr %8, align 4, !tbaa !9
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds i32, ptr %35, i64 %37
  %39 = load i32, ptr %38, align 4, !tbaa !9
  %40 = load ptr, ptr %5, align 8, !tbaa !5
  %41 = load i32, ptr %10, align 4, !tbaa !9
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds i32, ptr %40, i64 %42
  %44 = load i32, ptr %43, align 4, !tbaa !9
  %45 = add nsw i32 %39, %44
  %46 = load ptr, ptr %5, align 8, !tbaa !5
  %47 = load i32, ptr %9, align 4, !tbaa !9
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds i32, ptr %46, i64 %48
  %50 = load i32, ptr %49, align 4, !tbaa !9
  %51 = add nsw i32 %45, %50
  store i32 %51, ptr %11, align 4, !tbaa !9
  %52 = load i32, ptr %11, align 4, !tbaa !9
  %53 = icmp sgt i32 %52, 0
  br i1 %53, label %54, label %57

54:                                               ; preds = %34
  %55 = load i32, ptr %9, align 4, !tbaa !9
  %56 = add nsw i32 %55, -1
  store i32 %56, ptr %9, align 4, !tbaa !9
  br label %71

57:                                               ; preds = %34
  %58 = load i32, ptr %11, align 4, !tbaa !9
  %59 = icmp slt i32 %58, 0
  br i1 %59, label %60, label %63

60:                                               ; preds = %57
  %61 = load i32, ptr %10, align 4, !tbaa !9
  %62 = add nsw i32 %61, 1
  store i32 %62, ptr %10, align 4, !tbaa !9
  br label %70

63:                                               ; preds = %57
  %64 = load ptr, ptr %5, align 8, !tbaa !5
  %65 = load i32, ptr %8, align 4, !tbaa !9
  %66 = load i32, ptr %10, align 4, !tbaa !9
  %67 = load i32, ptr %9, align 4, !tbaa !9
  call void @add_ans(ptr noundef %64, i32 noundef %65, i32 noundef %66, i32 noundef %67)
  %68 = load i32, ptr %10, align 4, !tbaa !9
  %69 = add nsw i32 %68, 1
  store i32 %69, ptr %10, align 4, !tbaa !9
  br label %70

70:                                               ; preds = %63, %60
  br label %71

71:                                               ; preds = %70, %54
  br label %30, !llvm.loop !11

72:                                               ; preds = %30
  %73 = load i32, ptr %8, align 4, !tbaa !9
  %74 = add nsw i32 %73, 1
  store i32 %74, ptr %8, align 4, !tbaa !9
  %75 = load i32, ptr %6, align 4, !tbaa !9
  %76 = sub nsw i32 %75, 1
  store i32 %76, ptr %9, align 4, !tbaa !9
  br label %22, !llvm.loop !13

77:                                               ; preds = %22
  %78 = load i32, ptr @p, align 4, !tbaa !9
  %79 = load ptr, ptr %7, align 8, !tbaa !5
  store i32 %78, ptr %79, align 4, !tbaa !9
  %80 = load ptr, ptr @res, align 8, !tbaa !5
  store ptr %80, ptr %4, align 8
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  br label %81

81:                                               ; preds = %77, %14
  %82 = load ptr, ptr %4, align 8
  ret ptr %82
}

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

declare void @qsort(ptr noundef, i64 noundef, i64 noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
declare hidden i32 @intcmp(ptr noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
declare hidden void @add_ans(ptr noundef, i32 noundef, i32 noundef, i32 noundef) #0

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

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { "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 = { 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/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"

@res = external hidden local_unnamed_addr global ptr, align 8
@p = external hidden local_unnamed_addr global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local ptr @threeSum(ptr noundef %0, i32 noundef %1, ptr nocapture noundef writeonly %2) local_unnamed_addr #0 {
  %4 = icmp slt i32 %1, 3
  br i1 %4, label %5, label %.lr.ph37.preheader

5:                                                ; preds = %3
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %37

.lr.ph37.preheader:                               ; preds = %3
  store ptr null, ptr @res, align 8, !tbaa !9
  store i32 0, ptr @p, align 4, !tbaa !5
  %6 = zext nneg i32 %1 to i64
  tail call void @qsort(ptr noundef %0, i64 noundef %6, i64 noundef 4, ptr noundef nonnull @intcmp) #2
  %7 = add nsw i32 %1, -1
  %8 = zext nneg i32 %7 to i64
  %9 = add nsw i32 %1, -2
  %wide.trip.count = zext nneg i32 %9 to i64
  br label %.lr.ph37

.loopexit:                                        ; preds = %33, %.lr.ph37
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph37, !llvm.loop !11

.lr.ph37:                                         ; preds = %.lr.ph37.preheader, %.loopexit
  %indvars.iv = phi i64 [ 0, %.lr.ph37.preheader ], [ %indvars.iv.next, %.loopexit ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %10 = icmp ult i64 %indvars.iv.next, %8
  br i1 %10, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %.lr.ph37
  %11 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv
  %12 = trunc nuw nsw i64 %indvars.iv.next to i32
  %13 = trunc nuw nsw i64 %indvars.iv to i32
  br label %14

14:                                               ; preds = %.lr.ph, %33
  %.02835 = phi i32 [ %12, %.lr.ph ], [ %.1, %33 ]
  %.13034 = phi i32 [ %7, %.lr.ph ], [ %.2, %33 ]
  %15 = load i32, ptr %11, align 4, !tbaa !5
  %16 = sext i32 %.02835 to i64
  %17 = getelementptr inbounds i32, ptr %0, i64 %16
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = add nsw i32 %18, %15
  %20 = sext i32 %.13034 to i64
  %21 = getelementptr inbounds i32, ptr %0, i64 %20
  %22 = load i32, ptr %21, align 4, !tbaa !5
  %23 = add nsw i32 %19, %22
  %24 = icmp sgt i32 %23, 0
  br i1 %24, label %25, label %27

25:                                               ; preds = %14
  %26 = add nsw i32 %.13034, -1
  br label %33

27:                                               ; preds = %14
  %28 = icmp slt i32 %23, 0
  br i1 %28, label %29, label %31

29:                                               ; preds = %27
  %30 = add nsw i32 %.02835, 1
  br label %33

31:                                               ; preds = %27
  tail call void @add_ans(ptr noundef nonnull %0, i32 noundef %13, i32 noundef %.02835, i32 noundef %.13034)
  %32 = add nsw i32 %.02835, 1
  br label %33

33:                                               ; preds = %29, %31, %25
  %.2 = phi i32 [ %26, %25 ], [ %.13034, %29 ], [ %.13034, %31 ]
  %.1 = phi i32 [ %.02835, %25 ], [ %30, %29 ], [ %32, %31 ]
  %34 = icmp slt i32 %.1, %.2
  br i1 %34, label %14, label %.loopexit, !llvm.loop !13

._crit_edge:                                      ; preds = %.loopexit
  %35 = load i32, ptr @p, align 4, !tbaa !5
  store i32 %35, ptr %2, align 4, !tbaa !5
  %36 = load ptr, ptr @res, align 8, !tbaa !9
  br label %37

37:                                               ; preds = %._crit_edge, %5
  %.0 = phi ptr [ null, %5 ], [ %36, %._crit_edge ]
  ret ptr %.0
}

; Function Attrs: nofree
declare void @qsort(ptr noundef, i64 noundef, i64 noundef, ptr nocapture noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden i32 @intcmp(ptr noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
declare hidden void @add_ans(ptr noundef, i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

attributes #0 = { 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 #1 = { nofree "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 = { 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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


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