; LLVM IR from kokobd/leetcode-hs :: csrc/src (p16)

; 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 @p16_threeSumClosest(ptr noundef %0, i32 noundef %1, i32 noundef %2) #0 {
  %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 i8, align 1
  %10 = alloca i64, align 8
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i64, align 8
  %14 = alloca i64, align 8
  %15 = alloca i32, align 4
  %16 = alloca i32, align 4
  %17 = alloca i32, align 4
  %18 = 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
  %19 = load ptr, ptr %4, align 8, !tbaa !5
  %20 = load i32, ptr %5, align 4, !tbaa !9
  %21 = sext i32 %20 to i64
  call void @qsort(ptr noundef %19, i64 noundef %21, i64 noundef 4, ptr noundef @compInt)
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  store i32 2147483647, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  store i32 2147483647, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 1, ptr %9) #4
  store i8 0, ptr %9, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  store i64 0, ptr %10, align 8, !tbaa !13
  br label %22

22:                                               ; preds = %94, %3
  %23 = load i64, ptr %10, align 8, !tbaa !13
  %24 = load i32, ptr %5, align 4, !tbaa !9
  %25 = sub nsw i32 %24, 2
  %26 = sext i32 %25 to i64
  %27 = icmp ult i64 %23, %26
  br i1 %27, label %28, label %32

28:                                               ; preds = %22
  %29 = load i8, ptr %9, align 1, !tbaa !11, !range !15, !noundef !16
  %30 = trunc i8 %29 to i1
  %31 = xor i1 %30, true
  br label %32

32:                                               ; preds = %28, %22
  %33 = phi i1 [ false, %22 ], [ %31, %28 ]
  br i1 %33, label %35, label %34

34:                                               ; preds = %32
  store i32 2, ptr %11, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  br label %97

35:                                               ; preds = %32
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #4
  %36 = load ptr, ptr %4, align 8, !tbaa !5
  %37 = load i64, ptr %10, align 8, !tbaa !13
  %38 = getelementptr inbounds i32, ptr %36, i64 %37
  %39 = load i32, ptr %38, align 4, !tbaa !9
  store i32 %39, ptr %12, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #4
  %40 = load i64, ptr %10, align 8, !tbaa !13
  %41 = add i64 %40, 1
  store i64 %41, ptr %13, align 8, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #4
  %42 = load i32, ptr %5, align 4, !tbaa !9
  %43 = sub nsw i32 %42, 1
  %44 = sext i32 %43 to i64
  store i64 %44, ptr %14, align 8, !tbaa !13
  br label %45

45:                                               ; preds = %92, %35
  %46 = load i64, ptr %13, align 8, !tbaa !13
  %47 = load i64, ptr %14, align 8, !tbaa !13
  %48 = icmp ult i64 %46, %47
  br i1 %48, label %49, label %93

49:                                               ; preds = %45
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #4
  %50 = load ptr, ptr %4, align 8, !tbaa !5
  %51 = load i64, ptr %13, align 8, !tbaa !13
  %52 = getelementptr inbounds i32, ptr %50, i64 %51
  %53 = load i32, ptr %52, align 4, !tbaa !9
  store i32 %53, ptr %15, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #4
  %54 = load ptr, ptr %4, align 8, !tbaa !5
  %55 = load i64, ptr %14, align 8, !tbaa !13
  %56 = getelementptr inbounds i32, ptr %54, i64 %55
  %57 = load i32, ptr %56, align 4, !tbaa !9
  store i32 %57, ptr %16, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %17) #4
  %58 = load i32, ptr %12, align 4, !tbaa !9
  %59 = load i32, ptr %15, align 4, !tbaa !9
  %60 = add nsw i32 %58, %59
  %61 = load i32, ptr %16, align 4, !tbaa !9
  %62 = add nsw i32 %60, %61
  store i32 %62, ptr %17, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %18) #4
  %63 = load i32, ptr %17, align 4, !tbaa !9
  %64 = load i32, ptr %6, align 4, !tbaa !9
  %65 = sub nsw i32 %63, %64
  %66 = call i32 @llvm.abs.i32(i32 %65, i1 true)
  store i32 %66, ptr %18, align 4, !tbaa !9
  %67 = load i32, ptr %18, align 4, !tbaa !9
  %68 = load i32, ptr %8, align 4, !tbaa !9
  %69 = icmp slt i32 %67, %68
  br i1 %69, label %70, label %73

70:                                               ; preds = %49
  %71 = load i32, ptr %18, align 4, !tbaa !9
  store i32 %71, ptr %8, align 4, !tbaa !9
  %72 = load i32, ptr %17, align 4, !tbaa !9
  store i32 %72, ptr %7, align 4, !tbaa !9
  br label %73

73:                                               ; preds = %70, %49
  %74 = load i32, ptr %17, align 4, !tbaa !9
  %75 = load i32, ptr %6, align 4, !tbaa !9
  %76 = icmp slt i32 %74, %75
  br i1 %76, label %77, label %80

77:                                               ; preds = %73
  %78 = load i64, ptr %13, align 8, !tbaa !13
  %79 = add i64 %78, 1
  store i64 %79, ptr %13, align 8, !tbaa !13
  br label %89

80:                                               ; preds = %73
  %81 = load i32, ptr %17, align 4, !tbaa !9
  %82 = load i32, ptr %6, align 4, !tbaa !9
  %83 = icmp sgt i32 %81, %82
  br i1 %83, label %84, label %87

