; LLVM IR from kaynewbie/algorithm :: algorithm/CodingInterviews (merge-arrays)

; 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 @replaceWhiteSpace(ptr noundef %0, i64 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i64, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca ptr, align 8
  %13 = alloca i8, align 1
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  %15 = icmp eq ptr %14, null
  br i1 %15, label %16, label %18

16:                                               ; preds = %2
  %17 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %17, ptr %3, align 8
  br label %104

18:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %19 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %19, ptr %6, align 8, !tbaa !5
  store i32 0, ptr %7, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  store i32 0, ptr %8, align 4, !tbaa !11
  br label %20

20:                                               ; preds = %38, %18
  %21 = load i32, ptr %8, align 4, !tbaa !11
  %22 = sext i32 %21 to i64
  %23 = load i64, ptr %5, align 8, !tbaa !9
  %24 = icmp ult i64 %22, %23
  br i1 %24, label %26, label %25

25:                                               ; preds = %20
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  br label %41

26:                                               ; preds = %20
  %27 = load ptr, ptr %6, align 8, !tbaa !5
  %28 = load i32, ptr %8, align 4, !tbaa !11
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds i8, ptr %27, i64 %29
  %31 = load i8, ptr %30, align 1, !tbaa !13
  %32 = sext i8 %31 to i32
  %33 = icmp eq i32 %32, 32
  br i1 %33, label %34, label %37

34:                                               ; preds = %26
  %35 = load i32, ptr %7, align 4, !tbaa !11
  %36 = add nsw i32 %35, 1
  store i32 %36, ptr %7, align 4, !tbaa !11
  br label %37

37:                                               ; preds = %34, %26
  br label %38

38:                                               ; preds = %37
  %39 = load i32, ptr %8, align 4, !tbaa !11
  %40 = add nsw i32 %39, 1
  store i32 %40, ptr %8, align 4, !tbaa !11
  br label %20, !llvm.loop !14

41:                                               ; preds = %25
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %42 = load i64, ptr %5, align 8, !tbaa !9
  %43 = load i32, ptr %7, align 4, !tbaa !11
  %44 = mul nsw i32 %43, 2
  %45 = sext i32 %44 to i64
  %46 = add i64 %42, %45
  %47 = add i64 %46, 1
  store i64 %47, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %48 = load i64, ptr %9, align 8, !tbaa !9
  %49 = mul i64 %48, 1
  %50 = call noalias ptr @malloc(i64 noundef %49) #4
  store ptr %50, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  %51 = load ptr, ptr %4, align 8, !tbaa !5
  %52 = load i64, ptr %5, align 8, !tbaa !9
  %53 = getelementptr inbounds i8, ptr %51, i64 %52
  %54 = getelementptr inbounds i8, ptr %53, i64 -1
  store ptr %54, ptr %12, align 8, !tbaa !5
  %55 = load i64, ptr %9, align 8, !tbaa !9
  %56 = trunc i64 %55 to i32
  %57 = sub nsw i32 %56, 2
  store i32 %57, ptr %11, align 4, !tbaa !11
  br label %58

58:                                               ; preds = %93, %41
  %59 = load i32, ptr %11, align 4, !tbaa !11
  %60 = icmp sge i32 %59, 0
  br i1 %60, label %61, label %64

61:                                               ; preds = %58
  %62 = load ptr, ptr %12, align 8, !tbaa !5
  %63 = icmp ne ptr %62, null
  br label %64

64:                                               ; preds = %61, %58
  %65 = phi i1 [ false, %58 ], [ %63, %61 ]
  br i1 %65, label %66, label %98

66:                                               ; preds = %64
  call void @llvm.lifetime.start.p0(i64 1, ptr %13) #3
  %67 = load ptr, ptr %12, align 8, !tbaa !5
  %68 = load i8, ptr %67, align 1, !tbaa !13
  store i8 %68, ptr %13, align 1, !tbaa !13
  %69 = load i8, ptr %13, align 1, !tbaa !13
  %70 = sext i8 %69 to i32
  %71 = icmp eq i32 %70, 32
  br i1 %71, label %72, label %87

72:                                               ; preds = %66
  %73 = load ptr, ptr %10, align 8, !tbaa !5
  %74 = load i32, ptr %11, align 4, !tbaa !11
  %75 = sext i32 %74 to i64
  %76 = getelementptr inbounds i8, ptr %73, i64 %75
  store i8 48, ptr %76, align 1, !tbaa !13
  %77 = load ptr, ptr %10, align 8, !tbaa !5
  %78 = load i32, ptr %11, align 4, !tbaa !11
  %79 = add nsw i32 %78, -1
  store i32 %79, ptr %11, align 4, !tbaa !11
  %80 = sext i32 %79 to i64
  %81 = getelementptr inbounds i8, ptr %77, i64 %80
  store i8 50, ptr %81, align 1, !tbaa !13
  %82 = load ptr, ptr %10, align 8, !tbaa !5
  %83 = load i32, ptr %11, align 4, !tbaa !11
  %84 = add nsw i32 %83, -1
  store i32 %84, ptr %11, align 4, !tbaa !11
  %85 = sext i32 %84 to i64
  %86 = getelementptr inbounds i8, ptr %82, i64 %85
  store i8 37, ptr %86, align 1, !tbaa !13
  br label %93

87:                                               ; preds = %66
  %88 = load i8, ptr %13, align 1, !tbaa !13
  %89 = load ptr, ptr %10, align 8, !tbaa !5
  %90 = load i32, ptr %11, align 4, !tbaa !11
  %91 = sext i32 %90 to i64
  %92 = getelementptr inbounds i8, ptr %89, i64 %91
  store i8 %88, ptr %92, align 1, !tbaa !13
  br label %93

93:                                               ; preds = %87, %72
  %94 = load i32, ptr %11, align 4, !tbaa !11
  %95 = add nsw i32 %94, -1
  store i32 %95, ptr %11, align 4, !tbaa !11
  %96 = load ptr, ptr %12, align 8, !tbaa !5
  %97 = getelementptr inbounds i8, ptr %96, i32 -1
  store ptr %97, ptr %12, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 1, ptr %13) #3
  br label %58, !llvm.loop !16

98:                                               ; preds = %64
  %99 = load ptr, ptr %10, align 8, !tbaa !5
  %100 = load i64, ptr %9, align 8, !tbaa !9
  %101 = sub i64 %100, 1
  %102 = getelementptr inbounds i8, ptr %99, i64 %101
  store i8 0, ptr %102, align 1, !tbaa !13
  %103 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %103, ptr %3, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  br label %104

104:                                              ; preds = %98, %16
  %105 = load ptr, ptr %3, align 8
  ret ptr %105
}

; 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

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #2

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 allocsize(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 #3 = { nounwind }
attributes #4 = { nounwind allocsize(0) }

!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 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}


; 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 nounwind memory(readwrite, argmem: read) uwtable
define dso_local noalias noundef ptr @replaceWhiteSpace(ptr noundef readonly %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = icmp eq ptr %0, null
  br i1 %3, label %40, label %.preheader

.preheader:                                       ; preds = %2
  %.not = icmp eq i64 %1, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %.preheader
  %min.iters.check = icmp ult i64 %1, 8
  br i1 %min.iters.check, label %.lr.ph.preheader53, label %vector.ph

.lr.ph.preheader53:                               ; preds = %middle.block, %.lr.ph.preheader
  %indvars.iv.ph = phi i64 [ 0, %.lr.ph.preheader ], [ %n.vec, %middle.block ]
  %.03543.ph = phi i32 [ 0, %.lr.ph.preheader ], [ %13, %middle.block ]
  br label %.lr.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %1, -8
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.phi = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %10, %vector.body ]
  %vec.phi51 = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %11, %vector.body ]
  %4 = getelementptr inbounds i8, ptr %0, i64 %index
  %5 = getelementptr inbounds i8, ptr %4, i64 4
  %wide.load = load <4 x i8>, ptr %4, align 1, !tbaa !5
  %wide.load52 = load <4 x i8>, ptr %5, align 1, !tbaa !5
  %6 = icmp eq <4 x i8> %wide.load, <i8 32, i8 32, i8 32, i8 32>
  %7 = icmp eq <4 x i8> %wide.load52, <i8 32, i8 32, i8 32, i8 32>
  %8 = zext <4 x i1> %6 to <4 x i32>
  %9 = zext <4 x i1> %7 to <4 x i32>
  %10 = add <4 x i32> %vec.phi, %8
  %11 = add <4 x i32> %vec.phi51, %9
  %index.next = add nuw i64 %index, 8
  %12 = icmp eq i64 %index.next, %n.vec
  br i1 %12, label %middle.block, label %vector.body, !llvm.loop !8

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <4 x i32> %11, %10
  %13 = tail call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %bin.rdx)
  %cmp.n = icmp eq i64 %n.vec, %1
  br i1 %cmp.n, label %._crit_edge.loopexit, label %.lr.ph.preheader53

._crit_edge.loopexit:                             ; preds = %.lr.ph, %middle.block
  %spec.select.lcssa = phi i32 [ %13, %middle.block ], [ %spec.select, %.lr.ph ]
  %14 = shl nuw nsw i32 %spec.select.lcssa, 1
  %15 = zext nneg i32 %14 to i64
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %.preheader
  %.035.lcssa = phi i64 [ 0, %.preheader ], [ %15, %._crit_edge.loopexit ]
  %16 = add nsw i64 %.035.lcssa, %1
  %17 = add nsw i64 %16, 1
  %18 = tail call noalias ptr @malloc(i64 noundef %17) #3
  %19 = trunc i64 %17 to i32
  %invariant.gep = getelementptr i8, ptr %18, i64 -1
  %20 = icmp sgt i32 %19, 1
  br i1 %20, label %.lr.ph48.preheader, label %._crit_edge49

.lr.ph48.preheader:                               ; preds = %._crit_edge
  %21 = add nsw i32 %19, -2
  %22 = getelementptr inbounds i8, ptr %0, i64 %1
  br label %.lr.ph48

.lr.ph:                                           ; preds = %.lr.ph.preheader53, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ %indvars.iv.ph, %.lr.ph.preheader53 ]
  %.03543 = phi i32 [ %spec.select, %.lr.ph ], [ %.03543.ph, %.lr.ph.preheader53 ]
  %23 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv
  %24 = load i8, ptr %23, align 1, !tbaa !5
  %25 = icmp eq i8 %24, 32
  %26 = zext i1 %25 to i32
  %spec.select = add nuw nsw i32 %.03543, %26
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %1
  br i1 %exitcond.not, label %._crit_edge.loopexit, label %.lr.ph, !llvm.loop !12

.lr.ph48:                                         ; preds = %.lr.ph48.preheader, %36
  %.03346.pn = phi ptr [ %.03346, %36 ], [ %22, %.lr.ph48.preheader ]
  %.03445 = phi i32 [ %37, %36 ], [ %21, %.lr.ph48.preheader ]
  %.03346 = getelementptr inbounds i8, ptr %.03346.pn, i64 -1
  %27 = load i8, ptr %.03346, align 1, !tbaa !5
  %28 = icmp eq i8 %27, 32
  %29 = zext nneg i32 %.03445 to i64
  %30 = getelementptr inbounds i8, ptr %18, i64 %29
  br i1 %28, label %31, label %35

31:                                               ; preds = %.lr.ph48
  store i8 48, ptr %30, align 1, !tbaa !5
  %gep = getelementptr i8, ptr %invariant.gep, i64 %29
  store i8 50, ptr %gep, align 1, !tbaa !5
  %32 = add nsw i32 %.03445, -2
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds i8, ptr %18, i64 %33
  store i8 37, ptr %34, align 1, !tbaa !5
  br label %36

35:                                               ; preds = %.lr.ph48
  store i8 %27, ptr %30, align 1, !tbaa !5
  br label %36

36:                                               ; preds = %35, %31
  %.1 = phi i32 [ %32, %31 ], [ %.03445, %35 ]
  %37 = add nsw i32 %.1, -1
  %38 = icmp sgt i32 %.1, 0
  br i1 %38, label %.lr.ph48, label %._crit_edge49, !llvm.loop !13

._crit_edge49:                                    ; preds = %36, %._crit_edge
  %39 = getelementptr inbounds i8, ptr %18, i64 %16
  store i8 0, ptr %39, align 1, !tbaa !5
  br label %40

40:                                               ; preds = %2, %._crit_edge49
  %.0 = phi ptr [ %18, %._crit_edge49 ], [ null, %2 ]
  ret ptr %.0
}

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.vector.reduce.add.v4i32(<4 x i32>) #2

attributes #0 = { nofree nounwind memory(readwrite, argmem: read) 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 nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #3 = { nounwind allocsize(0) }

!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, !10, !11}
!9 = !{!"llvm.loop.mustprogress"}
!10 = !{!"llvm.loop.isvectorized", i32 1}
!11 = !{!"llvm.loop.unroll.runtime.disable"}
!12 = distinct !{!12, !9, !11, !10}
!13 = distinct !{!13, !9}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass