; LLVM IR from UtkarshPathrabe/Competitive-Coding :: SPOJ Solutions/KGSS.ll (Z5queryiiiii)

; 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.sum = type { i64, i64 }

@tree = external dso_local global [400001 x %struct.sum], align 16

; 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 nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) #2

; Function Attrs: mustprogress uwtable
define dso_local { i64, i64 } @_Z5queryiiiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4) #3 {
  %6 = alloca %struct.sum, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca %struct.sum, align 8
  %13 = alloca %struct.sum, align 8
  %14 = alloca i64, align 8
  store i32 %0, ptr %7, align 4, !tbaa !5
  store i32 %1, ptr %8, align 4, !tbaa !5
  store i32 %2, ptr %9, align 4, !tbaa !5
  store i32 %3, ptr %10, align 4, !tbaa !5
  store i32 %4, ptr %11, align 4, !tbaa !5
  %15 = load i32, ptr %8, align 4, !tbaa !5
  %16 = load i32, ptr %9, align 4, !tbaa !5
  %17 = icmp sgt i32 %15, %16
  br i1 %17, label %26, label %18

18:                                               ; preds = %5
  %19 = load i32, ptr %8, align 4, !tbaa !5
  %20 = load i32, ptr %11, align 4, !tbaa !5
  %21 = icmp sgt i32 %19, %20
  br i1 %21, label %26, label %22

22:                                               ; preds = %18
  %23 = load i32, ptr %9, align 4, !tbaa !5
  %24 = load i32, ptr %10, align 4, !tbaa !5
  %25 = icmp slt i32 %23, %24
  br i1 %25, label %26, label %29

26:                                               ; preds = %22, %18, %5
  %27 = getelementptr inbounds %struct.sum, ptr %6, i32 0, i32 0
  store i64 0, ptr %27, align 8, !tbaa !9
  %28 = getelementptr inbounds %struct.sum, ptr %6, i32 0, i32 1
  store i64 0, ptr %28, align 8, !tbaa !12
  br label %88

29:                                               ; preds = %22
  %30 = load i32, ptr %8, align 4, !tbaa !5
  %31 = load i32, ptr %10, align 4, !tbaa !5
  %32 = icmp sge i32 %30, %31
  br i1 %32, label %33, label %41

33:                                               ; preds = %29
  %34 = load i32, ptr %9, align 4, !tbaa !5
  %35 = load i32, ptr %11, align 4, !tbaa !5
  %36 = icmp sle i32 %34, %35
  br i1 %36, label %37, label %41

37:                                               ; preds = %33
  %38 = load i32, ptr %7, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [400001 x %struct.sum], ptr @tree, i64 0, i64 %39
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %6, ptr align 16 %40, i64 16, i1 false), !tbaa.struct !13
  br label %88

41:                                               ; preds = %33, %29
  call void @llvm.lifetime.start.p0(i64 16, ptr %12) #4
  %42 = load i32, ptr %7, align 4, !tbaa !5
  %43 = mul nsw i32 %42, 2
  %44 = load i32, ptr %8, align 4, !tbaa !5
  %45 = load i32, ptr %8, align 4, !tbaa !5
  %46 = load i32, ptr %9, align 4, !tbaa !5
  %47 = add nsw i32 %45, %46
  %48 = sdiv i32 %47, 2
  %49 = load i32, ptr %10, align 4, !tbaa !5
  %50 = load i32, ptr %11, align 4, !tbaa !5
  %51 = call { i64, i64 } @_Z5queryiiiii(i32 noundef %43, i32 noundef %44, i32 noundef %48, i32 noundef %49, i32 noundef %50)
  %52 = getelementptr inbounds { i64, i64 }, ptr %12, i32 0, i32 0
  %53 = extractvalue { i64, i64 } %51, 0
  store i64 %53, ptr %52, align 8
  %54 = getelementptr inbounds { i64, i64 }, ptr %12, i32 0, i32 1
  %55 = extractvalue { i64, i64 } %51, 1
  store i64 %55, ptr %54, align 8
  call void @llvm.lifetime.start.p0(i64 16, ptr %13) #4
  %56 = load i32, ptr %7, align 4, !tbaa !5
  %57 = mul nsw i32 %56, 2
  %58 = add nsw i32 %57, 1
  %59 = load i32, ptr %8, align 4, !tbaa !5
  %60 = load i32, ptr %9, align 4, !tbaa !5
  %61 = add nsw i32 %59, %60
  %62 = sdiv i32 %61, 2
  %63 = add nsw i32 %62, 1
  %64 = load i32, ptr %9, align 4, !tbaa !5
  %65 = load i32, ptr %10, align 4, !tbaa !5
  %66 = load i32, ptr %11, align 4, !tbaa !5
  %67 = call { i64, i64 } @_Z5queryiiiii(i32 noundef %58, i32 noundef %63, i32 noundef %64, i32 noundef %65, i32 noundef %66)
  %68 = getelementptr inbounds { i64, i64 }, ptr %13, i32 0, i32 0
  %69 = extractvalue { i64, i64 } %67, 0
  store i64 %69, ptr %68, align 8
  %70 = getelementptr inbounds { i64, i64 }, ptr %13, i32 0, i32 1
  %71 = extractvalue { i64, i64 } %67, 1
  store i64 %71, ptr %70, align 8
  %72 = getelementptr inbounds %struct.sum, ptr %6, i32 0, i32 0
  %73 = getelementptr inbounds %struct.sum, ptr %12, i32 0, i32 0
  %74 = getelementptr inbounds %struct.sum, ptr %13, i32 0, i32 0
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #4
  %75 = getelementptr inbounds %struct.sum, ptr %12, i32 0, i32 1
  %76 = load i64, ptr %75, align 8, !tbaa !12
  %77 = getelementptr inbounds %struct.sum, ptr %13, i32 0, i32 1
  %78 = load i64, ptr %77, align 8, !tbaa !12
  %79 = add nsw i64 %76, %78
  store i64 %79, ptr %14, align 8, !tbaa !14
  %80 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %74, ptr noundef nonnull align 8 dereferenceable(8) %14)
  %81 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %73, ptr noundef nonnull align 8 dereferenceable(8) %80)
  %82 = load i64, ptr %81, align 8, !tbaa !14
  store i64 %82, ptr %72, align 8, !tbaa !9
  %83 = getelementptr inbounds %struct.sum, ptr %6, i32 0, i32 1
  %84 = getelementptr inbounds %struct.sum, ptr %12, i32 0, i32 1
  %85 = getelementptr inbounds %struct.sum, ptr %13, i32 0, i32 1
  %86 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %84, ptr noundef nonnull align 8 dereferenceable(8) %85)
  %87 = load i64, ptr %86, align 8, !tbaa !14
  store i64 %87, ptr %83, align 8, !tbaa !12
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #4
  call void @llvm.lifetime.end.p0(i64 16, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 16, ptr %12) #4
  br label %88

88:                                               ; preds = %41, %37, %26
  %89 = load { i64, i64 }, ptr %6, align 8
  ret { i64, i64 } %89
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { 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 #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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !11, i64 0}
!10 = !{!"_ZTS3sum", !11, i64 0, !11, i64 8}
!11 = !{!"long long", !7, i64 0}
!12 = !{!10, !11, i64 8}
!13 = !{i64 0, i64 8, !14, i64 8, i64 8, !14}
!14 = !{!11, !11, 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"

%struct.sum = type { i64, i64 }

@tree = external dso_local local_unnamed_addr global [400001 x %struct.sum], align 16

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

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

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local { i64, i64 } @_Z5queryiiiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4) local_unnamed_addr #2 {
  %6 = alloca %struct.sum, align 8
  %7 = alloca %struct.sum, align 8
  %8 = alloca i64, align 8
  %9 = icmp sgt i32 %1, %2
  %10 = icmp sgt i32 %1, %4
  %or.cond = or i1 %9, %10
  %11 = icmp slt i32 %2, %3
  %or.cond27 = or i1 %11, %or.cond
  br i1 %or.cond27, label %36, label %12

12:                                               ; preds = %5
  %.not = icmp slt i32 %1, %3
  %.not26 = icmp sgt i32 %2, %4
  %or.cond28 = or i1 %.not, %.not26
  br i1 %or.cond28, label %16, label %13

13:                                               ; preds = %12
  %14 = sext i32 %0 to i64
  %15 = getelementptr inbounds [400001 x %struct.sum], ptr @tree, i64 0, i64 %14
  %.sroa.0.0.copyload = load i64, ptr %15, align 16, !tbaa !5
  %.sroa.4.0..sroa_idx = getelementptr inbounds i8, ptr %15, i64 8
  %.sroa.4.0.copyload = load i64, ptr %.sroa.4.0..sroa_idx, align 8, !tbaa !5
  br label %36

16:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %6) #3
  %17 = shl nsw i32 %0, 1
  %18 = add nsw i32 %2, %1
  %19 = sdiv i32 %18, 2
  %20 = tail call { i64, i64 } @_Z5queryiiiii(i32 noundef %17, i32 noundef %1, i32 noundef %19, i32 noundef %3, i32 noundef %4)
  %21 = extractvalue { i64, i64 } %20, 0
  store i64 %21, ptr %6, align 8
  %22 = getelementptr inbounds i8, ptr %6, i64 8
  %23 = extractvalue { i64, i64 } %20, 1
  store i64 %23, ptr %22, align 8
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %7) #3
  %24 = or disjoint i32 %17, 1
  %25 = add nsw i32 %19, 1
  %26 = tail call { i64, i64 } @_Z5queryiiiii(i32 noundef %24, i32 noundef %25, i32 noundef %2, i32 noundef %3, i32 noundef %4)
  %27 = extractvalue { i64, i64 } %26, 0
  store i64 %27, ptr %7, align 8
  %28 = getelementptr inbounds i8, ptr %7, i64 8
  %29 = extractvalue { i64, i64 } %26, 1
  store i64 %29, ptr %28, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %8) #3
  %30 = add nsw i64 %23, %29
  store i64 %30, ptr %8, align 8, !tbaa !5
  %31 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %7, ptr noundef nonnull align 8 dereferenceable(8) %8)
  %32 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %6, ptr noundef nonnull align 8 dereferenceable(8) %31)
  %33 = load i64, ptr %32, align 8, !tbaa !5
  %34 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %22, ptr noundef nonnull align 8 dereferenceable(8) %28)
  %35 = load i64, ptr %34, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %8) #3
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %7) #3
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %6) #3
  br label %36

36:                                               ; preds = %5, %16, %13
  %.sroa.0.0 = phi i64 [ %.sroa.0.0.copyload, %13 ], [ %33, %16 ], [ 0, %5 ]
  %.sroa.4.0 = phi i64 [ %.sroa.4.0.copyload, %13 ], [ %35, %16 ], [ 0, %5 ]
  %.fca.0.insert = insertvalue { i64, i64 } poison, i64 %.sroa.0.0, 0
  %.fca.1.insert = insertvalue { i64, i64 } %.fca.0.insert, i64 %.sroa.4.0, 1
  ret { i64, i64 } %.fca.1.insert
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 #3 = { 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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}


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