; LLVM IR from viveakrt/Competitive-programing :: SPOJ/salman.ll (Z5buildxxx)

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

%"class.std::vector" = type { %"struct.std::_Vector_base" }
%"struct.std::_Vector_base" = type { %"struct.std::_Vector_base<long long, std::allocator<long long>>::_Vector_impl" }
%"struct.std::_Vector_base<long long, std::allocator<long long>>::_Vector_impl" = type { %"struct.std::_Vector_base<long long, std::allocator<long long>>::_Vector_impl_data" }
%"struct.std::_Vector_base<long long, std::allocator<long long>>::_Vector_impl_data" = type { ptr, ptr, ptr }
%struct.node = type { i64, i64 }

@res = external dso_local global [524288 x i64], align 16
@s = external dso_local global %"class.std::vector", align 8
@tree = external dso_local global [1048576 x %struct.node], align 16
@lazy = external dso_local global [1048576 x i64], 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 void @_Z5buildxxx(i64 noundef %0, i64 noundef %1, i64 noundef %2) #1 {
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca %struct.node, align 8
  store i64 %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !5
  store i64 %2, ptr %6, align 8, !tbaa !5
  %9 = load i64, ptr %6, align 8, !tbaa !5
  %10 = getelementptr inbounds [1048576 x i64], ptr @lazy, i64 0, i64 %9
  store i64 0, ptr %10, align 8, !tbaa !5
  %11 = load i64, ptr %4, align 8, !tbaa !5
  %12 = load i64, ptr %5, align 8, !tbaa !5
  %13 = icmp eq i64 %11, %12
  br i1 %13, label %14, label %26

14:                                               ; preds = %3
  %15 = load i64, ptr %4, align 8, !tbaa !5
  %16 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSt6vectorIxSaIxEEixEm(ptr noundef nonnull align 8 dereferenceable(24) @s, i64 noundef %15) #4
  %17 = load i64, ptr %16, align 8, !tbaa !5
  %18 = getelementptr inbounds [524288 x i64], ptr @res, i64 0, i64 %17
  %19 = load i64, ptr %18, align 8, !tbaa !5
  %20 = load i64, ptr %6, align 8, !tbaa !5
  %21 = getelementptr inbounds [1048576 x %struct.node], ptr @tree, i64 0, i64 %20
  %22 = getelementptr inbounds %struct.node, ptr %21, i32 0, i32 0
  store i64 %19, ptr %22, align 16, !tbaa !9
  %23 = load i64, ptr %6, align 8, !tbaa !5
  %24 = getelementptr inbounds [1048576 x %struct.node], ptr @tree, i64 0, i64 %23
  %25 = getelementptr inbounds %struct.node, ptr %24, i32 0, i32 1
  store i64 %19, ptr %25, align 8, !tbaa !11
  br label %55

26:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %27 = load i64, ptr %4, align 8, !tbaa !5
  %28 = load i64, ptr %5, align 8, !tbaa !5
  %29 = add nsw i64 %27, %28
  %30 = sdiv i64 %29, 2
  store i64 %30, ptr %7, align 8, !tbaa !5
  %31 = load i64, ptr %4, align 8, !tbaa !5
  %32 = load i64, ptr %7, align 8, !tbaa !5
  %33 = load i64, ptr %6, align 8, !tbaa !5
  %34 = mul nsw i64 %33, 2
  call void @_Z5buildxxx(i64 noundef %31, i64 noundef %32, i64 noundef %34)
  %35 = load i64, ptr %7, align 8, !tbaa !5
  %36 = add nsw i64 %35, 1
  %37 = load i64, ptr %5, align 8, !tbaa !5
  %38 = load i64, ptr %6, align 8, !tbaa !5
  %39 = mul nsw i64 %38, 2
  %40 = add nsw i64 %39, 1
  call void @_Z5buildxxx(i64 noundef %36, i64 noundef %37, i64 noundef %40)
  call void @llvm.lifetime.start.p0(i64 16, ptr %8) #4
  %41 = load i64, ptr %6, align 8, !tbaa !5
  %42 = mul nsw i64 %41, 2
  %43 = getelementptr inbounds [1048576 x %struct.node], ptr @tree, i64 0, i64 %42
  %44 = load i64, ptr %6, align 8, !tbaa !5
  %45 = mul nsw i64 %44, 2
  %46 = add nsw i64 %45, 1
  %47 = getelementptr inbounds [1048576 x %struct.node], ptr @tree, i64 0, i64 %46
  %48 = call { i64, i64 } @_ZplRK4nodeS1_(ptr noundef nonnull align 8 dereferenceable(16) %43, ptr noundef nonnull align 8 dereferenceable(16) %47)
  %49 = getelementptr inbounds { i64, i64 }, ptr %8, i32 0, i32 0
  %50 = extractvalue { i64, i64 } %48, 0
  store i64 %50, ptr %49, align 8
  %51 = getelementptr inbounds { i64, i64 }, ptr %8, i32 0, i32 1
  %52 = extractvalue { i64, i64 } %48, 1
  store i64 %52, ptr %51, align 8
  %53 = load i64, ptr %6, align 8, !tbaa !5
  %54 = getelementptr inbounds [1048576 x %struct.node], ptr @tree, i64 0, i64 %53
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %54, ptr align 8 %8, i64 16, i1 false), !tbaa.struct !12
  call void @llvm.lifetime.end.p0(i64 16, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  br label %55

55:                                               ; preds = %26, %14
  ret void
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(8) ptr @_ZNSt6vectorIxSaIxEEixEm(ptr noundef nonnull align 8 dereferenceable(24), i64 noundef) #2 align 2

; Function Attrs: mustprogress uwtable
declare dso_local { i64, i64 } @_ZplRK4nodeS1_(ptr noundef nonnull align 8 dereferenceable(16), ptr noundef nonnull align 8 dereferenceable(16)) #1

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

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 = { 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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = !{!10, !6, i64 0}
!10 = !{!"_ZTS4node", !6, i64 0, !6, i64 8}
!11 = !{!10, !6, i64 8}
!12 = !{i64 0, i64 8, !5, i64 8, i64 8, !5}


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

%"class.std::vector" = type { %"struct.std::_Vector_base" }
%"struct.std::_Vector_base" = type { %"struct.std::_Vector_base<long long, std::allocator<long long>>::_Vector_impl" }
%"struct.std::_Vector_base<long long, std::allocator<long long>>::_Vector_impl" = type { %"struct.std::_Vector_base<long long, std::allocator<long long>>::_Vector_impl_data" }
%"struct.std::_Vector_base<long long, std::allocator<long long>>::_Vector_impl_data" = type { ptr, ptr, ptr }
%struct.node = type { i64, i64 }

@res = external dso_local local_unnamed_addr global [524288 x i64], align 16
@s = external dso_local global %"class.std::vector", align 8
@tree = external dso_local global [1048576 x %struct.node], align 16
@lazy = external dso_local local_unnamed_addr global [1048576 x i64], align 16

; Function Attrs: mustprogress uwtable
define dso_local void @_Z5buildxxx(i64 noundef %0, i64 noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = getelementptr inbounds [1048576 x i64], ptr @lazy, i64 0, i64 %2
  store i64 0, ptr %4, align 8, !tbaa !5
  %5 = icmp eq i64 %0, %1
  br i1 %5, label %6, label %13

6:                                                ; preds = %3
  %7 = tail call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSt6vectorIxSaIxEEixEm(ptr noundef nonnull align 8 dereferenceable(24) @s, i64 noundef %0) #2
  %8 = load i64, ptr %7, align 8, !tbaa !5
  %9 = getelementptr inbounds [524288 x i64], ptr @res, i64 0, i64 %8
  %10 = load i64, ptr %9, align 8, !tbaa !5
  br label %common.ret

common.ret:                                       ; preds = %6, %13
  %.sink20 = phi i64 [ %10, %6 ], [ %22, %13 ]
  %.sink = phi i64 [ %10, %6 ], [ %23, %13 ]
  %11 = getelementptr inbounds [1048576 x %struct.node], ptr @tree, i64 0, i64 %2
  store i64 %.sink20, ptr %11, align 16, !tbaa !5
  %12 = getelementptr inbounds i8, ptr %11, i64 8
  store i64 %.sink, ptr %12, align 8, !tbaa !5
  ret void

13:                                               ; preds = %3
  %14 = add nsw i64 %1, %0
  %15 = sdiv i64 %14, 2
  %16 = shl nsw i64 %2, 1
  tail call void @_Z5buildxxx(i64 noundef %0, i64 noundef %15, i64 noundef %16)
  %17 = add nsw i64 %15, 1
  %18 = or disjoint i64 %16, 1
  tail call void @_Z5buildxxx(i64 noundef %17, i64 noundef %1, i64 noundef %18)
  %19 = getelementptr inbounds [1048576 x %struct.node], ptr @tree, i64 0, i64 %16
  %20 = getelementptr inbounds [1048576 x %struct.node], ptr @tree, i64 0, i64 %18
  %21 = tail call { i64, i64 } @_ZplRK4nodeS1_(ptr noundef nonnull align 8 dereferenceable(16) %19, ptr noundef nonnull align 8 dereferenceable(16) %20)
  %22 = extractvalue { i64, i64 } %21, 0
  %23 = extractvalue { i64, i64 } %21, 1
  br label %common.ret
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(8) ptr @_ZNSt6vectorIxSaIxEEixEm(ptr noundef nonnull align 8 dereferenceable(24), i64 noundef) local_unnamed_addr #1 align 2

; Function Attrs: mustprogress uwtable
declare dso_local { i64, i64 } @_ZplRK4nodeS1_(ptr noundef nonnull align 8 dereferenceable(16), ptr noundef nonnull align 8 dereferenceable(16)) local_unnamed_addr #0

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 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 #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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}


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