; LLVM IR from AlgoGenesis/C :: String Algorithms/Longest_Common_Prefix (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"

@longestCommonPrefix.prefix = external hidden global [100 x i8], align 16

; 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 i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = getelementptr inbounds [100 x i8], ptr %9, i64 0
  %11 = getelementptr inbounds [100 x i8], ptr %10, i64 0, i64 0
  %12 = call ptr @strcpy(ptr noundef @longestCommonPrefix.prefix, ptr noundef %11) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  store i32 1, ptr %6, align 4, !tbaa !9
  br label %13

13:                                               ; preds = %67, %2
  %14 = load i32, ptr %6, align 4, !tbaa !9
  %15 = load i32, ptr %5, align 4, !tbaa !9
  %16 = icmp slt i32 %14, %15
  br i1 %16, label %18, label %17

17:                                               ; preds = %13
  store i32 2, ptr %7, align 4
  br label %70

18:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  store i32 0, ptr %8, align 4, !tbaa !9
  br label %19

19:                                               ; preds = %52, %18
  %20 = load i32, ptr %8, align 4, !tbaa !9
  %21 = sext i32 %20 to i64
  %22 = call i64 @strlen(ptr noundef @longestCommonPrefix.prefix) #5
  %23 = icmp ult i64 %21, %22
  br i1 %23, label %24, label %50

24:                                               ; preds = %19
  %25 = load i32, ptr %8, align 4, !tbaa !9
  %26 = sext i32 %25 to i64
  %27 = load ptr, ptr %4, align 8, !tbaa !5
  %28 = load i32, ptr %6, align 4, !tbaa !9
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [100 x i8], ptr %27, i64 %29
  %31 = getelementptr inbounds [100 x i8], ptr %30, i64 0, i64 0
  %32 = call i64 @strlen(ptr noundef %31) #5
  %33 = icmp ult i64 %26, %32
  br i1 %33, label %34, label %50

34:                                               ; preds = %24
  %35 = load i32, ptr %8, align 4, !tbaa !9
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [100 x i8], ptr @longestCommonPrefix.prefix, i64 0, i64 %36
  %38 = load i8, ptr %37, align 1, !tbaa !11
  %39 = sext i8 %38 to i32
  %40 = load ptr, ptr %4, align 8, !tbaa !5
  %41 = load i32, ptr %6, align 4, !tbaa !9
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds [100 x i8], ptr %40, i64 %42
  %44 = load i32, ptr %8, align 4, !tbaa !9
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [100 x i8], ptr %43, i64 0, i64 %45
  %47 = load i8, ptr %46, align 1, !tbaa !11
  %48 = sext i8 %47 to i32
  %49 = icmp eq i32 %39, %48
  br label %50

50:                                               ; preds = %34, %24, %19
  %51 = phi i1 [ false, %24 ], [ false, %19 ], [ %49, %34 ]
  br i1 %51, label %52, label %55

52:                                               ; preds = %50
  %53 = load i32, ptr %8, align 4, !tbaa !9
  %54 = add nsw i32 %53, 1
  store i32 %54, ptr %8, align 4, !tbaa !9
  br label %19, !llvm.loop !12

55:                                               ; preds = %50
  %56 = load i32, ptr %8, align 4, !tbaa !9
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds [100 x i8], ptr @longestCommonPrefix.prefix, i64 0, i64 %57
  store i8 0, ptr %58, align 1, !tbaa !11
  %59 = load i8, ptr @longestCommonPrefix.prefix, align 16, !tbaa !11
  %60 = sext i8 %59 to i32
  %61 = icmp eq i32 %60, 0
  br i1 %61, label %62, label %63

62:                                               ; preds = %55
  store ptr @longestCommonPrefix.prefix, ptr %3, align 8
  store i32 1, ptr %7, align 4
  br label %64

63:                                               ; preds = %55
  store i32 0, ptr %7, align 4
  br label %64

64:                                               ; preds = %63, %62
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  %65 = load i32, ptr %7, align 4
  switch i32 %65, label %70 [
    i32 0, label %66
  ]

66:                                               ; preds = %64
  br label %67

67:                                               ; preds = %66
  %68 = load i32, ptr %6, align 4, !tbaa !9
  %69 = add nsw i32 %68, 1
  store i32 %69, ptr %6, align 4, !tbaa !9
  br label %13, !llvm.loop !14

70:                                               ; preds = %64, %17
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  %71 = load i32, ptr %7, align 4
  switch i32 %71, label %75 [
    i32 2, label %72
    i32 1, label %73
  ]

72:                                               ; preds = %70
  store ptr @longestCommonPrefix.prefix, ptr %3, align 8
  br label %73

73:                                               ; preds = %72, %70
  %74 = load ptr, ptr %3, align 8
  ret ptr %74

75:                                               ; preds = %70
  unreachable
}

