; LLVM IR from AlgoGenesis/C :: Trie/LongestCommonPrefix (LongestCommonPrefix)

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

%struct.TrieNode = type { i8, [26 x ptr], i8, i32 }

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

; Function Attrs: nounwind uwtable
declare dso_local ptr @createNode(i8 noundef signext) #0

; 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

; Function Attrs: nounwind uwtable
declare dso_local void @insert(ptr noundef, ptr noundef) #0

; Function Attrs: nounwind willreturn memory(read)
declare i64 @strlen(ptr noundef) #3

; 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 ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i8, align 1
  %14 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %15 = call ptr @createNode(i8 noundef signext 0)
  store ptr %15, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %16

16:                                               ; preds = %28, %2
  %17 = load i32, ptr %7, align 4, !tbaa !9
  %18 = load i32, ptr %5, align 4, !tbaa !9
  %19 = icmp slt i32 %17, %18
  br i1 %19, label %21, label %20

20:                                               ; preds = %16
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  br label %31

21:                                               ; preds = %16
  %22 = load ptr, ptr %6, align 8, !tbaa !5
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  %24 = load i32, ptr %7, align 4, !tbaa !9
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds ptr, ptr %23, i64 %25
  %27 = load ptr, ptr %26, align 8, !tbaa !5
  call void @insert(ptr noundef %22, ptr noundef %27)
  br label %28

28:                                               ; preds = %21
  %29 = load i32, ptr %7, align 4, !tbaa !9
  %30 = add nsw i32 %29, 1
  store i32 %30, ptr %7, align 4, !tbaa !9
  br label %16, !llvm.loop !11

31:                                               ; preds = %20
  %32 = load ptr, ptr %6, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.TrieNode, ptr %32, i32 0, i32 2
  %34 = load i8, ptr %33, align 8, !tbaa !13, !range !16, !noundef !17
  %35 = trunc i8 %34 to i1
  br i1 %35, label %36, label %37

36:                                               ; preds = %31
  store ptr @.str, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %110

37:                                               ; preds = %31
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %38 = load ptr, ptr %4, align 8, !tbaa !5
  %39 = getelementptr inbounds ptr, ptr %38, i64 0
  %40 = load ptr, ptr %39, align 8, !tbaa !5
  store ptr %40, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %41 = load ptr, ptr %9, align 8, !tbaa !5
  %42 = call i64 @strlen(ptr noundef %41) #5
  %43 = add i64 %42, 1
  %44 = mul i64 1, %43
  %45 = call noalias ptr @malloc(i64 noundef %44) #6
  store ptr %45, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #4
  store i32 0, ptr %11, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #4
  store i32 0, ptr %12, align 4, !tbaa !9
  br label %46

46:                                               ; preds = %100, %37
  %47 = load ptr, ptr %9, align 8, !tbaa !5
  %48 = load i32, ptr %12, align 4, !tbaa !9
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds i8, ptr %47, i64 %49
  %51 = load i8, ptr %50, align 1, !tbaa !18
  %52 = sext i8 %51 to i32
  %53 = icmp ne i32 %52, 0
  br i1 %53, label %55, label %54

54:                                               ; preds = %46
  store i32 5, ptr %8, align 4
  br label %103

55:                                               ; preds = %46
  call void @llvm.lifetime.start.p0(i64 1, ptr %13) #4
  %56 = load ptr, ptr %9, align 8, !tbaa !5
  %57 = load i32, ptr %12, align 4, !tbaa !9
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds i8, ptr %56, i64 %58
  %60 = load i8, ptr %59, align 1, !tbaa !18
  store i8 %60, ptr %13, align 1, !tbaa !18
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #4
  %61 = load i8, ptr %13, align 1, !tbaa !18
  %62 = sext i8 %61 to i32
  %63 = sub nsw i32 %62, 97
  store i32 %63, ptr %14, align 4, !tbaa !9
  %64 = load ptr, ptr %6, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.TrieNode, ptr %64, i32 0, i32 3
  %66 = load i32, ptr %65, align 4, !tbaa !19
  %67 = icmp eq i32 %66, 1
  br i1 %67, label %68, label %95

68:                                               ; preds = %55
  %69 = load ptr, ptr %6, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.TrieNode, ptr %69, i32 0, i32 1
  %71 = load i32, ptr %14, align 4, !tbaa !9
  %72 = sext i32 %71 to i64
  %73 = getelementptr inbounds [26 x ptr], ptr %70, i64 0, i64 %72
  %74 = load ptr, ptr %73, align 8, !tbaa !5
  %75 = icmp ne ptr %74, null
  br i1 %75, label %76, label %95

76:                                               ; preds = %68
  %77 = load i8, ptr %13, align 1, !tbaa !18
  %78 = load ptr, ptr %10, align 8, !tbaa !5
  %79 = load i32, ptr %11, align 4, !tbaa !9
  %80 = add nsw i32 %79, 1
  store i32 %80, ptr %11, align 4, !tbaa !9
  %81 = sext i32 %79 to i64
  %82 = getelementptr inbounds i8, ptr %78, i64 %81
  store i8 %77, ptr %82, align 1, !tbaa !18
  %83 = load ptr, ptr %6, align 8, !tbaa !5
  %84 = getelementptr inbounds %struct.TrieNode, ptr %83, i32 0, i32 1
  %85 = load i32, ptr %14, align 4, !tbaa !9
  %86 = sext i32 %85 to i64
  %87 = getelementptr inbounds [26 x ptr], ptr %84, i64 0, i64 %86
  %88 = load ptr, ptr %87, align 8, !tbaa !5
  store ptr %88, ptr %6, align 8, !tbaa !5
  %89 = load ptr, ptr %6, align 8, !tbaa !5
  %90 = getelementptr inbounds %struct.TrieNode, ptr %89, i32 0, i32 2
  %91 = load i8, ptr %90, align 8, !tbaa !13, !range !16, !noundef !17
  %92 = trunc i8 %91 to i1
  br i1 %92, label %93, label %94

93:                                               ; preds = %76
  store i32 5, ptr %8, align 4
  br label %97

94:                                               ; preds = %76
  br label %96

95:                                               ; preds = %68, %55
  store i32 5, ptr %8, align 4
  br label %97

96:                                               ; preds = %94
  store i32 0, ptr %8, align 4
  br label %97

97:                                               ; preds = %96, %95, %93
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #4
  call void @llvm.lifetime.end.p0(i64 1, ptr %13) #4
  %98 = load i32, ptr %8, align 4
  switch i32 %98, label %103 [
    i32 0, label %99
  ]

99:                                               ; preds = %97
  br label %100

100:                                              ; preds = %99
  %101 = load i32, ptr %12, align 4, !tbaa !9
  %102 = add nsw i32 %101, 1
  store i32 %102, ptr %12, align 4, !tbaa !9
  br label %46, !llvm.loop !20

103:                                              ; preds = %97, %54
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #4
  br label %104

104:                                              ; preds = %103
  %105 = load ptr, ptr %10, align 8, !tbaa !5
  %106 = load i32, ptr %11, align 4, !tbaa !9
  %107 = sext i32 %106 to i64
  %108 = getelementptr inbounds i8, ptr %105, i64 %107
  store i8 0, ptr %108, align 1, !tbaa !18
  %109 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %109, ptr %3, align 8
  store i32 1, ptr %8, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  br label %110

110:                                              ; preds = %104, %36
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  %111 = load ptr, ptr %3, align 8
  ret ptr %111
}

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 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) }
attributes #6 = { 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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!14, !15, i64 216}
!14 = !{!"TrieNode", !7, i64 0, !7, i64 8, !15, i64 216, !10, i64 220}
!15 = !{!"_Bool", !7, i64 0}
!16 = !{i8 0, i8 2}
!17 = !{}
!18 = !{!7, !7, i64 0}
!19 = !{!14, !10, i64 220}
!20 = distinct !{!20, !12}


; 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
declare dso_local ptr @createNode(i8 noundef signext) local_unnamed_addr #0

; 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

; Function Attrs: nounwind uwtable
declare dso_local void @insert(ptr noundef, ptr noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local noundef ptr @longestCommonPrefix(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = tail call ptr @createNode(i8 noundef signext 0)
  %4 = icmp sgt i32 %1, 0
  br i1 %4, label %.lr.ph.preheader, label %._crit_edge

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

._crit_edge:                                      ; preds = %.lr.ph, %2
  %5 = getelementptr inbounds i8, ptr %3, i64 216
  %6 = load i8, ptr %5, align 8, !tbaa !5, !range !11, !noundef !12
  %7 = trunc nuw i8 %6 to i1
  br i1 %7, label %39, label %10

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next, %.lr.ph ]
  %8 = getelementptr inbounds ptr, ptr %0, i64 %indvars.iv
  %9 = load ptr, ptr %8, align 8, !tbaa !13
  tail call void @insert(ptr noundef %3, ptr noundef %9)
  %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 %._crit_edge, label %.lr.ph, !llvm.loop !15

10:                                               ; preds = %._crit_edge
  %11 = load ptr, ptr %0, align 8, !tbaa !13
  %12 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %11) #3
  %13 = add i64 %12, 1
  %14 = tail call noalias ptr @malloc(i64 noundef %13) #4
  %15 = load i8, ptr %11, align 1, !tbaa !17
  %.not40 = icmp eq i8 %15, 0
  br i1 %.not40, label %.thread, label %.lr.ph45

16:                                               ; preds = %29
  %indvars.iv.next54 = add nuw nsw i64 %indvars.iv53, 1
  %17 = getelementptr inbounds i8, ptr %11, i64 %indvars.iv.next54
  %18 = load i8, ptr %17, align 1, !tbaa !17
  %.not = icmp eq i8 %18, 0
  br i1 %.not, label %.thread.loopexit, label %.lr.ph45, !llvm.loop !18

.lr.ph45:                                         ; preds = %10, %16
  %indvars.iv53 = phi i64 [ %indvars.iv.next54, %16 ], [ 0, %10 ]
  %19 = phi i8 [ %18, %16 ], [ %15, %10 ]
  %.02843 = phi ptr [ %28, %16 ], [ %3, %10 ]
  %.02942 = phi i32 [ %30, %16 ], [ 0, %10 ]
  %20 = getelementptr inbounds i8, ptr %.02843, i64 220
  %21 = load i32, ptr %20, align 4, !tbaa !19
  %22 = icmp eq i32 %21, 1
  br i1 %22, label %23, label %.thread.loopexit.split.loop.exit

23:                                               ; preds = %.lr.ph45
  %24 = sext i8 %19 to i64
  %25 = add nsw i64 %24, -97
  %26 = getelementptr inbounds i8, ptr %.02843, i64 8
  %27 = getelementptr inbounds [26 x ptr], ptr %26, i64 0, i64 %25
  %28 = load ptr, ptr %27, align 8, !tbaa !13
  %.not35 = icmp eq ptr %28, null
  br i1 %.not35, label %.thread.loopexit.split.loop.exit55, label %29

29:                                               ; preds = %23
  %30 = add nuw nsw i32 %.02942, 1
  %31 = getelementptr inbounds i8, ptr %14, i64 %indvars.iv53
  store i8 %19, ptr %31, align 1, !tbaa !17
  %32 = getelementptr inbounds i8, ptr %28, i64 216
  %33 = load i8, ptr %32, align 8, !tbaa !5, !range !11, !noundef !12
  %34 = trunc nuw i8 %33 to i1
  br i1 %34, label %.thread.loopexit, label %16

.thread.loopexit.split.loop.exit:                 ; preds = %.lr.ph45
  %35 = trunc nuw nsw i64 %indvars.iv53 to i32
  br label %.thread.loopexit

.thread.loopexit.split.loop.exit55:               ; preds = %23
  %36 = trunc nuw nsw i64 %indvars.iv53 to i32
  br label %.thread.loopexit

.thread.loopexit:                                 ; preds = %29, %16, %.thread.loopexit.split.loop.exit55, %.thread.loopexit.split.loop.exit
  %.2.ph = phi i32 [ %35, %.thread.loopexit.split.loop.exit ], [ %36, %.thread.loopexit.split.loop.exit55 ], [ %30, %16 ], [ %30, %29 ]
  %37 = sext i32 %.2.ph to i64
  br label %.thread

.thread:                                          ; preds = %.thread.loopexit, %10
  %.2 = phi i64 [ 0, %10 ], [ %37, %.thread.loopexit ]
  %38 = getelementptr inbounds i8, ptr %14, i64 %.2
  store i8 0, ptr %38, align 1, !tbaa !17
  br label %39

39:                                               ; preds = %._crit_edge, %.thread
  %.0 = phi ptr [ %14, %.thread ], [ @.str, %._crit_edge ]
  ret ptr %.0
}

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,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 = { 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 willreturn memory(read) }
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, !9, i64 216}
!6 = !{!"TrieNode", !7, i64 0, !7, i64 8, !9, i64 216, !10, i64 220}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"_Bool", !7, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{i8 0, i8 2}
!12 = !{}
!13 = !{!14, !14, i64 0}
!14 = !{!"any pointer", !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!7, !7, i64 0}
!18 = distinct !{!18, !16}
!19 = !{!6, !10, i64 220}


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