; LLVM IR from kaynewbie/algorithm :: algorithm/CodingInterviews (question-17)

; 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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nounwind uwtable
define dso_local zeroext i1 @increment(ptr noundef %0) #1 {
  %2 = alloca i1, align 1
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = call i32 @lengthOf(ptr noundef %9)
  store i32 %10, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  store i32 0, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %11 = load i32, ptr %4, align 4, !tbaa !9
  %12 = sub nsw i32 %11, 1
  store i32 %12, ptr %7, align 4, !tbaa !9
  br label %13

13:                                               ; preds = %61, %1
  %14 = load i32, ptr %7, align 4, !tbaa !9
  %15 = icmp sge i32 %14, 0
  br i1 %15, label %17, label %16

16:                                               ; preds = %13
  store i32 2, ptr %8, align 4
  br label %64

17:                                               ; preds = %13
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = load i32, ptr %7, align 4, !tbaa !9
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds i8, ptr %18, i64 %20
  %22 = load i8, ptr %21, align 1, !tbaa !11
  %23 = sext i8 %22 to i32
  %24 = sub nsw i32 %23, 48
  %25 = load i32, ptr %5, align 4, !tbaa !9
  %26 = add nsw i32 %24, %25
  store i32 %26, ptr %6, align 4, !tbaa !9
  store i32 0, ptr %5, align 4, !tbaa !9
  %27 = load i32, ptr %7, align 4, !tbaa !9
  %28 = load i32, ptr %4, align 4, !tbaa !9
  %29 = sub nsw i32 %28, 1
  %30 = icmp eq i32 %27, %29
  br i1 %30, label %31, label %34

31:                                               ; preds = %17
  %32 = load i32, ptr %6, align 4, !tbaa !9
  %33 = add nsw i32 %32, 1
  store i32 %33, ptr %6, align 4, !tbaa !9
  br label %34

34:                                               ; preds = %31, %17
  %35 = load i32, ptr %6, align 4, !tbaa !9
  %36 = icmp sge i32 %35, 10
  br i1 %36, label %37, label %52

37:                                               ; preds = %34
  %38 = load i32, ptr %7, align 4, !tbaa !9
  %39 = icmp eq i32 %38, 0
  br i1 %39, label %40, label %41

40:                                               ; preds = %37
  store i1 true, ptr %2, align 1
  store i32 1, ptr %8, align 4
  br label %64

41:                                               ; preds = %37
  %42 = load i32, ptr %6, align 4, !tbaa !9
  %43 = sub nsw i32 %42, 10
  store i32 %43, ptr %6, align 4, !tbaa !9
  store i32 1, ptr %5, align 4, !tbaa !9
  %44 = load i32, ptr %6, align 4, !tbaa !9
  %45 = add nsw i32 48, %44
  %46 = trunc i32 %45 to i8
  %47 = load ptr, ptr %3, align 8, !tbaa !5
  %48 = load i32, ptr %7, align 4, !tbaa !9
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds i8, ptr %47, i64 %49
  store i8 %46, ptr %50, align 1, !tbaa !11
  br label %51

51:                                               ; preds = %41
  br label %60

52:                                               ; preds = %34
  %53 = load i32, ptr %6, align 4, !tbaa !9
  %54 = add nsw i32 %53, 48
  %55 = trunc i32 %54 to i8
  %56 = load ptr, ptr %3, align 8, !tbaa !5
  %57 = load i32, ptr %7, align 4, !tbaa !9
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds i8, ptr %56, i64 %58
  store i8 %55, ptr %59, align 1, !tbaa !11
  store i32 2, ptr %8, align 4
  br label %64

60:                                               ; preds = %51
  br label %61

61:                                               ; preds = %60
  %62 = load i32, ptr %7, align 4, !tbaa !9
  %63 = add nsw i32 %62, -1
  store i32 %63, ptr %7, align 4, !tbaa !9
  br label %13, !llvm.loop !12

64:                                               ; preds = %52, %40, %16
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  %65 = load i32, ptr %8, align 4
  switch i32 %65, label %67 [
    i32 2, label %66
  ]

66:                                               ; preds = %64
  store i1 false, ptr %2, align 1
  store i32 1, ptr %8, align 4
  br label %67

67:                                               ; preds = %66, %64
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  %68 = load i1, ptr %2, align 1
  ret i1 %68
}

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @lengthOf(ptr noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 = !{!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: nounwind uwtable
define dso_local noundef zeroext i1 @increment(ptr noundef %0) local_unnamed_addr #0 {
  %2 = tail call i32 @lengthOf(ptr noundef %0)
  %3 = add nsw i32 %2, -1
  %4 = icmp sgt i32 %2, 0
  br i1 %4, label %.lr.ph.preheader, label %.loopexit

.lr.ph.preheader:                                 ; preds = %1
  %5 = zext nneg i32 %3 to i64
  %6 = getelementptr inbounds i8, ptr %0, i64 %5
  %7 = load i8, ptr %6, align 1, !tbaa !5
  %8 = sext i8 %7 to i32
  %spec.select.peel = add nsw i32 %8, -47
  %9 = icmp sgt i8 %7, 56
  br i1 %9, label %10, label %.loopexit31

10:                                               ; preds = %.lr.ph.preheader
  %11 = icmp eq i32 %3, 0
  br i1 %11, label %.loopexit, label %.lr.ph.peel.next

.lr.ph.peel.next:                                 ; preds = %10
  %12 = trunc i32 %spec.select.peel to i8
  %13 = add nuw nsw i8 %12, 38
  store i8 %13, ptr %6, align 1, !tbaa !5
  %14 = add nsw i32 %2, -2
  %15 = zext nneg i32 %14 to i64
  %16 = getelementptr inbounds i8, ptr %0, i64 %15
  %17 = load i8, ptr %16, align 1, !tbaa !5
  %18 = sext i8 %17 to i32
  %spec.select40 = add nsw i32 %18, -47
  %19 = icmp sgt i8 %17, 56
  br i1 %19, label %.lr.ph43, label %.loopexit31

.lr.ph43:                                         ; preds = %.lr.ph.peel.next, %.lr.ph
  %spec.select42 = phi i32 [ %spec.select, %.lr.ph ], [ %spec.select40, %.lr.ph.peel.next ]
  %20 = phi ptr [ %26, %.lr.ph ], [ %16, %.lr.ph.peel.next ]
  %.0202541 = phi i32 [ %24, %.lr.ph ], [ %14, %.lr.ph.peel.next ]
  %21 = icmp eq i32 %.0202541, 0
  br i1 %21, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph43
  %22 = trunc i32 %spec.select42 to i8
  %23 = add nuw nsw i8 %22, 38
  store i8 %23, ptr %20, align 1, !tbaa !5
  %24 = add nsw i32 %.0202541, -1
  %25 = zext nneg i32 %24 to i64
  %26 = getelementptr inbounds i8, ptr %0, i64 %25
  %27 = load i8, ptr %26, align 1, !tbaa !5
  %28 = sext i8 %27 to i32
  %spec.select = add nsw i32 %28, -47
  %29 = icmp sgt i8 %27, 56
  br i1 %29, label %.lr.ph43, label %.loopexit31

.loopexit31:                                      ; preds = %.lr.ph, %.lr.ph.peel.next, %.lr.ph.preheader
  %.lcssa = phi ptr [ %6, %.lr.ph.preheader ], [ %16, %.lr.ph.peel.next ], [ %26, %.lr.ph ]
  %spec.select.lcssa = phi i32 [ %spec.select.peel, %.lr.ph.preheader ], [ %spec.select40, %.lr.ph.peel.next ], [ %spec.select, %.lr.ph ]
  %30 = trunc i32 %spec.select.lcssa to i8
  %31 = add i8 %30, 48
  store i8 %31, ptr %.lcssa, align 1, !tbaa !5
  br label %.loopexit

.loopexit:                                        ; preds = %.lr.ph43, %10, %1, %.loopexit31
  %.1 = phi i1 [ false, %.loopexit31 ], [ false, %1 ], [ true, %10 ], [ true, %.lr.ph43 ]
  ret i1 %.1
}

; Function Attrs: nounwind uwtable
declare dso_local i32 @lengthOf(ptr noundef) local_unnamed_addr #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" }

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


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