; LLVM IR from kaynewbie/algorithm :: algorithm/LeetCode/Easy (count-and-say)

; 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 [2 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local ptr @countAndSay(i32 noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %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
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #5
  store ptr @.str, ptr %4, align 8, !tbaa !9
  %11 = load i32, ptr %3, align 4, !tbaa !5
  %12 = icmp slt i32 %11, 1
  br i1 %12, label %13, label %14

13:                                               ; preds = %1
  store ptr null, ptr %2, align 8
  store i32 1, ptr %5, align 4
  br label %106

14:                                               ; preds = %1
  %15 = load i32, ptr %3, align 4, !tbaa !5
  %16 = icmp eq i32 %15, 1
  br i1 %16, label %17, label %19

17:                                               ; preds = %14
  %18 = load ptr, ptr %4, align 8, !tbaa !9
  store ptr %18, ptr %2, align 8
  store i32 1, ptr %5, align 4
  br label %106

19:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #5
  store i32 0, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #5
  store i32 0, ptr %8, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #5
  %20 = load i32, ptr %8, align 4, !tbaa !5
  %21 = add nsw i32 %20, 5
  %22 = sext i32 %21 to i64
  %23 = call noalias ptr @malloc(i64 noundef %22) #6
  store ptr %23, ptr %9, align 8, !tbaa !9
  %24 = load ptr, ptr %9, align 8, !tbaa !9
  %25 = load i32, ptr %8, align 4, !tbaa !5
  %26 = add nsw i32 %25, 5
  %27 = sext i32 %26 to i64
  call void @llvm.memset.p0.i64(ptr align 1 %24, i8 0, i64 %27, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #5
  store i32 1, ptr %10, align 4, !tbaa !5
  br label %28

28:                                               ; preds = %101, %19
  %29 = load i32, ptr %10, align 4, !tbaa !5
  %30 = load i32, ptr %3, align 4, !tbaa !5
  %31 = icmp slt i32 %29, %30
  br i1 %31, label %33, label %32

32:                                               ; preds = %28
  store i32 2, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #5
  br label %104

33:                                               ; preds = %28
  %34 = load i32, ptr %10, align 4, !tbaa !5
  %35 = icmp sgt i32 %34, 1
  br i1 %35, label %36, label %42

36:                                               ; preds = %33
  %37 = load i32, ptr %8, align 4, !tbaa !5
  %38 = mul nsw i32 %37, 2
  %39 = add nsw i32 %38, 5
  %40 = sext i32 %39 to i64
  %41 = call noalias ptr @calloc(i64 noundef %40, i64 noundef 1) #7
  store ptr %41, ptr %9, align 8, !tbaa !9
  br label %42

42:                                               ; preds = %36, %33
  store i32 0, ptr %6, align 4, !tbaa !5
  store i32 0, ptr %8, align 4, !tbaa !5
  br label %43

43:                                               ; preds = %76, %42
  %44 = load ptr, ptr %4, align 8, !tbaa !9
  %45 = load i32, ptr %6, align 4, !tbaa !5
  %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 = icmp ne i32 %49, 0
  br i1 %50, label %51, label %99

51:                                               ; preds = %43
  store i32 0, ptr %7, align 4, !tbaa !5
  br label %52

52:                                               ; preds = %73, %51
  %53 = load i32, ptr %7, align 4, !tbaa !5
  %54 = icmp eq i32 %53, 0
  br i1 %54, label %71, label %55

55:                                               ; preds = %52
  %56 = load ptr, ptr %4, align 8, !tbaa !9
  %57 = load i32, ptr %6, align 4, !tbaa !5
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds i8, ptr %56, i64 %58
  %60 = load i8, ptr %59, align 1, !tbaa !11
  %61 = sext i8 %60 to i32
  %62 = load ptr, ptr %4, align 8, !tbaa !9
  %63 = load i32, ptr %7, align 4, !tbaa !5
  %64 = load i32, ptr %6, align 4, !tbaa !5
  %65 = add nsw i32 %63, %64
  %66 = sext i32 %65 to i64
  %67 = getelementptr inbounds i8, ptr %62, i64 %66
  %68 = load i8, ptr %67, align 1, !tbaa !11
  %69 = sext i8 %68 to i32
  %70 = icmp eq i32 %61, %69
  br label %71

71:                                               ; preds = %55, %52
  %72 = phi i1 [ true, %52 ], [ %70, %55 ]
  br i1 %72, label %73, label %76

73:                                               ; preds = %71
  %74 = load i32, ptr %7, align 4, !tbaa !5
  %75 = add nsw i32 %74, 1
  store i32 %75, ptr %7, align 4, !tbaa !5
  br label %52, !llvm.loop !12

76:                                               ; preds = %71
  %77 = load i32, ptr %7, align 4, !tbaa !5
  %78 = add nsw i32 %77, 48
  %79 = trunc i32 %78 to i8
  %80 = load ptr, ptr %9, align 8, !tbaa !9
  %81 = load i32, ptr %8, align 4, !tbaa !5
  %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 ptr, ptr %4, align 8, !tbaa !9
  %85 = load i32, ptr %6, align 4, !tbaa !5
  %86 = sext i32 %85 to i64
  %87 = getelementptr inbounds i8, ptr %84, i64 %86
  %88 = load i8, ptr %87, align 1, !tbaa !11
  %89 = load ptr, ptr %9, align 8, !tbaa !9
  %90 = load i32, ptr %8, align 4, !tbaa !5
  %91 = add nsw i32 %90, 1
  %92 = sext i32 %91 to i64
  %93 = getelementptr inbounds i8, ptr %89, i64 %92
  store i8 %88, ptr %93, align 1, !tbaa !11
  %94 = load i32, ptr %8, align 4, !tbaa !5
  %95 = add nsw i32 %94, 2
  store i32 %95, ptr %8, align 4, !tbaa !5
  %96 = load i32, ptr %7, align 4, !tbaa !5
  %97 = load i32, ptr %6, align 4, !tbaa !5
  %98 = add nsw i32 %97, %96
  store i32 %98, ptr %6, align 4, !tbaa !5
  br label %43, !llvm.loop !14

99:                                               ; preds = %43
  %100 = load ptr, ptr %9, align 8, !tbaa !9
  store ptr %100, ptr %4, align 8, !tbaa !9
  br label %101

101:                                              ; preds = %99
  %102 = load i32, ptr %10, align 4, !tbaa !5
  %103 = add nsw i32 %102, 1
  store i32 %103, ptr %10, align 4, !tbaa !5
  br label %28, !llvm.loop !15

104:                                              ; preds = %32
  %105 = load ptr, ptr %4, align 8, !tbaa !9
  store ptr %105, ptr %2, align 8
  store i32 1, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #5
  br label %106

106:                                              ; preds = %104, %17, %13
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #5
  %107 = load ptr, ptr %2, align 8
  ret ptr %107
}

; 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 nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #3

; Function Attrs: nounwind allocsize(0,1)
declare noalias ptr @calloc(i64 noundef, i64 noundef) #4

; 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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #4 = { 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 #5 = { nounwind }
attributes #6 = { nounwind allocsize(0) }
attributes #7 = { nounwind allocsize(0,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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !13}
!15 = distinct !{!15, !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 [2 x i8], align 1

; Function Attrs: nofree nounwind uwtable
define dso_local ptr @countAndSay(i32 noundef %0) local_unnamed_addr #0 {
  %2 = icmp slt i32 %0, 1
  br i1 %2, label %.loopexit, label %3

3:                                                ; preds = %1
  %4 = icmp eq i32 %0, 1
  br i1 %4, label %.loopexit, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %3
  %calloc = tail call dereferenceable_or_null(5) ptr @calloc(i64 1, i64 5)
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %._crit_edge
  %.046 = phi i32 [ %29, %._crit_edge ], [ 1, %.lr.ph.preheader ]
  %.03245 = phi ptr [ %.1, %._crit_edge ], [ %calloc, %.lr.ph.preheader ]
  %.03344 = phi i64 [ %.134.lcssa, %._crit_edge ], [ 0, %.lr.ph.preheader ]
  %.03743 = phi ptr [ %.1, %._crit_edge ], [ @.str, %.lr.ph.preheader ]
  %5 = icmp ugt i32 %.046, 1
  br i1 %5, label %6, label %11

6:                                                ; preds = %.lr.ph
  %7 = shl i64 %.03344, 1
  %8 = add i64 %7, 5
  %9 = and i64 %8, 4294967295
  %10 = tail call noalias ptr @calloc(i64 noundef %9, i64 noundef 1) #2
  br label %11

11:                                               ; preds = %6, %.lr.ph
  %.1 = phi ptr [ %10, %6 ], [ %.03245, %.lr.ph ]
  %12 = load i8, ptr %.03743, align 1, !tbaa !5
  %.not40 = icmp eq i8 %12, 0
  br i1 %.not40, label %._crit_edge, label %.critedge.peel

.critedge.peel:                                   ; preds = %11, %.loopexit52
  %indvars.iv53 = phi i64 [ %indvars.iv.next54, %.loopexit52 ], [ 0, %11 ]
  %13 = phi i8 [ %28, %.loopexit52 ], [ %12, %11 ]
  %14 = phi ptr [ %27, %.loopexit52 ], [ %.03743, %11 ]
  %.03641 = phi i64 [ %16, %.loopexit52 ], [ 0, %11 ]
  %15 = and i64 %.03641, 4294967295
  br label %.critedge

.critedge:                                        ; preds = %.critedge, %.critedge.peel
  %indvars.iv = phi i64 [ 1, %.critedge.peel ], [ %indvars.iv.next, %.critedge ]
  %16 = add nuw nsw i64 %indvars.iv, %15
  %17 = getelementptr inbounds i8, ptr %.03743, i64 %16
  %18 = load i8, ptr %17, align 1, !tbaa !5
  %19 = icmp eq i8 %13, %18
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  br i1 %19, label %.critedge, label %.loopexit52, !llvm.loop !8

.loopexit52:                                      ; preds = %.critedge
  %20 = trunc i64 %indvars.iv to i8
  %21 = add i8 %20, 48
  %22 = getelementptr inbounds i8, ptr %.1, i64 %indvars.iv53
  store i8 %21, ptr %22, align 1, !tbaa !5
  %23 = load i8, ptr %14, align 1, !tbaa !5
  %24 = or disjoint i64 %indvars.iv53, 1
  %25 = getelementptr inbounds i8, ptr %.1, i64 %24
  store i8 %23, ptr %25, align 1, !tbaa !5
  %indvars.iv.next54 = add nuw nsw i64 %indvars.iv53, 2
  %26 = and i64 %16, 4294967295
  %27 = getelementptr inbounds i8, ptr %.03743, i64 %26
  %28 = load i8, ptr %27, align 1, !tbaa !5
  %.not = icmp eq i8 %28, 0
  br i1 %.not, label %._crit_edge, label %.critedge.peel, !llvm.loop !11

._crit_edge:                                      ; preds = %.loopexit52, %11
  %.134.lcssa = phi i64 [ 0, %11 ], [ %indvars.iv.next54, %.loopexit52 ]
  %29 = add nuw nsw i32 %.046, 1
  %exitcond.not = icmp eq i32 %29, %0
  br i1 %exitcond.not, label %.loopexit, label %.lr.ph, !llvm.loop !12

.loopexit:                                        ; preds = %._crit_edge, %3, %1
  %.038 = phi ptr [ null, %1 ], [ @.str, %3 ], [ %.1, %._crit_edge ]
  ret ptr %.038
}

; 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

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,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 = { nounwind allocsize(0,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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9, !10}
!9 = !{!"llvm.loop.mustprogress"}
!10 = !{!"llvm.loop.peeled.count", i32 1}
!11 = distinct !{!11, !9}
!12 = distinct !{!12, !9}


; Effective O3 passes: DSEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopFullUnrollPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass