; LLVM IR from HANDS-FREE/OpenRE :: 3_OS/STM32F1/RTOS/uCOS-III2/uC-LIB (lib_str)

; 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: 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: nounwind uwtable
define dso_local ptr @Str_Char_Replace_N(ptr noundef %0, i8 noundef signext %1, i8 noundef signext %2, i32 noundef %3) #1 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i8, align 1
  %8 = alloca i8, align 1
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i8 %1, ptr %7, align 1, !tbaa !9
  store i8 %2, ptr %8, align 1, !tbaa !9
  store i32 %3, ptr %9, align 4, !tbaa !10
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  %13 = load ptr, ptr %6, align 8, !tbaa !5
  %14 = icmp eq ptr %13, null
  br i1 %14, label %15, label %16

15:                                               ; preds = %4
  store ptr null, ptr %5, align 8
  store i32 1, ptr %12, align 4
  br label %53

16:                                               ; preds = %4
  %17 = load i32, ptr %9, align 4, !tbaa !10
  %18 = icmp ult i32 %17, 1
  br i1 %18, label %19, label %20

19:                                               ; preds = %16
  store ptr null, ptr %5, align 8
  store i32 1, ptr %12, align 4
  br label %53

20:                                               ; preds = %16
  %21 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %21, ptr %10, align 8, !tbaa !5
  %22 = load i32, ptr %9, align 4, !tbaa !10
  store i32 %22, ptr %11, align 4, !tbaa !10
  br label %23

23:                                               ; preds = %46, %20
  %24 = load ptr, ptr %10, align 8, !tbaa !5
  %25 = icmp ne ptr %24, null
  br i1 %25, label %26, label %34

26:                                               ; preds = %23
  %27 = load ptr, ptr %10, align 8, !tbaa !5
  %28 = load i8, ptr %27, align 1, !tbaa !9
  %29 = sext i8 %28 to i32
  %30 = icmp ne i32 %29, 0
  br i1 %30, label %31, label %34

31:                                               ; preds = %26
  %32 = load i32, ptr %11, align 4, !tbaa !10
  %33 = icmp ugt i32 %32, 0
  br label %34

34:                                               ; preds = %31, %26, %23
  %35 = phi i1 [ false, %26 ], [ false, %23 ], [ %33, %31 ]
  br i1 %35, label %36, label %51

36:                                               ; preds = %34
  %37 = load ptr, ptr %10, align 8, !tbaa !5
  %38 = load i8, ptr %37, align 1, !tbaa !9
  %39 = sext i8 %38 to i32
  %40 = load i8, ptr %7, align 1, !tbaa !9
  %41 = sext i8 %40 to i32
  %42 = icmp eq i32 %39, %41
  br i1 %42, label %43, label %46

43:                                               ; preds = %36
  %44 = load i8, ptr %8, align 1, !tbaa !9
  %45 = load ptr, ptr %10, align 8, !tbaa !5
  store i8 %44, ptr %45, align 1, !tbaa !9
  br label %46

46:                                               ; preds = %43, %36
  %47 = load ptr, ptr %10, align 8, !tbaa !5
  %48 = getelementptr inbounds i8, ptr %47, i32 1
  store ptr %48, ptr %10, align 8, !tbaa !5
  %49 = load i32, ptr %11, align 4, !tbaa !10
  %50 = add i32 %49, -1
  store i32 %50, ptr %11, align 4, !tbaa !10
  br label %23, !llvm.loop !12

51:                                               ; preds = %34
  %52 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %52, ptr %5, align 8
  store i32 1, ptr %12, align 4
  br label %53

53:                                               ; preds = %51, %19, %15
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  %54 = load ptr, ptr %5, align 8
  ret ptr %54
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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/C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"int", !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"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: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local noundef ptr @Str_Char_Replace_N(ptr noundef %0, i8 noundef signext %1, i8 noundef signext %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = icmp eq ptr %0, null
  %6 = icmp eq i32 %3, 0
  %or.cond20 = or i1 %5, %6
  br i1 %or.cond20, label %.critedge, label %.preheader

.preheader:                                       ; preds = %4
  %7 = load i8, ptr %0, align 1, !tbaa !5
  %.not = icmp eq i8 %7, 0
  br i1 %.not, label %.critedge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %11
  %8 = phi i8 [ %14, %11 ], [ %7, %.preheader ]
  %.022 = phi i32 [ %13, %11 ], [ %3, %.preheader ]
  %.01521 = phi ptr [ %12, %11 ], [ %0, %.preheader ]
  %9 = icmp eq i8 %8, %1
  br i1 %9, label %10, label %11

10:                                               ; preds = %.lr.ph
  store i8 %2, ptr %.01521, align 1, !tbaa !5
  br label %11

11:                                               ; preds = %10, %.lr.ph
  %12 = getelementptr inbounds i8, ptr %.01521, i64 1
  %13 = add i32 %.022, -1
  %14 = load i8, ptr %12, align 1, !tbaa !5
  %15 = icmp ne i8 %14, 0
  %16 = icmp ne i32 %13, 0
  %or.cond = select i1 %15, i1 %16, i1 false
  br i1 %or.cond, label %.lr.ph, label %.critedge, !llvm.loop !8

.critedge:                                        ; preds = %11, %.preheader, %4
  %.016 = phi ptr [ null, %4 ], [ %0, %.preheader ], [ %0, %11 ]
  ret ptr %.016
}

attributes #0 = { nofree norecurse nosync nounwind memory(readwrite, 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, !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass