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

; 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 void @_Z6updateiiiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4) #3 {
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca %struct.sum, align 8
  %12 = alloca %struct.sum, align 8
  %13 = alloca %struct.sum, align 8
  %14 = alloca i64, align 8
  store i32 %0, ptr %6, align 4, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !5
  store i32 %2, ptr %8, align 4, !tbaa !5
  store i32 %3, ptr %9, align 4, !tbaa !5
  store i32 %4, ptr %10, align 4, !tbaa !5
  %15 = load i32, ptr %7, align 4, !tbaa !5
  %16 = load i32, ptr %8, align 4, !tbaa !5
  %17 = icmp eq i32 %15, %16
  br i1 %17, label %18, label %32

18:                                               ; preds = %5
  %19 = load i32, ptr %7, align 4, !tbaa !5
  %20 = load i32, ptr %9, align 4, !tbaa !5
  %21 = icmp eq i32 %19, %20
  br i1 %21, label %22, label %32

22:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 16, ptr %11) #4
  %23 = getelementptr inbounds %struct.sum, ptr %11, i32 0, i32 0
  %24 = load i32, ptr %10, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  store i64 %25, ptr %23, align 8, !tbaa !9
  %26 = getelementptr inbounds %struct.sum, ptr %11, i32 0, i32 1
  %27 = load i32, ptr %10, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  store i64 %28, ptr %26, align 8, !tbaa !12
  %29 = load i32, ptr %6, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [400001 x %struct.sum], ptr @tree, i64 0, i64 %30
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %31, ptr align 8 %11, i64 16, i1 false), !tbaa.struct !13
  call void @llvm.lifetime.end.p0(i64 16, ptr %11) #4
  br label %100

32:                                               ; preds = %18, %5
  %33 = load i32, ptr %9, align 4, !tbaa !5
  %34 = load i32, ptr %7, align 4, !tbaa !5
  %35 = load i32, ptr %8, align 4, !tbaa !5
  %36 = add nsw i32 %34, %35
  %37 = sdiv i32 %36, 2
  %38 = icmp sle i32 %33, %37
  br i1 %38, label %39, label %49

39:                                               ; preds = %32
  %40 = load i32, ptr %6, align 4, !tbaa !5
  %41 = mul nsw i32 %40, 2
  %42 = load i32, ptr %7, align 4, !tbaa !5
  %43 = load i32, ptr %7, align 4, !tbaa !5
  %44 = load i32, ptr %8, align 4, !tbaa !5
  %45 = add nsw i32 %43, %44
  %46 = sdiv i32 %45, 2
  %47 = load i32, ptr %9, align 4, !tbaa !5
  %48 = load i32, ptr %10, align 4, !tbaa !5
  call void @_Z6updateiiiii(i32 noundef %41, i32 noundef %42, i32 noundef %46, i32 noundef %47, i32 noundef %48)
  br label %49

49:                                               ; preds = %39, %32
  %50 = load i32, ptr %9, align 4, !tbaa !5
  %51 = load i32, ptr %7, align 4, !tbaa !5
  %52 = load i32, ptr %8, align 4, !tbaa !5
  %53 = add nsw i32 %51, %52
  %54 = sdiv i32 %53, 2
  %55 = icmp sgt i32 %50, %54
  br i1 %55, label %56, label %68

56:                                               ; preds = %49
  %57 = load i32, ptr %6, align 4, !tbaa !5
  %58 = mul nsw i32 %57, 2
  %59 = add nsw i32 %58, 1
  %60 = load i32, ptr %7, align 4, !tbaa !5
  %61 = load i32, ptr %8, align 4, !tbaa !5
  %62 = add nsw i32 %60, %61
  %63 = sdiv i32 %62, 2
  %64 = add nsw i32 %63, 1
  %65 = load i32, ptr %8, align 4, !tbaa !5
  %66 = load i32, ptr %9, align 4, !tbaa !5
  %67 = load i32, ptr %10, align 4, !tbaa !5
  call void @_Z6updateiiiii(i32 noundef %59, i32 noundef %64, i32 noundef %65, i32 noundef %66, i32 noundef %67)
  br label %68

68:                                               ; preds = %56, %49
  call void @llvm.lifetime.start.p0(i64 16, ptr %12) #4
  %69 = load i32, ptr %6, align 4, !tbaa !5
  %70 = mul nsw i32 %69, 2
  %71 = sext i32 %70 to i64
  %72 = getelementptr inbounds [400001 x %struct.sum], ptr @tree, i64 0, i64 %71
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %12, ptr align 16 %72, i64 16, i1 false), !tbaa.struct !13
  call void @llvm.lifetime.start.p0(i64 16, ptr %13) #4
  %73 = load i32, ptr %6, align 4, !tbaa !5
  %74 = mul nsw i32 %73, 2
  %75 = add nsw i32 %74, 1
  %76 = sext i32 %75 to i64
  %77 = getelementptr inbounds [400001 x %struct.sum], ptr @tree, i64 0, i64 %76
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %13, ptr align 16 %77, i64 16, i1 false), !tbaa.struct !13
  %78 = getelementptr inbounds %struct.sum, ptr %12, i32 0, i32 0
  %79 = getelementptr inbounds %struct.sum, ptr %13, i32 0, i32 0
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #4
  %80 = getelementptr inbounds %struct.sum, ptr %12, i32 0, i32 1
  %81 = load i64, ptr %80, align 8, !tbaa !12
  %82 = getelementptr inbounds %struct.sum, ptr %13, i32 0, i32 1
  %83 = load i64, ptr %82, align 8, !tbaa !12
  %84 = add nsw i64 %81, %83
  store i64 %84, ptr %14, align 8, !tbaa !14
  %85 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %79, ptr noundef nonnull align 8 dereferenceable(8) %14)
  %86 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %78, ptr noundef nonnull align 8 dereferenceable(8) %85)
  %87 = load i64, ptr %86, align 8, !tbaa !14
  %88 = load i32, ptr %6, align 4, !tbaa !5
  %89 = sext i32 %88 to i64
  %90 = getelementptr inbounds [400001 x %struct.sum], ptr @tree, i64 0, i64 %89
  %91 = getelementptr inbounds %struct.sum, ptr %90, i32 0, i32 0
  store i64 %87, ptr %91, align 16, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #4
  %92 = getelementptr inbounds %struct.sum, ptr %12, i32 0, i32 1
  %93 = getelementptr inbounds %struct.sum, ptr %13, i32 0, i32 1
  %94 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %92, ptr noundef nonnull align 8 dereferenceable(8) %93)
  %95 = load i64, ptr %94, align 8, !tbaa !14
  %96 = load i32, ptr %6, align 4, !tbaa !5
  %97 = sext i32 %96 to i64
  %98 = getelementptr inbounds [400001 x %struct.sum], ptr @tree, i64 0, i64 %97
  %99 = getelementptr inbounds %struct.sum, ptr %98, i32 0, i32 1
  store i64 %95, ptr %99, align 8, !tbaa !12
  call void @llvm.lifetime.end.p0(i64 16, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 16, ptr %12) #4
  br label %100

100:                                              ; preds = %68, %22
  ret void
}

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 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: 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 #2

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z6updateiiiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4) local_unnamed_addr #3 {
  %6 = alloca %struct.sum, align 8
  %7 = alloca %struct.sum, align 8
  %8 = alloca i64, align 8
  %9 = icmp eq i32 %1, %2
  %10 = icmp eq i32 %1, %3
  %or.cond = and i1 %9, %10
  br i1 %or.cond, label %11, label %15

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

15:                                               ; preds = %5
  %16 = add nsw i32 %2, %1
  %17 = sdiv i32 %16, 2
  %.not = icmp slt i32 %17, %3
  %18 = shl nsw i32 %0, 1
  br i1 %.not, label %20, label %19

19:                                               ; preds = %15
  tail call void @_Z6updateiiiii(i32 noundef %18, i32 noundef %1, i32 noundef %17, i32 noundef %3, i32 noundef %4)
  %.pre = or disjoint i32 %18, 1
  br label %23

20:                                               ; preds = %15
  %21 = or disjoint i32 %18, 1
  %22 = add nsw i32 %17, 1
  tail call void @_Z6updateiiiii(i32 noundef %21, i32 noundef %22, i32 noundef %2, i32 noundef %3, i32 noundef %4)
  br label %23

23:                                               ; preds = %19, %20
  %.pre-phi32 = phi i32 [ %.pre, %19 ], [ %21, %20 ]
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %6) #4
  %24 = sext i32 %18 to i64
  %25 = getelementptr inbounds [400001 x %struct.sum], ptr @tree, i64 0, i64 %24
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %6, ptr noundef nonnull align 16 dereferenceable(16) %25, i64 16, i1 false), !tbaa.struct !9
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %7) #4
  %26 = sext i32 %.pre-phi32 to i64
  %27 = getelementptr inbounds [400001 x %struct.sum], ptr @tree, i64 0, i64 %26
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %7, ptr noundef nonnull align 16 dereferenceable(16) %27, i64 16, i1 false), !tbaa.struct !9
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %8) #4
  %28 = getelementptr inbounds i8, ptr %6, i64 8
  %29 = load i64, ptr %28, align 8, !tbaa !10
  %30 = getelementptr inbounds i8, ptr %7, i64 8
  %31 = load i64, ptr %30, align 8, !tbaa !10
  %32 = add nsw i64 %31, %29
  store i64 %32, ptr %8, align 8, !tbaa !5
  %33 = 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)
  %34 = 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) %33)
  %35 = load i64, ptr %34, align 8, !tbaa !5
  %36 = sext i32 %0 to i64
  %37 = getelementptr inbounds [400001 x %struct.sum], ptr @tree, i64 0, i64 %36
  store i64 %35, ptr %37, align 16, !tbaa !12
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %8) #4
  %38 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %28, ptr noundef nonnull align 8 dereferenceable(8) %30)
  %39 = load i64, ptr %38, align 8, !tbaa !5
  %40 = getelementptr inbounds i8, ptr %37, i64 8
  store i64 %39, ptr %40, align 8, !tbaa !10
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %7) #4
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %6) #4
  br label %41

41:                                               ; preds = %23, %11
  ret void
}

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


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