; LLVM IR from ravireddy07/cpp-book :: src/Code-Chef/MAR2021-Long/PAPARAZI.ll (Z8getCrossSt4pairIxxES0_S0_)

; 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.std::pair" = type { i64, i64 }

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

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z8getCrossSt4pairIxxES0_S0_(i64 %0, i64 %1, i64 %2, i64 %3, i64 %4, i64 %5) #1 {
  %7 = alloca i64, align 8
  %8 = alloca %"struct.std::pair", align 8
  %9 = alloca %"struct.std::pair", align 8
  %10 = alloca %"struct.std::pair", align 8
  %11 = alloca i64, align 8
  %12 = alloca i64, align 8
  %13 = alloca i32, align 4
  %14 = getelementptr inbounds { i64, i64 }, ptr %8, i32 0, i32 0
  store i64 %0, ptr %14, align 8
  %15 = getelementptr inbounds { i64, i64 }, ptr %8, i32 0, i32 1
  store i64 %1, ptr %15, align 8
  %16 = getelementptr inbounds { i64, i64 }, ptr %9, i32 0, i32 0
  store i64 %2, ptr %16, align 8
  %17 = getelementptr inbounds { i64, i64 }, ptr %9, i32 0, i32 1
  store i64 %3, ptr %17, align 8
  %18 = getelementptr inbounds { i64, i64 }, ptr %10, i32 0, i32 0
  store i64 %4, ptr %18, align 8
  %19 = getelementptr inbounds { i64, i64 }, ptr %10, i32 0, i32 1
  store i64 %5, ptr %19, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #2
  %20 = getelementptr inbounds %"struct.std::pair", ptr %9, i32 0, i32 1
  %21 = load i64, ptr %20, align 8, !tbaa !5
  %22 = getelementptr inbounds %"struct.std::pair", ptr %8, i32 0, i32 1
  %23 = load i64, ptr %22, align 8, !tbaa !5
  %24 = sub nsw i64 %21, %23
  %25 = getelementptr inbounds %"struct.std::pair", ptr %10, i32 0, i32 0
  %26 = load i64, ptr %25, align 8, !tbaa !10
  %27 = getelementptr inbounds %"struct.std::pair", ptr %9, i32 0, i32 0
  %28 = load i64, ptr %27, align 8, !tbaa !10
  %29 = sub nsw i64 %26, %28
  %30 = mul nsw i64 %24, %29
  store i64 %30, ptr %11, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #2
  %31 = getelementptr inbounds %"struct.std::pair", ptr %9, i32 0, i32 0
  %32 = load i64, ptr %31, align 8, !tbaa !10
  %33 = getelementptr inbounds %"struct.std::pair", ptr %8, i32 0, i32 0
  %34 = load i64, ptr %33, align 8, !tbaa !10
  %35 = sub nsw i64 %32, %34
  %36 = getelementptr inbounds %"struct.std::pair", ptr %10, i32 0, i32 1
  %37 = load i64, ptr %36, align 8, !tbaa !5
  %38 = getelementptr inbounds %"struct.std::pair", ptr %9, i32 0, i32 1
  %39 = load i64, ptr %38, align 8, !tbaa !5
  %40 = sub nsw i64 %37, %39
  %41 = mul nsw i64 %35, %40
  store i64 %41, ptr %12, align 8, !tbaa !11
  %42 = load i64, ptr %11, align 8, !tbaa !11
  %43 = load i64, ptr %12, align 8, !tbaa !11
  %44 = sub nsw i64 %42, %43
  %45 = icmp sgt i64 %44, 0
  br i1 %45, label %46, label %47

46:                                               ; preds = %6
  store i64 1, ptr %7, align 8
  store i32 1, ptr %13, align 4
  br label %54

47:                                               ; preds = %6
  %48 = load i64, ptr %11, align 8, !tbaa !11
  %49 = load i64, ptr %12, align 8, !tbaa !11
  %50 = sub nsw i64 %48, %49
  %51 = icmp eq i64 %50, 0
  br i1 %51, label %52, label %53

52:                                               ; preds = %47
  store i64 0, ptr %7, align 8
  store i32 1, ptr %13, align 4
  br label %54

53:                                               ; preds = %47
  store i64 2, ptr %7, align 8
  store i32 1, ptr %13, align 4
  br label %54

54:                                               ; preds = %53, %52, %46
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #2
  %55 = load i64, ptr %7, align 8
  ret i64 %55
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 = { 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, !7, i64 8}
!6 = !{!"_ZTSSt4pairIxxE", !7, i64 0, !7, i64 8}
!7 = !{!"long long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!6, !7, i64 0}
!11 = !{!7, !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 nofree norecurse nosync nounwind willreturn memory(none) uwtable
define dso_local noundef range(i64 0, 3) i64 @_Z8getCrossSt4pairIxxES0_S0_(i64 %0, i64 %1, i64 %2, i64 %3, i64 %4, i64 %5) local_unnamed_addr #0 {
  %7 = sub nsw i64 %3, %1
  %8 = sub nsw i64 %4, %2
  %9 = mul nsw i64 %8, %7
  %10 = sub nsw i64 %2, %0
  %.neg = sub i64 %3, %5
  %.neg11 = mul i64 %.neg, %10
  %11 = add i64 %.neg11, %9
  %12 = icmp eq i64 %11, 0
  %. = select i1 %12, i64 0, i64 2
  %.inv = icmp slt i64 %11, 1
  %.0 = select i1 %.inv, i64 %., i64 1
  ret i64 %.0
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(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)"}


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