; LLVM IR from upupming/algorithm :: kick-start/2021/RoundB/D. Truck Delivery.ll (Z3askiii)

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

@tree = external dso_local global [800040 x %struct.SegmentTree], align 16

; Function Attrs: mustprogress uwtable
declare dso_local noundef i64 @_Z3gcdxx(i64 noundef, i64 noundef) #0

; 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

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z3askiii(i32 noundef %0, i32 noundef %1, i32 noundef %2) #0 {
  %4 = alloca i64, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i64, align 8
  store i32 %0, ptr %5, align 4, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !5
  %10 = load i32, ptr %6, align 4, !tbaa !5
  %11 = load i32, ptr %5, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [800040 x %struct.SegmentTree], ptr @tree, i64 0, i64 %12
  %14 = getelementptr inbounds %struct.SegmentTree, ptr %13, i32 0, i32 0
  %15 = load i32, ptr %14, align 16, !tbaa !9
  %16 = icmp sle i32 %10, %15
  br i1 %16, label %17, label %31

17:                                               ; preds = %3
  %18 = load i32, ptr %7, align 4, !tbaa !5
  %19 = load i32, ptr %5, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [800040 x %struct.SegmentTree], ptr @tree, i64 0, i64 %20
  %22 = getelementptr inbounds %struct.SegmentTree, ptr %21, i32 0, i32 1
  %23 = load i32, ptr %22, align 4, !tbaa !12
  %24 = icmp sge i32 %18, %23
  br i1 %24, label %25, label %31

25:                                               ; preds = %17
  %26 = load i32, ptr %5, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [800040 x %struct.SegmentTree], ptr @tree, i64 0, i64 %27
  %29 = getelementptr inbounds %struct.SegmentTree, ptr %28, i32 0, i32 2
  %30 = load i64, ptr %29, align 8, !tbaa !13
  store i64 %30, ptr %4, align 8
  br label %71

31:                                               ; preds = %17, %3
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %32 = load i32, ptr %5, align 4, !tbaa !5
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [800040 x %struct.SegmentTree], ptr @tree, i64 0, i64 %33
  %35 = getelementptr inbounds %struct.SegmentTree, ptr %34, i32 0, i32 0
  %36 = load i32, ptr %35, align 16, !tbaa !9
  %37 = load i32, ptr %5, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [800040 x %struct.SegmentTree], ptr @tree, i64 0, i64 %38
  %40 = getelementptr inbounds %struct.SegmentTree, ptr %39, i32 0, i32 1
  %41 = load i32, ptr %40, align 4, !tbaa !12
  %42 = add nsw i32 %36, %41
  %43 = ashr i32 %42, 1
  store i32 %43, ptr %8, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  store i64 0, ptr %9, align 8, !tbaa !14
  %44 = load i32, ptr %6, align 4, !tbaa !5
  %45 = load i32, ptr %8, align 4, !tbaa !5
  %46 = icmp sle i32 %44, %45
  br i1 %46, label %47, label %55

47:                                               ; preds = %31
  %48 = load i64, ptr %9, align 8, !tbaa !14
  %49 = load i32, ptr %5, align 4, !tbaa !5
  %50 = mul nsw i32 2, %49
  %51 = load i32, ptr %6, align 4, !tbaa !5
  %52 = load i32, ptr %7, align 4, !tbaa !5
  %53 = call noundef i64 @_Z3askiii(i32 noundef %50, i32 noundef %51, i32 noundef %52)
  %54 = call noundef i64 @_Z3gcdxx(i64 noundef %48, i64 noundef %53)
  store i64 %54, ptr %9, align 8, !tbaa !14
  br label %55

55:                                               ; preds = %47, %31
  %56 = load i32, ptr %7, align 4, !tbaa !5
  %57 = load i32, ptr %8, align 4, !tbaa !5
  %58 = add nsw i32 %57, 1
  %59 = icmp sge i32 %56, %58
  br i1 %59, label %60, label %69

