; LLVM IR from freddiev4/dailyprogrammerchallenges :: Easy Challenges/Challenge 0011 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"

@calc_dow.arr = external hidden global [12 x i16], align 16

; Function Attrs: nounwind uwtable
define dso_local zeroext i16 @calc_dow(i16 noundef zeroext %0, i16 noundef zeroext %1, i16 noundef zeroext %2) #0 {
  %4 = alloca i16, align 2
  %5 = alloca i16, align 2
  %6 = alloca i16, align 2
  store i16 %0, ptr %4, align 2, !tbaa !5
  store i16 %1, ptr %5, align 2, !tbaa !5
  store i16 %2, ptr %6, align 2, !tbaa !5
  %7 = load i16, ptr %5, align 2, !tbaa !5
  %8 = zext i16 %7 to i32
  %9 = icmp slt i32 %8, 3
  %10 = zext i1 %9 to i32
  %11 = load i16, ptr %6, align 2, !tbaa !5
  %12 = zext i16 %11 to i32
  %13 = sub nsw i32 %12, %10
  %14 = trunc i32 %13 to i16
  store i16 %14, ptr %6, align 2, !tbaa !5
  %15 = load i16, ptr %6, align 2, !tbaa !5
  %16 = zext i16 %15 to i32
  %17 = load i16, ptr %6, align 2, !tbaa !5
  %18 = zext i16 %17 to i32
  %19 = sdiv i32 %18, 4
  %20 = add nsw i32 %16, %19
  %21 = load i16, ptr %6, align 2, !tbaa !5
  %22 = zext i16 %21 to i32
  %23 = sdiv i32 %22, 100
  %24 = sub nsw i32 %20, %23
  %25 = load i16, ptr %6, align 2, !tbaa !5
  %26 = zext i16 %25 to i32
  %27 = sdiv i32 %26, 400
  %28 = add nsw i32 %24, %27
  %29 = load i16, ptr %5, align 2, !tbaa !5
  %30 = zext i16 %29 to i32
  %31 = sub nsw i32 %30, 1
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [12 x i16], ptr @calc_dow.arr, i64 0, i64 %32
  %34 = load i16, ptr %33, align 2, !tbaa !5
  %35 = zext i16 %34 to i32
  %36 = add nsw i32 %28, %35
  %37 = load i16, ptr %4, align 2, !tbaa !5
  %38 = zext i16 %37 to i32
  %39 = add nsw i32 %36, %38
  %40 = srem i32 %39, 7
  %41 = trunc i32 %40 to i16
  ret i16 %41
}

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

!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 = !{!"short", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}


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

@calc_dow.arr = external hidden local_unnamed_addr global [12 x i16], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local zeroext range(i16 -6, 7) i16 @calc_dow(i16 noundef zeroext %0, i16 noundef zeroext %1, i16 noundef zeroext %2) local_unnamed_addr #0 {
  %4 = zext i16 %1 to i64
  %5 = icmp ult i16 %1, 3
  %.neg = sext i1 %5 to i16
  %6 = add i16 %.neg, %2
  %7 = zext i16 %6 to i32
  %8 = lshr i32 %7, 2
  %9 = udiv i16 %6, 100
  %.zext = zext nneg i16 %9 to i32
  %10 = udiv i16 %6, 400
  %.zext9 = zext nneg i16 %10 to i32
  %11 = add nsw i64 %4, -1
  %12 = getelementptr inbounds [12 x i16], ptr @calc_dow.arr, i64 0, i64 %11
  %13 = load i16, ptr %12, align 2, !tbaa !5
  %14 = zext i16 %13 to i32
  %15 = zext i16 %0 to i32
  %16 = add nuw nsw i32 %7, %15
  %17 = add nuw nsw i32 %16, %8
  %18 = sub nsw i32 %17, %.zext
  %19 = add nsw i32 %18, %.zext9
  %20 = add nsw i32 %19, %14
  %21 = srem i32 %20, 7
  %22 = trunc nsw i32 %21 to i16
  ret i16 %22
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, 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 = !{!"short", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InstCombinePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass