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

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

10:                                               ; preds = %1
  store ptr null, ptr %2, align 8
  store i32 1, ptr %6, align 4
  br label %70

11:                                               ; preds = %1
  br label %12

12:                                               ; preds = %25, %11
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = load i8, ptr %13, align 1, !tbaa !11
  %15 = sext i8 %14 to i32
  %16 = icmp ne i32 %15, 0
  br i1 %16, label %17, label %28

17:                                               ; preds = %12
  %18 = load ptr, ptr %5, align 8, !tbaa !5
  %19 = load i8, ptr %18, align 1, !tbaa !11
  %20 = sext i8 %19 to i32
  %21 = icmp eq i32 %20, 32
  br i1 %21, label %22, label %25

22:                                               ; preds = %17
  %23 = load i32, ptr %4, align 4, !tbaa !9
  %24 = add nsw i32 %23, 1
  store i32 %24, ptr %4, align 4, !tbaa !9
  br label %25

25:                                               ; preds = %22, %17
  %26 = load ptr, ptr %5, align 8, !tbaa !5
  %27 = getelementptr inbounds i8, ptr %26, i32 1
  store ptr %27, ptr %5, align 8, !tbaa !5
  br label %12, !llvm.loop !12

28:                                               ; preds = %12
  br label %29

29:                                               ; preds = %65, %28
  %30 = load i32, ptr %4, align 4, !tbaa !9
  %31 = icmp ne i32 %30, 0
  br i1 %31, label %32, label %68

32:                                               ; preds = %29
  %33 = load ptr, ptr %5, align 8, !tbaa !5
  %34 = load i8, ptr %33, align 1, !tbaa !11
  %35 = sext i8 %34 to i32
  %36 = icmp ne i32 %35, 32
  br i1 %36, label %37, label %45

37:                                               ; preds = %32
  %38 = load ptr, ptr %5, align 8, !tbaa !5
  %39 = load i8, ptr %38, align 1, !tbaa !11
  %40 = load ptr, ptr %5, align 8, !tbaa !5
  %41 = load i32, ptr %4, align 4, !tbaa !9
  %42 = mul nsw i32 2, %41
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds i8, ptr %40, i64 %43
  store i8 %39, ptr %44, align 1, !tbaa !11
  br label %65

45:                                               ; preds = %32
  %46 = load ptr, ptr %5, align 8, !tbaa !5
  %47 = load i32, ptr %4, align 4, !tbaa !9
  %48 = mul nsw i32 2, %47
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds i8, ptr %46, i64 %49
  store i8 48, ptr %50, align 1, !tbaa !11
  %51 = load ptr, ptr %5, align 8, !tbaa !5
  %52 = load i32, ptr %4, align 4, !tbaa !9
  %53 = mul nsw i32 2, %52
  %54 = sext i32 %53 to i64
  %55 = getelementptr inbounds i8, ptr %51, i64 %54
  %56 = getelementptr inbounds i8, ptr %55, i64 -1
  store i8 50, ptr %56, align 1, !tbaa !11
  %57 = load ptr, ptr %5, align 8, !tbaa !5
  %58 = load i32, ptr %4, align 4, !tbaa !9
  %59 = mul nsw i32 2, %58
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds i8, ptr %57, i64 %60
  %62 = getelementptr inbounds i8, ptr %61, i64 -2
  store i8 37, ptr %62, align 1, !tbaa !11
  %63 = load i32, ptr %4, align 4, !tbaa !9
  %64 = add nsw i32 %63, -1
  store i32 %64, ptr %4, align 4, !tbaa !9
  br label %65

65:                                               ; preds = %45, %37
  %66 = load ptr, ptr %5, align 8, !tbaa !5
  %67 = getelementptr inbounds i8, ptr %66, i32 -1
  store ptr %67, ptr %5, align 8, !tbaa !5
  br label %29, !llvm.loop !14

68:                                               ; preds = %29
  %69 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %69, ptr %2, align 8
  store i32 1, ptr %6, align 4
  br label %70

70:                                               ; preds = %68, %10
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  %71 = load ptr, ptr %2, align 8
  ret ptr %71
}

; 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

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 = !{!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"

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local noundef ptr @replace_blank(ptr noundef returned %0) local_unnamed_addr #0 {
  %2 = icmp eq ptr %0, null
  br i1 %2, label %.loopexit, label %.preheader27

.preheader27:                                     ; preds = %1, %6
  %.020 = phi i32 [ %.121, %6 ], [ 0, %1 ]
  %.0 = phi ptr [ %7, %6 ], [ %0, %1 ]
  %3 = load i8, ptr %.0, align 1, !tbaa !5
  switch i8 %3, label %6 [
    i8 0, label %.preheader
    i8 32, label %4
  ]

.preheader:                                       ; preds = %.preheader27
  %.not2528 = icmp eq i32 %.020, 0
  br i1 %.not2528, label %.loopexit, label %.lr.ph

4:                                                ; preds = %.preheader27
  %5 = add nsw i32 %.020, 1
  br label %6

6:                                                ; preds = %.preheader27, %4
  %.121 = phi i32 [ %5, %4 ], [ %.020, %.preheader27 ]
  %7 = getelementptr inbounds i8, ptr %.0, i64 1
  br label %.preheader27, !llvm.loop !8

.lr.phthread-pre-split:                           ; preds = %.thread, %13
  %.333 = phi i32 [ %.229, %.thread ], [ %16, %13 ]
  %8 = getelementptr inbounds i8, ptr %.130, i64 -1
  %.pr = load i8, ptr %8, align 1, !tbaa !5
  br label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %.lr.phthread-pre-split
  %9 = phi i8 [ %.pr, %.lr.phthread-pre-split ], [ 0, %.preheader ]
  %.130 = phi ptr [ %8, %.lr.phthread-pre-split ], [ %.0, %.preheader ]
  %.229 = phi i32 [ %.333, %.lr.phthread-pre-split ], [ %.020, %.preheader ]
  %.not26 = icmp eq i8 %9, 32
  %10 = shl nsw i32 %.229, 1
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds i8, ptr %.130, i64 %11
  br i1 %.not26, label %13, label %.thread

.thread:                                          ; preds = %.lr.ph
  store i8 %9, ptr %12, align 1, !tbaa !5
  br label %.lr.phthread-pre-split

13:                                               ; preds = %.lr.ph
  store i8 48, ptr %12, align 1, !tbaa !5
  %14 = getelementptr inbounds i8, ptr %12, i64 -1
  store i8 50, ptr %14, align 1, !tbaa !5
  %15 = getelementptr inbounds i8, ptr %12, i64 -2
  store i8 37, ptr %15, align 1, !tbaa !5
  %16 = add nsw i32 %.229, -1
  %.not25 = icmp eq i32 %16, 0
  br i1 %.not25, label %.loopexit, label %.lr.phthread-pre-split, !llvm.loop !10

.loopexit:                                        ; preds = %13, %.preheader, %1
  ret ptr %0
}

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"}
!10 = distinct !{!10, !9}


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