; LLVM IR from AllenDowney/ExercisesInC :: examples (strsplit)

; 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 void @strsplit3(ptr noundef %0, i8 noundef signext %1, ptr noundef %2, ptr noundef %3) #0 {
  %5 = alloca ptr, align 8
  %6 = alloca i8, align 1
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i8 %1, ptr %6, align 1, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !5
  store ptr %3, ptr %8, align 8, !tbaa !5
  br label %9

9:                                                ; preds = %16, %4
  %10 = load ptr, ptr %5, align 8, !tbaa !5
  %11 = load i8, ptr %10, align 1, !tbaa !9
  %12 = sext i8 %11 to i32
  %13 = load i8, ptr %6, align 1, !tbaa !9
  %14 = sext i8 %13 to i32
  %15 = icmp ne i32 %12, %14
  br i1 %15, label %16, label %22

16:                                               ; preds = %9
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  %18 = getelementptr inbounds i8, ptr %17, i32 1
  store ptr %18, ptr %5, align 8, !tbaa !5
  %19 = load i8, ptr %17, align 1, !tbaa !9
  %20 = load ptr, ptr %7, align 8, !tbaa !5
  %21 = getelementptr inbounds i8, ptr %20, i32 1
  store ptr %21, ptr %7, align 8, !tbaa !5
  store i8 %19, ptr %20, align 1, !tbaa !9
  br label %9, !llvm.loop !10

22:                                               ; preds = %9
  %23 = load ptr, ptr %7, align 8, !tbaa !5
  store i8 0, ptr %23, align 1, !tbaa !9
  %24 = load ptr, ptr %5, align 8, !tbaa !5
  %25 = getelementptr inbounds i8, ptr %24, i32 1
  store ptr %25, ptr %5, align 8, !tbaa !5
  br label %26

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

31:                                               ; preds = %26
  %32 = load ptr, ptr %5, align 8, !tbaa !5
  %33 = getelementptr inbounds i8, ptr %32, i32 1
  store ptr %33, ptr %5, align 8, !tbaa !5
  %34 = load i8, ptr %32, align 1, !tbaa !9
  %35 = load ptr, ptr %8, align 8, !tbaa !5
  %36 = getelementptr inbounds i8, ptr %35, i32 1
  store ptr %36, ptr %8, align 8, !tbaa !5
  store i8 %34, ptr %35, align 1, !tbaa !9
  br label %26, !llvm.loop !12

37:                                               ; preds = %26
  %38 = load ptr, ptr %8, align 8, !tbaa !5
  store i8 0, ptr %38, align 1, !tbaa !9
  ret void
}

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


; 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(readwrite, inaccessiblemem: none) uwtable
define dso_local void @strsplit3(ptr nocapture noundef readonly %0, i8 noundef signext %1, ptr nocapture noundef writeonly %2, ptr nocapture noundef writeonly %3) local_unnamed_addr #0 {
  %5 = load i8, ptr %0, align 1, !tbaa !5
  %.not12 = icmp eq i8 %5, %1
  br i1 %.not12, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %4, %.lr.ph
  %6 = phi i8 [ %9, %.lr.ph ], [ %5, %4 ]
  %.0914 = phi ptr [ %8, %.lr.ph ], [ %2, %4 ]
  %.01013 = phi ptr [ %7, %.lr.ph ], [ %0, %4 ]
  %7 = getelementptr inbounds i8, ptr %.01013, i64 1
  %8 = getelementptr inbounds i8, ptr %.0914, i64 1
  store i8 %6, ptr %.0914, align 1, !tbaa !5
  %9 = load i8, ptr %7, align 1, !tbaa !5
  %.not = icmp eq i8 %9, %1
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !8

._crit_edge:                                      ; preds = %.lr.ph, %4
  %.010.lcssa = phi ptr [ %0, %4 ], [ %7, %.lr.ph ]
  %.09.lcssa = phi ptr [ %2, %4 ], [ %8, %.lr.ph ]
  store i8 0, ptr %.09.lcssa, align 1, !tbaa !5
  %.116 = getelementptr inbounds i8, ptr %.010.lcssa, i64 1
  %10 = load i8, ptr %.116, align 1, !tbaa !5
  %.not1117 = icmp eq i8 %10, 0
  br i1 %.not1117, label %._crit_edge22, label %.lr.ph21

.lr.ph21:                                         ; preds = %._crit_edge, %.lr.ph21
  %11 = phi i8 [ %13, %.lr.ph21 ], [ %10, %._crit_edge ]
  %.119 = phi ptr [ %.1, %.lr.ph21 ], [ %.116, %._crit_edge ]
  %.018 = phi ptr [ %12, %.lr.ph21 ], [ %3, %._crit_edge ]
  %12 = getelementptr inbounds i8, ptr %.018, i64 1
  store i8 %11, ptr %.018, align 1, !tbaa !5
  %.1 = getelementptr inbounds i8, ptr %.119, i64 1
  %13 = load i8, ptr %.1, align 1, !tbaa !5
  %.not11 = icmp eq i8 %13, 0
  br i1 %.not11, label %._crit_edge22, label %.lr.ph21, !llvm.loop !10

._crit_edge22:                                    ; preds = %.lr.ph21, %._crit_edge
  %.0.lcssa = phi ptr [ %3, %._crit_edge ], [ %12, %.lr.ph21 ]
  store i8 0, ptr %.0.lcssa, align 1, !tbaa !5
  ret void
}

attributes #0 = { nofree norecurse nosync nounwind memory(readwrite, 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"}
!10 = distinct !{!10, !9}


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