; LLVM IR from Dev-XYS/Algorithms :: Segment-Tree(Sum) (main)

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

@T = external dso_local global [800000 x %struct.node], align 16
@n = external dso_local global i32, align 4
@q = external dso_local global i32, align 4
@d = external dso_local global [200000 x i32], align 16
@delta = external dso_local global i64, align 8
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [9 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [6 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [2 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [7 x i8], align 1
@.str.7 = external hidden unnamed_addr constant [24 x i8], align 1

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z10build_treeiii(i32 noundef, i32 noundef, i32 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
declare dso_local noundef i64 @_Z7get_sumiii(i32 noundef, i32 noundef, i32 noundef) #0

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z14update_segmentiii(i32 noundef, i32 noundef, i32 noundef) #0

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() #2 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i64, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  %9 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @n)
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #4
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %10

10:                                               ; preds = %20, %0
  %11 = load i32, ptr %2, align 4, !tbaa !5
  %12 = load i32, ptr @n, align 4, !tbaa !5
  %13 = icmp slt i32 %11, %12
  br i1 %13, label %15, label %14

14:                                               ; preds = %10
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #4
  br label %23

15:                                               ; preds = %10
  %16 = load i32, ptr %2, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [200000 x i32], ptr @d, i64 0, i64 %17
  %19 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %18)
  br label %20

20:                                               ; preds = %15
  %21 = load i32, ptr %2, align 4, !tbaa !5
  %22 = add nsw i32 %21, 1
  store i32 %22, ptr %2, align 4, !tbaa !5
  br label %10, !llvm.loop !9

23:                                               ; preds = %14
  %24 = load i32, ptr @n, align 4, !tbaa !5
  %25 = sub nsw i32 %24, 1
  call void @_Z10build_treeiii(i32 noundef 0, i32 noundef %25, i32 noundef 1)
  %26 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @q)
  br label %27

27:                                               ; preds = %84, %23
  %28 = load i32, ptr @q, align 4, !tbaa !5
  %29 = add nsw i32 %28, -1
  store i32 %29, ptr @q, align 4, !tbaa !5
  %30 = icmp ne i32 %28, 0
  br i1 %30, label %31, label %85

31:                                               ; preds = %27
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %32 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %3)
  %33 = load i32, ptr %3, align 4, !tbaa !5
  switch i32 %33, label %82 [
    i32 1, label %34
    i32 2, label %41
    i32 3, label %49
  ]

34:                                               ; preds = %31
  %35 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, ptr noundef %4, ptr noundef %5, ptr noundef %6)
  %36 = load i64, ptr %6, align 8, !tbaa !11
  store i64 %36, ptr @delta, align 8, !tbaa !11
  %37 = load i32, ptr %4, align 4, !tbaa !5
  %38 = sub nsw i32 %37, 1
  %39 = load i32, ptr %5, align 4, !tbaa !5
  %40 = sub nsw i32 %39, 1
  call void @_Z14update_segmentiii(i32 noundef %38, i32 noundef %40, i32 noundef 1)
  br label %84

41:                                               ; preds = %31
  %42 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.2, ptr noundef %4, ptr noundef %5)
  %43 = load i32, ptr %4, align 4, !tbaa !5
  %44 = sub nsw i32 %43, 1
  %45 = load i32, ptr %5, align 4, !tbaa !5
  %46 = sub nsw i32 %45, 1
  %47 = call noundef i64 @_Z7get_sumiii(i32 noundef %44, i32 noundef %46, i32 noundef 1)
  %48 = call i32 (ptr, ...) @printf(ptr noundef @.str.3, i64 noundef %47)
  br label %84

49:                                               ; preds = %31
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  store i32 0, ptr %7, align 4, !tbaa !5
  br label %50

50:                                               ; preds = %59, %49
  %51 = load i32, ptr %7, align 4, !tbaa !5
  %52 = load i32, ptr @n, align 4, !tbaa !5
  %53 = mul nsw i32 %52, 2
  %54 = icmp slt i32 %51, %53
  br i1 %54, label %56, label %55

55:                                               ; preds = %50
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  br label %62

56:                                               ; preds = %50
  %57 = load i32, ptr %7, align 4, !tbaa !5
  %58 = call i32 (ptr, ...) @printf(ptr noundef @.str.4, i32 noundef %57)
  br label %59

59:                                               ; preds = %56
  %60 = load i32, ptr %7, align 4, !tbaa !5
  %61 = add nsw i32 %60, 1
  store i32 %61, ptr %7, align 4, !tbaa !5
  br label %50, !llvm.loop !13

62:                                               ; preds = %55
  %63 = call i32 (ptr, ...) @printf(ptr noundef @.str.5)
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  store i32 0, ptr %8, align 4, !tbaa !5
  br label %64

64:                                               ; preds = %77, %62
  %65 = load i32, ptr %8, align 4, !tbaa !5
  %66 = load i32, ptr @n, align 4, !tbaa !5
  %67 = mul nsw i32 %66, 2
  %68 = icmp slt i32 %65, %67
  br i1 %68, label %70, label %69

69:                                               ; preds = %64
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  br label %80

70:                                               ; preds = %64
  %71 = load i32, ptr %8, align 4, !tbaa !5
  %72 = sext i32 %71 to i64
  %73 = getelementptr inbounds [800000 x %struct.node], ptr @T, i64 0, i64 %72
  %74 = getelementptr inbounds %struct.node, ptr %73, i32 0, i32 3
  %75 = load i64, ptr %74, align 8, !tbaa !14
  %76 = call i32 (ptr, ...) @printf(ptr noundef @.str.6, i64 noundef %75)
  br label %77

77:                                               ; preds = %70
  %78 = load i32, ptr %8, align 4, !tbaa !5
  %79 = add nsw i32 %78, 1
  store i32 %79, ptr %8, align 4, !tbaa !5
  br label %64, !llvm.loop !16

80:                                               ; preds = %69
  %81 = call i32 (ptr, ...) @printf(ptr noundef @.str.5)
  br label %84

82:                                               ; preds = %31
  %83 = call i32 (ptr, ...) @printf(ptr noundef @.str.7)
  br label %84

84:                                               ; preds = %82, %80, %41, %34
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  br label %27, !llvm.loop !17

85:                                               ; preds = %27
  ret i32 0
}

declare i32 @__isoc99_scanf(ptr noundef, ...) #3

declare i32 @printf(ptr noundef, ...) #3

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 = { mustprogress norecurse 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 = { "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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!12, !12, i64 0}
!12 = !{!"long long", !7, i64 0}
!13 = distinct !{!13, !10}
!14 = !{!15, !12, i64 16}
!15 = !{!"_ZTS4node", !6, i64 0, !6, i64 4, !12, i64 8, !12, i64 16}
!16 = distinct !{!16, !10}
!17 = distinct !{!17, !10}


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

@T = external dso_local local_unnamed_addr global [800000 x %struct.node], align 16
@n = external dso_local global i32, align 4
@q = external dso_local global i32, align 4
@d = external dso_local global [200000 x i32], align 16
@delta = external dso_local local_unnamed_addr global i64, align 8
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [9 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [6 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [2 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [7 x i8], align 1
@.str.7 = external hidden unnamed_addr constant [24 x i8], align 1

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z10build_treeiii(i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

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

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

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

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z14update_segmentiii(i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() local_unnamed_addr #2 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i64, align 8
  %5 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @n)
  %6 = load i32, ptr @n, align 4, !tbaa !5
  %7 = icmp sgt i32 %6, 0
  br i1 %7, label %.lr.ph, label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph, %0
  %.lcssa = phi i32 [ %6, %0 ], [ %14, %.lr.ph ]
  %8 = add nsw i32 %.lcssa, -1
  tail call void @_Z10build_treeiii(i32 noundef 0, i32 noundef %8, i32 noundef 1)
  %9 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @q)
  %10 = load i32, ptr @q, align 4, !tbaa !5
  %11 = add nsw i32 %10, -1
  store i32 %11, ptr @q, align 4, !tbaa !5
  %.not20 = icmp eq i32 %10, 0
  br i1 %.not20, label %._crit_edge23, label %.lr.ph22

.lr.ph:                                           ; preds = %0, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %0 ]
  %12 = getelementptr inbounds [200000 x i32], ptr @d, i64 0, i64 %indvars.iv
  %13 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %12)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %14 = load i32, ptr @n, align 4, !tbaa !5
  %15 = sext i32 %14 to i64
  %16 = icmp slt i64 %indvars.iv.next, %15
  br i1 %16, label %.lr.ph, label %._crit_edge, !llvm.loop !9

.lr.ph22:                                         ; preds = %._crit_edge, %54
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %1) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %2) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %3) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %4) #4
  %17 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %1)
  %18 = load i32, ptr %1, align 4, !tbaa !5
  switch i32 %18, label %52 [
    i32 1, label %21
    i32 2, label %28
    i32 3, label %.preheader
  ]

.preheader:                                       ; preds = %.lr.ph22
  %19 = load i32, ptr @n, align 4, !tbaa !5
  %20 = icmp sgt i32 %19, 0
  br i1 %20, label %.lr.ph14, label %._crit_edge15

21:                                               ; preds = %.lr.ph22
  %22 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull %2, ptr noundef nonnull %3, ptr noundef nonnull %4)
  %23 = load i64, ptr %4, align 8, !tbaa !11
  store i64 %23, ptr @delta, align 8, !tbaa !11
  %24 = load i32, ptr %2, align 4, !tbaa !5
  %25 = add nsw i32 %24, -1
  %26 = load i32, ptr %3, align 4, !tbaa !5
  %27 = add nsw i32 %26, -1
  call void @_Z14update_segmentiii(i32 noundef %25, i32 noundef %27, i32 noundef 1)
  br label %54

28:                                               ; preds = %.lr.ph22
  %29 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.2, ptr noundef nonnull %2, ptr noundef nonnull %3)
  %30 = load i32, ptr %2, align 4, !tbaa !5
  %31 = add nsw i32 %30, -1
  %32 = load i32, ptr %3, align 4, !tbaa !5
  %33 = add nsw i32 %32, -1
  %34 = call noundef i64 @_Z7get_sumiii(i32 noundef %31, i32 noundef %33, i32 noundef 1)
  %35 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3, i64 noundef %34)
  br label %54

._crit_edge15:                                    ; preds = %.lr.ph14, %.preheader
  %36 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.5)
  %37 = load i32, ptr @n, align 4, !tbaa !5
  %38 = icmp sgt i32 %37, 0
  br i1 %38, label %.lr.ph18, label %._crit_edge19

.lr.ph14:                                         ; preds = %.preheader, %.lr.ph14
  %.0913 = phi i32 [ %40, %.lr.ph14 ], [ 0, %.preheader ]
  %39 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.4, i32 noundef %.0913)
  %40 = add nuw nsw i32 %.0913, 1
  %41 = load i32, ptr @n, align 4, !tbaa !5
  %42 = shl nsw i32 %41, 1
  %43 = icmp slt i32 %40, %42
  br i1 %43, label %.lr.ph14, label %._crit_edge15, !llvm.loop !13

._crit_edge19:                                    ; preds = %.lr.ph18, %._crit_edge15
  %44 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.5)
  br label %54

.lr.ph18:                                         ; preds = %._crit_edge15, %.lr.ph18
  %indvars.iv26 = phi i64 [ %indvars.iv.next27, %.lr.ph18 ], [ 0, %._crit_edge15 ]
  %45 = getelementptr inbounds [800000 x %struct.node], ptr @T, i64 0, i64 %indvars.iv26, i32 3
  %46 = load i64, ptr %45, align 8, !tbaa !14
  %47 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.6, i64 noundef %46)
  %indvars.iv.next27 = add nuw nsw i64 %indvars.iv26, 1
  %48 = load i32, ptr @n, align 4, !tbaa !5
  %49 = shl nsw i32 %48, 1
  %50 = sext i32 %49 to i64
  %51 = icmp slt i64 %indvars.iv.next27, %50
  br i1 %51, label %.lr.ph18, label %._crit_edge19, !llvm.loop !16

52:                                               ; preds = %.lr.ph22
  %53 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.7)
  br label %54

54:                                               ; preds = %52, %._crit_edge19, %28, %21
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %4) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %3) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %2) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %1) #4
  %55 = load i32, ptr @q, align 4, !tbaa !5
  %56 = add nsw i32 %55, -1
  store i32 %56, ptr @q, align 4, !tbaa !5
  %.not = icmp eq i32 %55, 0
  br i1 %.not, label %._crit_edge23, label %.lr.ph22, !llvm.loop !17

._crit_edge23:                                    ; preds = %54, %._crit_edge
  ret i32 0
}

; Function Attrs: nofree nounwind
declare noundef i32 @__isoc99_scanf(ptr nocapture noundef readonly, ...) local_unnamed_addr #3

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #3

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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress norecurse 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 = { nofree nounwind "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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!12, !12, i64 0}
!12 = !{!"long long", !7, i64 0}
!13 = distinct !{!13, !10}
!14 = !{!15, !12, i64 16}
!15 = !{!"_ZTS4node", !6, i64 0, !6, i64 4, !12, i64 8, !12, i64 16}
!16 = distinct !{!16, !10}
!17 = distinct !{!17, !10}


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