; LLVM IR from sbshah97/DS-Algo-Handbook :: labs/lab10 (edit_bottom_up)

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

@x = external dso_local global [30 x i8], align 16
@y = external dso_local global [30 x i8], align 16
@mem = external dso_local global [30 x [30 x i32]], align 16
@.str = external hidden unnamed_addr constant [19 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [26 x i8], align 1

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  %8 = call i32 (ptr, ...) @printf(ptr noundef @.str)
  %9 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, ptr noundef @x, ptr noundef @y)
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #5
  %10 = call i64 @strlen(ptr noundef @x) #6
  %11 = trunc i64 %10 to i32
  store i32 %11, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #5
  %12 = call i64 @strlen(ptr noundef @y) #6
  %13 = trunc i64 %12 to i32
  store i32 %13, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #5
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %14

14:                                               ; preds = %36, %0
  %15 = load i32, ptr %4, align 4, !tbaa !5
  %16 = load i32, ptr %2, align 4, !tbaa !5
  %17 = icmp sle i32 %15, %16
  br i1 %17, label %19, label %18

18:                                               ; preds = %14
  store i32 2, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #5
  br label %39

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

20:                                               ; preds = %32, %19
  %21 = load i32, ptr %6, align 4, !tbaa !5
  %22 = load i32, ptr %3, align 4, !tbaa !5
  %23 = icmp sle i32 %21, %22
  br i1 %23, label %25, label %24

24:                                               ; preds = %20
  store i32 5, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #5
  br label %35

25:                                               ; preds = %20
  %26 = load i32, ptr %4, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [30 x [30 x i32]], ptr @mem, i64 0, i64 %27
  %29 = load i32, ptr %6, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [30 x i32], ptr %28, i64 0, i64 %30
  store i32 -1, ptr %31, align 4, !tbaa !5
  br label %32

32:                                               ; preds = %25
  %33 = load i32, ptr %6, align 4, !tbaa !5
  %34 = add nsw i32 %33, 1
  store i32 %34, ptr %6, align 4, !tbaa !5
  br label %20, !llvm.loop !9

35:                                               ; preds = %24
  br label %36

36:                                               ; preds = %35
  %37 = load i32, ptr %4, align 4, !tbaa !5
  %38 = add nsw i32 %37, 1
  store i32 %38, ptr %4, align 4, !tbaa !5
  br label %14, !llvm.loop !11

39:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #5
  %40 = load i32, ptr %2, align 4, !tbaa !5
  %41 = load i32, ptr %3, align 4, !tbaa !5
  %42 = call noundef i32 @_Z4editii(i32 noundef %40, i32 noundef %41)
  store i32 %42, ptr %7, align 4, !tbaa !5
  %43 = load i32, ptr %7, align 4, !tbaa !5
  %44 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i32 noundef %43)
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #5
  %45 = load i32, ptr %1, align 4
  ret i32 %45
}

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

declare i32 @__isoc99_scanf(ptr noundef, ...) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #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) #2

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z4editii(i32 noundef, i32 noundef) #4

attributes #0 = { mustprogress norecurse 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 = { "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 willreturn memory(argmem: readwrite) }
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 = { mustprogress 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 #5 = { nounwind }
attributes #6 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


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

@x = external dso_local global [30 x i8], align 16
@y = external dso_local global [30 x i8], align 16
@mem = external dso_local local_unnamed_addr global [30 x [30 x i32]], align 16
@.str = external hidden unnamed_addr constant [19 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [26 x i8], align 1

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str)
  %2 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull @x, ptr noundef nonnull @y)
  %3 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) @x) #5
  %4 = trunc i64 %3 to i32
  %5 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) @y) #5
  %6 = trunc i64 %5 to i32
  %.not17 = icmp slt i32 %4, 0
  %.not1415 = icmp slt i32 %6, 0
  %or.cond = select i1 %.not17, i1 true, i1 %.not1415
  br i1 %or.cond, label %._crit_edge19, label %.preheader.preheader

.preheader.preheader:                             ; preds = %0
  %7 = shl i64 %5, 2
  %8 = add nuw nsw i64 %7, 4
  %9 = and i64 %8, 17179869180
  %10 = add nuw nsw i64 %3, 1
  %wide.trip.count = and i64 %10, 4294967295
  %11 = add nsw i64 %wide.trip.count, -1
  %xtraiter = and i64 %10, 7
  %12 = icmp ult i64 %11, 7
  br i1 %12, label %._crit_edge19.loopexit.unr-lcssa, label %.preheader.preheader.new

.preheader.preheader.new:                         ; preds = %.preheader.preheader
  %unroll_iter = sub nsw i64 %wide.trip.count, %xtraiter
  br label %.preheader

.preheader:                                       ; preds = %.preheader, %.preheader.preheader.new
  %indvar = phi i64 [ 0, %.preheader.preheader.new ], [ %indvar.next.7, %.preheader ]
  %niter = phi i64 [ 0, %.preheader.preheader.new ], [ %niter.next.7, %.preheader ]
  %13 = mul nuw nsw i64 %indvar, 120
  %scevgep = getelementptr i8, ptr @mem, i64 %13
  tail call void @llvm.memset.p0.i64(ptr align 16 %scevgep, i8 -1, i64 %9, i1 false), !tbaa !5
  %14 = mul nuw i64 %indvar, 120
  %gep = getelementptr i8, ptr getelementptr (i8, ptr @mem, i64 120), i64 %14
  tail call void @llvm.memset.p0.i64(ptr align 8 %gep, i8 -1, i64 %9, i1 false), !tbaa !5
  %15 = mul nuw i64 %indvar, 120
  %gep23 = getelementptr i8, ptr getelementptr (i8, ptr @mem, i64 240), i64 %15
  tail call void @llvm.memset.p0.i64(ptr align 16 %gep23, i8 -1, i64 %9, i1 false), !tbaa !5
  %16 = mul nuw i64 %indvar, 120
  %gep24 = getelementptr i8, ptr getelementptr (i8, ptr @mem, i64 360), i64 %16
  tail call void @llvm.memset.p0.i64(ptr align 8 %gep24, i8 -1, i64 %9, i1 false), !tbaa !5
  %17 = mul nuw i64 %indvar, 120
  %gep25 = getelementptr i8, ptr getelementptr (i8, ptr @mem, i64 480), i64 %17
  tail call void @llvm.memset.p0.i64(ptr align 16 %gep25, i8 -1, i64 %9, i1 false), !tbaa !5
  %18 = mul nuw i64 %indvar, 120
  %gep26 = getelementptr i8, ptr getelementptr (i8, ptr @mem, i64 600), i64 %18
  tail call void @llvm.memset.p0.i64(ptr align 8 %gep26, i8 -1, i64 %9, i1 false), !tbaa !5
  %19 = mul nuw i64 %indvar, 120
  %gep27 = getelementptr i8, ptr getelementptr (i8, ptr @mem, i64 720), i64 %19
  tail call void @llvm.memset.p0.i64(ptr align 16 %gep27, i8 -1, i64 %9, i1 false), !tbaa !5
  %20 = mul nuw i64 %indvar, 120
  %gep28 = getelementptr i8, ptr getelementptr (i8, ptr @mem, i64 840), i64 %20
  tail call void @llvm.memset.p0.i64(ptr align 8 %gep28, i8 -1, i64 %9, i1 false), !tbaa !5
  %indvar.next.7 = add nuw nsw i64 %indvar, 8
  %niter.next.7 = add i64 %niter, 8
  %niter.ncmp.7 = icmp eq i64 %niter.next.7, %unroll_iter
  br i1 %niter.ncmp.7, label %._crit_edge19.loopexit.unr-lcssa, label %.preheader, !llvm.loop !9

._crit_edge19.loopexit.unr-lcssa:                 ; preds = %.preheader, %.preheader.preheader
  %indvar.unr = phi i64 [ 0, %.preheader.preheader ], [ %indvar.next.7, %.preheader ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge19, label %.preheader.epil

.preheader.epil:                                  ; preds = %._crit_edge19.loopexit.unr-lcssa, %.preheader.epil
  %indvar.epil = phi i64 [ %indvar.next.epil, %.preheader.epil ], [ %indvar.unr, %._crit_edge19.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.preheader.epil ], [ 0, %._crit_edge19.loopexit.unr-lcssa ]
  %21 = mul nuw nsw i64 %indvar.epil, 120
  %scevgep.epil = getelementptr i8, ptr @mem, i64 %21
  tail call void @llvm.memset.p0.i64(ptr align 8 %scevgep.epil, i8 -1, i64 %9, i1 false), !tbaa !5
  %indvar.next.epil = add nuw nsw i64 %indvar.epil, 1
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %._crit_edge19, label %.preheader.epil, !llvm.loop !11

._crit_edge19:                                    ; preds = %._crit_edge19.loopexit.unr-lcssa, %.preheader.epil, %0
  %22 = tail call noundef i32 @_Z4editii(i32 noundef %4, i32 noundef %6)
  %23 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %22)
  ret i32 0
}

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

; Function Attrs: nofree nounwind
declare noundef i32 @__isoc99_scanf(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: mustprogress uwtable
declare dso_local noundef i32 @_Z4editii(i32 noundef, i32 noundef) local_unnamed_addr #3

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #4

attributes #0 = { mustprogress norecurse 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 = { mustprogress 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 #4 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.unroll.disable"}


; Effective O3 passes: GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopIdiomRecognizePass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass