; LLVM IR from freddiev4/dailyprogrammerchallenges :: Easy Challenges/Challenge 0054 Easy/solutions (solution)

; 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: nounwind uwtable
define dso_local ptr @decodeMatrix(ptr noundef %0, i32 noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %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
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  %11 = load ptr, ptr %3, align 8, !tbaa !5
  %12 = call i64 @strlen(ptr noundef %11) #5
  %13 = trunc i64 %12 to i32
  store i32 %13, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %14 = load ptr, ptr %3, align 8, !tbaa !5
  %15 = call i64 @strlen(ptr noundef %14) #5
  %16 = mul i64 1, %15
  %17 = call noalias ptr @malloc(i64 noundef %16) #6
  store ptr %17, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  store i32 0, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  store i32 0, ptr %8, align 4, !tbaa !9
  br label %18

18:                                               ; preds = %52, %2
  %19 = load i32, ptr %7, align 4, !tbaa !9
  %20 = load i32, ptr %5, align 4, !tbaa !9
  %21 = load i32, ptr %4, align 4, !tbaa !9
  %22 = sdiv i32 %20, %21
  %23 = icmp slt i32 %19, %22
  br i1 %23, label %25, label %24

24:                                               ; preds = %18
  store i32 2, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  br label %55

25:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  store i32 0, ptr %10, align 4, !tbaa !9
  br label %26

26:                                               ; preds = %48, %25
  %27 = load i32, ptr %10, align 4, !tbaa !9
  %28 = load i32, ptr %4, align 4, !tbaa !9
  %29 = icmp slt i32 %27, %28
  br i1 %29, label %31, label %30

30:                                               ; preds = %26
  store i32 5, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  br label %51

31:                                               ; preds = %26
  %32 = load ptr, ptr %3, align 8, !tbaa !5
  %33 = load i32, ptr %7, align 4, !tbaa !9
  %34 = load i32, ptr %10, align 4, !tbaa !9
  %35 = load i32, ptr %5, align 4, !tbaa !9
  %36 = load i32, ptr %4, align 4, !tbaa !9
  %37 = sdiv i32 %35, %36
  %38 = mul nsw i32 %34, %37
  %39 = add nsw i32 %33, %38
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds i8, ptr %32, i64 %40
  %42 = load i8, ptr %41, align 1, !tbaa !11
  %43 = load ptr, ptr %6, align 8, !tbaa !5
  %44 = load i32, ptr %8, align 4, !tbaa !9
  %45 = add nsw i32 %44, 1
  store i32 %45, ptr %8, align 4, !tbaa !9
  %46 = sext i32 %44 to i64
  %47 = getelementptr inbounds i8, ptr %43, i64 %46
  store i8 %42, ptr %47, align 1, !tbaa !11
  br label %48

48:                                               ; preds = %31
  %49 = load i32, ptr %10, align 4, !tbaa !9
  %50 = add nsw i32 %49, 1
  store i32 %50, ptr %10, align 4, !tbaa !9
  br label %26, !llvm.loop !12

51:                                               ; preds = %30
  br label %52

52:                                               ; preds = %51
  %53 = load i32, ptr %7, align 4, !tbaa !9
  %54 = add nsw i32 %53, 1
  store i32 %54, ptr %7, align 4, !tbaa !9
  br label %18, !llvm.loop !14

55:                                               ; preds = %24
  %56 = load ptr, ptr %6, align 8, !tbaa !5
  %57 = load i32, ptr %5, align 4, !tbaa !9
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds i8, ptr %56, i64 %58
  store i8 0, ptr %59, align 1, !tbaa !11
  %60 = load ptr, ptr %6, align 8, !tbaa !5
  store i32 1, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  ret ptr %60
}

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

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

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

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 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 #3 = { 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 #4 = { nounwind }
attributes #5 = { nounwind willreturn memory(read) }
attributes #6 = { 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 = !{!"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 nounwind memory(write, argmem: read, inaccessiblemem: readwrite) uwtable
define dso_local noalias noundef ptr @decodeMatrix(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %0) #3
  %4 = trunc i64 %3 to i32
  %5 = tail call noalias ptr @malloc(i64 noundef %3) #4
  %6 = sdiv i32 %4, %1
  %7 = icmp sgt i32 %6, 0
  %8 = icmp sgt i32 %1, 0
  %or.cond = and i1 %7, %8
  br i1 %or.cond, label %.preheader.us.preheader, label %._crit_edge25

.preheader.us.preheader:                          ; preds = %2
  %9 = zext nneg i32 %6 to i64
  %wide.trip.count = zext nneg i32 %1 to i64
  %min.iters.check = icmp ugt i32 %1, 31
  %ident.check.not = icmp eq i32 %6, 1
  %or.cond40 = and i1 %min.iters.check, %ident.check.not
  %n.vec = and i64 %wide.trip.count, 2147483616
  %cmp.n = icmp eq i64 %n.vec, %wide.trip.count
  %xtraiter = and i64 %wide.trip.count, 3
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  %invariant.gep42 = getelementptr i8, ptr %5, i64 1
  %invariant.gep44 = getelementptr i8, ptr %5, i64 2
  %invariant.gep46 = getelementptr i8, ptr %5, i64 3
  br label %.preheader.us

.preheader.us:                                    ; preds = %.preheader.us.preheader, %._crit_edge.us
  %indvars.iv32 = phi i64 [ 0, %.preheader.us.preheader ], [ %indvars.iv.next33, %._crit_edge.us ]
  %.01824.us = phi i64 [ 0, %.preheader.us.preheader ], [ %indvars.iv.next28.lcssa, %._crit_edge.us ]
  %sext37 = shl i64 %.01824.us, 32
  %10 = ashr exact i64 %sext37, 32
  %invariant.gep = getelementptr inbounds i8, ptr %0, i64 %indvars.iv32
  br i1 %or.cond40, label %vector.ph, label %scalar.ph.preheader

vector.ph:                                        ; preds = %.preheader.us
  %ind.end = add nsw i64 %10, %n.vec
  %11 = getelementptr i8, ptr %5, i64 %10
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %12 = getelementptr inbounds i8, ptr %invariant.gep, i64 %index
  %13 = getelementptr inbounds i8, ptr %12, i64 16
  %wide.load = load <16 x i8>, ptr %12, align 1, !tbaa !5
  %wide.load39 = load <16 x i8>, ptr %13, align 1, !tbaa !5
  %14 = getelementptr i8, ptr %11, i64 %index
  %15 = getelementptr inbounds i8, ptr %14, i64 16
  store <16 x i8> %wide.load, ptr %14, align 1, !tbaa !5
  store <16 x i8> %wide.load39, ptr %15, align 1, !tbaa !5
  %index.next = add nuw i64 %index, 32
  %16 = icmp eq i64 %index.next, %n.vec
  br i1 %16, label %middle.block, label %vector.body, !llvm.loop !8

middle.block:                                     ; preds = %vector.body
  br i1 %cmp.n, label %._crit_edge.us, label %scalar.ph.preheader

scalar.ph.preheader:                              ; preds = %middle.block, %.preheader.us
  %indvars.iv27.ph = phi i64 [ %10, %.preheader.us ], [ %ind.end, %middle.block ]
  %indvars.iv.ph = phi i64 [ 0, %.preheader.us ], [ %n.vec, %middle.block ]
  br i1 %lcmp.mod.not, label %scalar.ph.prol.loopexit, label %scalar.ph.prol

scalar.ph.prol:                                   ; preds = %scalar.ph.preheader, %scalar.ph.prol
  %indvars.iv27.prol = phi i64 [ %indvars.iv.next28.prol, %scalar.ph.prol ], [ %indvars.iv27.ph, %scalar.ph.preheader ]
  %indvars.iv.prol = phi i64 [ %indvars.iv.next.prol, %scalar.ph.prol ], [ %indvars.iv.ph, %scalar.ph.preheader ]
  %prol.iter = phi i64 [ %prol.iter.next, %scalar.ph.prol ], [ 0, %scalar.ph.preheader ]
  %17 = mul nuw nsw i64 %indvars.iv.prol, %9
  %gep.prol = getelementptr inbounds i8, ptr %invariant.gep, i64 %17
  %18 = load i8, ptr %gep.prol, align 1, !tbaa !5
  %indvars.iv.next28.prol = add nsw i64 %indvars.iv27.prol, 1
  %19 = getelementptr inbounds i8, ptr %5, i64 %indvars.iv27.prol
  store i8 %18, ptr %19, align 1, !tbaa !5
  %indvars.iv.next.prol = add nuw nsw i64 %indvars.iv.prol, 1
  %prol.iter.next = add i64 %prol.iter, 1
  %prol.iter.cmp.not = icmp eq i64 %prol.iter.next, %xtraiter
  br i1 %prol.iter.cmp.not, label %scalar.ph.prol.loopexit, label %scalar.ph.prol, !llvm.loop !12