; Function Attrs: nounwind
declare ptr @strcpy(ptr 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: nounwind willreturn memory(read)
declare i64 @strlen(ptr noundef) #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 = { nounwind "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 willreturn memory(read) "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 willreturn memory(read) }

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

@longestCommonPrefix.prefix = external hidden global [100 x i8], align 16

; Function Attrs: nofree nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local noundef nonnull ptr @longestCommonPrefix(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = tail call ptr @strcpy(ptr noundef nonnull dereferenceable(1) @longestCommonPrefix.prefix, ptr noundef nonnull dereferenceable(1) %0) #3
  %4 = icmp sgt i32 %1, 1
  br i1 %4, label %.preheader.preheader, label %._crit_edge

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

.preheader:                                       ; preds = %.critedge, %.preheader.preheader
  %indvars.iv30 = phi i64 [ 1, %.preheader.preheader ], [ %indvars.iv.next31, %.critedge ]
  %5 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) @longestCommonPrefix.prefix) #4
  %.not27 = icmp eq i64 %5, 0
  br i1 %.not27, label %.critedge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader
  %6 = getelementptr inbounds [100 x i8], ptr %0, i64 %indvars.iv30
  %7 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %6) #4
  br label %9

8:                                                ; preds = %10
  %indvars.iv.next = add nuw i64 %indvars.iv, 1
  %exitcond29.not = icmp eq i64 %indvars.iv.next, %5
  br i1 %exitcond29.not, label %.critedge, label %9, !llvm.loop !5

9:                                                ; preds = %.lr.ph, %8
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %8 ]
  %exitcond.not = icmp eq i64 %indvars.iv, %7
  br i1 %exitcond.not, label %.critedge, label %10

10:                                               ; preds = %9
  %11 = getelementptr inbounds [100 x i8], ptr @longestCommonPrefix.prefix, i64 0, i64 %indvars.iv
  %12 = load i8, ptr %11, align 1, !tbaa !7
  %13 = getelementptr inbounds [100 x i8], ptr %0, i64 %indvars.iv30, i64 %indvars.iv
  %14 = load i8, ptr %13, align 1, !tbaa !7
  %15 = icmp eq i8 %12, %14
  br i1 %15, label %8, label %.critedge

.critedge:                                        ; preds = %10, %8, %9, %.preheader
  %.lcssa = phi i64 [ 0, %.preheader ], [ %7, %9 ], [ %5, %8 ], [ %indvars.iv, %10 ]
  %16 = getelementptr inbounds [100 x i8], ptr @longestCommonPrefix.prefix, i64 0, i64 %.lcssa
  store i8 0, ptr %16, align 1, !tbaa !7
  %17 = load i8, ptr @longestCommonPrefix.prefix, align 16, !tbaa !7
  %.not = icmp eq i8 %17, 0
  %indvars.iv.next31 = add nuw nsw i64 %indvars.iv30, 1
  %exitcond33.not = icmp eq i64 %indvars.iv.next31, %wide.trip.count
  %or.cond = select i1 %.not, i1 true, i1 %exitcond33.not
  br i1 %or.cond, label %._crit_edge, label %.preheader, !llvm.loop !10

._crit_edge:                                      ; preds = %.critedge, %2
  ret ptr @longestCommonPrefix.prefix
}

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: readwrite)
declare ptr @strcpy(ptr noalias noundef returned writeonly, ptr noalias nocapture noundef readonly) local_unnamed_addr #1

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #2

attributes #0 = { nofree nounwind memory(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 = { mustprogress nofree nounwind willreturn memory(argmem: readwrite) "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 nofree nounwind willreturn memory(argmem: read) "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 willreturn memory(read) }

!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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = !{!8, !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = distinct !{!10, !6}


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