; LLVM IR from LeetCode-in-C/LeetCode-in-C :: src/main/c/g0101_0200/s0128_longest_consecutive_sequence (Solution)

; 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
declare dso_local i32 @compare(ptr noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
define dso_local i32 @longestConsecutive(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca i32, align 4
  %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
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  %9 = load i32, ptr %5, align 4, !tbaa !9
  %10 = icmp eq i32 %9, 0
  br i1 %10, label %11, label %12

11:                                               ; preds = %2
  store i32 0, ptr %3, align 4
  br label %72

12:                                               ; preds = %2
  %13 = load ptr, ptr %4, align 8, !tbaa !5
  %14 = load i32, ptr %5, align 4, !tbaa !9
  %15 = sext i32 %14 to i64
  call void @qsort(ptr noundef %13, i64 noundef %15, i64 noundef 4, ptr noundef @compare)
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 -2147483648, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 1, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  store i32 0, ptr %8, align 4, !tbaa !9
  br label %16

16:                                               ; preds = %61, %12
  %17 = load i32, ptr %8, align 4, !tbaa !9
  %18 = load i32, ptr %5, align 4, !tbaa !9
  %19 = sub nsw i32 %18, 1
  %20 = icmp slt i32 %17, %19
  br i1 %20, label %22, label %21

21:                                               ; preds = %16
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  br label %64

22:                                               ; preds = %16
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  %24 = load i32, ptr %8, align 4, !tbaa !9
  %25 = add nsw i32 %24, 1
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds i32, ptr %23, i64 %26
  %28 = load i32, ptr %27, align 4, !tbaa !9
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  %30 = load i32, ptr %8, align 4, !tbaa !9
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds i32, ptr %29, i64 %31
  %33 = load i32, ptr %32, align 4, !tbaa !9
  %34 = add nsw i32 %33, 1
  %35 = icmp eq i32 %28, %34
  br i1 %35, label %36, label %39

36:                                               ; preds = %22
  %37 = load i32, ptr %7, align 4, !tbaa !9
  %38 = add nsw i32 %37, 1
  store i32 %38, ptr %7, align 4, !tbaa !9
  br label %60

39:                                               ; preds = %22
  %40 = load ptr, ptr %4, align 8, !tbaa !5
  %41 = load i32, ptr %8, align 4, !tbaa !9
  %42 = add nsw i32 %41, 1
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds i32, ptr %40, i64 %43
  %45 = load i32, ptr %44, align 4, !tbaa !9
  %46 = load ptr, ptr %4, align 8, !tbaa !5
  %47 = load i32, ptr %8, 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 = icmp ne i32 %45, %50
  br i1 %51, label %52, label %59

52:                                               ; preds = %39
  %53 = load i32, ptr %7, align 4, !tbaa !9
  %54 = load i32, ptr %6, align 4, !tbaa !9
  %55 = icmp sgt i32 %53, %54
  br i1 %55, label %56, label %58

56:                                               ; preds = %52
  %57 = load i32, ptr %7, align 4, !tbaa !9
  store i32 %57, ptr %6, align 4, !tbaa !9
  br label %58

58:                                               ; preds = %56, %52
  store i32 1, ptr %7, align 4, !tbaa !9
  br label %59

59:                                               ; preds = %58, %39
  br label %60

60:                                               ; preds = %59, %36
  br label %61

61:                                               ; preds = %60
  %62 = load i32, ptr %8, align 4, !tbaa !9
  %63 = add nsw i32 %62, 1
  store i32 %63, ptr %8, align 4, !tbaa !9
  br label %16, !llvm.loop !11

64:                                               ; preds = %21
  %65 = load i32, ptr %7, align 4, !tbaa !9
  %66 = load i32, ptr %6, align 4, !tbaa !9
  %67 = icmp sgt i32 %65, %66
  br i1 %67, label %68, label %70

68:                                               ; preds = %64
  %69 = load i32, ptr %7, align 4, !tbaa !9
  store i32 %69, ptr %6, align 4, !tbaa !9
  br label %70

70:                                               ; preds = %68, %64
  %71 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %71, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  br label %72

72:                                               ; preds = %70, %11
  %73 = load i32, ptr %3, align 4
  ret i32 %73
}

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

; 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 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 = { 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"}


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

; Function Attrs: nofree nounwind uwtable
define dso_local i32 @longestConsecutive(ptr noundef %0, i32 noundef %1) local_unnamed_addr #1 {
  %3 = icmp eq i32 %1, 0
  br i1 %3, label %36, label %4

4:                                                ; preds = %2
  %5 = sext i32 %1 to i64
  tail call void @qsort(ptr noundef %0, i64 noundef %5, i64 noundef 4, ptr noundef nonnull @compare) #4
  %6 = icmp sgt i32 %1, 1
  br i1 %6, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %4
  %7 = add nsw i32 %1, -1
  %wide.trip.count = zext i32 %7 to i64
  %.pre = load i32, ptr %0, align 4, !tbaa !5
  %xtraiter = and i64 %wide.trip.count, 1
  %8 = icmp eq i32 %7, 1
  br i1 %8, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %wide.trip.count, 4294967294
  br label %.lr.ph

._crit_edge.loopexit.unr-lcssa.loopexit:          ; preds = %35
  %9 = add nuw nsw i64 %indvars.iv, 3
  br label %._crit_edge.loopexit.unr-lcssa

._crit_edge.loopexit.unr-lcssa:                   ; preds = %._crit_edge.loopexit.unr-lcssa.loopexit, %.lr.ph.preheader
  %.2.lcssa.ph = phi i32 [ poison, %.lr.ph.preheader ], [ %.2.1, %._crit_edge.loopexit.unr-lcssa.loopexit ]
  %.1.lcssa.ph = phi i32 [ poison, %.lr.ph.preheader ], [ %.1.1, %._crit_edge.loopexit.unr-lcssa.loopexit ]
  %.unr = phi i32 [ %.pre, %.lr.ph.preheader ], [ %28, %._crit_edge.loopexit.unr-lcssa.loopexit ]
  %indvars.iv.unr = phi i64 [ 1, %.lr.ph.preheader ], [ %9, %._crit_edge.loopexit.unr-lcssa.loopexit ]
  %.02230.unr = phi i32 [ 1, %.lr.ph.preheader ], [ %.1.1, %._crit_edge.loopexit.unr-lcssa.loopexit ]
  %.02329.unr = phi i32 [ -2147483648, %.lr.ph.preheader ], [ %.2.1, %._crit_edge.loopexit.unr-lcssa.loopexit ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa
  %10 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.unr
  %11 = load i32, ptr %10, align 4, !tbaa !5
  %12 = add nsw i32 %.unr, 1
  %13 = icmp eq i32 %11, %12
  br i1 %13, label %16, label %14

14:                                               ; preds = %.lr.ph.epil
  %.not.epil = icmp eq i32 %11, %.unr
  br i1 %.not.epil, label %._crit_edge, label %15

15:                                               ; preds = %14
  %spec.select28.epil = tail call i32 @llvm.smax.i32(i32 %.02230.unr, i32 %.02329.unr)
  br label %._crit_edge

16:                                               ; preds = %.lr.ph.epil
  %17 = add nsw i32 %.02230.unr, 1
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %16, %15, %14, %4
  %.023.lcssa = phi i32 [ -2147483648, %4 ], [ %.2.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %.02329.unr, %16 ], [ %spec.select28.epil, %15 ], [ %.02329.unr, %14 ]
  %.022.lcssa = phi i32 [ 1, %4 ], [ %.1.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %17, %16 ], [ 1, %15 ], [ %.02230.unr, %14 ]
  %spec.select = tail call i32 @llvm.smax.i32(i32 %.022.lcssa, i32 %.023.lcssa)
  br label %36

.lr.ph:                                           ; preds = %35, %.lr.ph.preheader.new
  %18 = phi i32 [ %.pre, %.lr.ph.preheader.new ], [ %28, %35 ]
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader.new ], [ %indvars.iv.next.1, %35 ]
  %.02230 = phi i32 [ 1, %.lr.ph.preheader.new ], [ %.1.1, %35 ]
  %.02329 = phi i32 [ -2147483648, %.lr.ph.preheader.new ], [ %.2.1, %35 ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %35 ]
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %19 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.next
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = add nsw i32 %18, 1
  %22 = icmp eq i32 %20, %21
  br i1 %22, label %23, label %25

23:                                               ; preds = %.lr.ph
  %24 = add nsw i32 %.02230, 1
  br label %.lr.ph.1

25:                                               ; preds = %.lr.ph
  %.not = icmp eq i32 %20, %18
  br i1 %.not, label %.lr.ph.1, label %26

26:                                               ; preds = %25
  %spec.select28 = tail call i32 @llvm.smax.i32(i32 %.02230, i32 %.02329)
  br label %.lr.ph.1

.lr.ph.1:                                         ; preds = %23, %26, %25
  %.2 = phi i32 [ %.02329, %23 ], [ %spec.select28, %26 ], [ %.02329, %25 ]
  %.1 = phi i32 [ %24, %23 ], [ 1, %26 ], [ %.02230, %25 ]
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %27 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.next.1
  %28 = load i32, ptr %27, align 4, !tbaa !5
  %29 = add nsw i32 %20, 1
  %30 = icmp eq i32 %28, %29
  br i1 %30, label %33, label %31

31:                                               ; preds = %.lr.ph.1
  %.not.1 = icmp eq i32 %28, %20
  br i1 %.not.1, label %35, label %32

32:                                               ; preds = %31
  %spec.select28.1 = tail call i32 @llvm.smax.i32(i32 %.1, i32 %.2)
  br label %35

33:                                               ; preds = %.lr.ph.1
  %34 = add nsw i32 %.1, 1
  br label %35

35:                                               ; preds = %33, %32, %31
  %.2.1 = phi i32 [ %.2, %33 ], [ %spec.select28.1, %32 ], [ %.2, %31 ]
  %.1.1 = phi i32 [ %34, %33 ], [ 1, %32 ], [ %.1, %31 ]
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.loopexit.unr-lcssa.loopexit, label %.lr.ph, !llvm.loop !9

36:                                               ; preds = %2, %._crit_edge
  %.025 = phi i32 [ %spec.select, %._crit_edge ], [ 0, %2 ]
  ret i32 %.025
}

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #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 = { 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"}


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