; LLVM IR from kaynewbie/algorithm :: algorithm/LeetCode/Easy (longest-common-prefix)

; 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 ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i8, align 1
  %12 = alloca i8, align 1
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  %13 = load i32, ptr %5, align 4, !tbaa !9
  %14 = icmp sle i32 %13, 0
  br i1 %14, label %15, label %16

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

16:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  store i32 5, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %17 = load ptr, ptr %4, align 8, !tbaa !5
  %18 = load ptr, ptr %17, align 8, !tbaa !5
  store ptr %18, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %19 = load i32, ptr %6, align 4, !tbaa !9
  %20 = sext i32 %19 to i64
  %21 = call noalias ptr @calloc(i64 noundef %20, i64 noundef 1) #5
  store ptr %21, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  store i32 0, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  call void @llvm.lifetime.start.p0(i64 1, ptr %11) #4
  call void @llvm.lifetime.start.p0(i64 1, ptr %12) #4
  br label %22

22:                                               ; preds = %74, %16
  %23 = load ptr, ptr %7, align 8, !tbaa !5
  %24 = load i32, ptr %9, align 4, !tbaa !9
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds i8, ptr %23, i64 %25
  %27 = load i8, ptr %26, align 1, !tbaa !11
  %28 = sext i8 %27 to i32
  %29 = icmp ne i32 %28, 0
  br i1 %29, label %30, label %86

30:                                               ; preds = %22
  store i8 1, ptr %12, align 1, !tbaa !12
  store i32 1, ptr %10, align 4, !tbaa !9
  br label %31

31:                                               ; preds = %56, %30
  %32 = load i32, ptr %10, align 4, !tbaa !9
  %33 = load i32, ptr %5, align 4, !tbaa !9
  %34 = icmp slt i32 %32, %33
  br i1 %34, label %35, label %59

35:                                               ; preds = %31
  %36 = load ptr, ptr %4, align 8, !tbaa !5
  %37 = load i32, ptr %10, align 4, !tbaa !9
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds ptr, ptr %36, i64 %38
  %40 = load ptr, ptr %39, align 8, !tbaa !5
  %41 = load i32, ptr %9, align 4, !tbaa !9
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds i8, ptr %40, i64 %42
  %44 = load i8, ptr %43, align 1, !tbaa !11
  store i8 %44, ptr %11, align 1, !tbaa !11
  %45 = load ptr, ptr %7, align 8, !tbaa !5
  %46 = load i32, ptr %9, 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 = load i8, ptr %11, align 1, !tbaa !11
  %52 = sext i8 %51 to i32
  %53 = icmp ne i32 %50, %52
  br i1 %53, label %54, label %55

54:                                               ; preds = %35
  store i8 0, ptr %12, align 1, !tbaa !12
  br label %59

55:                                               ; preds = %35
  br label %56

56:                                               ; preds = %55
  %57 = load i32, ptr %10, align 4, !tbaa !9
  %58 = add nsw i32 %57, 1
  store i32 %58, ptr %10, align 4, !tbaa !9
  br label %31, !llvm.loop !14

59:                                               ; preds = %54, %31
  %60 = load i8, ptr %12, align 1, !tbaa !12, !range !16, !noundef !17
  %61 = trunc i8 %60 to i1
  br i1 %61, label %63, label %62

62:                                               ; preds = %59
  br label %86

63:                                               ; preds = %59
  %64 = load i32, ptr %9, align 4, !tbaa !9
  %65 = load i32, ptr %6, align 4, !tbaa !9
  %66 = icmp sge i32 %64, %65
  br i1 %66, label %67, label %74

67:                                               ; preds = %63
  %68 = load i32, ptr %6, align 4, !tbaa !9
  %69 = add nsw i32 %68, 10
  store i32 %69, ptr %6, align 4, !tbaa !9
  %70 = load ptr, ptr %8, align 8, !tbaa !5
  %71 = load i32, ptr %6, align 4, !tbaa !9
  %72 = sext i32 %71 to i64
  %73 = call ptr @realloc(ptr noundef %70, i64 noundef %72) #6
  store ptr %73, ptr %8, align 8, !tbaa !5
  br label %74

74:                                               ; preds = %67, %63
  %75 = load ptr, ptr %7, align 8, !tbaa !5
  %76 = load i32, ptr %9, align 4, !tbaa !9
  %77 = sext i32 %76 to i64
  %78 = getelementptr inbounds i8, ptr %75, i64 %77
  %79 = load i8, ptr %78, align 1, !tbaa !11
  %80 = load ptr, ptr %8, align 8, !tbaa !5
  %81 = load i32, ptr %9, align 4, !tbaa !9
  %82 = sext i32 %81 to i64
  %83 = getelementptr inbounds i8, ptr %80, i64 %82
  store i8 %79, ptr %83, align 1, !tbaa !11
  %84 = load i32, ptr %9, align 4, !tbaa !9
  %85 = add nsw i32 %84, 1
  store i32 %85, ptr %9, align 4, !tbaa !9
  br label %22, !llvm.loop !18

86:                                               ; preds = %62, %22
  %87 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %87, ptr %3, align 8
  call void @llvm.lifetime.end.p0(i64 1, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 1, 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 8, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  br label %88

88:                                               ; preds = %86, %15
  %89 = load ptr, ptr %3, align 8
  ret ptr %89
}

; 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,1)
declare noalias ptr @calloc(i64 noundef, i64 noundef) #2

; Function Attrs: nounwind allocsize(1)
declare ptr @realloc(ptr noundef, i64 noundef) #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 = { nounwind allocsize(0,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 #3 = { nounwind allocsize(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 #4 = { nounwind }
attributes #5 = { nounwind allocsize(0,1) }
attributes #6 = { nounwind allocsize(1) }

!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 = !{!13, !13, i64 0}
!13 = !{!"_Bool", !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{i8 0, i8 2}
!17 = !{}
!18 = distinct !{!18, !15}


; 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: nounwind uwtable
define dso_local ptr @longestCommonPrefix(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp slt i32 %1, 1
  br i1 %3, label %.loopexit, label %4

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

.preheader.lr.ph:                                 ; preds = %4
  %.not3438.not = icmp eq i32 %1, 1
  br i1 %.not3438.not, label %.preheader, label %.preheader.us.preheader

.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, %15
  %indvars.iv49 = phi i64 [ 0, %.preheader.us.preheader ], [ %indvars.iv.next50, %15 ]
  %8 = phi i8 [ %7, %.preheader.us.preheader ], [ %19, %15 ]
  %.02942.us = phi ptr [ %6, %.preheader.us.preheader ], [ %.1.us, %15 ]
  %.03041.us = phi i32 [ 5, %.preheader.us.preheader ], [ %.131.us, %15 ]
  %9 = getelementptr inbounds i8, ptr %5, i64 %indvars.iv49
  br label %20

10:                                               ; preds = %20
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %..critedge_crit_edge.us, label %20, !llvm.loop !10

11:                                               ; preds = %..critedge_crit_edge.us
  %12 = add nsw i32 %.03041.us, 10
  %13 = sext i32 %12 to i64
  %14 = tail call ptr @realloc(ptr noundef %.02942.us, i64 noundef %13) #4
  %.pre = load i8, ptr %9, align 1, !tbaa !9
  br label %15

15:                                               ; preds = %11, %..critedge_crit_edge.us
  %16 = phi i8 [ %.pre, %11 ], [ %8, %..critedge_crit_edge.us ]
  %.131.us = phi i32 [ %12, %11 ], [ %.03041.us, %..critedge_crit_edge.us ]
  %.1.us = phi ptr [ %14, %11 ], [ %.02942.us, %..critedge_crit_edge.us ]
  %17 = getelementptr inbounds i8, ptr %.1.us, i64 %indvars.iv49
  store i8 %16, ptr %17, align 1, !tbaa !9
  %indvars.iv.next50 = add nuw nsw i64 %indvars.iv49, 1
  %18 = getelementptr inbounds i8, ptr %5, i64 %indvars.iv.next50
  %19 = load i8, ptr %18, align 1, !tbaa !9
  %.not.us = icmp eq i8 %19, 0
  br i1 %.not.us, label %.loopexit, label %.preheader.us, !llvm.loop !12

20:                                               ; preds = %.preheader.us, %10
  %indvars.iv = phi i64 [ 1, %.preheader.us ], [ %indvars.iv.next, %10 ]
  %21 = getelementptr inbounds ptr, ptr %0, i64 %indvars.iv
  %22 = load ptr, ptr %21, align 8, !tbaa !5
  %23 = getelementptr inbounds i8, ptr %22, i64 %indvars.iv49
  %24 = load i8, ptr %23, align 1, !tbaa !9
  %.not33.us = icmp eq i8 %8, %24
  br i1 %.not33.us, label %10, label %.loopexit

..critedge_crit_edge.us:                          ; preds = %10
  %25 = sext i32 %.03041.us to i64
  %.not35.us = icmp slt i64 %indvars.iv49, %25
  br i1 %.not35.us, label %15, label %11

.preheader:                                       ; preds = %.preheader.lr.ph, %33
  %26 = phi i8 [ %37, %33 ], [ %7, %.preheader.lr.ph ]
  %indvars.iv51 = phi i64 [ %indvars.iv.next52, %33 ], [ 0, %.preheader.lr.ph ]
  %.02942 = phi ptr [ %.1, %33 ], [ %6, %.preheader.lr.ph ]
  %.03041 = phi i32 [ %.131, %33 ], [ 5, %.preheader.lr.ph ]
  %27 = sext i32 %.03041 to i64
  %.not35 = icmp slt i64 %indvars.iv51, %27
  br i1 %.not35, label %33, label %28

28:                                               ; preds = %.preheader
  %29 = getelementptr inbounds i8, ptr %5, i64 %indvars.iv51
  %30 = add nsw i32 %.03041, 10
  %31 = sext i32 %30 to i64
  %32 = tail call ptr @realloc(ptr noundef %.02942, i64 noundef %31) #4
  %.pre54 = load i8, ptr %29, align 1, !tbaa !9
  br label %33

33:                                               ; preds = %28, %.preheader
  %34 = phi i8 [ %.pre54, %28 ], [ %26, %.preheader ]
  %.131 = phi i32 [ %30, %28 ], [ %.03041, %.preheader ]
  %.1 = phi ptr [ %32, %28 ], [ %.02942, %.preheader ]
  %35 = getelementptr inbounds i8, ptr %.1, i64 %indvars.iv51
  store i8 %34, ptr %35, align 1, !tbaa !9
  %indvars.iv.next52 = add nuw nsw i64 %indvars.iv51, 1
  %36 = getelementptr inbounds i8, ptr %5, i64 %indvars.iv.next52
  %37 = load i8, ptr %36, align 1, !tbaa !9
  %.not = icmp eq i8 %37, 0
  br i1 %.not, label %.loopexit, label %.preheader, !llvm.loop !12

.loopexit:                                        ; preds = %15, %20, %33, %4, %2
  %.026 = phi ptr [ @.str, %2 ], [ %6, %4 ], [ %.1, %33 ], [ %.02942.us, %20 ], [ %.1.us, %15 ]
  ret ptr %.026
}

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

; Function Attrs: mustprogress nounwind willreturn allockind("realloc") allocsize(1) memory(argmem: readwrite, inaccessiblemem: readwrite)
declare noalias noundef ptr @realloc(ptr allocptr nocapture noundef, i64 noundef) local_unnamed_addr #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 = { mustprogress nofree nounwind willreturn allockind("alloc,zeroed") allocsize(0,1) 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 = { mustprogress nounwind willreturn allockind("realloc") allocsize(1) memory(argmem: readwrite, 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 #3 = { nounwind allocsize(0,1) }
attributes #4 = { nounwind allocsize(1) }

!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"}
!12 = distinct !{!12, !11}


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