; LLVM IR from ritikbanger/Hacktoberfest2022-DSA :: 30. CPP Programs/Median of two sorted array.ll (Z9getMedianPiS_i)

; 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: mustprogress uwtable
define dso_local noundef i32 @_Z9getMedianPiS_i(ptr noundef %0, ptr noundef %1, i32 noundef %2) #0 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !9
  %11 = load i32, ptr %7, align 4, !tbaa !9
  %12 = icmp sle i32 %11, 0
  br i1 %12, label %13, label %14

13:                                               ; preds = %3
  store i32 -1, ptr %4, align 4
  br label %121

14:                                               ; preds = %3
  %15 = load i32, ptr %7, align 4, !tbaa !9
  %16 = icmp eq i32 %15, 1
  br i1 %16, label %17, label %26

17:                                               ; preds = %14
  %18 = load ptr, ptr %5, align 8, !tbaa !5
  %19 = getelementptr inbounds i32, ptr %18, i64 0
  %20 = load i32, ptr %19, align 4, !tbaa !9
  %21 = load ptr, ptr %6, align 8, !tbaa !5
  %22 = getelementptr inbounds i32, ptr %21, i64 0
  %23 = load i32, ptr %22, align 4, !tbaa !9
  %24 = add nsw i32 %20, %23
  %25 = sdiv i32 %24, 2
  store i32 %25, ptr %4, align 4
  br label %121

26:                                               ; preds = %14
  %27 = load i32, ptr %7, align 4, !tbaa !9
  %28 = icmp eq i32 %27, 2
  br i1 %28, label %29, label %44

29:                                               ; preds = %26
  %30 = load ptr, ptr %5, align 8, !tbaa !5
  %31 = getelementptr inbounds i32, ptr %30, i64 0
  %32 = load ptr, ptr %6, align 8, !tbaa !5
  %33 = getelementptr inbounds i32, ptr %32, i64 0
  %34 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %31, ptr noundef nonnull align 4 dereferenceable(4) %33)
  %35 = load i32, ptr %34, align 4, !tbaa !9
  %36 = load ptr, ptr %5, align 8, !tbaa !5
  %37 = getelementptr inbounds i32, ptr %36, i64 1
  %38 = load ptr, ptr %6, align 8, !tbaa !5
  %39 = getelementptr inbounds i32, ptr %38, i64 1
  %40 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %37, ptr noundef nonnull align 4 dereferenceable(4) %39)
  %41 = load i32, ptr %40, align 4, !tbaa !9
  %42 = add nsw i32 %35, %41
  %43 = sdiv i32 %42, 2
  store i32 %43, ptr %4, align 4
  br label %121

44:                                               ; preds = %26
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  %45 = load ptr, ptr %5, align 8, !tbaa !5
  %46 = load i32, ptr %7, align 4, !tbaa !9
  %47 = call noundef i32 @_Z6medianPii(ptr noundef %45, i32 noundef %46)
  store i32 %47, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  %48 = load ptr, ptr %6, align 8, !tbaa !5
  %49 = load i32, ptr %7, align 4, !tbaa !9
  %50 = call noundef i32 @_Z6medianPii(ptr noundef %48, i32 noundef %49)
  store i32 %50, ptr %9, align 4, !tbaa !9
  %51 = load i32, ptr %8, align 4, !tbaa !9
  %52 = load i32, ptr %9, align 4, !tbaa !9
  %53 = icmp eq i32 %51, %52
  br i1 %53, label %54, label %56

54:                                               ; preds = %44
  %55 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %55, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %120

56:                                               ; preds = %44
  %57 = load i32, ptr %8, align 4, !tbaa !9
  %58 = load i32, ptr %9, align 4, !tbaa !9
  %59 = icmp slt i32 %57, %58
  br i1 %59, label %60, label %90

60:                                               ; preds = %56
  %61 = load i32, ptr %7, align 4, !tbaa !9
  %62 = srem i32 %61, 2
  %63 = icmp eq i32 %62, 0
  br i1 %63, label %64, label %78

