; LLVM IR from nonstriater/Learn-Algorithms :: 9 Algorithms Job Interview/codes/5 array (longest_continuious_number)

; 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 @longest_continuious_number(ptr noundef %0, ptr noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  store i32 0, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  %13 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %13, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %14, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %15, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  store i32 0, ptr %10, align 4, !tbaa !9
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  %17 = icmp eq ptr %16, null
  br i1 %17, label %21, label %18

18:                                               ; preds = %2
  %19 = load ptr, ptr %5, align 8, !tbaa !5
  %20 = icmp eq ptr %19, null
  br i1 %20, label %21, label %22

21:                                               ; preds = %18, %2
  store i32 0, ptr %3, align 4
  store i32 1, ptr %11, align 4
  br label %87

22:                                               ; preds = %18
  br label %23

23:                                               ; preds = %54, %22
  %24 = load ptr, ptr %9, align 8, !tbaa !5
  %25 = load i8, ptr %24, align 1, !tbaa !11
  %26 = sext i8 %25 to i32
  %27 = icmp ne i32 %26, 0
  br i1 %27, label %28, label %57

28:                                               ; preds = %23
  %29 = load ptr, ptr %9, align 8, !tbaa !5
  %30 = load i8, ptr %29, align 1, !tbaa !11
  %31 = sext i8 %30 to i32
  %32 = icmp slt i32 %31, 48
  br i1 %32, label %38, label %33

33:                                               ; preds = %28
  %34 = load ptr, ptr %9, align 8, !tbaa !5
  %35 = load i8, ptr %34, align 1, !tbaa !11
  %36 = sext i8 %35 to i32
  %37 = icmp sgt i32 %36, 57
  br i1 %37, label %38, label %46

38:                                               ; preds = %33, %28
  %39 = load i32, ptr %10, align 4, !tbaa !9
  %40 = load i32, ptr %6, align 4, !tbaa !9
  %41 = icmp sgt i32 %39, %40
  br i1 %41, label %42, label %45

42:                                               ; preds = %38
  %43 = load i32, ptr %10, align 4, !tbaa !9
  store i32 %43, ptr %6, align 4, !tbaa !9
  %44 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %44, ptr %7, align 8, !tbaa !5
  br label %45

45:                                               ; preds = %42, %38
  store i32 0, ptr %10, align 4, !tbaa !9
  br label %54

46:                                               ; preds = %33
  %47 = load i32, ptr %10, align 4, !tbaa !9
  %48 = icmp eq i32 %47, 0
  br i1 %48, label %49, label %51

49:                                               ; preds = %46
  %50 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %50, ptr %8, align 8, !tbaa !5
  br label %51

51:                                               ; preds = %49, %46
  %52 = load i32, ptr %10, align 4, !tbaa !9
  %53 = add nsw i32 %52, 1
  store i32 %53, ptr %10, align 4, !tbaa !9
  br label %54

54:                                               ; preds = %51, %45
  %55 = load ptr, ptr %9, align 8, !tbaa !5
  %56 = getelementptr inbounds i8, ptr %55, i32 1
  store ptr %56, ptr %9, align 8, !tbaa !5
  br label %23, !llvm.loop !12

57:                                               ; preds = %23
  %58 = load i32, ptr %10, align 4, !tbaa !9
  %59 = load i32, ptr %6, align 4, !tbaa !9
  %60 = icmp sgt i32 %58, %59
  br i1 %60, label %61, label %64

61:                                               ; preds = %57
  %62 = load i32, ptr %10, align 4, !tbaa !9
  store i32 %62, ptr %6, align 4, !tbaa !9
  %63 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %63, ptr %7, align 8, !tbaa !5
  br label %64

64:                                               ; preds = %61, %57
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  store i32 0, ptr %12, align 4, !tbaa !9
  br label %65

65:                                               ; preds = %69, %64
  %66 = load i32, ptr %12, align 4, !tbaa !9
  %67 = load i32, ptr %6, align 4, !tbaa !9
  %68 = icmp slt i32 %66, %67
  br i1 %68, label %69, label %81

69:                                               ; preds = %65
  %70 = load ptr, ptr %7, align 8, !tbaa !5
  %71 = load i32, ptr %12, align 4, !tbaa !9
  %72 = sext i32 %71 to i64
  %73 = getelementptr inbounds i8, ptr %70, i64 %72
  %74 = load i8, ptr %73, align 1, !tbaa !11
  %75 = load ptr, ptr %5, align 8, !tbaa !5
  %76 = load i32, ptr %12, align 4, !tbaa !9
  %77 = sext i32 %76 to i64
  %78 = getelementptr inbounds i8, ptr %75, i64 %77
  store i8 %74, ptr %78, align 1, !tbaa !11
  %79 = load i32, ptr %12, align 4, !tbaa !9
  %80 = add nsw i32 %79, 1
  store i32 %80, ptr %12, align 4, !tbaa !9
  br label %65, !llvm.loop !14

81:                                               ; preds = %65
  %82 = load ptr, ptr %5, align 8, !tbaa !5
  %83 = load i32, ptr %12, align 4, !tbaa !9
  %84 = sext i32 %83 to i64
  %85 = getelementptr inbounds i8, ptr %82, i64 %84
  store i8 0, ptr %85, align 1, !tbaa !11
  %86 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %86, ptr %3, align 4
  store i32 1, ptr %11, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  br label %87

87:                                               ; preds = %81, %21
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  %88 = load i32, ptr %3, align 4
  ret i32 %88
}

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

; 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 = { 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 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !13}


; 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 norecurse nosync nounwind memory(read, argmem: readwrite, inaccessiblemem: none) uwtable
define dso_local i32 @longest_continuious_number(ptr noundef readonly %0, ptr noundef writeonly %1) local_unnamed_addr #0 {
  %3 = ptrtoint ptr %1 to i64
  %4 = icmp eq ptr %0, null
  %5 = icmp eq ptr %1, null
  %or.cond = or i1 %4, %5
  br i1 %or.cond, label %47, label %.preheader

.preheader:                                       ; preds = %2
  %6 = load i8, ptr %0, align 1, !tbaa !5
  %.not50 = icmp eq i8 %6, 0
  br i1 %.not50, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %14
  %7 = phi i8 [ %16, %14 ], [ %6, %.preheader ]
  %.03155 = phi i32 [ %.1, %14 ], [ 0, %.preheader ]
  %.03254 = phi ptr [ %15, %14 ], [ %0, %.preheader ]
  %.03353 = phi ptr [ %.2, %14 ], [ %0, %.preheader ]
  %.03552 = phi ptr [ %.237, %14 ], [ %0, %.preheader ]
  %.03851 = phi i32 [ %.240, %14 ], [ 0, %.preheader ]
  %8 = add i8 %7, -58
  %or.cond45 = icmp ult i8 %8, -10
  br i1 %or.cond45, label %9, label %11

9:                                                ; preds = %.lr.ph
  %10 = icmp sgt i32 %.03155, %.03851
  %spec.select = tail call i32 @llvm.smax.i32(i32 %.03155, i32 %.03851)
  %spec.select46 = select i1 %10, ptr %.03353, ptr %.03552
  br label %14

11:                                               ; preds = %.lr.ph
  %12 = icmp eq i32 %.03155, 0
  %spec.select47 = select i1 %12, ptr %.03254, ptr %.03353
  %13 = add nsw i32 %.03155, 1
  br label %14

14:                                               ; preds = %11, %9
  %.240 = phi i32 [ %spec.select, %9 ], [ %.03851, %11 ]
  %.237 = phi ptr [ %spec.select46, %9 ], [ %.03552, %11 ]
  %.2 = phi ptr [ %.03353, %9 ], [ %spec.select47, %11 ]
  %.1 = phi i32 [ 0, %9 ], [ %13, %11 ]
  %15 = getelementptr inbounds i8, ptr %.03254, i64 1
  %16 = load i8, ptr %15, align 1, !tbaa !5
  %.not = icmp eq i8 %16, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !8

._crit_edge:                                      ; preds = %14, %.preheader
  %.038.lcssa = phi i32 [ 0, %.preheader ], [ %.240, %14 ]
  %.035.lcssa = phi ptr [ %0, %.preheader ], [ %.237, %14 ]
  %.033.lcssa = phi ptr [ %0, %.preheader ], [ %.2, %14 ]
  %.031.lcssa = phi i32 [ 0, %.preheader ], [ %.1, %14 ]
  %17 = icmp sgt i32 %.031.lcssa, %.038.lcssa
  %spec.select48 = tail call i32 @llvm.smax.i32(i32 %.031.lcssa, i32 %.038.lcssa)
  %spec.select49 = select i1 %17, ptr %.033.lcssa, ptr %.035.lcssa
  %18 = icmp sgt i32 %spec.select48, 0
  br i1 %18, label %iter.check, label %._crit_edge62

iter.check:                                       ; preds = %._crit_edge
  %spec.select4965 = ptrtoint ptr %spec.select49 to i64
  %wide.trip.count = zext nneg i32 %spec.select48 to i64
  %min.iters.check = icmp ult i32 %spec.select48, 8
  %19 = sub i64 %3, %spec.select4965
  %diff.check = icmp ult i64 %19, 32
  %or.cond74 = select i1 %min.iters.check, i1 true, i1 %diff.check
  br i1 %or.cond74, label %.lr.ph61.preheader, label %vector.main.loop.iter.check

vector.main.loop.iter.check:                      ; preds = %iter.check
  %min.iters.check66 = icmp ult i32 %spec.select48, 32
  br i1 %min.iters.check66, label %vec.epilog.ph, label %vector.ph

vector.ph:                                        ; preds = %vector.main.loop.iter.check
  %n.vec = and i64 %wide.trip.count, 2147483616
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %20 = getelementptr inbounds i8, ptr %spec.select49, i64 %index
  %21 = getelementptr inbounds i8, ptr %20, i64 16
  %wide.load = load <16 x i8>, ptr %20, align 1, !tbaa !5
  %wide.load67 = load <16 x i8>, ptr %21, align 1, !tbaa !5
  %22 = getelementptr inbounds i8, ptr %1, i64 %index
  %23 = getelementptr inbounds i8, ptr %22, i64 16
  store <16 x i8> %wide.load, ptr %22, align 1, !tbaa !5
  store <16 x i8> %wide.load67, ptr %23, align 1, !tbaa !5
  %index.next = add nuw i64 %index, 32
  %24 = icmp eq i64 %index.next, %n.vec
  br i1 %24, label %middle.block, label %vector.body, !llvm.loop !10

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %n.vec, %wide.trip.count
  br i1 %cmp.n, label %._crit_edge62.loopexit, label %vec.epilog.iter.check

vec.epilog.iter.check:                            ; preds = %middle.block
  %n.vec.remaining = and i64 %wide.trip.count, 24
  %min.epilog.iters.check = icmp eq i64 %n.vec.remaining, 0
  br i1 %min.epilog.iters.check, label %.lr.ph61.preheader, label %vec.epilog.ph

vec.epilog.ph:                                    ; preds = %vector.main.loop.iter.check, %vec.epilog.iter.check
  %vec.epilog.resume.val = phi i64 [ %n.vec, %vec.epilog.iter.check ], [ 0, %vector.main.loop.iter.check ]
  %n.vec69 = and i64 %wide.trip.count, 2147483640
  br label %vec.epilog.vector.body

vec.epilog.vector.body:                           ; preds = %vec.epilog.vector.body, %vec.epilog.ph
  %index70 = phi i64 [ %vec.epilog.resume.val, %vec.epilog.ph ], [ %index.next72, %vec.epilog.vector.body ]
  %25 = getelementptr inbounds i8, ptr %spec.select49, i64 %index70
  %wide.load71 = load <8 x i8>, ptr %25, align 1, !tbaa !5
  %26 = getelementptr inbounds i8, ptr %1, i64 %index70
  store <8 x i8> %wide.load71, ptr %26, align 1, !tbaa !5
  %index.next72 = add nuw i64 %index70, 8
  %27 = icmp eq i64 %index.next72, %n.vec69
  br i1 %27, label %vec.epilog.middle.block, label %vec.epilog.vector.body, !llvm.loop !13

vec.epilog.middle.block:                          ; preds = %vec.epilog.vector.body
  %cmp.n73 = icmp eq i64 %n.vec69, %wide.trip.count
  br i1 %cmp.n73, label %._crit_edge62.loopexit, label %.lr.ph61.preheader

.lr.ph61.preheader:                               ; preds = %vec.epilog.middle.block, %iter.check, %vec.epilog.iter.check
  %indvars.iv.ph = phi i64 [ 0, %iter.check ], [ %n.vec, %vec.epilog.iter.check ], [ %n.vec69, %vec.epilog.middle.block ]
  %xtraiter = and i64 %wide.trip.count, 3
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.lr.ph61.prol.loopexit, label %.lr.ph61.prol

