; LLVM IR from ravireddy07/cpp-book :: src/Coding-Blocks/FriendsAtCodingBlocks.ll (Z8FriendCBxxxx)

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

@lent = external dso_local global i64, align 8
@primes = external dso_local global %"class.std::vector", align 8
@dp = external dso_local global [20 x [65536 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 noundef i64 @_Z8FriendCBxxxx(i64 noundef %0, i64 noundef %1, i64 noundef %2, i64 noundef %3) #1 {
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = alloca i64, align 8
  %12 = alloca i64, align 8
  %13 = alloca i64, align 8
  store i64 %0, ptr %6, align 8, !tbaa !5
  store i64 %1, ptr %7, align 8, !tbaa !5
  store i64 %2, ptr %8, align 8, !tbaa !5
  store i64 %3, ptr %9, align 8, !tbaa !5
  %14 = load i64, ptr %8, align 8, !tbaa !5
  %15 = trunc i64 %14 to i32
  %16 = call i32 @llvm.ctpop.i32(i32 %15)
  %17 = sext i32 %16 to i64
  %18 = load i64, ptr %6, align 8, !tbaa !5
  %19 = add nsw i64 %18, 1
  %20 = icmp eq i64 %17, %19
  br i1 %20, label %21, label %22

21:                                               ; preds = %4
  store i64 0, ptr %5, align 8
  br label %103

22:                                               ; preds = %4
  %23 = load i64, ptr %7, align 8, !tbaa !5
  %24 = add nsw i64 %23, 1
  %25 = getelementptr inbounds [20 x [65536 x i64]], ptr @dp, i64 0, i64 %24
  %26 = load i64, ptr %8, align 8, !tbaa !5
  %27 = getelementptr inbounds [65536 x i64], ptr %25, i64 0, i64 %26
  %28 = load i64, ptr %27, align 8, !tbaa !5
  %29 = icmp ne i64 %28, -1
  br i1 %29, label %30, label %37

30:                                               ; preds = %22
  %31 = load i64, ptr %7, align 8, !tbaa !5
  %32 = add nsw i64 %31, 1
  %33 = getelementptr inbounds [20 x [65536 x i64]], ptr @dp, i64 0, i64 %32
  %34 = load i64, ptr %8, align 8, !tbaa !5
  %35 = getelementptr inbounds [65536 x i64], ptr %33, i64 0, i64 %34
  %36 = load i64, ptr %35, align 8, !tbaa !5
  store i64 %36, ptr %5, align 8
  br label %103

37:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #5
  %38 = load i64, ptr @lent, align 8, !tbaa !5
  store i64 %38, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #5
  store i64 0, ptr %11, align 8, !tbaa !5
  br label %39

39:                                               ; preds = %93, %37
  %40 = load i64, ptr %11, align 8, !tbaa !5
  %41 = load i64, ptr %6, align 8, !tbaa !5
  %42 = icmp sle i64 %40, %41
  br i1 %42, label %44, label %43

43:                                               ; preds = %39
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #5
  br label %96

44:                                               ; preds = %39
  %45 = load i64, ptr %11, align 8, !tbaa !5
  %46 = trunc i64 %45 to i32
  %47 = shl i32 1, %46
  %48 = sext i32 %47 to i64
  %49 = load i64, ptr %8, align 8, !tbaa !5
  %50 = and i64 %48, %49
  %51 = icmp ne i64 %50, 0
  br i1 %51, label %52, label %53

52:                                               ; preds = %44
  br label %93

53:                                               ; preds = %44
  %54 = load i64, ptr %7, align 8, !tbaa !5
  %55 = icmp eq i64 %54, -1
  br i1 %55, label %56, label %69

56:                                               ; preds = %53
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #5
  %57 = load i64, ptr %6, align 8, !tbaa !5
  %58 = load i64, ptr %11, align 8, !tbaa !5
  %59 = load i64, ptr %8, align 8, !tbaa !5
  %60 = load i64, ptr %11, align 8, !tbaa !5
  %61 = trunc i64 %60 to i32
  %62 = shl i32 1, %61
  %63 = sext i32 %62 to i64
  %64 = or i64 %59, %63
  %65 = load i64, ptr %9, align 8, !tbaa !5
  %66 = call noundef i64 @_Z8FriendCBxxxx(i64 noundef %57, i64 noundef %58, i64 noundef %64, i64 noundef %65)
  store i64 %66, ptr %12, align 8, !tbaa !5
  %67 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %10, ptr noundef nonnull align 8 dereferenceable(8) %12)
  %68 = load i64, ptr %67, align 8, !tbaa !5
  store i64 %68, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #5
  br label %92

69:                                               ; preds = %53
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #5
  %70 = load i64, ptr %7, align 8, !tbaa !5
  %71 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSt6vectorIxSaIxEEixEm(ptr noundef nonnull align 8 dereferenceable(24) @primes, i64 noundef %70) #5
  %72 = load i64, ptr %71, align 8, !tbaa !5
  %73 = load i64, ptr %11, align 8, !tbaa !5
  %74 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSt6vectorIxSaIxEEixEm(ptr noundef nonnull align 8 dereferenceable(24) @primes, i64 noundef %73) #5
  %75 = load i64, ptr %74, align 8, !tbaa !5
  %76 = mul nsw i64 %72, %75
  %77 = load i64, ptr %9, align 8, !tbaa !5
  %78 = srem i64 %76, %77
  %79 = load i64, ptr %6, align 8, !tbaa !5
  %80 = load i64, ptr %11, align 8, !tbaa !5
  %81 = load i64, ptr %8, align 8, !tbaa !5
  %82 = load i64, ptr %11, align 8, !tbaa !5
  %83 = trunc i64 %82 to i32
  %84 = shl i32 1, %83
  %85 = sext i32 %84 to i64
  %86 = or i64 %81, %85
  %87 = load i64, ptr %9, align 8, !tbaa !5
  %88 = call noundef i64 @_Z8FriendCBxxxx(i64 noundef %79, i64 noundef %80, i64 noundef %86, i64 noundef %87)
  %89 = add nsw i64 %78, %88
  store i64 %89, ptr %13, align 8, !tbaa !5
  %90 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %10, ptr noundef nonnull align 8 dereferenceable(8) %13)
  %91 = load i64, ptr %90, align 8, !tbaa !5
  store i64 %91, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #5
  br label %92

92:                                               ; preds = %69, %56
  br label %93

93:                                               ; preds = %92, %52
  %94 = load i64, ptr %11, align 8, !tbaa !5
  %95 = add nsw i64 %94, 1
  store i64 %95, ptr %11, align 8, !tbaa !5
  br label %39, !llvm.loop !9

96:                                               ; preds = %43
  %97 = load i64, ptr %10, align 8, !tbaa !5
  %98 = load i64, ptr %7, align 8, !tbaa !5
  %99 = add nsw i64 %98, 1
  %100 = getelementptr inbounds [20 x [65536 x i64]], ptr @dp, i64 0, i64 %99
  %101 = load i64, ptr %8, align 8, !tbaa !5
  %102 = getelementptr inbounds [65536 x i64], ptr %100, i64 0, i64 %101
  store i64 %97, ptr %102, align 8, !tbaa !5
  store i64 %97, ptr %5, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #5
  br label %103

103:                                              ; preds = %96, %30, %21
  %104 = load i64, ptr %5, align 8
  ret i64 %104
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.ctpop.i32(i32) #2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) #3

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

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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #3 = { 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 #4 = { 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 #5 = { 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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


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

@lent = external dso_local local_unnamed_addr global i64, align 8
@primes = external dso_local global %"class.std::vector", align 8
@dp = external dso_local local_unnamed_addr global [20 x [65536 x i64]], 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 nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z8FriendCBxxxx(i64 noundef %0, i64 noundef %1, i64 noundef %2, i64 noundef %3) local_unnamed_addr #1 {
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = trunc i64 %2 to i32
  %9 = tail call range(i32 0, 33) i32 @llvm.ctpop.i32(i32 %8)
  %10 = zext nneg i32 %9 to i64
  %11 = add nsw i64 %0, 1
  %12 = icmp eq i64 %11, %10
  br i1 %12, label %54, label %13

13:                                               ; preds = %4
  %14 = add nsw i64 %1, 1
  %15 = getelementptr inbounds [20 x [65536 x i64]], ptr @dp, i64 0, i64 %14, i64 %2
  %16 = load i64, ptr %15, align 8, !tbaa !5
  %.not = icmp eq i64 %16, -1
  br i1 %.not, label %17, label %54

17:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %5) #4
  %18 = load i64, ptr @lent, align 8, !tbaa !5
  store i64 %18, ptr %5, align 8, !tbaa !5
  %.not3234 = icmp slt i64 %0, 0
  br i1 %.not3234, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %17
  %19 = icmp eq i64 %1, -1
  br i1 %19, label %.lr.ph.split.us, label %.lr.ph.split

.lr.ph.split.us:                                  ; preds = %.lr.ph, %30
  %20 = phi i64 [ %31, %30 ], [ %18, %.lr.ph ]
  %.035.us = phi i64 [ %32, %30 ], [ 0, %.lr.ph ]
  %21 = trunc i64 %.035.us to i32
  %22 = shl nuw i32 1, %21
  %23 = sext i32 %22 to i64
  %24 = and i64 %23, %2
  %.not33.us = icmp eq i64 %24, 0
  br i1 %.not33.us, label %25, label %30

25:                                               ; preds = %.lr.ph.split.us
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %6) #4
  %26 = or i64 %23, %2
  %27 = call noundef i64 @_Z8FriendCBxxxx(i64 noundef %0, i64 noundef %.035.us, i64 noundef %26, i64 noundef %3)
  store i64 %27, ptr %6, align 8, !tbaa !5
  %28 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %5, ptr noundef nonnull align 8 dereferenceable(8) %6)
  %29 = load i64, ptr %28, align 8, !tbaa !5
  store i64 %29, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %6) #4
  br label %30

30:                                               ; preds = %25, %.lr.ph.split.us
  %31 = phi i64 [ %29, %25 ], [ %20, %.lr.ph.split.us ]
  %32 = add nuw i64 %.035.us, 1
  %exitcond37.not = icmp eq i64 %.035.us, %0
  br i1 %exitcond37.not, label %._crit_edge, label %.lr.ph.split.us, !llvm.loop !9

._crit_edge:                                      ; preds = %51, %30, %17
  %33 = phi i64 [ %18, %17 ], [ %31, %30 ], [ %52, %51 ]
  store i64 %33, ptr %15, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %5) #4
  br label %54

.lr.ph.split:                                     ; preds = %.lr.ph, %51
  %34 = phi i64 [ %52, %51 ], [ %18, %.lr.ph ]
  %.035 = phi i64 [ %53, %51 ], [ 0, %.lr.ph ]
  %35 = trunc i64 %.035 to i32
  %36 = shl nuw i32 1, %35
  %37 = sext i32 %36 to i64
  %38 = and i64 %37, %2
  %.not33 = icmp eq i64 %38, 0
  br i1 %.not33, label %39, label %51

39:                                               ; preds = %.lr.ph.split
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %7) #4
  %40 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSt6vectorIxSaIxEEixEm(ptr noundef nonnull align 8 dereferenceable(24) @primes, i64 noundef %1) #4
  %41 = load i64, ptr %40, align 8, !tbaa !5
  %42 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSt6vectorIxSaIxEEixEm(ptr noundef nonnull align 8 dereferenceable(24) @primes, i64 noundef %.035) #4
  %43 = load i64, ptr %42, align 8, !tbaa !5
  %44 = mul nsw i64 %43, %41
  %45 = srem i64 %44, %3
  %46 = or i64 %37, %2
  %47 = call noundef i64 @_Z8FriendCBxxxx(i64 noundef %0, i64 noundef %.035, i64 noundef %46, i64 noundef %3)
  %48 = add nsw i64 %47, %45
  store i64 %48, ptr %7, align 8, !tbaa !5
  %49 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %5, ptr noundef nonnull align 8 dereferenceable(8) %7)
  %50 = load i64, ptr %49, align 8, !tbaa !5
  store i64 %50, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %7) #4
  br label %51

51:                                               ; preds = %39, %.lr.ph.split
  %52 = phi i64 [ %50, %39 ], [ %34, %.lr.ph.split ]
  %53 = add nuw i64 %.035, 1
  %exitcond.not = icmp eq i64 %.035, %0
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph.split, !llvm.loop !9

54:                                               ; preds = %13, %4, %._crit_edge
  %.028 = phi i64 [ %33, %._crit_edge ], [ 0, %4 ], [ %16, %13 ]
  ret i64 %.028
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.ctpop.i32(i32) #2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) local_unnamed_addr #3

; 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

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #3 = { 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 #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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


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