; LLVM IR from SuprDewd/CompetitiveProgramming :: code/data-structures/persistent_segment_tree.ll (Z6updateiii)

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

@segcnt = external dso_local global i32, align 4
@segs = external dso_local global [2000000 x %struct.segment], 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 nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

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

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

12:                                               ; preds = %3
  %13 = load i32, ptr %5, align 4, !tbaa !5
  %14 = load i32, ptr %7, align 4, !tbaa !5
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds [2000000 x %struct.segment], ptr @segs, i64 0, i64 %15
  %17 = getelementptr inbounds %struct.segment, ptr %16, i32 0, i32 0
  %18 = load i32, ptr %17, align 4, !tbaa !9
  %19 = icmp slt i32 %13, %18
  br i1 %19, label %28, label %20

20:                                               ; preds = %12
  %21 = load i32, ptr %5, align 4, !tbaa !5
  %22 = load i32, ptr %7, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [2000000 x %struct.segment], ptr @segs, i64 0, i64 %23
  %25 = getelementptr inbounds %struct.segment, ptr %24, i32 0, i32 1
  %26 = load i32, ptr %25, align 4, !tbaa !11
  %27 = icmp sgt i32 %21, %26
  br i1 %27, label %28, label %30

28:                                               ; preds = %20, %12
  %29 = load i32, ptr %7, align 4, !tbaa !5
  store i32 %29, ptr %4, align 4
  br label %87

30:                                               ; preds = %20
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %31 = load i32, ptr @segcnt, align 4, !tbaa !5
  %32 = add nsw i32 %31, 1
  store i32 %32, ptr @segcnt, align 4, !tbaa !5
  store i32 %31, ptr %8, align 4, !tbaa !5
  %33 = load i32, ptr %7, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [2000000 x %struct.segment], ptr @segs, i64 0, i64 %34
  %36 = getelementptr inbounds %struct.segment, ptr %35, i32 0, i32 0
  %37 = load i32, ptr %36, align 4, !tbaa !9
  %38 = load i32, ptr %8, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [2000000 x %struct.segment], ptr @segs, i64 0, i64 %39
  %41 = getelementptr inbounds %struct.segment, ptr %40, i32 0, i32 0
  store i32 %37, ptr %41, align 4, !tbaa !9
  %42 = load i32, ptr %7, align 4, !tbaa !5
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds [2000000 x %struct.segment], ptr @segs, i64 0, i64 %43
  %45 = getelementptr inbounds %struct.segment, ptr %44, i32 0, i32 1
  %46 = load i32, ptr %45, align 4, !tbaa !11
  %47 = load i32, ptr %8, align 4, !tbaa !5
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [2000000 x %struct.segment], ptr @segs, i64 0, i64 %48
  %50 = getelementptr inbounds %struct.segment, ptr %49, i32 0, i32 1
  store i32 %46, ptr %50, align 4, !tbaa !11
  %51 = load i32, ptr %5, align 4, !tbaa !5
  %52 = load i32, ptr %6, align 4, !tbaa !5
  %53 = load i32, ptr %7, align 4, !tbaa !5
  %54 = sext i32 %53 to i64
  %55 = getelementptr inbounds [2000000 x %struct.segment], ptr @segs, i64 0, i64 %54
  %56 = getelementptr inbounds %struct.segment, ptr %55, i32 0, i32 2
  %57 = load i32, ptr %56, align 4, !tbaa !12
  %58 = call noundef i32 @_Z6updateiii(i32 noundef %51, i32 noundef %52, i32 noundef %57)
  %59 = load i32, ptr %8, align 4, !tbaa !5
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds [2000000 x %struct.segment], ptr @segs, i64 0, i64 %60
  %62 = getelementptr inbounds %struct.segment, ptr %61, i32 0, i32 2
  store i32 %58, ptr %62, align 4, !tbaa !12
  %63 = load i32, ptr %5, align 4, !tbaa !5
  %64 = load i32, ptr %6, align 4, !tbaa !5
  %65 = load i32, ptr %7, align 4, !tbaa !5
  %66 = sext i32 %65 to i64
  %67 = getelementptr inbounds [2000000 x %struct.segment], ptr @segs, i64 0, i64 %66
  %68 = getelementptr inbounds %struct.segment, ptr %67, i32 0, i32 3
  %69 = load i32, ptr %68, align 4, !tbaa !13
  %70 = call noundef i32 @_Z6updateiii(i32 noundef %63, i32 noundef %64, i32 noundef %69)
  %71 = load i32, ptr %8, align 4, !tbaa !5
  %72 = sext i32 %71 to i64
  %73 = getelementptr inbounds [2000000 x %struct.segment], ptr @segs, i64 0, i64 %72
  %74 = getelementptr inbounds %struct.segment, ptr %73, i32 0, i32 3
  store i32 %70, ptr %74, align 4, !tbaa !13
  %75 = load i32, ptr %7, align 4, !tbaa !5
  %76 = sext i32 %75 to i64
  %77 = getelementptr inbounds [2000000 x %struct.segment], ptr @segs, i64 0, i64 %76
  %78 = getelementptr inbounds %struct.segment, ptr %77, i32 0, i32 4
  %79 = load i32, ptr %78, align 4, !tbaa !14
  %80 = load i32, ptr %6, align 4, !tbaa !5
  %81 = add nsw i32 %79, %80
  %82 = load i32, ptr %8, align 4, !tbaa !5
  %83 = sext i32 %82 to i64
  %84 = getelementptr inbounds [2000000 x %struct.segment], ptr @segs, i64 0, i64 %83
  %85 = getelementptr inbounds %struct.segment, ptr %84, i32 0, i32 4
  store i32 %81, ptr %85, align 4, !tbaa !14
  %86 = load i32, ptr %8, align 4, !tbaa !5
  store i32 %86, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  br label %87

87:                                               ; preds = %30, %28, %11
  %88 = load i32, ptr %4, align 4
  ret i32 %88
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 = !{!"_ZTS7segment", !6, i64 0, !6, i64 4, !6, i64 8, !6, i64 12, !6, i64 16}
!11 = !{!10, !6, i64 4}
!12 = !{!10, !6, i64 8}
!13 = !{!10, !6, i64 12}
!14 = !{!10, !6, i64 16}


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

@segcnt = external dso_local local_unnamed_addr global i32, align 4
@segs = external dso_local local_unnamed_addr global [2000000 x %struct.segment], align 16

; Function Attrs: mustprogress nofree nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_Z6updateiii(i32 noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = icmp eq i32 %2, -1
  br i1 %4, label %common.ret25, label %5

5:                                                ; preds = %3
  %6 = sext i32 %2 to i64
  %7 = getelementptr inbounds [2000000 x %struct.segment], ptr @segs, i64 0, i64 %6
  %8 = load i32, ptr %7, align 4, !tbaa !5
  %9 = icmp sgt i32 %8, %0
  br i1 %9, label %common.ret25, label %10

10:                                               ; preds = %5
  %11 = getelementptr inbounds i8, ptr %7, i64 4
  %12 = load i32, ptr %11, align 4, !tbaa !10
  %13 = icmp slt i32 %12, %0
  br i1 %13, label %common.ret25, label %14

common.ret25:                                     ; preds = %5, %10, %3, %14
  %common.ret25.op = phi i32 [ %15, %14 ], [ -1, %3 ], [ %2, %10 ], [ %2, %5 ]
  ret i32 %common.ret25.op

14:                                               ; preds = %10
  %15 = load i32, ptr @segcnt, align 4, !tbaa !11
  %16 = add nsw i32 %15, 1
  store i32 %16, ptr @segcnt, align 4, !tbaa !11
  %17 = sext i32 %15 to i64
  %18 = getelementptr inbounds [2000000 x %struct.segment], ptr @segs, i64 0, i64 %17
  store i32 %8, ptr %18, align 4, !tbaa !5
  %19 = getelementptr inbounds i8, ptr %18, i64 4
  store i32 %12, ptr %19, align 4, !tbaa !10
  %20 = getelementptr inbounds i8, ptr %7, i64 8
  %21 = load i32, ptr %20, align 4, !tbaa !12
  %22 = tail call noundef i32 @_Z6updateiii(i32 noundef %0, i32 noundef %1, i32 noundef %21)
  %23 = getelementptr inbounds i8, ptr %18, i64 8
  store i32 %22, ptr %23, align 4, !tbaa !12
  %24 = getelementptr inbounds i8, ptr %7, i64 12
  %25 = load i32, ptr %24, align 4, !tbaa !13
  %26 = tail call noundef i32 @_Z6updateiii(i32 noundef %0, i32 noundef %1, i32 noundef %25)
  %27 = getelementptr inbounds i8, ptr %18, i64 12
  store i32 %26, ptr %27, align 4, !tbaa !13
  %28 = getelementptr inbounds i8, ptr %7, i64 16
  %29 = load i32, ptr %28, align 4, !tbaa !14
  %30 = add nsw i32 %29, %1
  %31 = getelementptr inbounds i8, ptr %18, i64 16
  store i32 %30, ptr %31, align 4, !tbaa !14
  br label %common.ret25
}

attributes #0 = { mustprogress nofree nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: 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)"}
!5 = !{!6, !7, i64 0}
!6 = !{!"_ZTS7segment", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12, !7, i64 16}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!6, !7, i64 4}
!11 = !{!7, !7, i64 0}
!12 = !{!6, !7, i64 8}
!13 = !{!6, !7, i64 12}
!14 = !{!6, !7, i64 16}


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