; LLVM IR from wuzhouhui/leetcode :: 16_three_sum_closest (threeSumClosest)

; 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: nounwind uwtable
define dso_local i32 @threeSumClosest(ptr noundef %0, i32 noundef %1, i32 noundef %2) #0 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %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
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store i32 %2, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #4
  %16 = load ptr, ptr %5, align 8, !tbaa !5
  %17 = load i32, ptr %6, align 4, !tbaa !9
  %18 = sext i32 %17 to i64
  call void @qsort(ptr noundef %16, i64 noundef %18, i64 noundef 4, ptr noundef @cmp)
  %19 = load ptr, ptr %5, align 8, !tbaa !5
  %20 = getelementptr inbounds i32, ptr %19, i64 0
  %21 = load i32, ptr %20, align 4, !tbaa !9
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  %23 = getelementptr inbounds i32, ptr %22, i64 1
  %24 = load i32, ptr %23, align 4, !tbaa !9
  %25 = add nsw i32 %21, %24
  %26 = load ptr, ptr %5, align 8, !tbaa !5
  %27 = getelementptr inbounds i32, ptr %26, i64 2
  %28 = load i32, ptr %27, align 4, !tbaa !9
  %29 = add nsw i32 %25, %28
  store i32 %29, ptr %13, align 4, !tbaa !9
  %30 = load i32, ptr %13, align 4, !tbaa !9
  %31 = load i32, ptr %7, align 4, !tbaa !9
  %32 = sub nsw i32 %30, %31
  %33 = call i32 @llvm.abs.i32(i32 %32, i1 true)
  store i32 %33, ptr %12, align 4, !tbaa !9
  store i32 0, ptr %8, align 4, !tbaa !9
  br label %34

34:                                               ; preds = %94, %3
  %35 = load i32, ptr %8, align 4, !tbaa !9
  %36 = load i32, ptr %6, align 4, !tbaa !9
  %37 = icmp slt i32 %35, %36
  br i1 %37, label %38, label %97

38:                                               ; preds = %34
  %39 = load i32, ptr %8, align 4, !tbaa !9
  %40 = add nsw i32 %39, 1
  store i32 %40, ptr %9, align 4, !tbaa !9
  %41 = load i32, ptr %6, align 4, !tbaa !9
  %42 = sub nsw i32 %41, 1
  store i32 %42, ptr %10, align 4, !tbaa !9
  br label %43

43:                                               ; preds = %92, %38
  %44 = load i32, ptr %9, align 4, !tbaa !9
  %45 = load i32, ptr %10, align 4, !tbaa !9
  %46 = icmp slt i32 %44, %45
  br i1 %46, label %47, label %93

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

68:                                               ; preds = %47
  %69 = load i32, ptr %10, align 4, !tbaa !9
  %70 = add nsw i32 %69, -1
  store i32 %70, ptr %10, align 4, !tbaa !9
  br label %81

71:                                               ; preds = %47
  %72 = load i32, ptr %14, align 4, !tbaa !9
  %73 = load i32, ptr %7, align 4, !tbaa !9
  %74 = icmp slt i32 %72, %73
  br i1 %74, label %75, label %78

75:                                               ; preds = %71
  %76 = load i32, ptr %9, align 4, !tbaa !9
  %77 = add nsw i32 %76, 1
  store i32 %77, ptr %9, align 4, !tbaa !9
  br label %80

78:                                               ; preds = %71
  %79 = load i32, ptr %14, align 4, !tbaa !9
  store i32 %79, ptr %4, align 4
  store i32 1, ptr %15, align 4
  br label %99

80:                                               ; preds = %75
  br label %81

81:                                               ; preds = %80, %68
  %82 = load i32, ptr %14, align 4, !tbaa !9
  %83 = load i32, ptr %7, align 4, !tbaa !9
  %84 = sub nsw i32 %82, %83
  %85 = call i32 @llvm.abs.i32(i32 %84, i1 true)
  store i32 %85, ptr %11, align 4, !tbaa !9
  %86 = load i32, ptr %11, align 4, !tbaa !9
  %87 = load i32, ptr %12, align 4, !tbaa !9
  %88 = icmp slt i32 %86, %87
  br i1 %88, label %89, label %92

89:                                               ; preds = %81
  %90 = load i32, ptr %11, align 4, !tbaa !9
  store i32 %90, ptr %12, align 4, !tbaa !9
  %91 = load i32, ptr %14, align 4, !tbaa !9
  store i32 %91, ptr %13, align 4, !tbaa !9
  br label %92

92:                                               ; preds = %89, %81
  br label %43, !llvm.loop !11

93:                                               ; preds = %43
  br label %94

94:                                               ; preds = %93
  %95 = load i32, ptr %8, align 4, !tbaa !9
  %96 = add nsw i32 %95, 1
  store i32 %96, ptr %8, align 4, !tbaa !9
  br label %34, !llvm.loop !13

97:                                               ; preds = %34
  %98 = load i32, ptr %13, align 4, !tbaa !9
  store i32 %98, ptr %4, align 4
  store i32 1, ptr %15, align 4
  br label %99

99:                                               ; preds = %97, %78
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  %100 = load i32, ptr %4, align 4
  ret i32 %100
}

; 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 @cmp(ptr noundef, ptr noundef) #0

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

; 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 = { 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: nofree nounwind uwtable
define dso_local i32 @threeSumClosest(ptr noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = sext i32 %1 to i64
  tail call void @qsort(ptr noundef %0, i64 noundef %4, i64 noundef 4, ptr noundef nonnull @cmp) #5
  %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 = getelementptr inbounds i8, ptr %0, i64 8
  %10 = load i32, ptr %9, align 4, !tbaa !5
  %11 = add nsw i32 %8, %10
  %12 = icmp sgt i32 %1, 0
  br i1 %12, label %.lr.ph61, label %.loopexit48

.lr.ph61:                                         ; preds = %3
  %13 = sub nsw i32 %11, %2
  %14 = tail call i32 @llvm.abs.i32(i32 %13, i1 true)
  %15 = add nsw i32 %1, -1
  %16 = zext nneg i32 %15 to i64
  %wide.trip.count = zext nneg i32 %1 to i64
  br label %17

.loopexit:                                        ; preds = %38, %17
  %.136.lcssa = phi i32 [ %.03559, %17 ], [ %spec.select, %38 ]
  %.1.lcssa = phi i32 [ %.03460, %17 ], [ %spec.select47, %38 ]
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %.loopexit48, label %17, !llvm.loop !9

17:                                               ; preds = %.lr.ph61, %.loopexit
  %indvars.iv = phi i64 [ 0, %.lr.ph61 ], [ %indvars.iv.next, %.loopexit ]
  %.03460 = phi i32 [ %11, %.lr.ph61 ], [ %.1.lcssa, %.loopexit ]
  %.03559 = phi i32 [ %14, %.lr.ph61 ], [ %.136.lcssa, %.loopexit ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %18 = icmp ult i64 %indvars.iv.next, %16
  br i1 %18, label %.lr.ph, label %.loopexit

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

22:                                               ; preds = %.lr.ph, %38
  %.156 = phi i32 [ %.03460, %.lr.ph ], [ %spec.select47, %38 ]
  %.13655 = phi i32 [ %.03559, %.lr.ph ], [ %spec.select, %38 ]
  %.03954 = phi i32 [ %15, %.lr.ph ], [ %.140, %38 ]
  %.04153 = phi i32 [ %21, %.lr.ph ], [ %.142, %38 ]
  %23 = sext i32 %.04153 to i64
  %24 = getelementptr inbounds i32, ptr %0, i64 %23
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %26 = add nsw i32 %25, %20
  %27 = sext i32 %.03954 to i64
  %28 = getelementptr inbounds i32, ptr %0, i64 %27
  %29 = load i32, ptr %28, align 4, !tbaa !5
  %30 = add nsw i32 %26, %29
  %31 = icmp sgt i32 %30, %2
  br i1 %31, label %32, label %34

32:                                               ; preds = %22
  %33 = add nsw i32 %.03954, -1
  br label %38

34:                                               ; preds = %22
  %35 = icmp slt i32 %30, %2
  br i1 %35, label %36, label %.loopexit48

36:                                               ; preds = %34
  %37 = add nsw i32 %.04153, 1
  br label %38

38:                                               ; preds = %36, %32
  %.142 = phi i32 [ %.04153, %32 ], [ %37, %36 ]
  %.140 = phi i32 [ %33, %32 ], [ %.03954, %36 ]
  %39 = sub nsw i32 %30, %2
  %40 = tail call i32 @llvm.abs.i32(i32 %39, i1 true)
  %41 = icmp slt i32 %40, %.13655
  %spec.select = tail call i32 @llvm.smin.i32(i32 %40, i32 %.13655)
  %spec.select47 = select i1 %41, i32 %30, i32 %.156
  %42 = icmp slt i32 %.142, %.140
  br i1 %42, label %22, label %.loopexit, !llvm.loop !11

.loopexit48:                                      ; preds = %.loopexit, %34, %3
  %.0 = phi i32 [ %11, %3 ], [ %30, %34 ], [ %.1.lcssa, %.loopexit ]
  ret i32 %.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 @cmp(ptr noundef, ptr noundef) #2

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smin.i32(i32, i32) #4

attributes #0 = { 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 #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 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 speculatable willreturn memory(none) }
attributes #4 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = !{!"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: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass