; LLVM IR from Mohammed-Shoaib/Coding-Problems :: CodeChef/Easy (E0045)

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

@.str = external hidden unnamed_addr constant [4 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [3 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local ptr @chefAndGround(i32 noundef %0, i32 noundef %1, ptr noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  store i32 %0, ptr %5, align 4, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  store i32 0, ptr %8, align 4, !tbaa !5
  store i32 -1, ptr %9, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  store i32 0, ptr %10, align 4, !tbaa !5
  br label %13

13:                                               ; preds = %33, %3
  %14 = load i32, ptr %10, align 4, !tbaa !5
  %15 = load i32, ptr %5, align 4, !tbaa !5
  %16 = icmp slt i32 %14, %15
  br i1 %16, label %18, label %17

17:                                               ; preds = %13
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  br label %36

18:                                               ; preds = %13
  %19 = load i32, ptr %9, align 4, !tbaa !5
  %20 = load ptr, ptr %7, align 8, !tbaa !9
  %21 = load i32, ptr %10, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds i32, ptr %20, i64 %22
  %24 = load i32, ptr %23, align 4, !tbaa !5
  %25 = icmp slt i32 %19, %24
  br i1 %25, label %26, label %32

26:                                               ; preds = %18
  %27 = load ptr, ptr %7, align 8, !tbaa !9
  %28 = load i32, ptr %10, align 4, !tbaa !5
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds i32, ptr %27, i64 %29
  %31 = load i32, ptr %30, align 4, !tbaa !5
  store i32 %31, ptr %9, align 4, !tbaa !5
  br label %32

32:                                               ; preds = %26, %18
  br label %33

33:                                               ; preds = %32
  %34 = load i32, ptr %10, align 4, !tbaa !5
  %35 = add nsw i32 %34, 1
  store i32 %35, ptr %10, align 4, !tbaa !5
  br label %13, !llvm.loop !11

36:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  store i32 0, ptr %11, align 4, !tbaa !5
  br label %37

37:                                               ; preds = %52, %36
  %38 = load i32, ptr %11, align 4, !tbaa !5
  %39 = load i32, ptr %5, align 4, !tbaa !5
  %40 = icmp slt i32 %38, %39
  br i1 %40, label %42, label %41

41:                                               ; preds = %37
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  br label %55

42:                                               ; preds = %37
  %43 = load i32, ptr %9, align 4, !tbaa !5
  %44 = load ptr, ptr %7, align 8, !tbaa !9
  %45 = load i32, ptr %11, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds i32, ptr %44, i64 %46
  %48 = load i32, ptr %47, align 4, !tbaa !5
  %49 = sub nsw i32 %43, %48
  %50 = load i32, ptr %8, align 4, !tbaa !5
  %51 = add nsw i32 %50, %49
  store i32 %51, ptr %8, align 4, !tbaa !5
  br label %52

52:                                               ; preds = %42
  %53 = load i32, ptr %11, align 4, !tbaa !5
  %54 = add nsw i32 %53, 1
  store i32 %54, ptr %11, align 4, !tbaa !5
  br label %37, !llvm.loop !13

55:                                               ; preds = %41
  %56 = load i32, ptr %6, align 4, !tbaa !5
  %57 = load i32, ptr %8, align 4, !tbaa !5
  %58 = sub nsw i32 %56, %57
  %59 = load i32, ptr %5, align 4, !tbaa !5
  %60 = srem i32 %58, %59
  %61 = icmp eq i32 %60, 0
  br i1 %61, label %62, label %63

62:                                               ; preds = %55
  store ptr @.str, ptr %4, align 8
  store i32 1, ptr %12, align 4
  br label %64

63:                                               ; preds = %55
  store ptr @.str.1, ptr %4, align 8
  store i32 1, ptr %12, align 4
  br label %64

64:                                               ; preds = %63, %62
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  %65 = load ptr, ptr %4, align 8
  ret ptr %65
}

; 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

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


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

@.str = external hidden unnamed_addr constant [4 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [3 x i8], align 1

; Function Attrs: nofree norecurse nosync nounwind memory(argmem: read) uwtable
define dso_local nonnull ptr @chefAndGround(i32 noundef %0, i32 noundef %1, ptr nocapture noundef readonly %2) local_unnamed_addr #0 {
  %4 = icmp sgt i32 %0, 0
  br i1 %4, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %3
  %wide.trip.count = zext nneg i32 %0 to i64
  %min.iters.check = icmp ult i32 %0, 8
  br i1 %min.iters.check, label %.lr.ph.preheader58, label %vector.ph

.lr.ph.preheader58:                               ; preds = %middle.block, %.lr.ph.preheader
  %indvars.iv.ph = phi i64 [ 0, %.lr.ph.preheader ], [ %n.vec, %middle.block ]
  %.01924.ph = phi i32 [ -1, %.lr.ph.preheader ], [ %10, %middle.block ]
  br label %.lr.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %wide.trip.count, 2147483640
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.phi = phi <4 x i32> [ <i32 -1, i32 -1, i32 -1, i32 -1>, %vector.ph ], [ %7, %vector.body ]
  %vec.phi37 = phi <4 x i32> [ <i32 -1, i32 -1, i32 -1, i32 -1>, %vector.ph ], [ %8, %vector.body ]
  %5 = getelementptr inbounds i32, ptr %2, i64 %index
  %6 = getelementptr inbounds i8, ptr %5, i64 16
  %wide.load = load <4 x i32>, ptr %5, align 4, !tbaa !5
  %wide.load38 = load <4 x i32>, ptr %6, align 4, !tbaa !5
  %7 = tail call <4 x i32> @llvm.smax.v4i32(<4 x i32> %vec.phi, <4 x i32> %wide.load)
  %8 = tail call <4 x i32> @llvm.smax.v4i32(<4 x i32> %vec.phi37, <4 x i32> %wide.load38)
  %index.next = add nuw i64 %index, 8
  %9 = icmp eq i64 %index.next, %n.vec
  br i1 %9, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %rdx.minmax = tail call <4 x i32> @llvm.smax.v4i32(<4 x i32> %7, <4 x i32> %8)
  %10 = tail call i32 @llvm.vector.reduce.smax.v4i32(<4 x i32> %rdx.minmax)
  %cmp.n = icmp eq i64 %n.vec, %wide.trip.count
  br i1 %cmp.n, label %.preheader, label %.lr.ph.preheader58

.preheader:                                       ; preds = %.lr.ph, %middle.block
  %spec.select.lcssa = phi i32 [ %10, %middle.block ], [ %spec.select, %.lr.ph ]
  br i1 %4, label %.lr.ph28.preheader, label %._crit_edge

.lr.ph28.preheader:                               ; preds = %.preheader
  %wide.trip.count34 = zext nneg i32 %0 to i64
  %min.iters.check41 = icmp ult i32 %0, 8
  br i1 %min.iters.check41, label %.lr.ph28.preheader55, label %vector.ph42

.lr.ph28.preheader55:                             ; preds = %middle.block39, %.lr.ph28.preheader
  %indvars.iv31.ph = phi i64 [ 0, %.lr.ph28.preheader ], [ %n.vec44, %middle.block39 ]
  %.02026.ph = phi i32 [ 0, %.lr.ph28.preheader ], [ %18, %middle.block39 ]
  br label %.lr.ph28

vector.ph42:                                      ; preds = %.lr.ph28.preheader
  %n.vec44 = and i64 %wide.trip.count, 2147483640
  %broadcast.splatinsert = insertelement <4 x i32> poison, i32 %spec.select.lcssa, i64 0
  %broadcast.splat = shufflevector <4 x i32> %broadcast.splatinsert, <4 x i32> poison, <4 x i32> zeroinitializer
  br label %vector.body46

vector.body46:                                    ; preds = %vector.body46, %vector.ph42
  %index47 = phi i64 [ 0, %vector.ph42 ], [ %index.next52, %vector.body46 ]
  %vec.phi48 = phi <4 x i32> [ zeroinitializer, %vector.ph42 ], [ %15, %vector.body46 ]
  %vec.phi49 = phi <4 x i32> [ zeroinitializer, %vector.ph42 ], [ %16, %vector.body46 ]
  %11 = getelementptr inbounds i32, ptr %2, i64 %index47
  %12 = getelementptr inbounds i8, ptr %11, i64 16
  %wide.load50 = load <4 x i32>, ptr %11, align 4, !tbaa !5
  %wide.load51 = load <4 x i32>, ptr %12, align 4, !tbaa !5
  %13 = add <4 x i32> %vec.phi48, %broadcast.splat
  %14 = add <4 x i32> %vec.phi49, %broadcast.splat
  %15 = sub <4 x i32> %13, %wide.load50
  %16 = sub <4 x i32> %14, %wide.load51
  %index.next52 = add nuw i64 %index47, 8
  %17 = icmp eq i64 %index.next52, %n.vec44
  br i1 %17, label %middle.block39, label %vector.body46, !llvm.loop !13

middle.block39:                                   ; preds = %vector.body46
  %bin.rdx = add <4 x i32> %16, %15
  %18 = tail call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %bin.rdx)
  %cmp.n53 = icmp eq i64 %n.vec44, %wide.trip.count
  br i1 %cmp.n53, label %._crit_edge, label %.lr.ph28.preheader55

.lr.ph:                                           ; preds = %.lr.ph.preheader58, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ %indvars.iv.ph, %.lr.ph.preheader58 ]
  %.01924 = phi i32 [ %spec.select, %.lr.ph ], [ %.01924.ph, %.lr.ph.preheader58 ]
  %19 = getelementptr inbounds i32, ptr %2, i64 %indvars.iv
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %spec.select = tail call i32 @llvm.smax.i32(i32 %.01924, i32 %20)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %.preheader, label %.lr.ph, !llvm.loop !14

._crit_edge:                                      ; preds = %.lr.ph28, %middle.block39, %3, %.preheader
  %.020.lcssa = phi i32 [ 0, %.preheader ], [ 0, %3 ], [ %18, %middle.block39 ], [ %27, %.lr.ph28 ]
  %21 = sub nsw i32 %1, %.020.lcssa
  %22 = srem i32 %21, %0
  %23 = icmp eq i32 %22, 0
  %.str..str.1 = select i1 %23, ptr @.str, ptr @.str.1
  ret ptr %.str..str.1

.lr.ph28:                                         ; preds = %.lr.ph28.preheader55, %.lr.ph28
  %indvars.iv31 = phi i64 [ %indvars.iv.next32, %.lr.ph28 ], [ %indvars.iv31.ph, %.lr.ph28.preheader55 ]
  %.02026 = phi i32 [ %27, %.lr.ph28 ], [ %.02026.ph, %.lr.ph28.preheader55 ]
  %24 = getelementptr inbounds i32, ptr %2, i64 %indvars.iv31
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %26 = add i32 %.02026, %spec.select.lcssa
  %27 = sub i32 %26, %25
  %indvars.iv.next32 = add nuw nsw i64 %indvars.iv31, 1
  %exitcond35.not = icmp eq i64 %indvars.iv.next32, %wide.trip.count34
  br i1 %exitcond35.not, label %._crit_edge, label %.lr.ph28, !llvm.loop !15
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare <4 x i32> @llvm.smax.v4i32(<4 x i32>, <4 x i32>) #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.vector.reduce.smax.v4i32(<4 x i32>) #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.vector.reduce.add.v4i32(<4 x i32>) #1

attributes #0 = { nofree norecurse nosync nounwind memory(argmem: read) 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 speculatable willreturn memory(none) }

!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, !11, !12}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!"llvm.loop.isvectorized", i32 1}
!12 = !{!"llvm.loop.unroll.runtime.disable"}
!13 = distinct !{!13, !10, !11, !12}
!14 = distinct !{!14, !10, !12, !11}
!15 = distinct !{!15, !10, !12, !11}


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