60:                                               ; preds = %55
  %61 = load i64, ptr %9, align 8, !tbaa !14
  %62 = load i32, ptr %5, align 4, !tbaa !5
  %63 = mul nsw i32 2, %62
  %64 = add nsw i32 %63, 1
  %65 = load i32, ptr %6, align 4, !tbaa !5
  %66 = load i32, ptr %7, align 4, !tbaa !5
  %67 = call noundef i64 @_Z3askiii(i32 noundef %64, i32 noundef %65, i32 noundef %66)
  %68 = call noundef i64 @_Z3gcdxx(i64 noundef %61, i64 noundef %67)
  store i64 %68, ptr %9, align 8, !tbaa !14
  br label %69

69:                                               ; preds = %60, %55
  %70 = load i64, ptr %9, align 8, !tbaa !14
  store i64 %70, ptr %4, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  br label %71

71:                                               ; preds = %69, %25
  %72 = load i64, ptr %4, align 8
  ret i64 %72
}

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 = { 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++ TBAA"}
!9 = !{!10, !6, i64 0}
!10 = !{!"_ZTS11SegmentTree", !6, i64 0, !6, i64 4, !11, i64 8}
!11 = !{!"long long", !7, i64 0}
!12 = !{!10, !6, i64 4}
!13 = !{!10, !11, i64 8}
!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.SegmentTree = type { i32, i32, i64 }

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

; Function Attrs: mustprogress uwtable
declare dso_local noundef i64 @_Z3gcdxx(i64 noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z3askiii(i32 noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = sext i32 %0 to i64
  %5 = getelementptr inbounds [800040 x %struct.SegmentTree], ptr @tree, i64 0, i64 %4
  %6 = load i32, ptr %5, align 16, !tbaa !5
  %.not = icmp slt i32 %6, %1
  %.phi.trans.insert = getelementptr inbounds i8, ptr %5, i64 4
  %.pre = load i32, ptr %.phi.trans.insert, align 4, !tbaa !11
  %.not24 = icmp sgt i32 %.pre, %2
  %or.cond = select i1 %.not, i1 true, i1 %.not24
  br i1 %or.cond, label %._crit_edge, label %7

7:                                                ; preds = %3
  %8 = getelementptr inbounds i8, ptr %5, i64 8
  %9 = load i64, ptr %8, align 8, !tbaa !12
  br label %common.ret27

._crit_edge:                                      ; preds = %3
  %10 = add nsw i32 %.pre, %6
  %11 = ashr i32 %10, 1
  %.not25 = icmp slt i32 %11, %1
  br i1 %.not25, label %16, label %12

12:                                               ; preds = %._crit_edge
  %13 = shl nsw i32 %0, 1
  %14 = tail call noundef i64 @_Z3askiii(i32 noundef %13, i32 noundef %1, i32 noundef %2)
  %15 = tail call noundef i64 @_Z3gcdxx(i64 noundef 0, i64 noundef %14)
  br label %16

16:                                               ; preds = %12, %._crit_edge
  %.0 = phi i64 [ %15, %12 ], [ 0, %._crit_edge ]
  %.not26.not = icmp slt i32 %11, %2
  br i1 %.not26.not, label %17, label %common.ret27

common.ret27:                                     ; preds = %16, %7, %17
  %common.ret27.op = phi i64 [ %21, %17 ], [ %9, %7 ], [ %.0, %16 ]
  ret i64 %common.ret27.op

17:                                               ; preds = %16
  %18 = shl nsw i32 %0, 1
  %19 = or disjoint i32 %18, 1
  %20 = tail call noundef i64 @_Z3askiii(i32 noundef %19, i32 noundef %1, i32 noundef %2)
  %21 = tail call noundef i64 @_Z3gcdxx(i64 noundef %.0, i64 noundef %20)
  br label %common.ret27
}

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

!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 0}
!6 = !{!"_ZTS11SegmentTree", !7, i64 0, !7, i64 4, !10, i64 8}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"long long", !8, i64 0}
!11 = !{!6, !7, i64 4}
!12 = !{!6, !10, i64 8}


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