84:                                               ; preds = %80
  %85 = load i64, ptr %14, align 8, !tbaa !13
  %86 = add i64 %85, -1
  store i64 %86, ptr %14, align 8, !tbaa !13
  br label %88

87:                                               ; preds = %80
  store i8 1, ptr %9, align 1, !tbaa !11
  store i32 6, ptr %11, align 4
  br label %90

88:                                               ; preds = %84
  br label %89

89:                                               ; preds = %88, %77
  store i32 0, ptr %11, align 4
  br label %90

90:                                               ; preds = %89, %87
  call void @llvm.lifetime.end.p0(i64 4, ptr %18) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %17) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #4
  %91 = load i32, ptr %11, align 4
  switch i32 %91, label %99 [
    i32 0, label %92
    i32 6, label %93
  ]

92:                                               ; preds = %90
  br label %45, !llvm.loop !17

93:                                               ; preds = %90, %45
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #4
  br label %94

94:                                               ; preds = %93
  %95 = load i64, ptr %10, align 8, !tbaa !13
  %96 = add i64 %95, 1
  store i64 %96, ptr %10, align 8, !tbaa !13
  br label %22, !llvm.loop !19

97:                                               ; preds = %34
  %98 = load i32, ptr %7, align 4, !tbaa !9
  store i32 1, ptr %11, align 4
  call void @llvm.lifetime.end.p0(i64 1, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  ret i32 %98

99:                                               ; preds = %90
  unreachable
}

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

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

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

; 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) #2

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 = { "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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!12, !12, i64 0}
!12 = !{!"_Bool", !7, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"long", !7, i64 0}
!15 = !{i8 0, i8 2}
!16 = !{}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = distinct !{!19, !18}


; 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 @p16_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 @compInt) #5
  %5 = add nsw i32 %1, -2
  %6 = sext i32 %5 to i64
  %7 = icmp eq i32 %5, 0
  br i1 %7, label %._crit_edge, label %.lr.ph72

.lr.ph72:                                         ; preds = %3
  %8 = add nsw i32 %1, -1
  %9 = sext i32 %8 to i64
  br label %10

._crit_edge:                                      ; preds = %.thread, %27, %3
  %.0.lcssa = phi i32 [ 2147483647, %3 ], [ %spec.select52, %27 ], [ %.3, %.thread ]
  ret i32 %.0.lcssa

10:                                               ; preds = %.lr.ph72, %.thread
  %.071 = phi i32 [ 2147483647, %.lr.ph72 ], [ %.3, %.thread ]
  %.03270 = phi i32 [ 2147483647, %.lr.ph72 ], [ %.335, %.thread ]
  %.04068 = phi i64 [ 0, %.lr.ph72 ], [ %13, %.thread ]
  %11 = getelementptr inbounds i32, ptr %0, i64 %.04068
  %12 = load i32, ptr %11, align 4, !tbaa !5
  %13 = add nuw i64 %.04068, 1
  %14 = icmp ult i64 %13, %9
  br i1 %14, label %.lr.ph, label %.thread

.lr.ph:                                           ; preds = %10, %31
  %.161 = phi i32 [ %spec.select52, %31 ], [ %.071, %10 ]
  %.13360 = phi i32 [ %spec.select, %31 ], [ %.03270, %10 ]
  %.04159 = phi i64 [ %.243, %31 ], [ %9, %10 ]
  %.04458 = phi i64 [ %.246, %31 ], [ %13, %10 ]
  %15 = getelementptr inbounds i32, ptr %0, i64 %.04458
  %16 = load i32, ptr %15, align 4, !tbaa !5
  %17 = getelementptr inbounds i32, ptr %0, i64 %.04159
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = add nsw i32 %16, %12
  %20 = add nsw i32 %19, %18
  %21 = sub nsw i32 %20, %2
  %22 = tail call i32 @llvm.abs.i32(i32 %21, i1 true)
  %23 = icmp slt i32 %22, %.13360
  %spec.select = tail call i32 @llvm.smin.i32(i32 %22, i32 %.13360)
  %spec.select52 = select i1 %23, i32 %20, i32 %.161
  %24 = icmp slt i32 %20, %2
  br i1 %24, label %25, label %27

25:                                               ; preds = %.lr.ph
  %26 = add nuw i64 %.04458, 1
  br label %31

27:                                               ; preds = %.lr.ph
  %28 = icmp sgt i32 %20, %2
  br i1 %28, label %29, label %._crit_edge

29:                                               ; preds = %27
  %30 = add i64 %.04159, -1
  br label %31

31:                                               ; preds = %25, %29
  %.246 = phi i64 [ %26, %25 ], [ %.04458, %29 ]
  %.243 = phi i64 [ %.04159, %25 ], [ %30, %29 ]
  %32 = icmp ult i64 %.246, %.243
  br i1 %32, label %.lr.ph, label %.thread

.thread:                                          ; preds = %31, %10
  %.335 = phi i32 [ %.03270, %10 ], [ %spec.select, %31 ]
  %.3 = phi i32 [ %.071, %10 ], [ %spec.select52, %31 ]
  %.not = icmp ult i64 %13, %6
  br i1 %.not, label %10, label %._crit_edge, !llvm.loop !9
}

; 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 @compInt(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"}


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