; LLVM IR from next-step-for-interview-prep/Coding-Ninjas-DSA-course_code_and_notes_cpp :: Milestone-2=Intro_to_C++/questions and solutions/Lec-10=Character-arrays_and_2D-arra_df64057d (Z15reverseEachWordPc)

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

7:                                                ; preds = %78, %1
  %8 = load ptr, ptr %2, align 8, !tbaa !5
  %9 = load i32, ptr %4, align 4, !tbaa !9
  %10 = sext i32 %9 to i64
  %11 = getelementptr inbounds i8, ptr %8, i64 %10
  %12 = load i8, ptr %11, align 1, !tbaa !11
  %13 = sext i8 %12 to i32
  %14 = icmp ne i32 %13, 0
  br i1 %14, label %15, label %81

15:                                               ; preds = %7
  %16 = load i32, ptr %4, align 4, !tbaa !9
  %17 = sext i32 %16 to i64
  %18 = load ptr, ptr %2, align 8, !tbaa !5
  %19 = call i64 @strlen(ptr noundef %18) #4
  %20 = icmp ugt i64 %17, %19
  br i1 %20, label %21, label %22

21:                                               ; preds = %15
  br label %81

22:                                               ; preds = %15
  %23 = load i32, ptr %4, align 4, !tbaa !9
  store i32 %23, ptr %3, align 4, !tbaa !9
  br label %24

24:                                               ; preds = %42, %22
  %25 = load ptr, ptr %2, align 8, !tbaa !5
  %26 = load i32, ptr %4, align 4, !tbaa !9
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds i8, ptr %25, i64 %27
  %29 = load i8, ptr %28, align 1, !tbaa !11
  %30 = sext i8 %29 to i32
  %31 = icmp ne i32 %30, 32
  br i1 %31, label %32, label %40

32:                                               ; preds = %24
  %33 = load ptr, ptr %2, align 8, !tbaa !5
  %34 = load i32, ptr %4, align 4, !tbaa !9
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds i8, ptr %33, i64 %35
  %37 = load i8, ptr %36, align 1, !tbaa !11
  %38 = sext i8 %37 to i32
  %39 = icmp ne i32 %38, 0
  br label %40

40:                                               ; preds = %32, %24
  %41 = phi i1 [ false, %24 ], [ %39, %32 ]
  br i1 %41, label %42, label %45

42:                                               ; preds = %40
  %43 = load i32, ptr %4, align 4, !tbaa !9
  %44 = add nsw i32 %43, 1
  store i32 %44, ptr %4, align 4, !tbaa !9
  br label %24, !llvm.loop !12

45:                                               ; preds = %40
  %46 = load i32, ptr %4, align 4, !tbaa !9
  %47 = sub nsw i32 %46, 1
  store i32 %47, ptr %5, align 4, !tbaa !9
  br label %48

48:                                               ; preds = %52, %45
  %49 = load i32, ptr %3, align 4, !tbaa !9
  %50 = load i32, ptr %5, align 4, !tbaa !9
  %51 = icmp sle i32 %49, %50
  br i1 %51, label %52, label %78

52:                                               ; preds = %48
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %53 = load ptr, ptr %2, align 8, !tbaa !5
  %54 = load i32, ptr %3, align 4, !tbaa !9
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds i8, ptr %53, i64 %55
  %57 = load i8, ptr %56, align 1, !tbaa !11
  %58 = sext i8 %57 to i32
  store i32 %58, ptr %6, align 4, !tbaa !9
  %59 = load ptr, ptr %2, align 8, !tbaa !5
  %60 = load i32, ptr %5, align 4, !tbaa !9
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds i8, ptr %59, i64 %61
  %63 = load i8, ptr %62, align 1, !tbaa !11
  %64 = load ptr, ptr %2, align 8, !tbaa !5
  %65 = load i32, ptr %3, align 4, !tbaa !9
  %66 = sext i32 %65 to i64
  %67 = getelementptr inbounds i8, ptr %64, i64 %66
  store i8 %63, ptr %67, align 1, !tbaa !11
  %68 = load i32, ptr %6, align 4, !tbaa !9
  %69 = trunc i32 %68 to i8
  %70 = load ptr, ptr %2, align 8, !tbaa !5
  %71 = load i32, ptr %5, align 4, !tbaa !9
  %72 = sext i32 %71 to i64
  %73 = getelementptr inbounds i8, ptr %70, i64 %72
  store i8 %69, ptr %73, align 1, !tbaa !11
  %74 = load i32, ptr %5, align 4, !tbaa !9
  %75 = add nsw i32 %74, -1
  store i32 %75, ptr %5, align 4, !tbaa !9
  %76 = load i32, ptr %3, align 4, !tbaa !9
  %77 = add nsw i32 %76, 1
  store i32 %77, ptr %3, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  br label %48, !llvm.loop !14

78:                                               ; preds = %48
  %79 = load i32, ptr %4, align 4, !tbaa !9
  %80 = add nsw i32 %79, 1
  store i32 %80, ptr %4, align 4, !tbaa !9
  br label %7, !llvm.loop !15

81:                                               ; preds = %21, %7
  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 void
}

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

; Function Attrs: nounwind willreturn memory(read)
declare i64 @strlen(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 = { mustprogress 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 willreturn memory(read) "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 }
attributes #4 = { nounwind willreturn memory(read) }

!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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !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"

; Function Attrs: mustprogress nofree nounwind memory(argmem: readwrite) uwtable
define dso_local void @_Z15reverseEachWordPc(ptr nocapture noundef %0) local_unnamed_addr #0 {
  %2 = load i8, ptr %0, align 1, !tbaa !5
  %.not33 = icmp eq i8 %2, 0
  br i1 %.not33, label %._crit_edge37, label %.lr.ph36

.lr.ph36:                                         ; preds = %1, %._crit_edge
  %3 = phi i64 [ %16, %._crit_edge ], [ 0, %1 ]
  %.02534 = phi i32 [ %15, %._crit_edge ], [ 0, %1 ]
  %4 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %0) #2
  %5 = icmp ult i64 %4, %3
  br i1 %5, label %._crit_edge37, label %.preheader.preheader

.preheader.preheader:                             ; preds = %.lr.ph36
  %6 = sext i32 %.02534 to i64
  br label %.preheader

.preheader:                                       ; preds = %.preheader.preheader, %9
  %indvars.iv = phi i64 [ %6, %.preheader.preheader ], [ %indvars.iv.next, %9 ]
  %7 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv
  %8 = load i8, ptr %7, align 1, !tbaa !5
  switch i8 %8, label %9 [
    i8 32, label %.critedge
    i8 0, label %.critedge
  ]

9:                                                ; preds = %.preheader
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  br label %.preheader, !llvm.loop !8

.critedge:                                        ; preds = %.preheader, %.preheader
  %10 = trunc nsw i64 %indvars.iv to i32
  %.not29.not30 = icmp slt i32 %.02534, %10
  br i1 %.not29.not30, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %.critedge, %.lr.ph
  %indvars.iv42 = phi i64 [ %indvars.iv.next43, %.lr.ph ], [ %indvars.iv, %.critedge ]
  %indvars.iv40 = phi i64 [ %indvars.iv.next41, %.lr.ph ], [ %6, %.critedge ]
  %indvars.iv.next43 = add nsw i64 %indvars.iv42, -1
  %11 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv40
  %12 = load i8, ptr %11, align 1, !tbaa !5
  %13 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv.next43
  %14 = load i8, ptr %13, align 1, !tbaa !5
  store i8 %14, ptr %11, align 1, !tbaa !5
  store i8 %12, ptr %13, align 1, !tbaa !5
  %indvars.iv.next41 = add nsw i64 %indvars.iv40, 1
  %.not29.not = icmp slt i64 %indvars.iv.next41, %indvars.iv.next43
  br i1 %.not29.not, label %.lr.ph, label %._crit_edge, !llvm.loop !10

._crit_edge:                                      ; preds = %.lr.ph, %.critedge
  %15 = add nsw i32 %10, 1
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds i8, ptr %0, i64 %16
  %18 = load i8, ptr %17, align 1, !tbaa !5
  %.not = icmp eq i8 %18, 0
  br i1 %.not, label %._crit_edge37, label %.lr.ph36, !llvm.loop !11

._crit_edge37:                                    ; preds = %._crit_edge, %.lr.ph36, %1
  ret void
}

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #1

attributes #0 = { mustprogress nofree nounwind memory(argmem: readwrite) 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 memory(argmem: read) "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 willreturn memory(read) }

!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++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}
!10 = distinct !{!10, !9}
!11 = distinct !{!11, !9}


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