; LLVM IR from SSBMTonberry/pocketlzma :: ref_spec/lzma1900/CPP/Common/MyString.ll (ZNK7AString21ReverseFind_PathSeparEv)

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

%class.AString = type { ptr, i32, i32 }

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

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_ZNK7AString21ReverseFind_PathSeparEv(ptr noundef nonnull align 8 dereferenceable(16) %0) #1 align 2 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i8, align 1
  %6 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  %7 = load ptr, ptr %3, align 8
  %8 = getelementptr inbounds %class.AString, ptr %7, i32 0, i32 1
  %9 = load i32, ptr %8, align 8, !tbaa !9
  %10 = icmp eq i32 %9, 0
  br i1 %10, label %11, label %12

11:                                               ; preds = %1
  store i32 -1, ptr %2, align 4
  br label %47

12:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  %13 = getelementptr inbounds %class.AString, ptr %7, i32 0, i32 0
  %14 = load ptr, ptr %13, align 8, !tbaa !12
  %15 = getelementptr inbounds %class.AString, ptr %7, i32 0, i32 1
  %16 = load i32, ptr %15, align 8, !tbaa !9
  %17 = zext i32 %16 to i64
  %18 = getelementptr inbounds i8, ptr %14, i64 %17
  %19 = getelementptr inbounds i8, ptr %18, i64 -1
  store ptr %19, ptr %4, align 8, !tbaa !5
  br label %20

20:                                               ; preds = %45, %12
  call void @llvm.lifetime.start.p0(i64 1, ptr %5) #2
  %21 = load ptr, ptr %4, align 8, !tbaa !5
  %22 = load i8, ptr %21, align 1, !tbaa !13
  store i8 %22, ptr %5, align 1, !tbaa !13
  %23 = load i8, ptr %5, align 1, !tbaa !13
  %24 = sext i8 %23 to i32
  %25 = icmp eq i32 %24, 47
  br i1 %25, label %26, label %34

26:                                               ; preds = %20
  %27 = load ptr, ptr %4, align 8, !tbaa !5
  %28 = getelementptr inbounds %class.AString, ptr %7, i32 0, i32 0
  %29 = load ptr, ptr %28, align 8, !tbaa !12
  %30 = ptrtoint ptr %27 to i64
  %31 = ptrtoint ptr %29 to i64
  %32 = sub i64 %30, %31
  %33 = trunc i64 %32 to i32
  store i32 %33, ptr %2, align 4
  store i32 1, ptr %6, align 4
  br label %43

34:                                               ; preds = %20
  %35 = load ptr, ptr %4, align 8, !tbaa !5
  %36 = getelementptr inbounds %class.AString, ptr %7, i32 0, i32 0
  %37 = load ptr, ptr %36, align 8, !tbaa !12
  %38 = icmp eq ptr %35, %37
  br i1 %38, label %39, label %40

39:                                               ; preds = %34
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %6, align 4
  br label %43

40:                                               ; preds = %34
  %41 = load ptr, ptr %4, align 8, !tbaa !5
  %42 = getelementptr inbounds i8, ptr %41, i32 -1
  store ptr %42, ptr %4, align 8, !tbaa !5
  store i32 0, ptr %6, align 4
  br label %43

43:                                               ; preds = %40, %39, %26
  call void @llvm.lifetime.end.p0(i64 1, ptr %5) #2
  %44 = load i32, ptr %6, align 4
  switch i32 %44, label %46 [
    i32 0, label %45
  ]

45:                                               ; preds = %43
  br label %20, !llvm.loop !14

46:                                               ; preds = %43
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  br label %47

47:                                               ; preds = %46, %11
  %48 = load i32, ptr %2, align 4
  ret i32 %48
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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++ TBAA"}
!9 = !{!10, !11, i64 8}
!10 = !{!"_ZTS7AString", !6, i64 0, !11, i64 8, !11, i64 12}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !6, i64 0}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"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: mustprogress nofree norecurse nosync nounwind willreturn memory(read, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_ZNK7AString21ReverseFind_PathSeparEv(ptr nocapture noundef nonnull readonly align 8 dereferenceable(16) %0) local_unnamed_addr #0 align 2 {
  %2 = getelementptr inbounds i8, ptr %0, i64 8
  %3 = load i32, ptr %2, align 8, !tbaa !5
  %4 = icmp eq i32 %3, 0
  br i1 %4, label %.thread, label %5

5:                                                ; preds = %1
  %6 = load ptr, ptr %0, align 8, !tbaa !11
  %7 = zext i32 %3 to i64
  %8 = getelementptr inbounds i8, ptr %6, i64 %7
  br label %9

9:                                                ; preds = %17, %5
  %.pn = phi ptr [ %8, %5 ], [ %.08, %17 ]
  %.08 = getelementptr inbounds i8, ptr %.pn, i64 -1
  %10 = load i8, ptr %.08, align 1, !tbaa !12
  %11 = icmp eq i8 %10, 47
  br i1 %11, label %12, label %17

12:                                               ; preds = %9
  %13 = ptrtoint ptr %.08 to i64
  %14 = ptrtoint ptr %6 to i64
  %15 = sub i64 %13, %14
  %16 = trunc i64 %15 to i32
  br label %.thread

17:                                               ; preds = %9
  %18 = icmp eq ptr %.08, %6
  br i1 %18, label %.thread, label %9, !llvm.loop !13

.thread:                                          ; preds = %17, %12, %1
  %.2 = phi i32 [ -1, %1 ], [ %16, %12 ], [ -1, %17 ]
  ret i32 %.2
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn 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, !10, i64 8}
!6 = !{!"_ZTS7AString", !7, i64 0, !10, i64 8, !10, i64 12}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!6, !7, i64 0}
!12 = !{!8, !8, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


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