; LLVM IR from wuzhouhui/leetcode :: 38_count_and_say (countAndSay)

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

@res = external hidden global ptr, align 8
@p = external hidden global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local ptr @countAndSay(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i8, align 1
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 1, ptr %7) #3
  store ptr null, ptr @res, align 8, !tbaa !9
  store i32 1, ptr %3, align 4, !tbaa !5
  br label %8

8:                                                ; preds = %80, %1
  %9 = load i32, ptr %3, align 4, !tbaa !5
  %10 = load i32, ptr %2, align 4, !tbaa !5
  %11 = icmp sle i32 %9, %10
  br i1 %11, label %12, label %83

12:                                               ; preds = %8
  %13 = load i32, ptr %3, align 4, !tbaa !5
  %14 = icmp eq i32 %13, 1
  br i1 %14, label %15, label %21

15:                                               ; preds = %12
  call void @add_one(i8 noundef signext 49)
  %16 = load ptr, ptr @res, align 8, !tbaa !9
  %17 = load i32, ptr @p, align 4, !tbaa !5
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds i8, ptr %16, i64 %18
  store i8 0, ptr %19, align 1, !tbaa !11
  %20 = load ptr, ptr @res, align 8, !tbaa !9
  store ptr %20, ptr %6, align 8, !tbaa !9
  br label %80

21:                                               ; preds = %12
  store ptr null, ptr @res, align 8, !tbaa !9
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %22

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

29:                                               ; preds = %22
  %30 = load ptr, ptr %6, align 8, !tbaa !9
  %31 = load i32, ptr %4, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds i8, ptr %30, i64 %32
  %34 = load i8, ptr %33, align 1, !tbaa !11
  store i8 %34, ptr %7, align 1, !tbaa !11
  %35 = load i32, ptr %4, align 4, !tbaa !5
  %36 = add nsw i32 %35, 1
  store i32 %36, ptr %4, align 4, !tbaa !5
  store i32 1, ptr %5, align 4, !tbaa !5
  br label %37

37:                                               ; preds = %65, %29
  %38 = load ptr, ptr %6, align 8, !tbaa !9
  %39 = load i32, ptr %4, align 4, !tbaa !5
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds i8, ptr %38, i64 %40
  %42 = load i8, ptr %41, align 1, !tbaa !11
  %43 = sext i8 %42 to i32
  %44 = icmp ne i32 %43, 0
  br i1 %44, label %45, label %60

45:                                               ; preds = %37
  %46 = load ptr, ptr %6, align 8, !tbaa !9
  %47 = load i32, ptr %4, align 4, !tbaa !5
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds i8, ptr %46, i64 %48
  %50 = load i8, ptr %49, align 1, !tbaa !11
  %51 = sext i8 %50 to i32
  %52 = load ptr, ptr %6, align 8, !tbaa !9
  %53 = load i32, ptr %4, align 4, !tbaa !5
  %54 = sub nsw i32 %53, 1
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds i8, ptr %52, i64 %55
  %57 = load i8, ptr %56, align 1, !tbaa !11
  %58 = sext i8 %57 to i32
  %59 = icmp eq i32 %51, %58
  br label %60

60:                                               ; preds = %45, %37
  %61 = phi i1 [ false, %37 ], [ %59, %45 ]
  br i1 %61, label %62, label %68

62:                                               ; preds = %60
  %63 = load i32, ptr %5, align 4, !tbaa !5
  %64 = add nsw i32 %63, 1
  store i32 %64, ptr %5, align 4, !tbaa !5
  br label %65

65:                                               ; preds = %62
  %66 = load i32, ptr %4, align 4, !tbaa !5
  %67 = add nsw i32 %66, 1
  store i32 %67, ptr %4, align 4, !tbaa !5
  br label %37, !llvm.loop !12

68:                                               ; preds = %60
  %69 = load i32, ptr %5, align 4, !tbaa !5
  %70 = add nsw i32 %69, 48
  %71 = trunc i32 %70 to i8
  call void @add_one(i8 noundef signext %71)
  %72 = load i8, ptr %7, align 1, !tbaa !11
  call void @add_one(i8 noundef signext %72)
  br label %22, !llvm.loop !14

73:                                               ; preds = %22
  %74 = load ptr, ptr %6, align 8, !tbaa !9
  call void @free(ptr noundef %74) #3
  %75 = load ptr, ptr @res, align 8, !tbaa !9
  %76 = load i32, ptr @p, align 4, !tbaa !5
  %77 = sext i32 %76 to i64
  %78 = getelementptr inbounds i8, ptr %75, i64 %77
  store i8 0, ptr %78, align 1, !tbaa !11
  %79 = load ptr, ptr @res, align 8, !tbaa !9
  store ptr %79, ptr %6, align 8, !tbaa !9
  br label %80

80:                                               ; preds = %73, %15
  %81 = load i32, ptr %3, align 4, !tbaa !5
  %82 = add nsw i32 %81, 1
  store i32 %82, ptr %3, align 4, !tbaa !5
  br label %8, !llvm.loop !15

83:                                               ; preds = %8
  %84 = load ptr, ptr @res, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 1, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  ret ptr %84
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nounwind uwtable
declare hidden void @add_one(i8 noundef signext) #0

; Function Attrs: nounwind
declare void @free(ptr noundef) #2

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

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

@res = external hidden local_unnamed_addr global ptr, align 8
@p = external hidden local_unnamed_addr global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local ptr @countAndSay(i32 noundef %0) local_unnamed_addr #0 {
  store ptr null, ptr @res, align 8, !tbaa !5
  %.not38 = icmp slt i32 %0, 1
  br i1 %.not38, label %._crit_edge43, label %.lr.ph42

.lr.ph42:                                         ; preds = %1, %24
  %.040 = phi i32 [ %29, %24 ], [ 1, %1 ]
  %.01939 = phi ptr [ %.1, %24 ], [ undef, %1 ]
  %2 = icmp eq i32 %.040, 1
  br i1 %2, label %3, label %4

3:                                                ; preds = %.lr.ph42
  tail call void @add_one(i8 noundef signext 49)
  br label %24

4:                                                ; preds = %.lr.ph42
  store ptr null, ptr @res, align 8, !tbaa !5
  %5 = load i8, ptr %.01939, align 1, !tbaa !9
  %.not2536 = icmp eq i8 %5, 0
  br i1 %.not2536, label %._crit_edge, label %.preheader

.preheader:                                       ; preds = %4, %.critedge
  %6 = phi i8 [ %23, %.critedge ], [ %5, %4 ]
  %.02137 = phi i32 [ %.122.lcssa, %.critedge ], [ 0, %4 ]
  %.12227 = add nsw i32 %.02137, 1
  %7 = sext i32 %.12227 to i64
  %8 = getelementptr inbounds i8, ptr %.01939, i64 %7
  %9 = load i8, ptr %8, align 1, !tbaa !9
  %.not2628 = icmp eq i8 %9, 0
  br i1 %.not2628, label %.critedge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %.preheader
  %10 = sext i32 %.02137 to i64
  %11 = add nsw i64 %10, 1
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %16
  %indvars.iv44 = phi i64 [ %10, %.lr.ph.preheader ], [ %indvars.iv.next45, %16 ]
  %indvars.iv = phi i64 [ %11, %.lr.ph.preheader ], [ %indvars.iv.next, %16 ]
  %12 = phi i8 [ %9, %.lr.ph.preheader ], [ %19, %16 ]
  %.12231 = phi i32 [ %.12227, %.lr.ph.preheader ], [ %.122, %16 ]
  %.02030 = phi i8 [ 1, %.lr.ph.preheader ], [ %17, %16 ]
  %13 = getelementptr inbounds i8, ptr %.01939, i64 %indvars.iv44
  %14 = load i8, ptr %13, align 1, !tbaa !9
  %15 = icmp eq i8 %12, %14
  br i1 %15, label %16, label %.critedge.loopexit.split.loop.exit46

16:                                               ; preds = %.lr.ph
  %17 = add i8 %.02030, 1
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %.122 = add nsw i32 %.12231, 1
  %18 = getelementptr inbounds i8, ptr %.01939, i64 %indvars.iv.next
  %19 = load i8, ptr %18, align 1, !tbaa !9
  %.not26 = icmp eq i8 %19, 0
  %indvars.iv.next45 = add nsw i64 %indvars.iv44, 1
  br i1 %.not26, label %.critedge.loopexit, label %.lr.ph, !llvm.loop !10

.critedge.loopexit.split.loop.exit46:             ; preds = %.lr.ph
  %20 = trunc nsw i64 %indvars.iv to i32
  br label %.critedge.loopexit

.critedge.loopexit:                               ; preds = %16, %.critedge.loopexit.split.loop.exit46
  %.020.lcssa.ph = phi i8 [ %.02030, %.critedge.loopexit.split.loop.exit46 ], [ %17, %16 ]
  %.122.lcssa.ph = phi i32 [ %20, %.critedge.loopexit.split.loop.exit46 ], [ %.122, %16 ]
  %.pre = sext i32 %.122.lcssa.ph to i64
  br label %.critedge

.critedge:                                        ; preds = %.critedge.loopexit, %.preheader
  %.pre-phi = phi i64 [ %.pre, %.critedge.loopexit ], [ %7, %.preheader ]
  %.020.lcssa = phi i8 [ %.020.lcssa.ph, %.critedge.loopexit ], [ 1, %.preheader ]
  %.122.lcssa = phi i32 [ %.122.lcssa.ph, %.critedge.loopexit ], [ %.12227, %.preheader ]
  %21 = add i8 %.020.lcssa, 48
  tail call void @add_one(i8 noundef signext %21)
  tail call void @add_one(i8 noundef signext %6)
  %22 = getelementptr inbounds i8, ptr %.01939, i64 %.pre-phi
  %23 = load i8, ptr %22, align 1, !tbaa !9
  %.not25 = icmp eq i8 %23, 0
  br i1 %.not25, label %._crit_edge, label %.preheader, !llvm.loop !12

._crit_edge:                                      ; preds = %.critedge, %4
  tail call void @free(ptr noundef nonnull %.01939) #2
  br label %24

24:                                               ; preds = %._crit_edge, %3
  %25 = load ptr, ptr @res, align 8, !tbaa !5
  %26 = load i32, ptr @p, align 4, !tbaa !13
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds i8, ptr %25, i64 %27
  store i8 0, ptr %28, align 1, !tbaa !9
  %.1 = load ptr, ptr @res, align 8, !tbaa !5
  %29 = add nuw i32 %.040, 1
  %exitcond.not = icmp eq i32 %.040, %0
  br i1 %exitcond.not, label %._crit_edge43, label %.lr.ph42, !llvm.loop !15

._crit_edge43:                                    ; preds = %24, %1
  %30 = phi ptr [ null, %1 ], [ %.1, %24 ]
  ret ptr %30
}

; Function Attrs: nounwind uwtable
declare hidden void @add_one(i8 noundef signext) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #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 = { mustprogress nounwind willreturn allockind("free") 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 #2 = { nounwind }

!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}
!13 = !{!14, !14, i64 0}
!14 = !{!"int", !7, i64 0}
!15 = distinct !{!15, !11}


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