; LLVM IR from kokobd/leetcode-hs :: csrc/src (p14)

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

14:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %15 = call noalias ptr @malloc(i64 noundef 1) #5
  store ptr %15, ptr %6, align 8, !tbaa !5
  %16 = load ptr, ptr %6, align 8, !tbaa !5
  %17 = getelementptr inbounds i8, ptr %16, i64 0
  store i8 0, ptr %17, align 1, !tbaa !11
  %18 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %18, ptr %3, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  br label %84

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

20:                                               ; preds = %65, %19
  br label %21

21:                                               ; preds = %20
  call void @llvm.lifetime.start.p0(i64 1, ptr %8) #4
  %22 = load ptr, ptr %4, align 8, !tbaa !5
  %23 = getelementptr inbounds ptr, ptr %22, i64 0
  %24 = load ptr, ptr %23, align 8, !tbaa !5
  %25 = load i32, ptr %7, align 4, !tbaa !9
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds i8, ptr %24, i64 %26
  %28 = load i8, ptr %27, align 1, !tbaa !11
  store i8 %28, ptr %8, align 1, !tbaa !11
  %29 = load i8, ptr %8, align 1, !tbaa !11
  %30 = sext i8 %29 to i32
  %31 = icmp eq i32 %30, 0
  br i1 %31, label %32, label %33

32:                                               ; preds = %21
  store i32 4, ptr %10, align 4
  br label %63

33:                                               ; preds = %21
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  store i32 1, ptr %9, align 4, !tbaa !9
  br label %34

34:                                               ; preds = %55, %33
  %35 = load i32, ptr %9, align 4, !tbaa !9
  %36 = load i32, ptr %5, align 4, !tbaa !9
  %37 = icmp slt i32 %35, %36
  br i1 %37, label %39, label %38

38:                                               ; preds = %34
  store i32 5, ptr %10, align 4
  br label %58

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

53:                                               ; preds = %39
  store i32 4, ptr %10, align 4
  br label %58

54:                                               ; preds = %39
  br label %55

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

58:                                               ; preds = %53, %38
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  %59 = load i32, ptr %10, align 4
  switch i32 %59, label %63 [
    i32 5, label %60
  ]

60:                                               ; preds = %58
  %61 = load i32, ptr %7, align 4, !tbaa !9
  %62 = add nsw i32 %61, 1
  store i32 %62, ptr %7, align 4, !tbaa !9
  store i32 0, ptr %10, align 4
  br label %63

63:                                               ; preds = %60, %58, %32
  call void @llvm.lifetime.end.p0(i64 1, ptr %8) #4
  %64 = load i32, ptr %10, align 4
  switch i32 %64, label %83 [
    i32 0, label %65
    i32 4, label %66
  ]

65:                                               ; preds = %63
  br label %20

66:                                               ; preds = %63
  %67 = load i32, ptr %7, align 4, !tbaa !9
  %68 = add nsw i32 %67, 1
  %69 = sext i32 %68 to i64
  %70 = mul i64 %69, 1
  %71 = call noalias ptr @malloc(i64 noundef %70) #5
  store ptr %71, ptr %11, align 8, !tbaa !5
  %72 = load ptr, ptr %11, align 8, !tbaa !5
  %73 = load ptr, ptr %4, align 8, !tbaa !5
  %74 = getelementptr inbounds ptr, ptr %73, i64 0
  %75 = load ptr, ptr %74, align 8, !tbaa !5
  %76 = load i32, ptr %7, align 4, !tbaa !9
  %77 = sext i32 %76 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %72, ptr align 1 %75, i64 %77, i1 false)
  %78 = load ptr, ptr %11, align 8, !tbaa !5
  %79 = load i32, ptr %7, align 4, !tbaa !9
  %80 = sext i32 %79 to i64
  %81 = getelementptr inbounds i8, ptr %78, i64 %80
  store i8 0, ptr %81, align 1, !tbaa !11
  %82 = load ptr, ptr %11, align 8, !tbaa !5
  store ptr %82, ptr %3, align 8
  store i32 1, ptr %10, align 4
  br label %83

83:                                               ; preds = %66, %63
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  br label %84

84:                                               ; preds = %83, %14
  %85 = load ptr, ptr %3, align 8
  ret ptr %85
}