64:                                               ; preds = %60
  %65 = load ptr, ptr %5, align 8, !tbaa !5
  %66 = load i32, ptr %7, align 4, !tbaa !9
  %67 = sdiv i32 %66, 2
  %68 = sext i32 %67 to i64
  %69 = getelementptr inbounds i32, ptr %65, i64 %68
  %70 = getelementptr inbounds i32, ptr %69, i64 -1
  %71 = load ptr, ptr %6, align 8, !tbaa !5
  %72 = load i32, ptr %7, align 4, !tbaa !9
  %73 = load i32, ptr %7, align 4, !tbaa !9
  %74 = sdiv i32 %73, 2
  %75 = sub nsw i32 %72, %74
  %76 = add nsw i32 %75, 1
  %77 = call noundef i32 @_Z9getMedianPiS_i(ptr noundef %70, ptr noundef %71, i32 noundef %76)
  store i32 %77, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %120

78:                                               ; preds = %60
  %79 = load ptr, ptr %5, align 8, !tbaa !5
  %80 = load i32, ptr %7, align 4, !tbaa !9
  %81 = sdiv i32 %80, 2
  %82 = sext i32 %81 to i64
  %83 = getelementptr inbounds i32, ptr %79, i64 %82
  %84 = load ptr, ptr %6, align 8, !tbaa !5
  %85 = load i32, ptr %7, align 4, !tbaa !9
  %86 = load i32, ptr %7, align 4, !tbaa !9
  %87 = sdiv i32 %86, 2
  %88 = sub nsw i32 %85, %87
  %89 = call noundef i32 @_Z9getMedianPiS_i(ptr noundef %83, ptr noundef %84, i32 noundef %88)
  store i32 %89, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %120

90:                                               ; preds = %56
  %91 = load i32, ptr %7, align 4, !tbaa !9
  %92 = srem i32 %91, 2
  %93 = icmp eq i32 %92, 0
  br i1 %93, label %94, label %108

94:                                               ; preds = %90
  %95 = load ptr, ptr %6, align 8, !tbaa !5
  %96 = load i32, ptr %7, align 4, !tbaa !9
  %97 = sdiv i32 %96, 2
  %98 = sext i32 %97 to i64
  %99 = getelementptr inbounds i32, ptr %95, i64 %98
  %100 = getelementptr inbounds i32, ptr %99, i64 -1
  %101 = load ptr, ptr %5, align 8, !tbaa !5
  %102 = load i32, ptr %7, align 4, !tbaa !9
  %103 = load i32, ptr %7, align 4, !tbaa !9
  %104 = sdiv i32 %103, 2
  %105 = sub nsw i32 %102, %104
  %106 = add nsw i32 %105, 1
  %107 = call noundef i32 @_Z9getMedianPiS_i(ptr noundef %100, ptr noundef %101, i32 noundef %106)
  store i32 %107, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %120

108:                                              ; preds = %90
  %109 = load ptr, ptr %6, align 8, !tbaa !5
  %110 = load i32, ptr %7, align 4, !tbaa !9
  %111 = sdiv i32 %110, 2
  %112 = sext i32 %111 to i64
  %113 = getelementptr inbounds i32, ptr %109, i64 %112
  %114 = load ptr, ptr %5, align 8, !tbaa !5
  %115 = load i32, ptr %7, align 4, !tbaa !9
  %116 = load i32, ptr %7, align 4, !tbaa !9
  %117 = sdiv i32 %116, 2
  %118 = sub nsw i32 %115, %117
  %119 = call noundef i32 @_Z9getMedianPiS_i(ptr noundef %113, ptr noundef %114, i32 noundef %118)
  store i32 %119, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %120

120:                                              ; preds = %108, %94, %78, %64, %54
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  br label %121

121:                                              ; preds = %120, %29, %17, %13
  %122 = load i32, ptr %4, align 4
  ret i32 %122
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) #1

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z6medianPii(ptr noundef, i32 noundef) #3

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #2

attributes #0 = { mustprogress 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 = { inlinehint 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 #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 #4 = { 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}


; 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 nounwind uwtable
define dso_local noundef i32 @_Z9getMedianPiS_i(ptr noundef %0, ptr noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = icmp slt i32 %2, 1
  br i1 %4, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %3, %tailrecurse.outer.backedge
  %.tr45.ph61 = phi i32 [ %30, %tailrecurse.outer.backedge ], [ %2, %3 ]
  %.tr44.ph60 = phi ptr [ %.tr55, %tailrecurse.outer.backedge ], [ %1, %3 ]
  %.tr.ph59 = phi ptr [ %.tr.ph.be, %tailrecurse.outer.backedge ], [ %0, %3 ]
  br label %5

5:                                                ; preds = %.lr.ph, %tailrecurse.backedge
  %.tr4556 = phi i32 [ %.tr45.ph61, %.lr.ph ], [ %30, %tailrecurse.backedge ]
  %.tr55 = phi ptr [ %.tr.ph59, %.lr.ph ], [ %.tr.be, %tailrecurse.backedge ]
  switch i32 %.tr4556, label %20 [
    i32 1, label %6
    i32 2, label %11
  ]

6:                                                ; preds = %5
  %7 = load i32, ptr %.tr55, align 4, !tbaa !5
  %8 = load i32, ptr %.tr44.ph60, align 4, !tbaa !5
  %9 = add nsw i32 %8, %7
  %10 = sdiv i32 %9, 2
  br label %.loopexit

11:                                               ; preds = %5
  %12 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %.tr55, ptr noundef nonnull align 4 dereferenceable(4) %.tr44.ph60)
  %13 = load i32, ptr %12, align 4, !tbaa !5
  %14 = getelementptr inbounds i8, ptr %.tr55, i64 4
  %15 = getelementptr inbounds i8, ptr %.tr44.ph60, i64 4
  %16 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %14, ptr noundef nonnull align 4 dereferenceable(4) %15)
  %17 = load i32, ptr %16, align 4, !tbaa !5
  %18 = add nsw i32 %17, %13
  %19 = sdiv i32 %18, 2
  br label %.loopexit

20:                                               ; preds = %5
  %21 = tail call noundef i32 @_Z6medianPii(ptr noundef %.tr55, i32 noundef %.tr4556)
  %22 = tail call noundef i32 @_Z6medianPii(ptr noundef %.tr44.ph60, i32 noundef %.tr4556)
  %23 = icmp eq i32 %21, %22
  br i1 %23, label %.loopexit, label %24

24:                                               ; preds = %20
  %25 = icmp slt i32 %21, %22
  %26 = and i32 %.tr4556, 1
  %27 = icmp eq i32 %26, 0
  %28 = lshr i32 %.tr4556, 1
  %29 = zext nneg i32 %28 to i64
  %.sink = or i32 %.tr4556, 1
  %.tr.be.idx = select i1 %27, i64 -4, i64 0
  %30 = sub i32 %.sink, %28
  %31 = icmp slt i32 %30, 1
  br i1 %25, label %tailrecurse.backedge, label %tailrecurse.outer.backedge

tailrecurse.backedge:                             ; preds = %24
  %32 = getelementptr inbounds i32, ptr %.tr55, i64 %29
  %.tr.be = getelementptr inbounds i8, ptr %32, i64 %.tr.be.idx
  br i1 %31, label %.loopexit, label %5

tailrecurse.outer.backedge:                       ; preds = %24
  %33 = getelementptr inbounds i32, ptr %.tr44.ph60, i64 %29
  %.tr.ph.be = getelementptr inbounds i8, ptr %33, i64 %.tr.be.idx
  br i1 %31, label %.loopexit, label %.lr.ph

.loopexit:                                        ; preds = %tailrecurse.outer.backedge, %tailrecurse.backedge, %20, %3, %11, %6
  %.1 = phi i32 [ %10, %6 ], [ %19, %11 ], [ -1, %3 ], [ -1, %tailrecurse.backedge ], [ %21, %20 ], [ -1, %tailrecurse.outer.backedge ]
  ret i32 %.1
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #1

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z6medianPii(ptr noundef, i32 noundef) local_unnamed_addr #0

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 = { inlinehint 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" }

!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: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass