; LLVM IR from VastoLorde95/Competitive-Programming :: Hackerrank/Week of Code/Week 15/Problem A/A.ll (Z9goodblocki)

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

@g = external dso_local global [9 x [9 x i32]], align 16
@m = external dso_local global [10 x i32], align 16

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

; 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: mustprogress nounwind uwtable
define dso_local noundef zeroext i1 @_Z9goodblocki(i32 noundef %0) #2 {
  %2 = alloca i1, align 1
  %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
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  %10 = load i32, ptr %3, align 4, !tbaa !5
  %11 = mul nsw i32 3, %10
  %12 = srem i32 %11, 9
  store i32 %12, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %13 = load i32, ptr %3, align 4, !tbaa !5
  %14 = sdiv i32 %13, 3
  %15 = mul nsw i32 %14, 3
  store i32 %15, ptr %5, align 4, !tbaa !5
  call void @llvm.memset.p0.i64(ptr align 16 @m, i8 0, i64 40, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 0, ptr %6, align 4, !tbaa !5
  br label %16

16:                                               ; preds = %43, %1
  %17 = load i32, ptr %6, align 4, !tbaa !5
  %18 = icmp slt i32 %17, 3
  br i1 %18, label %20, label %19

19:                                               ; preds = %16
  store i32 2, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  br label %46

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

21:                                               ; preds = %39, %20
  %22 = load i32, ptr %8, align 4, !tbaa !5
  %23 = icmp slt i32 %22, 3
  br i1 %23, label %25, label %24

24:                                               ; preds = %21
  store i32 5, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  br label %42

25:                                               ; preds = %21
  %26 = load i32, ptr %5, align 4, !tbaa !5
  %27 = load i32, ptr %6, align 4, !tbaa !5
  %28 = add nsw i32 %26, %27
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [9 x [9 x i32]], ptr @g, i64 0, i64 %29
  %31 = load i32, ptr %4, align 4, !tbaa !5
  %32 = load i32, ptr %8, align 4, !tbaa !5
  %33 = add nsw i32 %31, %32
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [9 x i32], ptr %30, i64 0, i64 %34
  %36 = load i32, ptr %35, align 4, !tbaa !5
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [10 x i32], ptr @m, i64 0, i64 %37
  store i32 1, ptr %38, align 4, !tbaa !5
  br label %39

39:                                               ; preds = %25
  %40 = load i32, ptr %8, align 4, !tbaa !5
  %41 = add nsw i32 %40, 1
  store i32 %41, ptr %8, align 4, !tbaa !5
  br label %21, !llvm.loop !9

42:                                               ; preds = %24
  br label %43

43:                                               ; preds = %42
  %44 = load i32, ptr %6, align 4, !tbaa !5
  %45 = add nsw i32 %44, 1
  store i32 %45, ptr %6, align 4, !tbaa !5
  br label %16, !llvm.loop !11

46:                                               ; preds = %19
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  store i32 1, ptr %9, align 4, !tbaa !5
  br label %47

47:                                               ; preds = %59, %46
  %48 = load i32, ptr %9, align 4, !tbaa !5
  %49 = icmp sle i32 %48, 9
  br i1 %49, label %51, label %50

50:                                               ; preds = %47
  store i32 8, ptr %7, align 4
  br label %62

51:                                               ; preds = %47
  %52 = load i32, ptr %9, align 4, !tbaa !5
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds [10 x i32], ptr @m, i64 0, i64 %53
  %55 = load i32, ptr %54, align 4, !tbaa !5
  %56 = icmp ne i32 %55, 0
  br i1 %56, label %58, label %57

57:                                               ; preds = %51
  store i1 false, ptr %2, align 1
  store i32 1, ptr %7, align 4
  br label %62

58:                                               ; preds = %51
  br label %59

59:                                               ; preds = %58
  %60 = load i32, ptr %9, align 4, !tbaa !5
  %61 = add nsw i32 %60, 1
  store i32 %61, ptr %9, align 4, !tbaa !5
  br label %47, !llvm.loop !12

62:                                               ; preds = %57, %50
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  %63 = load i32, ptr %7, align 4
  switch i32 %63, label %65 [
    i32 8, label %64
  ]

64:                                               ; preds = %62
  store i1 true, ptr %2, align 1
  store i32 1, ptr %7, align 4
  br label %65

65:                                               ; preds = %64, %62
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  %66 = load i1, ptr %2, align 1
  ret i1 %66
}

attributes #0 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress 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 #3 = { 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 = !{!"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}
!12 = distinct !{!12, !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"

@g = external dso_local local_unnamed_addr global [9 x [9 x i32]], align 16
@m = external dso_local local_unnamed_addr global [10 x i32], align 16

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

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef zeroext i1 @_Z9goodblocki(i32 noundef %0) local_unnamed_addr #1 {
.preheader18:
  %1 = mul nsw i32 %0, 3
  %2 = srem i32 %1, 9
  %3 = srem i32 %0, 3
  %4 = sub nsw i32 %0, %3
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(40) @m, i8 0, i64 40, i1 false)
  %5 = sext i32 %2 to i64
  %6 = sext i32 %4 to i64
  %7 = getelementptr inbounds [9 x [9 x i32]], ptr @g, i64 0, i64 %6, i64 %5
  %8 = load i32, ptr %7, align 4, !tbaa !5
  %9 = sext i32 %8 to i64
  %10 = getelementptr inbounds [10 x i32], ptr @m, i64 0, i64 %9
  store i32 1, ptr %10, align 4, !tbaa !5
  %11 = add nsw i64 %5, 1
  %12 = getelementptr inbounds [9 x [9 x i32]], ptr @g, i64 0, i64 %6, i64 %11
  %13 = load i32, ptr %12, align 4, !tbaa !5
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [10 x i32], ptr @m, i64 0, i64 %14
  store i32 1, ptr %15, align 4, !tbaa !5
  %16 = add nsw i64 %5, 2
  %17 = getelementptr inbounds [9 x [9 x i32]], ptr @g, i64 0, i64 %6, i64 %16
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [10 x i32], ptr @m, i64 0, i64 %19
  store i32 1, ptr %20, align 4, !tbaa !5
  %21 = add nsw i64 %6, 1
  %22 = getelementptr inbounds [9 x [9 x i32]], ptr @g, i64 0, i64 %21, i64 %5
  %23 = load i32, ptr %22, align 4, !tbaa !5
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [10 x i32], ptr @m, i64 0, i64 %24
  store i32 1, ptr %25, align 4, !tbaa !5
  %26 = getelementptr inbounds [9 x [9 x i32]], ptr @g, i64 0, i64 %21, i64 %11
  %27 = load i32, ptr %26, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [10 x i32], ptr @m, i64 0, i64 %28
  store i32 1, ptr %29, align 4, !tbaa !5
  %30 = getelementptr inbounds [9 x [9 x i32]], ptr @g, i64 0, i64 %21, i64 %16
  %31 = load i32, ptr %30, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [10 x i32], ptr @m, i64 0, i64 %32
  store i32 1, ptr %33, align 4, !tbaa !5
  %34 = add nsw i64 %6, 2
  %35 = getelementptr inbounds [9 x [9 x i32]], ptr @g, i64 0, i64 %34, i64 %5
  %36 = load i32, ptr %35, align 4, !tbaa !5
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [10 x i32], ptr @m, i64 0, i64 %37
  store i32 1, ptr %38, align 4, !tbaa !5
  %39 = getelementptr inbounds [9 x [9 x i32]], ptr @g, i64 0, i64 %34, i64 %11
  %40 = load i32, ptr %39, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [10 x i32], ptr @m, i64 0, i64 %41
  store i32 1, ptr %42, align 4, !tbaa !5
  %43 = getelementptr inbounds [9 x [9 x i32]], ptr @g, i64 0, i64 %34, i64 %16
  %44 = load i32, ptr %43, align 4, !tbaa !5
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [10 x i32], ptr @m, i64 0, i64 %45
  store i32 1, ptr %46, align 4, !tbaa !5
  %47 = load <8 x i32>, ptr getelementptr inbounds (i8, ptr @m, i64 4), align 4
  %.fr = freeze <8 x i32> %47
  %48 = load i32, ptr getelementptr inbounds (i8, ptr @m, i64 36), align 4
  %.not.8 = icmp ne i32 %48, 0
  %49 = icmp eq <8 x i32> %.fr, zeroinitializer
  %50 = bitcast <8 x i1> %49 to i8
  %51 = icmp eq i8 %50, 0
  %op.rdx = select i1 %51, i1 %.not.8, i1 false
  ret i1 %op.rdx
}

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #1 = { mustprogress nofree norecurse nosync nounwind willreturn memory(readwrite, argmem: none, 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}


; Effective O3 passes: GVNPass,GlobalDCEPass,GlobalOptPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,PostOrderFunctionAttrsPass,ReassociatePass,SLPVectorizerPass,SROAPass,SimplifyCFGPass,TailCallElimPass