; LLVM IR from gem5/gem5 :: ext/systemc/src/sysc/utils/sc_string.ll (ZNK5sc_dt13sc_string_old6substrEii)

; 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.sc_dt::sc_string_old" = type { ptr }
%"class.sc_dt::sc_string_rep" = type { i32, i32, ptr }

@.str = external hidden unnamed_addr constant [1 x i8], align 1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZNK5sc_dt13sc_string_old6lengthEv(ptr noundef nonnull align 8 dereferenceable(8)) #0 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZNK5sc_dt13sc_string_old6substrEii(ptr dead_on_unwind noalias writable sret(%"class.sc_dt::sc_string_old") align 8 %0, ptr noundef nonnull align 8 dereferenceable(8) %1, i32 noundef %2, i32 noundef %3) #1 align 2 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store ptr %0, ptr %5, align 8
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !9
  store i32 %3, ptr %8, align 4, !tbaa !9
  %9 = load ptr, ptr %6, align 8
  %10 = load i32, ptr %7, align 4, !tbaa !9
  %11 = icmp slt i32 %10, 0
  br i1 %11, label %27, label %12

12:                                               ; preds = %4
  %13 = load i32, ptr %8, align 4, !tbaa !9
  %14 = icmp slt i32 %13, 0
  br i1 %14, label %27, label %15

15:                                               ; preds = %12
  %16 = load i32, ptr %7, align 4, !tbaa !9
  %17 = load i32, ptr %8, align 4, !tbaa !9
  %18 = icmp sgt i32 %16, %17
  br i1 %18, label %27, label %19

19:                                               ; preds = %15
  %20 = load i32, ptr %7, align 4, !tbaa !9
  %21 = call noundef i32 @_ZNK5sc_dt13sc_string_old6lengthEv(ptr noundef nonnull align 8 dereferenceable(8) %9)
  %22 = icmp sge i32 %20, %21
  br i1 %22, label %27, label %23

23:                                               ; preds = %19
  %24 = load i32, ptr %8, align 4, !tbaa !9
  %25 = call noundef i32 @_ZNK5sc_dt13sc_string_old6lengthEv(ptr noundef nonnull align 8 dereferenceable(8) %9)
  %26 = icmp sge i32 %24, %25
  br i1 %26, label %27, label %28

27:                                               ; preds = %23, %19, %15, %12, %4
  call void @_ZN5sc_dt13sc_string_oldC1EPKc(ptr noundef nonnull align 8 dereferenceable(8) %0, ptr noundef @.str)
  br label %40

28:                                               ; preds = %23
  %29 = getelementptr inbounds %"class.sc_dt::sc_string_old", ptr %9, i32 0, i32 0
  %30 = load ptr, ptr %29, align 8, !tbaa !11
  %31 = getelementptr inbounds %"class.sc_dt::sc_string_rep", ptr %30, i32 0, i32 2
  %32 = load ptr, ptr %31, align 8, !tbaa !13
  %33 = load i32, ptr %7, align 4, !tbaa !9
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds i8, ptr %32, i64 %34
  %36 = load i32, ptr %8, align 4, !tbaa !9
  %37 = load i32, ptr %7, align 4, !tbaa !9
  %38 = sub nsw i32 %36, %37
  %39 = add nsw i32 %38, 1
  call void @_ZN5sc_dt13sc_string_oldC1EPKci(ptr noundef nonnull align 8 dereferenceable(8) %0, ptr noundef %35, i32 noundef %39)
  br label %40

40:                                               ; preds = %28, %27
  ret void
}

declare void @_ZN5sc_dt13sc_string_oldC1EPKc(ptr, ptr)

declare void @_ZN5sc_dt13sc_string_oldC1EPKci(ptr, ptr, i32)

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 = { mustprogress 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"_ZTSN5sc_dt13sc_string_oldE", !6, i64 0}
!13 = !{!14, !6, i64 8}
!14 = !{!"_ZTSN5sc_dt13sc_string_repE", !10, i64 0, !10, i64 4, !6, i64 8}


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

%"class.sc_dt::sc_string_old" = type { ptr }

@.str = external hidden unnamed_addr constant [1 x i8], align 1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZNK5sc_dt13sc_string_old6lengthEv(ptr noundef nonnull align 8 dereferenceable(8)) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZNK5sc_dt13sc_string_old6substrEii(ptr dead_on_unwind noalias writable sret(%"class.sc_dt::sc_string_old") align 8 %0, ptr noundef nonnull align 8 dereferenceable(8) %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #1 align 2 {
  %5 = or i32 %3, %2
  %or.cond.not = icmp slt i32 %5, 0
  %6 = icmp sgt i32 %2, %3
  %or.cond = or i1 %6, %or.cond.not
  br i1 %or.cond, label %11, label %7

7:                                                ; preds = %4
  %8 = tail call noundef i32 @_ZNK5sc_dt13sc_string_old6lengthEv(ptr noundef nonnull align 8 dereferenceable(8) %1)
  %.not = icmp sgt i32 %8, %2
  br i1 %.not, label %9, label %11

9:                                                ; preds = %7
  %10 = tail call noundef i32 @_ZNK5sc_dt13sc_string_old6lengthEv(ptr noundef nonnull align 8 dereferenceable(8) %1)
  %.not11 = icmp sgt i32 %10, %3
  br i1 %.not11, label %12, label %11

11:                                               ; preds = %9, %7, %4
  tail call void @_ZN5sc_dt13sc_string_oldC1EPKc(ptr noundef nonnull align 8 dereferenceable(8) %0, ptr noundef nonnull @.str)
  br label %19

12:                                               ; preds = %9
  %13 = load ptr, ptr %1, align 8, !tbaa !5
  %14 = getelementptr inbounds i8, ptr %13, i64 8
  %15 = load ptr, ptr %14, align 8, !tbaa !10
  %16 = zext nneg i32 %2 to i64
  %17 = getelementptr inbounds i8, ptr %15, i64 %16
  %reass.sub = sub i32 %3, %2
  %18 = add i32 %reass.sub, 1
  tail call void @_ZN5sc_dt13sc_string_oldC1EPKci(ptr noundef nonnull align 8 dereferenceable(8) %0, ptr noundef %17, i32 noundef %18)
  br label %19

19:                                               ; preds = %12, %11
  ret void
}

declare void @_ZN5sc_dt13sc_string_oldC1EPKc(ptr, ptr) local_unnamed_addr

declare void @_ZN5sc_dt13sc_string_oldC1EPKci(ptr, ptr, i32) local_unnamed_addr

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 = { mustprogress 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, !7, i64 0}
!6 = !{!"_ZTSN5sc_dt13sc_string_oldE", !7, i64 0}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!11, !7, i64 8}
!11 = !{!"_ZTSN5sc_dt13sc_string_repE", !12, i64 0, !12, i64 4, !7, i64 8}
!12 = !{!"int", !8, i64 0}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InstCombinePass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass