; LLVM IR from mbheinen/bpa-ipf-tsp :: libgui (stringpart)

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

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

; Function Attrs: nounwind uwtable
define dso_local void @stringPart(ptr noundef %0, ptr noundef %1, i32 noundef %2, i32 noundef %3) #0 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  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
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #4
  %13 = load i32, ptr %8, align 4, !tbaa !9
  %14 = load i32, ptr %7, align 4, !tbaa !9
  %15 = sub nsw i32 %13, %14
  %16 = add nsw i32 %15, 1
  store i32 %16, ptr %11, align 4, !tbaa !9
  %17 = load i32, ptr %11, align 4, !tbaa !9
  %18 = icmp slt i32 %17, 1
  br i1 %18, label %25, label %19

19:                                               ; preds = %4
  %20 = load i32, ptr %11, align 4, !tbaa !9
  %21 = icmp sgt i32 %20, 512
  br i1 %21, label %25, label %22

22:                                               ; preds = %19
  %23 = load i32, ptr %7, align 4, !tbaa !9
  %24 = icmp slt i32 %23, 0
  br i1 %24, label %25, label %44

25:                                               ; preds = %22, %19, %4
  %26 = call i32 (ptr, ...) @printf(ptr noundef @.str)
  %27 = call i32 (ptr, ...) @printf(ptr noundef @.str.1)
  %28 = call i32 (ptr, ...) @printf(ptr noundef @.str)
  %29 = load i32, ptr %11, align 4, !tbaa !9
  %30 = icmp slt i32 %29, 1
  br i1 %30, label %31, label %32

31:                                               ; preds = %25
  store i32 1, ptr %11, align 4, !tbaa !9
  br label %32

32:                                               ; preds = %31, %25
  %33 = load i32, ptr %11, align 4, !tbaa !9
  %34 = icmp sgt i32 %33, 512
  br i1 %34, label %35, label %36

35:                                               ; preds = %32
  store i32 512, ptr %11, align 4, !tbaa !9
  br label %36

36:                                               ; preds = %35, %32
  %37 = load i32, ptr %7, align 4, !tbaa !9
  %38 = icmp slt i32 %37, 0
  br i1 %38, label %39, label %40

39:                                               ; preds = %36
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %40

40:                                               ; preds = %39, %36
  %41 = load i32, ptr %11, align 4, !tbaa !9
  %42 = load i32, ptr %7, align 4, !tbaa !9
  %43 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i32 noundef %41, i32 noundef %42)
  br label %44

44:                                               ; preds = %40, %22
  store i32 0, ptr %10, align 4, !tbaa !9
  %45 = load ptr, ptr %5, align 8, !tbaa !5
  %46 = call i64 @strlen(ptr noundef %45) #5
  %47 = sub i64 %46, 1
  %48 = trunc i64 %47 to i32
  store i32 %48, ptr %12, align 4, !tbaa !9
  br label %49

49:                                               ; preds = %57, %44
  %50 = load ptr, ptr %5, align 8, !tbaa !5
  %51 = load i32, ptr %12, align 4, !tbaa !9
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds i8, ptr %50, i64 %52
  %54 = load i8, ptr %53, align 1, !tbaa !11
  %55 = sext i8 %54 to i32
  %56 = icmp eq i32 %55, 10
  br i1 %56, label %57, label %60

57:                                               ; preds = %49
  %58 = load i32, ptr %12, align 4, !tbaa !9
  %59 = add nsw i32 %58, -1
  store i32 %59, ptr %12, align 4, !tbaa !9
  br label %49, !llvm.loop !12

60:                                               ; preds = %49
  %61 = load i32, ptr %7, align 4, !tbaa !9
  store i32 %61, ptr %9, align 4, !tbaa !9
  br label %62

62:                                               ; preds = %88, %60
  %63 = load i32, ptr %9, align 4, !tbaa !9
  %64 = load i32, ptr %8, align 4, !tbaa !9
  %65 = icmp sle i32 %63, %64
  br i1 %65, label %66, label %91

66:                                               ; preds = %62
  %67 = load i32, ptr %9, align 4, !tbaa !9
  %68 = load i32, ptr %12, align 4, !tbaa !9
  %69 = icmp sle i32 %67, %68
  br i1 %69, label %70, label %81

70:                                               ; preds = %66
  %71 = load ptr, ptr %5, align 8, !tbaa !5
  %72 = load i32, ptr %9, align 4, !tbaa !9
  %73 = sext i32 %72 to i64
  %74 = getelementptr inbounds i8, ptr %71, i64 %73
  %75 = load i8, ptr %74, align 1, !tbaa !11
  %76 = load ptr, ptr %6, align 8, !tbaa !5
  %77 = load i32, ptr %10, align 4, !tbaa !9
  %78 = add nsw i32 %77, 1
  store i32 %78, ptr %10, align 4, !tbaa !9
  %79 = sext i32 %77 to i64
  %80 = getelementptr inbounds i8, ptr %76, i64 %79
  store i8 %75, ptr %80, align 1, !tbaa !11
  br label %87

81:                                               ; preds = %66
  %82 = load ptr, ptr %6, align 8, !tbaa !5
  %83 = load i32, ptr %10, align 4, !tbaa !9
  %84 = add nsw i32 %83, 1
  store i32 %84, ptr %10, align 4, !tbaa !9
  %85 = sext i32 %83 to i64
  %86 = getelementptr inbounds i8, ptr %82, i64 %85
  store i8 32, ptr %86, align 1, !tbaa !11
  br label %87

87:                                               ; preds = %81, %70
  br label %88

88:                                               ; preds = %87
  %89 = load i32, ptr %9, align 4, !tbaa !9
  %90 = add nsw i32 %89, 1
  store i32 %90, ptr %9, align 4, !tbaa !9
  br label %62, !llvm.loop !14

91:                                               ; preds = %62
  %92 = load ptr, ptr %6, align 8, !tbaa !5
  %93 = load i32, ptr %10, align 4, !tbaa !9
  %94 = sext i32 %93 to i64
  %95 = getelementptr inbounds i8, ptr %92, i64 %94
  store i8 0, ptr %95, align 1, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  ret void
}

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

declare i32 @printf(ptr noundef, ...) #2

; Function Attrs: nounwind willreturn memory(read)
declare i64 @strlen(ptr noundef) #3

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

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 = { "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 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 #4 = { nounwind }
attributes #5 = { 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/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}


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

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

; Function Attrs: nofree nounwind uwtable
define dso_local void @stringPart(ptr nocapture noundef readonly %0, ptr nocapture noundef writeonly %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = sub nsw i32 %3, %2
  %or.cond = icmp ugt i32 %5, 511
  %6 = icmp slt i32 %2, 0
  %or.cond3 = or i1 %6, %or.cond
  br i1 %or.cond3, label %7, label %15

7:                                                ; preds = %4
  %8 = icmp slt i32 %5, 0
  %9 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str)
  %10 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1)
  %11 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str)
  %12 = tail call i32 @llvm.umin.i32(i32 %5, i32 511)
  %13 = add nuw nsw i32 %12, 1
  %spec.store.select5 = select i1 %8, i32 1, i32 %13
  %spec.store.select4 = tail call i32 @llvm.smax.i32(i32 %2, i32 0)
  %14 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %spec.store.select5, i32 noundef %spec.store.select4)
  br label %15

15:                                               ; preds = %4, %7
  %.034 = phi i32 [ %spec.store.select4, %7 ], [ %2, %4 ]
  %16 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %0) #4
  %17 = trunc i64 %16 to i32
  br label %18

18:                                               ; preds = %18, %15
  %.0.in = phi i32 [ %17, %15 ], [ %.0, %18 ]
  %.0 = add i32 %.0.in, -1
  %19 = sext i32 %.0 to i64
  %20 = getelementptr inbounds i8, ptr %0, i64 %19
  %21 = load i8, ptr %20, align 1, !tbaa !5
  %22 = icmp eq i8 %21, 10
  br i1 %22, label %18, label %.preheader, !llvm.loop !8

.preheader:                                       ; preds = %18
  %.not37 = icmp sgt i32 %.034, %3
  br i1 %.not37, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %.preheader
  %23 = sext i32 %.034 to i64
  %24 = add i32 %3, 1
  %25 = sub i32 %24, %.034
  %wide.trip.count = zext i32 %25 to i64
  %xtraiter = and i64 %wide.trip.count, 1
  %26 = icmp eq i32 %25, 1
  br i1 %26, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %wide.trip.count, 4294967294
  %invariant.gep = getelementptr i8, ptr %0, i64 1
  br label %.lr.ph

.lr.ph:                                           ; preds = %33, %.lr.ph.preheader.new
  %indvars.iv40 = phi i64 [ %23, %.lr.ph.preheader.new ], [ %indvars.iv.next41.1, %33 ]
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader.new ], [ %indvars.iv.next.1, %33 ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %33 ]
  %.not36 = icmp sgt i64 %indvars.iv40, %19
  br i1 %.not36, label %.lr.ph.1, label %27

27:                                               ; preds = %.lr.ph
  %28 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv40
  %29 = load i8, ptr %28, align 1, !tbaa !5
  br label %.lr.ph.1

.lr.ph.1:                                         ; preds = %.lr.ph, %27
  %.sink = phi i8 [ %29, %27 ], [ 32, %.lr.ph ]
  %30 = getelementptr inbounds i8, ptr %1, i64 %indvars.iv
  store i8 %.sink, ptr %30, align 1
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %.not36.1.not = icmp slt i64 %indvars.iv40, %19
  br i1 %.not36.1.not, label %31, label %33

31:                                               ; preds = %.lr.ph.1
  %gep = getelementptr i8, ptr %invariant.gep, i64 %indvars.iv40
  %32 = load i8, ptr %gep, align 1, !tbaa !5
  br label %33

33:                                               ; preds = %31, %.lr.ph.1
  %.sink.1 = phi i8 [ %32, %31 ], [ 32, %.lr.ph.1 ]
  %34 = getelementptr inbounds i8, ptr %1, i64 %indvars.iv.next
  store i8 %.sink.1, ptr %34, align 1
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %indvars.iv.next41.1 = add nsw i64 %indvars.iv40, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !10

._crit_edge.loopexit.unr-lcssa:                   ; preds = %33, %.lr.ph.preheader
  %indvars.iv40.unr = phi i64 [ %23, %.lr.ph.preheader ], [ %indvars.iv.next41.1, %33 ]
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next.1, %33 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge.loopexit, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa
  %.not36.epil = icmp sgt i64 %indvars.iv40.unr, %19
  br i1 %.not36.epil, label %._crit_edge.loopexit.epilog-lcssa, label %35

35:                                               ; preds = %.lr.ph.epil
  %36 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv40.unr
  %37 = load i8, ptr %36, align 1, !tbaa !5
  br label %._crit_edge.loopexit.epilog-lcssa

._crit_edge.loopexit.epilog-lcssa:                ; preds = %35, %.lr.ph.epil
  %.sink.epil = phi i8 [ %37, %35 ], [ 32, %.lr.ph.epil ]
  %38 = getelementptr inbounds i8, ptr %1, i64 %indvars.iv.unr
  store i8 %.sink.epil, ptr %38, align 1
  br label %._crit_edge.loopexit

._crit_edge.loopexit:                             ; preds = %._crit_edge.loopexit.unr-lcssa, %._crit_edge.loopexit.epilog-lcssa
  %39 = zext nneg i32 %25 to i64
  br label %._crit_edge

._crit_edge:                                      ; preds = %.preheader, %._crit_edge.loopexit
  %.032.lcssa = phi i64 [ %39, %._crit_edge.loopexit ], [ 0, %.preheader ]
  %40 = getelementptr inbounds i8, ptr %1, i64 %.032.lcssa
  store i8 0, ptr %40, align 1, !tbaa !5
  ret void
}

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #3

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.umin.i32(i32, i32) #3

attributes #0 = { nofree 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 = { nofree nounwind "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 = { 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 #3 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = !{!"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: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,MergedLoadStoreMotionPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass