; LLVM IR from smv1999/CompetitiveProgrammingQuestionBank :: DSA 450 GFG/maximum and minimum of an array.ll (Z9getMinMaxPii)

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

%struct.Pair = type { i32, i32 }

; Function Attrs: mustprogress nounwind uwtable
define dso_local i64 @_Z9getMinMaxPii(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca %struct.Pair, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %8 = load i32, ptr %5, align 4, !tbaa !9
  %9 = icmp eq i32 %8, 1
  br i1 %9, label %10, label %19

10:                                               ; preds = %2
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  %12 = getelementptr inbounds i32, ptr %11, i64 0
  %13 = load i32, ptr %12, align 4, !tbaa !9
  %14 = getelementptr inbounds %struct.Pair, ptr %3, i32 0, i32 1
  store i32 %13, ptr %14, align 4, !tbaa !11
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  %16 = getelementptr inbounds i32, ptr %15, i64 0
  %17 = load i32, ptr %16, align 4, !tbaa !9
  %18 = getelementptr inbounds %struct.Pair, ptr %3, i32 0, i32 0
  store i32 %17, ptr %18, align 4, !tbaa !13
  store i32 1, ptr %7, align 4
  br label %88

19:                                               ; preds = %2
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = getelementptr inbounds i32, ptr %20, i64 0
  %22 = load i32, ptr %21, align 4, !tbaa !9
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  %24 = getelementptr inbounds i32, ptr %23, i64 1
  %25 = load i32, ptr %24, align 4, !tbaa !9
  %26 = icmp sgt i32 %22, %25
  br i1 %26, label %27, label %36

27:                                               ; preds = %19
  %28 = load ptr, ptr %4, align 8, !tbaa !5
  %29 = getelementptr inbounds i32, ptr %28, i64 0
  %30 = load i32, ptr %29, align 4, !tbaa !9
  %31 = getelementptr inbounds %struct.Pair, ptr %3, i32 0, i32 1
  store i32 %30, ptr %31, align 4, !tbaa !11
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = getelementptr inbounds i32, ptr %32, i64 1
  %34 = load i32, ptr %33, align 4, !tbaa !9
  %35 = getelementptr inbounds %struct.Pair, ptr %3, i32 0, i32 0
  store i32 %34, ptr %35, align 4, !tbaa !13
  br label %45

36:                                               ; preds = %19
  %37 = load ptr, ptr %4, align 8, !tbaa !5
  %38 = getelementptr inbounds i32, ptr %37, i64 1
  %39 = load i32, ptr %38, align 4, !tbaa !9
  %40 = getelementptr inbounds %struct.Pair, ptr %3, i32 0, i32 1
  store i32 %39, ptr %40, align 4, !tbaa !11
  %41 = load ptr, ptr %4, align 8, !tbaa !5
  %42 = getelementptr inbounds i32, ptr %41, i64 0
  %43 = load i32, ptr %42, align 4, !tbaa !9
  %44 = getelementptr inbounds %struct.Pair, ptr %3, i32 0, i32 0
  store i32 %43, ptr %44, align 4, !tbaa !13
  br label %45

45:                                               ; preds = %36, %27
  store i32 2, ptr %6, align 4, !tbaa !9
  br label %46

46:                                               ; preds = %84, %45
  %47 = load i32, ptr %6, align 4, !tbaa !9
  %48 = load i32, ptr %5, align 4, !tbaa !9
  %49 = icmp slt i32 %47, %48
  br i1 %49, label %50, label %87

50:                                               ; preds = %46
  %51 = load ptr, ptr %4, align 8, !tbaa !5
  %52 = load i32, ptr %6, align 4, !tbaa !9
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds i32, ptr %51, i64 %53
  %55 = load i32, ptr %54, align 4, !tbaa !9
  %56 = getelementptr inbounds %struct.Pair, ptr %3, i32 0, i32 1
  %57 = load i32, ptr %56, align 4, !tbaa !11
  %58 = icmp sgt i32 %55, %57
  br i1 %58, label %59, label %66

59:                                               ; preds = %50
  %60 = load ptr, ptr %4, align 8, !tbaa !5
  %61 = load i32, ptr %6, align 4, !tbaa !9
  %62 = sext i32 %61 to i64
  %63 = getelementptr inbounds i32, ptr %60, i64 %62
  %64 = load i32, ptr %63, align 4, !tbaa !9
  %65 = getelementptr inbounds %struct.Pair, ptr %3, i32 0, i32 1
  store i32 %64, ptr %65, align 4, !tbaa !11
  br label %83

66:                                               ; preds = %50
  %67 = load ptr, ptr %4, align 8, !tbaa !5
  %68 = load i32, ptr %6, align 4, !tbaa !9
  %69 = sext i32 %68 to i64
  %70 = getelementptr inbounds i32, ptr %67, i64 %69
  %71 = load i32, ptr %70, align 4, !tbaa !9
  %72 = getelementptr inbounds %struct.Pair, ptr %3, i32 0, i32 0
  %73 = load i32, ptr %72, align 4, !tbaa !13
  %74 = icmp slt i32 %71, %73
  br i1 %74, label %75, label %82

75:                                               ; preds = %66
  %76 = load ptr, ptr %4, align 8, !tbaa !5
  %77 = load i32, ptr %6, align 4, !tbaa !9
  %78 = sext i32 %77 to i64
  %79 = getelementptr inbounds i32, ptr %76, i64 %78
  %80 = load i32, ptr %79, align 4, !tbaa !9
  %81 = getelementptr inbounds %struct.Pair, ptr %3, i32 0, i32 0
  store i32 %80, ptr %81, align 4, !tbaa !13
  br label %82

82:                                               ; preds = %75, %66
  br label %83

83:                                               ; preds = %82, %59
  br label %84

84:                                               ; preds = %83
  %85 = load i32, ptr %6, align 4, !tbaa !9
  %86 = add nsw i32 %85, 1
  store i32 %86, ptr %6, align 4, !tbaa !9
  br label %46, !llvm.loop !14

87:                                               ; preds = %46
  store i32 1, ptr %7, align 4
  br label %88

88:                                               ; preds = %87, %10
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  %89 = load i64, ptr %3, align 4
  ret i64 %89
}

; 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 = { 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 #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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 4}
!12 = !{!"_ZTS4Pair", !10, i64 0, !10, i64 4}
!13 = !{!12, !10, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"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: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: read) uwtable
define dso_local i64 @_Z9getMinMaxPii(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp eq i32 %1, 1
  %4 = load i32, ptr %0, align 4, !tbaa !5
  br i1 %3, label %.loopexit, label %5

5:                                                ; preds = %2
  %6 = getelementptr inbounds i8, ptr %0, i64 4
  %7 = load i32, ptr %6, align 4, !tbaa !5
  %. = tail call i32 @llvm.smin.i32(i32 %4, i32 %7)
  %.26 = tail call i32 @llvm.smax.i32(i32 %4, i32 %7)
  %8 = icmp sgt i32 %1, 2
  br i1 %8, label %.lr.ph.preheader, label %.loopexit

.lr.ph.preheader:                                 ; preds = %5
  %wide.trip.count = zext nneg i32 %1 to i64
  %xtraiter = and i64 %wide.trip.count, 1
  %9 = icmp eq i32 %1, 3
  br i1 %9, label %.loopexit.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %10 = and i64 %wide.trip.count, 2147483646
  %11 = add nsw i64 %10, -4
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %indvars.iv = phi i64 [ 2, %.lr.ph.preheader.new ], [ %indvars.iv.next.1, %.lr.ph ]
  %.sroa.6.128 = phi i32 [ %.26, %.lr.ph.preheader.new ], [ %.sroa.6.2.1, %.lr.ph ]
  %.sroa.0.127 = phi i32 [ %., %.lr.ph.preheader.new ], [ %.sroa.0.2.1, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %.lr.ph ]
  %12 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv
  %13 = load i32, ptr %12, align 4, !tbaa !5
  %14 = icmp sgt i32 %13, %.sroa.6.128
  %spec.select = tail call i32 @llvm.smin.i32(i32 %13, i32 %.sroa.0.127)
  %.sroa.0.2 = select i1 %14, i32 %.sroa.0.127, i32 %spec.select
  %.sroa.6.2 = tail call i32 @llvm.smax.i32(i32 %13, i32 %.sroa.6.128)
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %15 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.next
  %16 = load i32, ptr %15, align 4, !tbaa !5
  %17 = icmp sgt i32 %16, %.sroa.6.2
  %spec.select.1 = tail call i32 @llvm.smin.i32(i32 %16, i32 %.sroa.0.2)
  %.sroa.0.2.1 = select i1 %17, i32 %.sroa.0.2, i32 %spec.select.1
  %.sroa.6.2.1 = tail call i32 @llvm.smax.i32(i32 %16, i32 %.sroa.6.2)
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter, %11
  br i1 %niter.ncmp.1, label %.loopexit.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !9

.loopexit.loopexit.unr-lcssa:                     ; preds = %.lr.ph, %.lr.ph.preheader
  %.sroa.0.2.lcssa.ph = phi i32 [ poison, %.lr.ph.preheader ], [ %.sroa.0.2.1, %.lr.ph ]
  %.sroa.6.2.lcssa.ph = phi i32 [ poison, %.lr.ph.preheader ], [ %.sroa.6.2.1, %.lr.ph ]
  %indvars.iv.unr = phi i64 [ 2, %.lr.ph.preheader ], [ %indvars.iv.next.1, %.lr.ph ]
  %.sroa.6.128.unr = phi i32 [ %.26, %.lr.ph.preheader ], [ %.sroa.6.2.1, %.lr.ph ]
  %.sroa.0.127.unr = phi i32 [ %., %.lr.ph.preheader ], [ %.sroa.0.2.1, %.lr.ph ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.loopexit, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %.loopexit.loopexit.unr-lcssa
  %18 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.unr
  %19 = load i32, ptr %18, align 4, !tbaa !5
  %20 = icmp sgt i32 %19, %.sroa.6.128.unr
  %spec.select.epil = tail call i32 @llvm.smin.i32(i32 %19, i32 %.sroa.0.127.unr)
  %.sroa.0.2.epil = select i1 %20, i32 %.sroa.0.127.unr, i32 %spec.select.epil
  %.sroa.6.2.epil = tail call i32 @llvm.smax.i32(i32 %19, i32 %.sroa.6.128.unr)
  br label %.loopexit

.loopexit:                                        ; preds = %.lr.ph.epil, %.loopexit.loopexit.unr-lcssa, %2, %5
  %.sroa.0.3 = phi i32 [ %., %5 ], [ %4, %2 ], [ %.sroa.0.2.lcssa.ph, %.loopexit.loopexit.unr-lcssa ], [ %.sroa.0.2.epil, %.lr.ph.epil ]
  %.sroa.6.3 = phi i32 [ %.26, %5 ], [ %4, %2 ], [ %.sroa.6.2.lcssa.ph, %.loopexit.loopexit.unr-lcssa ], [ %.sroa.6.2.epil, %.lr.ph.epil ]
  %.sroa.6.0.insert.ext = zext i32 %.sroa.6.3 to i64
  %.sroa.6.0.insert.shift = shl nuw i64 %.sroa.6.0.insert.ext, 32
  %.sroa.0.0.insert.ext = zext i32 %.sroa.0.3 to i64
  %.sroa.0.0.insert.insert = or disjoint i64 %.sroa.6.0.insert.shift, %.sroa.0.0.insert.ext
  ret i64 %.sroa.0.0.insert.insert
}

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

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

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn 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++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


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