; LLVM IR from 6leetcode/6leetcode :: questions/Algorithms/0014. Longest Common Prefix (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"

@.str = external hidden unnamed_addr constant [1 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local ptr @longestCommonPrefix(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i8, align 1
  %10 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  %11 = load i32, ptr %5, align 4, !tbaa !9
  %12 = icmp eq i32 %11, 0
  br i1 %12, label %13, label %14

13:                                               ; preds = %2
  store ptr @.str, ptr %3, align 8
  br label %80

14:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 0, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %15 = call noalias ptr @malloc(i64 noundef 1000) #4
  store ptr %15, ptr %8, align 8, !tbaa !5
  br label %16

16:                                               ; preds = %72, %14
  %17 = load ptr, ptr %4, align 8, !tbaa !5
  %18 = getelementptr inbounds ptr, ptr %17, i64 0
  %19 = load ptr, ptr %18, align 8, !tbaa !5
  %20 = load i32, ptr %7, align 4, !tbaa !9
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds i8, ptr %19, i64 %21
  %23 = load i8, ptr %22, align 1, !tbaa !11
  %24 = sext i8 %23 to i32
  %25 = icmp ne i32 %24, 0
  br i1 %25, label %26, label %73

26:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 1, ptr %9) #3
  %27 = load ptr, ptr %4, align 8, !tbaa !5
  %28 = getelementptr inbounds ptr, ptr %27, i64 0
  %29 = load ptr, ptr %28, align 8, !tbaa !5
  %30 = load i32, ptr %7, align 4, !tbaa !9
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds i8, ptr %29, i64 %31
  %33 = load i8, ptr %32, align 1, !tbaa !11
  store i8 %33, ptr %9, align 1, !tbaa !11
  store i32 1, ptr %6, align 4, !tbaa !9
  br label %34

34:                                               ; preds = %54, %26
  %35 = load i32, ptr %6, align 4, !tbaa !9
  %36 = load i32, ptr %5, align 4, !tbaa !9
  %37 = icmp slt i32 %35, %36
  br i1 %37, label %38, label %57

38:                                               ; preds = %34
  %39 = load i8, ptr %9, align 1, !tbaa !11
  %40 = sext i8 %39 to i32
  %41 = load ptr, ptr %4, align 8, !tbaa !5
  %42 = load i32, ptr %6, align 4, !tbaa !9
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds ptr, ptr %41, i64 %43
  %45 = load ptr, ptr %44, align 8, !tbaa !5
  %46 = load i32, ptr %7, align 4, !tbaa !9
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds i8, ptr %45, i64 %47
  %49 = load i8, ptr %48, align 1, !tbaa !11
  %50 = sext i8 %49 to i32
  %51 = icmp ne i32 %40, %50
  br i1 %51, label %52, label %53

52:                                               ; preds = %38
  br label %57

53:                                               ; preds = %38
  br label %54

54:                                               ; preds = %53
  %55 = load i32, ptr %6, align 4, !tbaa !9
  %56 = add nsw i32 %55, 1
  store i32 %56, ptr %6, align 4, !tbaa !9
  br label %34, !llvm.loop !12

57:                                               ; preds = %52, %34
  %58 = load i32, ptr %6, align 4, !tbaa !9
  %59 = load i32, ptr %5, align 4, !tbaa !9
  %60 = icmp eq i32 %58, %59
  br i1 %60, label %61, label %68

61:                                               ; preds = %57
  %62 = load i8, ptr %9, align 1, !tbaa !11
  %63 = load ptr, ptr %8, align 8, !tbaa !5
  %64 = load i32, ptr %7, align 4, !tbaa !9
  %65 = add nsw i32 %64, 1
  store i32 %65, ptr %7, align 4, !tbaa !9
  %66 = sext i32 %64 to i64
  %67 = getelementptr inbounds i8, ptr %63, i64 %66
  store i8 %62, ptr %67, align 1, !tbaa !11
  br label %69

68:                                               ; preds = %57
  store i32 3, ptr %10, align 4
  br label %70

69:                                               ; preds = %61
  store i32 0, ptr %10, align 4
  br label %70

70:                                               ; preds = %69, %68
  call void @llvm.lifetime.end.p0(i64 1, ptr %9) #3
  %71 = load i32, ptr %10, align 4
  switch i32 %71, label %82 [
    i32 0, label %72
    i32 3, label %73
  ]

72:                                               ; preds = %70
  br label %16, !llvm.loop !14

73:                                               ; preds = %70, %16
  %74 = load ptr, ptr %8, align 8, !tbaa !5
  %75 = load i32, ptr %7, align 4, !tbaa !9
  %76 = add nsw i32 %75, 1
  store i32 %76, ptr %7, align 4, !tbaa !9
  %77 = sext i32 %75 to i64
  %78 = getelementptr inbounds i8, ptr %74, i64 %77
  store i8 0, ptr %78, align 1, !tbaa !11
  %79 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %79, ptr %3, align 8
  store i32 1, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  br label %80

80:                                               ; preds = %73, %13
  %81 = load ptr, ptr %3, align 8
  ret ptr %81

82:                                               ; preds = %70
  unreachable
}

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

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #2

; 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 allocsize(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 = { nounwind }
attributes #4 = { nounwind allocsize(0) }

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

@.str = external hidden unnamed_addr constant [1 x i8], align 1

; Function Attrs: nofree nounwind memory(readwrite, argmem: read) uwtable
define dso_local noundef ptr @longestCommonPrefix(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp eq i32 %1, 0
  br i1 %3, label %31, label %4

4:                                                ; preds = %2
  %5 = tail call noalias dereferenceable_or_null(1000) ptr @malloc(i64 noundef 1000) #2
  %6 = load ptr, ptr %0, align 8, !tbaa !5
  %7 = load i8, ptr %6, align 1, !tbaa !9
  %.not33 = icmp eq i8 %7, 0
  br i1 %.not33, label %.thread, label %.preheader.lr.ph

.preheader.lr.ph:                                 ; preds = %4
  %8 = icmp sgt i32 %1, 1
  br i1 %8, label %.preheader.us.preheader, label %.preheader.lr.ph.split

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

.preheader.us:                                    ; preds = %.preheader.us.preheader, %._crit_edge.us.thread
  %indvars.iv48 = phi i64 [ 0, %.preheader.us.preheader ], [ %indvars.iv.next49, %._crit_edge.us.thread ]
  %9 = phi i8 [ %7, %.preheader.us.preheader ], [ %20, %._crit_edge.us.thread ]
  %.02334.us = phi i32 [ 0, %.preheader.us.preheader ], [ %17, %._crit_edge.us.thread ]
  br label %10

10:                                               ; preds = %.preheader.us, %21
  %indvars.iv45 = phi i64 [ 1, %.preheader.us ], [ %indvars.iv.next46, %21 ]
  %11 = getelementptr inbounds ptr, ptr %0, i64 %indvars.iv45
  %12 = load ptr, ptr %11, align 8, !tbaa !5
  %13 = getelementptr inbounds i8, ptr %12, i64 %indvars.iv48
  %14 = load i8, ptr %13, align 1, !tbaa !9
  %.not26.us = icmp eq i8 %9, %14
  br i1 %.not26.us, label %21, label %._crit_edge.us

._crit_edge.us:                                   ; preds = %10
  %15 = trunc nuw nsw i64 %indvars.iv45 to i32
  %16 = icmp eq i32 %15, %1
  br i1 %16, label %._crit_edge.us.thread, label %.thread.loopexit.split.loop.exit

._crit_edge.us.thread:                            ; preds = %21, %._crit_edge.us
  %indvars.iv.next49 = add nuw nsw i64 %indvars.iv48, 1
  %17 = add nuw nsw i32 %.02334.us, 1
  %18 = getelementptr inbounds i8, ptr %5, i64 %indvars.iv48
  store i8 %9, ptr %18, align 1, !tbaa !9
  %19 = getelementptr inbounds i8, ptr %6, i64 %indvars.iv.next49
  %20 = load i8, ptr %19, align 1, !tbaa !9
  %.not.us = icmp eq i8 %20, 0
  br i1 %.not.us, label %.thread, label %.preheader.us

21:                                               ; preds = %10
  %indvars.iv.next46 = add nuw nsw i64 %indvars.iv45, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next46, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge.us.thread, label %10, !llvm.loop !10

.preheader.lr.ph.split:                           ; preds = %.preheader.lr.ph
  %22 = icmp eq i32 %1, 1
  br i1 %22, label %.preheader.us39, label %.thread

.preheader.us39:                                  ; preds = %.preheader.lr.ph.split, %.preheader.us39
  %indvars.iv = phi i64 [ %indvars.iv.next, %.preheader.us39 ], [ 0, %.preheader.lr.ph.split ]
  %23 = phi i8 [ %26, %.preheader.us39 ], [ %7, %.preheader.lr.ph.split ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %24 = getelementptr inbounds i8, ptr %5, i64 %indvars.iv
  store i8 %23, ptr %24, align 1, !tbaa !9
  %25 = getelementptr inbounds i8, ptr %6, i64 %indvars.iv.next
  %26 = load i8, ptr %25, align 1, !tbaa !9
  %.not.us41 = icmp eq i8 %26, 0
  br i1 %.not.us41, label %.thread.loopexit44, label %.preheader.us39

.thread.loopexit44:                               ; preds = %.preheader.us39
  %27 = trunc nuw nsw i64 %indvars.iv.next to i32
  br label %.thread

.thread.loopexit.split.loop.exit:                 ; preds = %._crit_edge.us
  %28 = trunc nuw nsw i64 %indvars.iv48 to i32
  br label %.thread

.thread:                                          ; preds = %._crit_edge.us.thread, %.thread.loopexit.split.loop.exit, %.thread.loopexit44, %.preheader.lr.ph.split, %4
  %.023.lcssa = phi i32 [ 0, %4 ], [ 0, %.preheader.lr.ph.split ], [ %27, %.thread.loopexit44 ], [ %28, %.thread.loopexit.split.loop.exit ], [ %17, %._crit_edge.us.thread ]
  %29 = zext nneg i32 %.023.lcssa to i64
  %30 = getelementptr inbounds i8, ptr %5, i64 %29
  store i8 0, ptr %30, align 1, !tbaa !9
  br label %31

31:                                               ; preds = %2, %.thread
  %.021 = phi ptr [ %5, %.thread ], [ @.str, %2 ]
  ret ptr %.021
}

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #1

attributes #0 = { nofree nounwind memory(readwrite, argmem: read) 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 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 allocsize(0) }

!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 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}


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