.lr.ph61.prol:                                    ; preds = %.lr.ph61.preheader, %.lr.ph61.prol
  %indvars.iv.prol = phi i64 [ %indvars.iv.next.prol, %.lr.ph61.prol ], [ %indvars.iv.ph, %.lr.ph61.preheader ]
  %prol.iter = phi i64 [ %prol.iter.next, %.lr.ph61.prol ], [ 0, %.lr.ph61.preheader ]
  %28 = getelementptr inbounds i8, ptr %spec.select49, i64 %indvars.iv.prol
  %29 = load i8, ptr %28, align 1, !tbaa !5
  %30 = getelementptr inbounds i8, ptr %1, i64 %indvars.iv.prol
  store i8 %29, ptr %30, align 1, !tbaa !5
  %indvars.iv.next.prol = add nuw nsw i64 %indvars.iv.prol, 1
  %prol.iter.next = add i64 %prol.iter, 1
  %prol.iter.cmp.not = icmp eq i64 %prol.iter.next, %xtraiter
  br i1 %prol.iter.cmp.not, label %.lr.ph61.prol.loopexit, label %.lr.ph61.prol, !llvm.loop !14

.lr.ph61.prol.loopexit:                           ; preds = %.lr.ph61.prol, %.lr.ph61.preheader
  %indvars.iv.unr = phi i64 [ %indvars.iv.ph, %.lr.ph61.preheader ], [ %indvars.iv.next.prol, %.lr.ph61.prol ]
  %31 = sub nsw i64 %indvars.iv.ph, %wide.trip.count
  %32 = icmp ugt i64 %31, -4
  br i1 %32, label %._crit_edge62.loopexit, label %.lr.ph61

.lr.ph61:                                         ; preds = %.lr.ph61.prol.loopexit, %.lr.ph61
  %indvars.iv = phi i64 [ %indvars.iv.next.3, %.lr.ph61 ], [ %indvars.iv.unr, %.lr.ph61.prol.loopexit ]
  %33 = getelementptr inbounds i8, ptr %spec.select49, i64 %indvars.iv
  %34 = load i8, ptr %33, align 1, !tbaa !5
  %35 = getelementptr inbounds i8, ptr %1, i64 %indvars.iv
  store i8 %34, ptr %35, align 1, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %36 = getelementptr inbounds i8, ptr %spec.select49, i64 %indvars.iv.next
  %37 = load i8, ptr %36, align 1, !tbaa !5
  %38 = getelementptr inbounds i8, ptr %1, i64 %indvars.iv.next
  store i8 %37, ptr %38, align 1, !tbaa !5
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %39 = getelementptr inbounds i8, ptr %spec.select49, i64 %indvars.iv.next.1
  %40 = load i8, ptr %39, align 1, !tbaa !5
  %41 = getelementptr inbounds i8, ptr %1, i64 %indvars.iv.next.1
  store i8 %40, ptr %41, align 1, !tbaa !5
  %indvars.iv.next.2 = add nuw nsw i64 %indvars.iv, 3
  %42 = getelementptr inbounds i8, ptr %spec.select49, i64 %indvars.iv.next.2
  %43 = load i8, ptr %42, align 1, !tbaa !5
  %44 = getelementptr inbounds i8, ptr %1, i64 %indvars.iv.next.2
  store i8 %43, ptr %44, align 1, !tbaa !5
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 4
  %exitcond.not.3 = icmp eq i64 %indvars.iv.next.3, %wide.trip.count
  br i1 %exitcond.not.3, label %._crit_edge62.loopexit, label %.lr.ph61, !llvm.loop !16

._crit_edge62.loopexit:                           ; preds = %.lr.ph61.prol.loopexit, %.lr.ph61, %vec.epilog.middle.block, %middle.block
  %45 = zext nneg i32 %spec.select48 to i64
  br label %._crit_edge62

._crit_edge62:                                    ; preds = %._crit_edge, %._crit_edge62.loopexit
  %.0.lcssa = phi i64 [ %45, %._crit_edge62.loopexit ], [ 0, %._crit_edge ]
  %46 = getelementptr inbounds i8, ptr %1, i64 %.0.lcssa
  store i8 0, ptr %46, align 1, !tbaa !5
  br label %47

47:                                               ; preds = %2, %._crit_edge62
  %.042 = phi i32 [ %spec.select48, %._crit_edge62 ], [ 0, %2 ]
  ret i32 %.042
}

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

attributes #0 = { nofree norecurse nosync nounwind memory(read, argmem: readwrite, inaccessiblemem: none) 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 speculatable willreturn memory(none) }

!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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}
!10 = distinct !{!10, !9, !11, !12}
!11 = !{!"llvm.loop.isvectorized", i32 1}
!12 = !{!"llvm.loop.unroll.runtime.disable"}
!13 = distinct !{!13, !9, !11, !12}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.unroll.disable"}
!16 = distinct !{!16, !9, !11}


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