; 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: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { nounwind }
attributes #5 = { 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"}


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

.preheader40:                                     ; preds = %2
  %4 = load ptr, ptr %0, align 8, !tbaa !5
  %5 = load i8, ptr %4, align 1, !tbaa !9
  %6 = icmp eq i8 %5, 0
  br i1 %6, label %select.unfold, label %.preheader.lr.ph

.preheader.lr.ph:                                 ; preds = %.preheader40
  %.not3045.not = icmp eq i32 %1, 1
  br i1 %.not3045.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, %._crit_edge.us
  %indvars.iv57 = phi i64 [ 0, %.preheader.us.preheader ], [ %indvars.iv.next58, %._crit_edge.us ]
  %7 = phi i8 [ %5, %.preheader.us.preheader ], [ %15, %._crit_edge.us ]
  br label %9

8:                                                ; preds = %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.us, label %9, !llvm.loop !10

9:                                                ; preds = %.preheader.us, %8
  %indvars.iv = phi i64 [ 1, %.preheader.us ], [ %indvars.iv.next, %8 ]
  %10 = getelementptr inbounds ptr, ptr %0, i64 %indvars.iv
  %11 = load ptr, ptr %10, align 8, !tbaa !5
  %12 = getelementptr inbounds i8, ptr %11, i64 %indvars.iv57
  %13 = load i8, ptr %12, align 1, !tbaa !9
  %.not.us = icmp eq i8 %13, %7
  br i1 %.not.us, label %8, label %select.unfold.loopexit52

._crit_edge.us:                                   ; preds = %8
  %indvars.iv.next58 = add nuw nsw i64 %indvars.iv57, 1
  %14 = getelementptr inbounds i8, ptr %4, i64 %indvars.iv.next58
  %15 = load i8, ptr %14, align 1, !tbaa !9
  %16 = icmp eq i8 %15, 0
  br i1 %16, label %select.unfold.loopexit53, label %.preheader.us

17:                                               ; preds = %2
  %18 = tail call noalias dereferenceable_or_null(1) ptr @malloc(i64 noundef 1) #3
  br label %30

.preheader:                                       ; preds = %.preheader.lr.ph, %.preheader
  %indvars.iv59 = phi i64 [ %indvars.iv.next60, %.preheader ], [ 0, %.preheader.lr.ph ]
  %indvars.iv.next60 = add nuw nsw i64 %indvars.iv59, 1
  %19 = getelementptr inbounds i8, ptr %4, i64 %indvars.iv.next60
  %20 = load i8, ptr %19, align 1, !tbaa !9
  %21 = icmp eq i8 %20, 0
  br i1 %21, label %select.unfold.loopexit, label %.preheader

select.unfold.loopexit:                           ; preds = %.preheader
  %22 = trunc nuw nsw i64 %indvars.iv.next60 to i32
  br label %select.unfold

select.unfold.loopexit52:                         ; preds = %9
  %23 = trunc nuw nsw i64 %indvars.iv57 to i32
  br label %select.unfold

select.unfold.loopexit53:                         ; preds = %._crit_edge.us
  %24 = trunc nuw i64 %indvars.iv.next58 to i32
  br label %select.unfold

select.unfold:                                    ; preds = %select.unfold.loopexit53, %select.unfold.loopexit52, %select.unfold.loopexit, %.preheader40
  %.02644 = phi i32 [ 0, %.preheader40 ], [ %22, %select.unfold.loopexit ], [ %23, %select.unfold.loopexit52 ], [ %24, %select.unfold.loopexit53 ]
  %25 = add nuw nsw i32 %.02644, 1
  %26 = zext nneg i32 %25 to i64
  %27 = tail call noalias ptr @malloc(i64 noundef %26) #3
  %28 = zext nneg i32 %.02644 to i64
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %27, ptr nonnull align 1 %4, i64 %28, i1 false)
  %29 = getelementptr inbounds i8, ptr %27, i64 %28
  br label %30

30:                                               ; preds = %select.unfold, %17
  %.sink = phi ptr [ %29, %select.unfold ], [ %18, %17 ]
  %.1 = phi ptr [ %27, %select.unfold ], [ %18, %17 ]
  store i8 0, ptr %.sink, align 1, !tbaa !9
  ret ptr %.1
}

; 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: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

attributes #0 = { nofree 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 nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass