; LLVM IR from wuzhouhui/leetcode :: 18_four_sum (fourSum)

; 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 @fourSum(ptr noundef %0, i32 noundef %1, i32 noundef %2, ptr noundef %3) #0 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %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
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !9
  store i32 %2, ptr %8, align 4, !tbaa !9
  store ptr %3, ptr %9, align 8, !tbaa !5
  %15 = load i32, ptr %7, align 4, !tbaa !9
  %16 = icmp slt i32 %15, 4
  br i1 %16, label %17, label %19

17:                                               ; preds = %4
  %18 = load ptr, ptr %9, align 8, !tbaa !5
  store i32 0, ptr %18, align 4, !tbaa !9
  store ptr null, ptr %5, align 8
  br label %102

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

25:                                               ; preds = %91, %19
  %26 = load i32, ptr %13, align 4, !tbaa !9
  %27 = load i32, ptr %10, align 4, !tbaa !9
  %28 = sub nsw i32 %26, %27
  %29 = icmp sge i32 %28, 3
  br i1 %29, label %30, label %98

30:                                               ; preds = %25
  br label %31

31:                                               ; preds = %86, %30
  %32 = load i32, ptr %13, align 4, !tbaa !9
  %33 = load i32, ptr %11, align 4, !tbaa !9
  %34 = sub nsw i32 %32, %33
  %35 = icmp sge i32 %34, 2
  br i1 %35, label %36, label %91

36:                                               ; preds = %31
  %37 = load i32, ptr %11, align 4, !tbaa !9
  %38 = add nsw i32 %37, 1
  store i32 %38, ptr %12, align 4, !tbaa !9
  br label %39

39:                                               ; preds = %83, %70, %36
  %40 = load i32, ptr %12, align 4, !tbaa !9
  %41 = load i32, ptr %13, align 4, !tbaa !9
  %42 = icmp slt i32 %40, %41
  br i1 %42, label %43, label %86

43:                                               ; preds = %39
  %44 = load ptr, ptr %6, align 8, !tbaa !5
  %45 = load i32, ptr %10, align 4, !tbaa !9
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds i32, ptr %44, i64 %46
  %48 = load i32, ptr %47, align 4, !tbaa !9
  %49 = load ptr, ptr %6, align 8, !tbaa !5
  %50 = load i32, ptr %11, align 4, !tbaa !9
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds i32, ptr %49, i64 %51
  %53 = load i32, ptr %52, align 4, !tbaa !9
  %54 = add nsw i32 %48, %53
  %55 = load ptr, ptr %6, align 8, !tbaa !5
  %56 = load i32, ptr %12, align 4, !tbaa !9
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds i32, ptr %55, i64 %57
  %59 = load i32, ptr %58, align 4, !tbaa !9
  %60 = add nsw i32 %54, %59
  %61 = load ptr, ptr %6, align 8, !tbaa !5
  %62 = load i32, ptr %13, align 4, !tbaa !9
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds i32, ptr %61, i64 %63
  %65 = load i32, ptr %64, align 4, !tbaa !9
  %66 = add nsw i32 %60, %65
  store i32 %66, ptr %14, align 4, !tbaa !9
  %67 = load i32, ptr %14, align 4, !tbaa !9
  %68 = load i32, ptr %8, align 4, !tbaa !9
  %69 = icmp sgt i32 %67, %68
  br i1 %69, label %70, label %73

70:                                               ; preds = %43
  %71 = load i32, ptr %13, align 4, !tbaa !9
  %72 = add nsw i32 %71, -1
  store i32 %72, ptr %13, align 4, !tbaa !9
  br label %39, !llvm.loop !11

73:                                               ; preds = %43
  %74 = load i32, ptr %14, align 4, !tbaa !9
  %75 = load i32, ptr %8, align 4, !tbaa !9
  %76 = icmp eq i32 %74, %75
  br i1 %76, label %77, label %83

77:                                               ; preds = %73
  %78 = load ptr, ptr %6, align 8, !tbaa !5
  %79 = load i32, ptr %10, align 4, !tbaa !9
  %80 = load i32, ptr %11, align 4, !tbaa !9
  %81 = load i32, ptr %12, align 4, !tbaa !9
  %82 = load i32, ptr %13, align 4, !tbaa !9
  call void @add_ans(ptr noundef %78, i32 noundef %79, i32 noundef %80, i32 noundef %81, i32 noundef %82)
  br label %83

83:                                               ; preds = %77, %73
  %84 = load i32, ptr %12, align 4, !tbaa !9
  %85 = add nsw i32 %84, 1
  store i32 %85, ptr %12, align 4, !tbaa !9
  br label %39, !llvm.loop !11

86:                                               ; preds = %39
  %87 = load i32, ptr %11, align 4, !tbaa !9
  %88 = add nsw i32 %87, 1
  store i32 %88, ptr %11, align 4, !tbaa !9
  %89 = load i32, ptr %7, align 4, !tbaa !9
  %90 = sub nsw i32 %89, 1
  store i32 %90, ptr %13, align 4, !tbaa !9
  br label %31, !llvm.loop !13

91:                                               ; preds = %31
  %92 = load i32, ptr %10, align 4, !tbaa !9
  %93 = add nsw i32 %92, 1
  store i32 %93, ptr %10, align 4, !tbaa !9
  %94 = load i32, ptr %10, align 4, !tbaa !9
  %95 = add nsw i32 %94, 1
  store i32 %95, ptr %11, align 4, !tbaa !9
  %96 = load i32, ptr %7, align 4, !tbaa !9
  %97 = sub nsw i32 %96, 1
  store i32 %97, ptr %13, align 4, !tbaa !9
  br label %25, !llvm.loop !14

98:                                               ; preds = %25
  %99 = load i32, ptr @p, align 4, !tbaa !9
  %100 = load ptr, ptr %9, align 8, !tbaa !5
  store i32 %99, ptr %100, align 4, !tbaa !9
  %101 = load ptr, ptr @res, align 8, !tbaa !5
  store ptr %101, ptr %5, align 8
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  br label %102

102:                                              ; preds = %98, %17
  %103 = load ptr, ptr %5, align 8
  ret ptr %103
}

; 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, 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}
!14 = distinct !{!14, !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 @fourSum(ptr noundef %0, i32 noundef %1, i32 noundef %2, ptr nocapture noundef writeonly %3) local_unnamed_addr #0 {
  %5 = icmp slt i32 %1, 4
  br i1 %5, label %6, label %.preheader.preheader

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

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

.loopexit45:                                      ; preds = %.loopexit
  %indvars.iv.next66 = add nuw nsw i64 %indvars.iv65, 1
  %indvars.iv.next59 = add nuw nsw i64 %indvars.iv58, 1
  %indvars.iv.next71 = add nuw nsw i64 %indvars.iv70, 1
  %exitcond76.not = icmp eq i64 %indvars.iv.next71, %wide.trip.count75
  br i1 %exitcond76.not, label %._crit_edge, label %.lr.ph51, !llvm.loop !11

.lr.ph51:                                         ; preds = %.loopexit45, %.preheader.preheader
  %indvars.iv70 = phi i64 [ 0, %.preheader.preheader ], [ %indvars.iv.next71, %.loopexit45 ]
  %indvars.iv65 = phi i64 [ 1, %.preheader.preheader ], [ %indvars.iv.next66, %.loopexit45 ]
  %indvars.iv58 = phi i64 [ 2, %.preheader.preheader ], [ %indvars.iv.next59, %.loopexit45 ]
  %12 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv70
  %13 = trunc nuw nsw i64 %indvars.iv70 to i32
  br label %14

.loopexit:                                        ; preds = %.outer, %30, %14
  %indvars.iv.next61 = add nuw nsw i64 %indvars.iv60, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next68, %wide.trip.count
  br i1 %exitcond.not, label %.loopexit45, label %14, !llvm.loop !13

14:                                               ; preds = %.lr.ph51, %.loopexit
  %indvars.iv67 = phi i64 [ %indvars.iv65, %.lr.ph51 ], [ %indvars.iv.next68, %.loopexit ]
  %indvars.iv60 = phi i64 [ %indvars.iv58, %.lr.ph51 ], [ %indvars.iv.next61, %.loopexit ]
  %indvars.iv.next68 = add nuw nsw i64 %indvars.iv67, 1
  %15 = icmp ult i64 %indvars.iv.next68, %9
  br i1 %15, label %.lr.ph.lr.ph, label %.loopexit

.lr.ph.lr.ph:                                     ; preds = %14
  %16 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv67
  %17 = trunc nuw nsw i64 %indvars.iv67 to i32
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.lr.ph, %.outer
  %indvars.iv62 = phi i64 [ %indvars.iv60, %.lr.ph.lr.ph ], [ %indvars.iv.next63, %.outer ]
  %.2.ph49 = phi i32 [ %8, %.lr.ph.lr.ph ], [ %33, %.outer ]
  %18 = load i32, ptr %12, align 4, !tbaa !5
  %19 = load i32, ptr %16, align 4, !tbaa !5
  %20 = add nsw i32 %19, %18
  %21 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv62
  %22 = load i32, ptr %21, align 4, !tbaa !5
  %23 = add nsw i32 %20, %22
  %24 = sext i32 %.2.ph49 to i64
  br label %25

25:                                               ; preds = %.lr.ph, %30
  %indvars.iv = phi i64 [ %24, %.lr.ph ], [ %indvars.iv.next, %30 ]
  %26 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv
  %27 = load i32, ptr %26, align 4, !tbaa !5
  %28 = add nsw i32 %23, %27
  %29 = icmp sgt i32 %28, %2
  br i1 %29, label %30, label %32

30:                                               ; preds = %25
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %31 = icmp slt i64 %indvars.iv62, %indvars.iv.next
  br i1 %31, label %25, label %.loopexit, !llvm.loop !14

32:                                               ; preds = %25
  %33 = trunc nsw i64 %indvars.iv to i32
  %34 = icmp eq i32 %28, %2
  br i1 %34, label %35, label %.outer

35:                                               ; preds = %32
  %36 = trunc nuw nsw i64 %indvars.iv62 to i32
  tail call void @add_ans(ptr noundef nonnull %0, i32 noundef %13, i32 noundef %17, i32 noundef %36, i32 noundef %33)
  br label %.outer

.outer:                                           ; preds = %35, %32
  %indvars.iv.next63 = add nuw nsw i64 %indvars.iv62, 1
  %sext = shl i64 %indvars.iv, 32
  %37 = ashr exact i64 %sext, 32
  %38 = icmp slt i64 %indvars.iv.next63, %37
  br i1 %38, label %.lr.ph, label %.loopexit, !llvm.loop !14

._crit_edge:                                      ; preds = %.loopexit45
  %39 = load i32, ptr @p, align 4, !tbaa !5
  store i32 %39, ptr %3, align 4, !tbaa !5
  %40 = load ptr, ptr @res, align 8, !tbaa !9
  br label %41

41:                                               ; preds = %._crit_edge, %6
  %.0 = phi ptr [ null, %6 ], [ %40, %._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, 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}
!14 = distinct !{!14, !12}


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