; LLVM IR from bqi343/cp-notebook :: Contests/USACO Solutions/2013-14/Feb 2014/14 Feb G3.ll (Z3insP5tnodeSt4pairIiiE)

; 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.std::pair.0" = type { i32, i32 }
%"struct.std::pair" = type { ptr, ptr }

; Function Attrs: mustprogress uwtable
declare dso_local { ptr, ptr } @_Z5splitP5tnodei(ptr 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 ptr @_Z5mergeP5tnodeS0_(ptr noundef, ptr noundef) #0

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z3getP5tnode(ptr noundef) #0

; Function Attrs: mustprogress uwtable
declare dso_local noundef ptr @_Z3remP5tnode(ptr noundef) #0

; Function Attrs: nobuiltin nounwind
declare void @_ZdlPvm(ptr noundef, i64 noundef) #2

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_Z3insP5tnodeSt4pairIiiE(ptr noundef %0, i64 %1) #0 personality ptr @__gxx_personality_v0 {
  %3 = alloca %"struct.std::pair.0", align 4
  %4 = alloca ptr, align 8
  %5 = alloca %"struct.std::pair", align 8
  %6 = alloca ptr, align 8
  %7 = alloca %"struct.std::pair.0", align 4
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  store i64 %1, ptr %3, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 16, ptr %5) #5
  %10 = load ptr, ptr %4, align 8, !tbaa !5
  %11 = getelementptr inbounds %"struct.std::pair.0", ptr %3, i32 0, i32 0
  %12 = load i32, ptr %11, align 4, !tbaa !9
  %13 = call { ptr, ptr } @_Z5splitP5tnodei(ptr noundef %10, i32 noundef %12)
  %14 = getelementptr inbounds { ptr, ptr }, ptr %5, i32 0, i32 0
  %15 = extractvalue { ptr, ptr } %13, 0
  store ptr %15, ptr %14, align 8
  %16 = getelementptr inbounds { ptr, ptr }, ptr %5, i32 0, i32 1
  %17 = extractvalue { ptr, ptr } %13, 1
  store ptr %17, ptr %16, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  %18 = call noalias noundef nonnull ptr @_Znwm(i64 noundef 32) #6
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %7, ptr align 4 %3, i64 8, i1 false)
  %19 = load i64, ptr %7, align 4
  invoke void @_ZN5tnodeC2ESt4pairIiiE(ptr noundef nonnull align 8 dereferenceable(32) %18, i64 %19)
          to label %20 unwind label %36

20:                                               ; preds = %2
  store ptr %18, ptr %6, align 8, !tbaa !5
  br label %21

21:                                               ; preds = %31, %20
  %22 = getelementptr inbounds %"struct.std::pair", ptr %5, i32 0, i32 1
  %23 = load ptr, ptr %22, align 8, !tbaa !12
  %24 = call noundef i32 @_Z3getP5tnode(ptr noundef %23)
  %25 = getelementptr inbounds %"struct.std::pair.0", ptr %3, i32 0, i32 0
  %26 = load i32, ptr %25, align 4, !tbaa !9
  %27 = getelementptr inbounds %"struct.std::pair.0", ptr %3, i32 0, i32 1
  %28 = load i32, ptr %27, align 4, !tbaa !14
  %29 = sub nsw i32 %26, %28
  %30 = icmp sge i32 %24, %29
  br i1 %30, label %31, label %40

31:                                               ; preds = %21
  %32 = getelementptr inbounds %"struct.std::pair", ptr %5, i32 0, i32 1
  %33 = load ptr, ptr %32, align 8, !tbaa !12
  %34 = call noundef ptr @_Z3remP5tnode(ptr noundef %33)
  %35 = getelementptr inbounds %"struct.std::pair", ptr %5, i32 0, i32 1
  store ptr %34, ptr %35, align 8, !tbaa !12
  br label %21, !llvm.loop !15

36:                                               ; preds = %2
  %37 = landingpad { ptr, i32 }
          cleanup
  %38 = extractvalue { ptr, i32 } %37, 0
  store ptr %38, ptr %8, align 8
  %39 = extractvalue { ptr, i32 } %37, 1
  store i32 %39, ptr %9, align 4
  call void @_ZdlPvm(ptr noundef %18, i64 noundef 32) #7
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 16, ptr %5) #5
  br label %48

40:                                               ; preds = %21
  %41 = getelementptr inbounds %"struct.std::pair", ptr %5, i32 0, i32 0
  %42 = load ptr, ptr %41, align 8, !tbaa !17
  %43 = load ptr, ptr %6, align 8, !tbaa !5
  %44 = call noundef ptr @_Z5mergeP5tnodeS0_(ptr noundef %42, ptr noundef %43)
  %45 = getelementptr inbounds %"struct.std::pair", ptr %5, i32 0, i32 1
  %46 = load ptr, ptr %45, align 8, !tbaa !12
  %47 = call noundef ptr @_Z5mergeP5tnodeS0_(ptr noundef %44, ptr noundef %46)
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 16, ptr %5) #5
  ret ptr %47

48:                                               ; preds = %36
  %49 = load ptr, ptr %8, align 8
  %50 = load i32, ptr %9, align 4
  %51 = insertvalue { ptr, i32 } poison, ptr %49, 0
  %52 = insertvalue { ptr, i32 } %51, i32 %50, 1
  resume { ptr, i32 } %52
}

; Function Attrs: nobuiltin allocsize(0)
declare noundef nonnull ptr @_Znwm(i64 noundef) #3

; 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) #4

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN5tnodeC2ESt4pairIiiE(ptr noundef nonnull align 8 dereferenceable(32), i64) unnamed_addr #0 align 2

declare i32 @__gxx_personality_v0(...)

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 = { nobuiltin 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 #3 = { nobuiltin allocsize(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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { nounwind }
attributes #6 = { builtin allocsize(0) }
attributes #7 = { builtin 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !11, i64 0}
!10 = !{!"_ZTSSt4pairIiiE", !11, i64 0, !11, i64 4}
!11 = !{!"int", !7, i64 0}
!12 = !{!13, !6, i64 8}
!13 = !{!"_ZTSSt4pairIP5tnodeS1_E", !6, i64 0, !6, i64 8}
!14 = !{!10, !11, i64 4}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!13, !6, 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"

; Function Attrs: mustprogress uwtable
declare dso_local { ptr, ptr } @_Z5splitP5tnodei(ptr noundef, i32 noundef) local_unnamed_addr #0

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

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z3getP5tnode(ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
declare dso_local noundef ptr @_Z3remP5tnode(ptr noundef) local_unnamed_addr #0

; Function Attrs: nobuiltin nounwind
declare void @_ZdlPvm(ptr noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_Z3insP5tnodeSt4pairIiiE(ptr noundef %0, i64 %1) local_unnamed_addr #0 personality ptr @__gxx_personality_v0 {
  %.sroa.06.0.extract.trunc = trunc i64 %1 to i32
  %3 = tail call { ptr, ptr } @_Z5splitP5tnodei(ptr noundef %0, i32 noundef %.sroa.06.0.extract.trunc)
  %4 = tail call noalias noundef nonnull dereferenceable(32) ptr @_Znwm(i64 noundef 32) #3
  invoke void @_ZN5tnodeC2ESt4pairIiiE(ptr noundef nonnull align 8 dereferenceable(32) %4, i64 %1)
          to label %5 unwind label %11

5:                                                ; preds = %2
  %.sroa.4.0.extract.shift = lshr i64 %1, 32
  %.sroa.4.0.extract.trunc = trunc nuw i64 %.sroa.4.0.extract.shift to i32
  %6 = extractvalue { ptr, ptr } %3, 1
  %7 = tail call noundef i32 @_Z3getP5tnode(ptr noundef %6)
  %8 = sub nsw i32 %.sroa.06.0.extract.trunc, %.sroa.4.0.extract.trunc
  %.not8 = icmp slt i32 %7, %8
  br i1 %.not8, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %5, %.lr.ph
  %.sroa.5.09 = phi ptr [ %9, %.lr.ph ], [ %6, %5 ]
  %9 = tail call noundef ptr @_Z3remP5tnode(ptr noundef %.sroa.5.09)
  %10 = tail call noundef i32 @_Z3getP5tnode(ptr noundef %9)
  %.not = icmp slt i32 %10, %8
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !5

11:                                               ; preds = %2
  %12 = landingpad { ptr, i32 }
          cleanup
  tail call void @_ZdlPvm(ptr noundef nonnull %4, i64 noundef 32) #4
  resume { ptr, i32 } %12

._crit_edge:                                      ; preds = %.lr.ph, %5
  %.sroa.5.0.lcssa = phi ptr [ %6, %5 ], [ %9, %.lr.ph ]
  %13 = extractvalue { ptr, ptr } %3, 0
  %14 = tail call noundef ptr @_Z5mergeP5tnodeS0_(ptr noundef %13, ptr noundef nonnull %4)
  %15 = tail call noundef ptr @_Z5mergeP5tnodeS0_(ptr noundef %14, ptr noundef %.sroa.5.0.lcssa)
  ret ptr %15
}

; Function Attrs: nobuiltin allocsize(0)
declare noundef nonnull ptr @_Znwm(i64 noundef) local_unnamed_addr #2

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN5tnodeC2ESt4pairIiiE(ptr noundef nonnull align 8 dereferenceable(32), i64) unnamed_addr #0 align 2

declare i32 @__gxx_personality_v0(...)

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 = { nobuiltin 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 #2 = { nobuiltin allocsize(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 = { builtin allocsize(0) }
attributes #4 = { builtin 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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}


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