scalar.ph.prol.loopexit:                          ; preds = %scalar.ph.prol, %scalar.ph.preheader
  %indvars.iv.next28.lcssa41.unr = phi i64 [ poison, %scalar.ph.preheader ], [ %indvars.iv.next28.prol, %scalar.ph.prol ]
  %indvars.iv27.unr = phi i64 [ %indvars.iv27.ph, %scalar.ph.preheader ], [ %indvars.iv.next28.prol, %scalar.ph.prol ]
  %indvars.iv.unr = phi i64 [ %indvars.iv.ph, %scalar.ph.preheader ], [ %indvars.iv.next.prol, %scalar.ph.prol ]
  %20 = sub nsw i64 %indvars.iv.ph, %wide.trip.count
  %21 = icmp ugt i64 %20, -4
  br i1 %21, label %._crit_edge.us, label %scalar.ph

scalar.ph:                                        ; preds = %scalar.ph.prol.loopexit, %scalar.ph
  %indvars.iv27 = phi i64 [ %indvars.iv.next28.3, %scalar.ph ], [ %indvars.iv27.unr, %scalar.ph.prol.loopexit ]
  %indvars.iv = phi i64 [ %indvars.iv.next.3, %scalar.ph ], [ %indvars.iv.unr, %scalar.ph.prol.loopexit ]
  %22 = mul nuw nsw i64 %indvars.iv, %9
  %gep = getelementptr inbounds i8, ptr %invariant.gep, i64 %22
  %23 = load i8, ptr %gep, align 1, !tbaa !5
  %24 = getelementptr inbounds i8, ptr %5, i64 %indvars.iv27
  store i8 %23, ptr %24, align 1, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %25 = mul nuw nsw i64 %indvars.iv.next, %9
  %gep.1 = getelementptr inbounds i8, ptr %invariant.gep, i64 %25
  %26 = load i8, ptr %gep.1, align 1, !tbaa !5
  %gep43 = getelementptr i8, ptr %invariant.gep42, i64 %indvars.iv27
  store i8 %26, ptr %gep43, align 1, !tbaa !5
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %27 = mul nuw nsw i64 %indvars.iv.next.1, %9
  %gep.2 = getelementptr inbounds i8, ptr %invariant.gep, i64 %27
  %28 = load i8, ptr %gep.2, align 1, !tbaa !5
  %gep45 = getelementptr i8, ptr %invariant.gep44, i64 %indvars.iv27
  store i8 %28, ptr %gep45, align 1, !tbaa !5
  %indvars.iv.next.2 = add nuw nsw i64 %indvars.iv, 3
  %29 = mul nuw nsw i64 %indvars.iv.next.2, %9
  %gep.3 = getelementptr inbounds i8, ptr %invariant.gep, i64 %29
  %30 = load i8, ptr %gep.3, align 1, !tbaa !5
  %indvars.iv.next28.3 = add nsw i64 %indvars.iv27, 4
  %gep47 = getelementptr i8, ptr %invariant.gep46, i64 %indvars.iv27
  store i8 %30, ptr %gep47, align 1, !tbaa !5
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 4
  %exitcond.not.3 = icmp eq i64 %indvars.iv.next.3, %wide.trip.count
  br i1 %exitcond.not.3, label %._crit_edge.us, label %scalar.ph, !llvm.loop !14

._crit_edge.us:                                   ; preds = %scalar.ph.prol.loopexit, %scalar.ph, %middle.block
  %indvars.iv.next28.lcssa = phi i64 [ %ind.end, %middle.block ], [ %indvars.iv.next28.lcssa41.unr, %scalar.ph.prol.loopexit ], [ %indvars.iv.next28.3, %scalar.ph ]
  %indvars.iv.next33 = add nuw nsw i64 %indvars.iv32, 1
  %exitcond36.not = icmp eq i64 %indvars.iv.next33, %9
  br i1 %exitcond36.not, label %._crit_edge25, label %.preheader.us, !llvm.loop !15

._crit_edge25:                                    ; preds = %._crit_edge.us, %2
  %sext = shl i64 %3, 32
  %31 = ashr exact i64 %sext, 32
  %32 = getelementptr inbounds i8, ptr %5, i64 %31
  store i8 0, ptr %32, align 1, !tbaa !5
  ret ptr %5
}

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

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

attributes #0 = { nofree nounwind memory(write, argmem: read, inaccessiblemem: readwrite) 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 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 #2 = { 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 #3 = { nounwind willreturn memory(read) }
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 = !{!"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, !13}
!13 = !{!"llvm.loop.unroll.disable"}
!14 = distinct !{!14, !9, !10}
!15 = distinct !{!15, !9}


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