; LLVM IR from freddiev4/dailyprogrammerchallenges :: Easy Challenges/Challenge 0051 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: nounwind uwtable
declare dso_local void @print_array(ptr noundef, i32 noundef) #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: nounwind uwtable
define dso_local void @print_combinations(ptr noundef %0, ptr noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4, i32 noundef %5) #0 {
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  store ptr %0, ptr %7, align 8, !tbaa !5
  store ptr %1, ptr %8, align 8, !tbaa !5
  store i32 %2, ptr %9, align 4, !tbaa !9
  store i32 %3, ptr %10, align 4, !tbaa !9
  store i32 %4, ptr %11, align 4, !tbaa !9
  store i32 %5, ptr %12, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #2
  %14 = load i32, ptr %11, align 4, !tbaa !9
  %15 = load i32, ptr %12, align 4, !tbaa !9
  %16 = icmp eq i32 %14, %15
  br i1 %16, label %17, label %20

17:                                               ; preds = %6
  %18 = load ptr, ptr %8, align 8, !tbaa !5
  %19 = load i32, ptr %12, align 4, !tbaa !9
  call void @print_array(ptr noundef %18, i32 noundef %19)
  br label %20

20:                                               ; preds = %17, %6
  %21 = load i32, ptr %9, align 4, !tbaa !9
  store i32 %21, ptr %13, align 4, !tbaa !9
  br label %22

22:                                               ; preds = %55, %20
  %23 = load i32, ptr %13, align 4, !tbaa !9
  %24 = load i32, ptr %10, align 4, !tbaa !9
  %25 = icmp sle i32 %23, %24
  br i1 %25, label %26, label %35

26:                                               ; preds = %22
  %27 = load i32, ptr %10, align 4, !tbaa !9
  %28 = load i32, ptr %13, align 4, !tbaa !9
  %29 = sub nsw i32 %27, %28
  %30 = add nsw i32 %29, 1
  %31 = load i32, ptr %12, align 4, !tbaa !9
  %32 = load i32, ptr %11, align 4, !tbaa !9
  %33 = sub nsw i32 %31, %32
  %34 = icmp sge i32 %30, %33
  br label %35

35:                                               ; preds = %26, %22
  %36 = phi i1 [ false, %22 ], [ %34, %26 ]
  br i1 %36, label %37, label %58

37:                                               ; preds = %35
  %38 = load ptr, ptr %7, align 8, !tbaa !5
  %39 = load i32, ptr %13, align 4, !tbaa !9
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds i32, ptr %38, i64 %40
  %42 = load i32, ptr %41, align 4, !tbaa !9
  %43 = load ptr, ptr %8, align 8, !tbaa !5
  %44 = load i32, ptr %11, align 4, !tbaa !9
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds i32, ptr %43, i64 %45
  store i32 %42, ptr %46, align 4, !tbaa !9
  %47 = load ptr, ptr %7, align 8, !tbaa !5
  %48 = load ptr, ptr %8, align 8, !tbaa !5
  %49 = load i32, ptr %13, align 4, !tbaa !9
  %50 = add nsw i32 %49, 1
  %51 = load i32, ptr %10, align 4, !tbaa !9
  %52 = load i32, ptr %11, align 4, !tbaa !9
  %53 = add nsw i32 %52, 1
  %54 = load i32, ptr %12, align 4, !tbaa !9
  call void @print_combinations(ptr noundef %47, ptr noundef %48, i32 noundef %50, i32 noundef %51, i32 noundef %53, i32 noundef %54)
  br label %55

55:                                               ; preds = %37
  %56 = load i32, ptr %13, align 4, !tbaa !9
  %57 = add nsw i32 %56, 1
  store i32 %57, ptr %13, align 4, !tbaa !9
  br label %22, !llvm.loop !11

58:                                               ; preds = %35
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #2
  ret void
}

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 }

!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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; 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: nounwind uwtable
declare dso_local void @print_array(ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @print_combinations(ptr nocapture noundef readonly %0, ptr noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4, i32 noundef %5) local_unnamed_addr #0 {
  %7 = icmp eq i32 %4, %5
  br i1 %7, label %8, label %9

8:                                                ; preds = %6
  tail call void @print_array(ptr noundef %1, i32 noundef %4)
  br label %9

9:                                                ; preds = %8, %6
  %.not24 = icmp sgt i32 %2, %3
  br i1 %.not24, label %.critedge, label %.lr.ph

.lr.ph:                                           ; preds = %9
  %10 = add i32 %3, 1
  %11 = sub nsw i32 %5, %4
  %12 = sext i32 %4 to i64
  %13 = getelementptr inbounds i32, ptr %1, i64 %12
  %14 = add nsw i32 %4, 1
  %15 = sext i32 %2 to i64
  br label %16

16:                                               ; preds = %.lr.ph, %18
  %indvars.iv = phi i64 [ %15, %.lr.ph ], [ %indvars.iv.next, %18 ]
  %indvars27 = trunc i64 %indvars.iv to i32
  %17 = sub i32 %10, %indvars27
  %.not23 = icmp slt i32 %17, %11
  br i1 %.not23, label %.critedge, label %18

18:                                               ; preds = %16
  %19 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv
  %20 = load i32, ptr %19, align 4, !tbaa !5
  store i32 %20, ptr %13, align 4, !tbaa !5
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %21 = trunc i64 %indvars.iv.next to i32
  tail call void @print_combinations(ptr noundef %0, ptr noundef %1, i32 noundef %21, i32 noundef %3, i32 noundef %14, i32 noundef %5)
  %exitcond.not = icmp eq i32 %10, %21
  br i1 %exitcond.not, label %.critedge, label %16, !llvm.loop !9

.critedge:                                        ; preds = %16, %18, %9
  ret void
}

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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass