; LLVM IR from 6leetcode/6leetcode :: questions/Algorithms/0016. 3Sum Closest (Testing)

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

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

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @cmpfunc(ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @threeSumClosest(ptr noundef %0, i32 noundef %1, i32 noundef %2) #1 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  store i32 %2, ptr %6, align 4, !tbaa !9
  %12 = load ptr, ptr %4, align 8, !tbaa !5
  %13 = load i32, ptr %5, align 4, !tbaa !9
  %14 = sext i32 %13 to i64
  call void @qsort(ptr noundef %12, i64 noundef %14, i64 noundef 4, ptr noundef @cmpfunc)
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  %16 = getelementptr inbounds i32, ptr %15, i64 0
  %17 = load i32, ptr %16, align 4, !tbaa !9
  %18 = load ptr, ptr %4, align 8, !tbaa !5
  %19 = getelementptr inbounds i32, ptr %18, i64 1
  %20 = load i32, ptr %19, align 4, !tbaa !9
  %21 = add nsw i32 %17, %20
  %22 = load ptr, ptr %4, align 8, !tbaa !5
  %23 = load i32, ptr %5, align 4, !tbaa !9
  %24 = sub nsw i32 %23, 1
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds i32, ptr %22, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !9
  %28 = add nsw i32 %21, %27
  store i32 %28, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  store i32 0, ptr %8, align 4, !tbaa !9
  br label %29

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

33:                                               ; preds = %29
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  br label %87

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

39:                                               ; preds = %82, %34
  %40 = load i32, ptr %9, align 4, !tbaa !9
  %41 = load i32, ptr %10, align 4, !tbaa !9
  %42 = icmp slt i32 %40, %41
  br i1 %42, label %43, label %83

43:                                               ; preds = %39
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #4
  %44 = load ptr, ptr %4, align 8, !tbaa !5
  %45 = load i32, ptr %8, 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 %4, align 8, !tbaa !5
  %50 = load i32, ptr %9, 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 %4, align 8, !tbaa !5
  %56 = load i32, ptr %10, 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
  store i32 %60, ptr %11, align 4, !tbaa !9
  %61 = load i32, ptr %6, align 4, !tbaa !9
  %62 = load i32, ptr %7, align 4, !tbaa !9
  %63 = sub nsw i32 %61, %62
  %64 = call i32 @llvm.abs.i32(i32 %63, i1 true)
  %65 = load i32, ptr %6, align 4, !tbaa !9
  %66 = load i32, ptr %11, align 4, !tbaa !9
  %67 = sub nsw i32 %65, %66
  %68 = call i32 @llvm.abs.i32(i32 %67, i1 true)
  %69 = icmp sgt i32 %64, %68
  br i1 %69, label %70, label %72

70:                                               ; preds = %43
  %71 = load i32, ptr %11, align 4, !tbaa !9
  store i32 %71, ptr %7, align 4, !tbaa !9
  br label %72

72:                                               ; preds = %70, %43
  %73 = load i32, ptr %6, align 4, !tbaa !9
  %74 = load i32, ptr %11, align 4, !tbaa !9
  %75 = icmp sgt i32 %73, %74
  br i1 %75, label %76, label %79

76:                                               ; preds = %72
  %77 = load i32, ptr %9, align 4, !tbaa !9
  %78 = add nsw i32 %77, 1
  store i32 %78, ptr %9, align 4, !tbaa !9
  br label %82

79:                                               ; preds = %72
  %80 = load i32, ptr %10, align 4, !tbaa !9
  %81 = add nsw i32 %80, -1
  store i32 %81, ptr %10, align 4, !tbaa !9
  br label %82

82:                                               ; preds = %79, %76
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #4
  br label %39, !llvm.loop !11

83:                                               ; preds = %39
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  br label %84

84:                                               ; preds = %83
  %85 = load i32, ptr %8, align 4, !tbaa !9
  %86 = add nsw i32 %85, 1
  store i32 %86, ptr %8, align 4, !tbaa !9
  br label %29, !llvm.loop !13

87:                                               ; preds = %33
  %88 = load i32, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  ret i32 %88
}

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

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

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

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

; Function Attrs: nofree nounwind uwtable
define dso_local i32 @threeSumClosest(ptr noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #1 {
  %4 = sext i32 %1 to i64
  tail call void @qsort(ptr noundef %0, i64 noundef %4, i64 noundef 4, ptr noundef nonnull @cmpfunc) #4
  %5 = load i32, ptr %0, align 4, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %0, i64 4
  %7 = load i32, ptr %6, align 4, !tbaa !5
  %8 = add nsw i32 %7, %5
  %9 = add nsw i32 %1, -1
  %10 = sext i32 %9 to i64
  %11 = getelementptr inbounds i32, ptr %0, i64 %10
  %12 = load i32, ptr %11, align 4, !tbaa !5
  %13 = add nsw i32 %8, %12
  %14 = icmp sgt i32 %1, 0
  br i1 %14, label %.lr.ph38.preheader, label %._crit_edge

.lr.ph38.preheader:                               ; preds = %3
  %wide.trip.count = zext nneg i32 %1 to i64
  br label %.lr.ph38

.loopexit:                                        ; preds = %19, %.lr.ph38
  %.1.lcssa = phi i32 [ %.037, %.lr.ph38 ], [ %spec.select, %19 ]
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph38, !llvm.loop !9

._crit_edge:                                      ; preds = %.loopexit, %3
  %.0.lcssa = phi i32 [ %13, %3 ], [ %.1.lcssa, %.loopexit ]
  ret i32 %.0.lcssa

.lr.ph38:                                         ; preds = %.lr.ph38.preheader, %.loopexit
  %indvars.iv = phi i64 [ 0, %.lr.ph38.preheader ], [ %indvars.iv.next, %.loopexit ]
  %.037 = phi i32 [ %13, %.lr.ph38.preheader ], [ %.1.lcssa, %.loopexit ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %15 = icmp slt i64 %indvars.iv.next, %10
  br i1 %15, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %.lr.ph38
  %16 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv
  %17 = load i32, ptr %16, align 4, !tbaa !5
  %18 = trunc nuw nsw i64 %indvars.iv.next to i32
  br label %19

19:                                               ; preds = %.lr.ph, %19
  %.135 = phi i32 [ %.037, %.lr.ph ], [ %spec.select, %19 ]
  %.02834 = phi i32 [ %9, %.lr.ph ], [ %.129, %19 ]
  %.03033 = phi i32 [ %18, %.lr.ph ], [ %.131, %19 ]
  %20 = zext nneg i32 %.03033 to i64
  %21 = getelementptr inbounds i32, ptr %0, i64 %20
  %22 = load i32, ptr %21, align 4, !tbaa !5
  %23 = add nsw i32 %22, %17
  %24 = sext i32 %.02834 to i64
  %25 = getelementptr inbounds i32, ptr %0, i64 %24
  %26 = load i32, ptr %25, align 4, !tbaa !5
  %27 = add nsw i32 %23, %26
  %28 = sub nsw i32 %2, %.135
  %29 = tail call i32 @llvm.abs.i32(i32 %28, i1 true)
  %30 = sub nsw i32 %2, %27
  %31 = tail call i32 @llvm.abs.i32(i32 %30, i1 true)
  %32 = icmp ugt i32 %29, %31
  %spec.select = select i1 %32, i32 %27, i32 %.135
  %33 = icmp slt i32 %27, %2
  %34 = zext i1 %33 to i32
  %.131 = add nuw nsw i32 %.03033, %34
  %not. = xor i1 %33, true
  %35 = sext i1 %not. to i32
  %.129 = add nsw i32 %.02834, %35
  %36 = icmp slt i32 %.131, %.129
  br i1 %36, label %19, label %.loopexit, !llvm.loop !11
}

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

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

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 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 #2 = { 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 #3 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { 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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


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