; LLVM IR from DimitrisJim/leetcode_solutions :: C/Algorithms (58)

; 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 i32 @lengthOfLastWord(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  br label %4

4:                                                ; preds = %42, %1
  %5 = load ptr, ptr %2, align 8, !tbaa !5
  %6 = load i8, ptr %5, align 1, !tbaa !9
  %7 = icmp ne i8 %6, 0
  br i1 %7, label %8, label %43

8:                                                ; preds = %4
  store i32 0, ptr %3, align 4, !tbaa !10
  br label %9

9:                                                ; preds = %21, %8
  %10 = load ptr, ptr %2, align 8, !tbaa !5
  %11 = load i8, ptr %10, align 1, !tbaa !9
  %12 = sext i8 %11 to i32
  %13 = icmp ne i32 %12, 0
  br i1 %13, label %14, label %19

14:                                               ; preds = %9
  %15 = load ptr, ptr %2, align 8, !tbaa !5
  %16 = load i8, ptr %15, align 1, !tbaa !9
  %17 = sext i8 %16 to i32
  %18 = icmp ne i32 %17, 32
  br label %19

19:                                               ; preds = %14, %9
  %20 = phi i1 [ false, %9 ], [ %18, %14 ]
  br i1 %20, label %21, label %26

21:                                               ; preds = %19
  %22 = load ptr, ptr %2, align 8, !tbaa !5
  %23 = getelementptr inbounds i8, ptr %22, i32 1
  store ptr %23, ptr %2, align 8, !tbaa !5
  %24 = load i32, ptr %3, align 4, !tbaa !10
  %25 = add nsw i32 %24, 1
  store i32 %25, ptr %3, align 4, !tbaa !10
  br label %9, !llvm.loop !12

26:                                               ; preds = %19
  br label %27

27:                                               ; preds = %39, %26
  %28 = load ptr, ptr %2, align 8, !tbaa !5
  %29 = load i8, ptr %28, align 1, !tbaa !9
  %30 = sext i8 %29 to i32
  %31 = icmp ne i32 %30, 0
  br i1 %31, label %32, label %37

32:                                               ; preds = %27
  %33 = load ptr, ptr %2, align 8, !tbaa !5
  %34 = load i8, ptr %33, align 1, !tbaa !9
  %35 = sext i8 %34 to i32
  %36 = icmp eq i32 %35, 32
  br label %37

37:                                               ; preds = %32, %27
  %38 = phi i1 [ false, %27 ], [ %36, %32 ]
  br i1 %38, label %39, label %42

39:                                               ; preds = %37
  %40 = load ptr, ptr %2, align 8, !tbaa !5
  %41 = getelementptr inbounds i8, ptr %40, i32 1
  store ptr %41, ptr %2, align 8, !tbaa !5
  br label %27, !llvm.loop !14

42:                                               ; preds = %37
  br label %4, !llvm.loop !15

43:                                               ; preds = %4
  %44 = load i32, ptr %3, align 4, !tbaa !10
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  ret i32 %44
}

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

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

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

; Function Attrs: nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) uwtable
define dso_local i32 @lengthOfLastWord(ptr nocapture noundef readonly %0) local_unnamed_addr #0 {
  %.pr = load i8, ptr %0, align 1, !tbaa !5
  %.not17 = icmp eq i8 %.pr, 0
  br i1 %.not17, label %.critedge2._crit_edge, label %.preheader

.preheader:                                       ; preds = %1, %.preheader.backedge
  %2 = phi i8 [ %.be, %.preheader.backedge ], [ %.pr, %1 ]
  %.111 = phi ptr [ %.111.be, %.preheader.backedge ], [ %0, %1 ]
  %.1 = phi i32 [ %.1.be, %.preheader.backedge ], [ 0, %1 ]
  switch i8 %2, label %3 [
    i8 0, label %.critedge.preheader
    i8 32, label %.critedge.preheader
  ]

.critedge.preheader:                              ; preds = %.preheader, %.preheader
  br label %.critedge

3:                                                ; preds = %.preheader
  %4 = getelementptr inbounds i8, ptr %.111, i64 1
  %5 = add nuw nsw i32 %.1, 1
  %.pre = load i8, ptr %4, align 1, !tbaa !5
  br label %.preheader.backedge

.preheader.backedge:                              ; preds = %.critedge, %3
  %.be = phi i8 [ %.pre, %3 ], [ %6, %.critedge ]
  %.111.be = phi ptr [ %4, %3 ], [ %.2, %.critedge ]
  %.1.be = phi i32 [ %5, %3 ], [ 0, %.critedge ]
  br label %.preheader, !llvm.loop !8

.critedge:                                        ; preds = %.critedge.preheader, %.critedge
  %.2 = phi ptr [ %7, %.critedge ], [ %.111, %.critedge.preheader ]
  %6 = load i8, ptr %.2, align 1, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %.2, i64 1
  switch i8 %6, label %.preheader.backedge [
    i8 32, label %.critedge
    i8 0, label %.critedge2._crit_edge
  ]

.critedge2._crit_edge:                            ; preds = %.critedge, %1
  %.0.lcssa = phi i32 [ undef, %1 ], [ %.1, %.critedge ]
  ret i32 %.0.lcssa
}

attributes #0 = { nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) 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"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}


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