; LLVM IR from nonstriater/Learn-Algorithms :: 9 Algorithms Job Interview/codes/1 string/revert_by_word.ll (revert_by_word)

; 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
declare dso_local void @_reverse(ptr noundef, ptr noundef) #0

; 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

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

7:                                                ; preds = %41, %1
  %8 = load ptr, ptr %4, align 8, !tbaa !5
  %9 = load i8, ptr %8, align 1, !tbaa !9
  %10 = sext i8 %9 to i32
  %11 = icmp ne i32 %10, 0
  br i1 %11, label %12, label %42

12:                                               ; preds = %7
  %13 = load ptr, ptr %4, align 8, !tbaa !5
  %14 = load i8, ptr %13, align 1, !tbaa !9
  %15 = sext i8 %14 to i32
  %16 = icmp eq i32 %15, 32
  br i1 %16, label %17, label %22

17:                                               ; preds = %12
  %18 = load ptr, ptr %4, align 8, !tbaa !5
  %19 = getelementptr inbounds i8, ptr %18, i32 1
  store ptr %19, ptr %4, align 8, !tbaa !5
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = getelementptr inbounds i8, ptr %20, i32 1
  store ptr %21, ptr %3, align 8, !tbaa !5
  br label %41

22:                                               ; preds = %12
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  %24 = load i8, ptr %23, align 1, !tbaa !9
  %25 = sext i8 %24 to i32
  %26 = icmp eq i32 %25, 32
  br i1 %26, label %32, label %27

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

32:                                               ; preds = %27, %22
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = load ptr, ptr %3, align 8, !tbaa !5
  %35 = getelementptr inbounds i8, ptr %34, i64 -1
  call void @_reverse(ptr noundef %33, ptr noundef %35)
  %36 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %36, ptr %4, align 8, !tbaa !5
  br label %40

37:                                               ; preds = %27
  %38 = load ptr, ptr %3, align 8, !tbaa !5
  %39 = getelementptr inbounds i8, ptr %38, i32 1
  store ptr %39, ptr %3, align 8, !tbaa !5
  br label %40

40:                                               ; preds = %37, %32
  br label %41

41:                                               ; preds = %40, %17
  br label %7, !llvm.loop !10

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

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 = distinct !{!10, !11}
!11 = !{!"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
declare dso_local void @_reverse(ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local noundef ptr @_revert_by_word(ptr noundef returned %0) local_unnamed_addr #0 {
  br label %.outer

.outer:                                           ; preds = %.outer.backedge, %1
  %.013.ph = phi ptr [ %0, %1 ], [ %.013.ph.be, %.outer.backedge ]
  %.0.ph = phi ptr [ %0, %1 ], [ %.0.ph.be, %.outer.backedge ]
  %2 = load i8, ptr %.0.ph, align 1, !tbaa !5
  switch i8 %2, label %3 [
    i8 0, label %.loopexit
    i8 32, label %.loopexit20
  ]

3:                                                ; preds = %.outer
  %4 = load i8, ptr %.013.ph, align 1, !tbaa !5
  switch i8 %4, label %.outer.backedge [
    i8 32, label %.peel.next
    i8 0, label %.peel.next
  ]

.peel.next:                                       ; preds = %3, %3
  %5 = getelementptr inbounds i8, ptr %.013.ph, i64 -1
  tail call void @_reverse(ptr noundef nonnull %.0.ph, ptr noundef nonnull %5)
  %6 = getelementptr inbounds i8, ptr %.013.ph, i64 -1
  br label %7

7:                                                ; preds = %.peel.next, %12
  %8 = load i8, ptr %.013.ph, align 1, !tbaa !5
  switch i8 %8, label %10 [
    i8 0, label %.loopexit
    i8 32, label %.loopexit20
  ]

.loopexit20:                                      ; preds = %7, %.outer
  %.0.lcssa17 = phi ptr [ %.0.ph, %.outer ], [ %.013.ph, %7 ]
  %9 = getelementptr inbounds i8, ptr %.0.lcssa17, i64 1
  br label %.outer.backedge

.outer.backedge:                                  ; preds = %3, %10, %.loopexit20
  %.0.ph.be = phi ptr [ %9, %.loopexit20 ], [ %.0.ph, %3 ], [ %.013.ph, %10 ]
  %.013.ph.be = getelementptr inbounds i8, ptr %.013.ph, i64 1
  br label %.outer, !llvm.loop !8

10:                                               ; preds = %7
  %11 = load i8, ptr %.013.ph, align 1, !tbaa !5
  switch i8 %11, label %.outer.backedge [
    i8 32, label %12
    i8 0, label %12
  ]

12:                                               ; preds = %10, %10
  tail call void @_reverse(ptr noundef nonnull %.013.ph, ptr noundef nonnull %6)
  br label %7, !llvm.loop !10

.loopexit:                                        ; preds = %.outer, %7
  ret ptr %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"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}
!10 = distinct !{!10, !9, !11}
!11 = !{!"llvm.loop.peeled.count", i32 1}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,LICMPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass