; LLVM IR from TLE-MEC/Hack-CP-DSA :: Hackerrank/Sales by Match/solution.ll (Z12sockMerchantiSt6vectorIiSaIiEE)

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z12sockMerchantiSt6vectorIiSaIiEE(i32 noundef %0, ptr noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca [1000 x i32], align 16
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4000, ptr %5) #3
  call void @llvm.memset.p0.i64(ptr align 16 %5, i8 0, i64 4000, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 0, ptr %6, align 4, !tbaa !5
  br label %10

10:                                               ; preds = %24, %2
  %11 = load i32, ptr %6, align 4, !tbaa !5
  %12 = load i32, ptr %3, align 4, !tbaa !5
  %13 = icmp slt i32 %11, %12
  br i1 %13, label %15, label %14

14:                                               ; preds = %10
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  br label %27

15:                                               ; preds = %10
  %16 = load i32, ptr %6, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) %1, i64 noundef %17) #3
  %19 = load i32, ptr %18, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [1000 x i32], ptr %5, i64 0, i64 %20
  %22 = load i32, ptr %21, align 4, !tbaa !5
  %23 = add nsw i32 %22, 1
  store i32 %23, ptr %21, align 4, !tbaa !5
  br label %24

24:                                               ; preds = %15
  %25 = load i32, ptr %6, align 4, !tbaa !5
  %26 = add nsw i32 %25, 1
  store i32 %26, ptr %6, align 4, !tbaa !5
  br label %10, !llvm.loop !11

27:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 0, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  store i32 0, ptr %8, align 4, !tbaa !5
  br label %28

28:                                               ; preds = %49, %27
  %29 = load i32, ptr %8, align 4, !tbaa !5
  %30 = icmp slt i32 %29, 1000
  br i1 %30, label %32, label %31

31:                                               ; preds = %28
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  br label %52

32:                                               ; preds = %28
  %33 = load i32, ptr %8, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [1000 x i32], ptr %5, i64 0, i64 %34
  %36 = load i32, ptr %35, align 4, !tbaa !5
  %37 = icmp eq i32 %36, 0
  br i1 %37, label %38, label %39

38:                                               ; preds = %32
  br label %49

39:                                               ; preds = %32
  %40 = load i32, ptr %8, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [1000 x i32], ptr %5, i64 0, i64 %41
  %43 = load i32, ptr %42, align 4, !tbaa !5
  %44 = sdiv i32 %43, 2
  %45 = load i32, ptr %8, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [1000 x i32], ptr %5, i64 0, i64 %46
  store i32 %44, ptr %47, align 4, !tbaa !5
  br label %48

48:                                               ; preds = %39
  br label %49

49:                                               ; preds = %48, %38
  %50 = load i32, ptr %8, align 4, !tbaa !5
  %51 = add nsw i32 %50, 1
  store i32 %51, ptr %8, align 4, !tbaa !5
  br label %28, !llvm.loop !13

52:                                               ; preds = %31
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  store i32 0, ptr %9, align 4, !tbaa !5
  br label %53

53:                                               ; preds = %64, %52
  %54 = load i32, ptr %9, align 4, !tbaa !5
  %55 = icmp slt i32 %54, 1000
  br i1 %55, label %57, label %56

56:                                               ; preds = %53
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  br label %67

57:                                               ; preds = %53
  %58 = load i32, ptr %9, align 4, !tbaa !5
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds [1000 x i32], ptr %5, i64 0, i64 %59
  %61 = load i32, ptr %60, align 4, !tbaa !5
  %62 = load i32, ptr %7, align 4, !tbaa !5
  %63 = add nsw i32 %62, %61
  store i32 %63, ptr %7, align 4, !tbaa !5
  br label %64

64:                                               ; preds = %57
  %65 = load i32, ptr %9, align 4, !tbaa !5
  %66 = add nsw i32 %65, 1
  store i32 %66, ptr %9, align 4, !tbaa !5
  br label %53, !llvm.loop !14

67:                                               ; preds = %56
  %68 = load i32, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4000, ptr %5) #3
  ret i32 %68
}

; 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 nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

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

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { 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, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}
!14 = distinct !{!14, !12}


; 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 nounwind uwtable
define dso_local noundef i32 @_Z12sockMerchantiSt6vectorIiSaIiEE(i32 noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  %3 = alloca [1000 x i32], align 16
  call void @llvm.lifetime.start.p0(i64 4000, ptr nonnull %3) #4
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(4000) %3, i8 0, i64 4000, i1 false)
  %4 = icmp sgt i32 %0, 0
  br i1 %4, label %.lr.ph.preheader, label %vector.body.preheader

.lr.ph.preheader:                                 ; preds = %2
  %wide.trip.count = zext nneg i32 %0 to i64
  br label %.lr.ph

vector.body:                                      ; preds = %vector.body.preheader, %pred.store.continue46
  %index = phi i64 [ %index.next, %pred.store.continue46 ], [ 0, %vector.body.preheader ]
  %5 = or disjoint i64 %index, 4
  %6 = getelementptr inbounds [1000 x i32], ptr %3, i64 0, i64 %index
  %7 = getelementptr inbounds i8, ptr %6, i64 16
  %wide.load = load <4 x i32>, ptr %6, align 16, !tbaa !5
  %wide.load32 = load <4 x i32>, ptr %7, align 16, !tbaa !5
  %8 = icmp ne <4 x i32> %wide.load, zeroinitializer
  %9 = icmp ne <4 x i32> %wide.load32, zeroinitializer
  %10 = extractelement <4 x i1> %8, i64 0
  br i1 %10, label %pred.store.if, label %pred.store.continue

pred.store.if:                                    ; preds = %vector.body
  %11 = getelementptr inbounds [1000 x i32], ptr %3, i64 0, i64 %index
  %12 = extractelement <4 x i32> %wide.load, i64 0
  %13 = sdiv i32 %12, 2
  store i32 %13, ptr %11, align 16, !tbaa !5
  br label %pred.store.continue

pred.store.continue:                              ; preds = %pred.store.if, %vector.body
  %14 = extractelement <4 x i1> %8, i64 1
  br i1 %14, label %pred.store.if33, label %pred.store.continue34

pred.store.if33:                                  ; preds = %pred.store.continue
  %15 = or disjoint i64 %index, 1
  %16 = getelementptr inbounds [1000 x i32], ptr %3, i64 0, i64 %15
  %17 = extractelement <4 x i32> %wide.load, i64 1
  %18 = sdiv i32 %17, 2
  store i32 %18, ptr %16, align 4, !tbaa !5
  br label %pred.store.continue34

pred.store.continue34:                            ; preds = %pred.store.if33, %pred.store.continue
  %19 = extractelement <4 x i1> %8, i64 2
  br i1 %19, label %pred.store.if35, label %pred.store.continue36

pred.store.if35:                                  ; preds = %pred.store.continue34
  %20 = or disjoint i64 %index, 2
  %21 = getelementptr inbounds [1000 x i32], ptr %3, i64 0, i64 %20
  %22 = extractelement <4 x i32> %wide.load, i64 2
  %23 = sdiv i32 %22, 2
  store i32 %23, ptr %21, align 8, !tbaa !5
  br label %pred.store.continue36

pred.store.continue36:                            ; preds = %pred.store.if35, %pred.store.continue34
  %24 = extractelement <4 x i1> %8, i64 3
  br i1 %24, label %pred.store.if37, label %pred.store.continue38

pred.store.if37:                                  ; preds = %pred.store.continue36
  %25 = or disjoint i64 %index, 3
  %26 = getelementptr inbounds [1000 x i32], ptr %3, i64 0, i64 %25
  %27 = extractelement <4 x i32> %wide.load, i64 3
  %28 = sdiv i32 %27, 2
  store i32 %28, ptr %26, align 4, !tbaa !5
  br label %pred.store.continue38

pred.store.continue38:                            ; preds = %pred.store.if37, %pred.store.continue36
  %29 = extractelement <4 x i1> %9, i64 0
  br i1 %29, label %pred.store.if39, label %pred.store.continue40

pred.store.if39:                                  ; preds = %pred.store.continue38
  %30 = getelementptr inbounds [1000 x i32], ptr %3, i64 0, i64 %5
  %31 = extractelement <4 x i32> %wide.load32, i64 0
  %32 = sdiv i32 %31, 2
  store i32 %32, ptr %30, align 16, !tbaa !5
  br label %pred.store.continue40

pred.store.continue40:                            ; preds = %pred.store.if39, %pred.store.continue38
  %33 = extractelement <4 x i1> %9, i64 1
  br i1 %33, label %pred.store.if41, label %pred.store.continue42

pred.store.if41:                                  ; preds = %pred.store.continue40
  %34 = or disjoint i64 %index, 5
  %35 = getelementptr inbounds [1000 x i32], ptr %3, i64 0, i64 %34
  %36 = extractelement <4 x i32> %wide.load32, i64 1
  %37 = sdiv i32 %36, 2
  store i32 %37, ptr %35, align 4, !tbaa !5
  br label %pred.store.continue42

pred.store.continue42:                            ; preds = %pred.store.if41, %pred.store.continue40
  %38 = extractelement <4 x i1> %9, i64 2
  br i1 %38, label %pred.store.if43, label %pred.store.continue44

pred.store.if43:                                  ; preds = %pred.store.continue42
  %39 = or disjoint i64 %index, 6
  %40 = getelementptr inbounds [1000 x i32], ptr %3, i64 0, i64 %39
  %41 = extractelement <4 x i32> %wide.load32, i64 2
  %42 = sdiv i32 %41, 2
  store i32 %42, ptr %40, align 8, !tbaa !5
  br label %pred.store.continue44

pred.store.continue44:                            ; preds = %pred.store.if43, %pred.store.continue42
  %43 = extractelement <4 x i1> %9, i64 3
  br i1 %43, label %pred.store.if45, label %pred.store.continue46

pred.store.if45:                                  ; preds = %pred.store.continue44
  %44 = or disjoint i64 %index, 7
  %45 = getelementptr inbounds [1000 x i32], ptr %3, i64 0, i64 %44
  %46 = extractelement <4 x i32> %wide.load32, i64 3
  %47 = sdiv i32 %46, 2
  store i32 %47, ptr %45, align 4, !tbaa !5
  br label %pred.store.continue46

pred.store.continue46:                            ; preds = %pred.store.if45, %pred.store.continue44
  %index.next = add nuw i64 %index, 8
  %48 = icmp eq i64 %index.next, 1000
  br i1 %48, label %vector.body51, label %vector.body, !llvm.loop !9

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next, %.lr.ph ]
  %49 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) %1, i64 noundef %indvars.iv) #4
  %50 = load i32, ptr %49, align 4, !tbaa !5
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds [1000 x i32], ptr %3, i64 0, i64 %51
  %53 = load i32, ptr %52, align 4, !tbaa !5
  %54 = add nsw i32 %53, 1
  store i32 %54, ptr %52, align 4, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %vector.body.preheader, label %.lr.ph, !llvm.loop !13

vector.body.preheader:                            ; preds = %.lr.ph, %2
  br label %vector.body

vector.body51:                                    ; preds = %pred.store.continue46, %vector.body51
  %index52 = phi i64 [ %index.next56.4, %vector.body51 ], [ 0, %pred.store.continue46 ]
  %vec.phi = phi <4 x i32> [ %73, %vector.body51 ], [ zeroinitializer, %pred.store.continue46 ]
  %vec.phi53 = phi <4 x i32> [ %74, %vector.body51 ], [ zeroinitializer, %pred.store.continue46 ]
  %55 = getelementptr inbounds [1000 x i32], ptr %3, i64 0, i64 %index52
  %56 = getelementptr inbounds i8, ptr %55, i64 16
  %wide.load54 = load <4 x i32>, ptr %55, align 16, !tbaa !5
  %wide.load55 = load <4 x i32>, ptr %56, align 16, !tbaa !5
  %57 = add <4 x i32> %wide.load54, %vec.phi
  %58 = add <4 x i32> %wide.load55, %vec.phi53
  %index.next56 = add nuw nsw i64 %index52, 8
  %59 = getelementptr inbounds [1000 x i32], ptr %3, i64 0, i64 %index.next56
  %60 = getelementptr inbounds i8, ptr %59, i64 16
  %wide.load54.1 = load <4 x i32>, ptr %59, align 16, !tbaa !5
  %wide.load55.1 = load <4 x i32>, ptr %60, align 16, !tbaa !5
  %61 = add <4 x i32> %wide.load54.1, %57
  %62 = add <4 x i32> %wide.load55.1, %58
  %index.next56.1 = add nuw nsw i64 %index52, 16
  %63 = getelementptr inbounds [1000 x i32], ptr %3, i64 0, i64 %index.next56.1
  %64 = getelementptr inbounds i8, ptr %63, i64 16
  %wide.load54.2 = load <4 x i32>, ptr %63, align 16, !tbaa !5
  %wide.load55.2 = load <4 x i32>, ptr %64, align 16, !tbaa !5
  %65 = add <4 x i32> %wide.load54.2, %61
  %66 = add <4 x i32> %wide.load55.2, %62
  %index.next56.2 = add nuw nsw i64 %index52, 24
  %67 = getelementptr inbounds [1000 x i32], ptr %3, i64 0, i64 %index.next56.2
  %68 = getelementptr inbounds i8, ptr %67, i64 16
  %wide.load54.3 = load <4 x i32>, ptr %67, align 16, !tbaa !5
  %wide.load55.3 = load <4 x i32>, ptr %68, align 16, !tbaa !5
  %69 = add <4 x i32> %wide.load54.3, %65
  %70 = add <4 x i32> %wide.load55.3, %66
  %index.next56.3 = add nuw nsw i64 %index52, 32
  %71 = getelementptr inbounds [1000 x i32], ptr %3, i64 0, i64 %index.next56.3
  %72 = getelementptr inbounds i8, ptr %71, i64 16
  %wide.load54.4 = load <4 x i32>, ptr %71, align 16, !tbaa !5
  %wide.load55.4 = load <4 x i32>, ptr %72, align 16, !tbaa !5
  %73 = add <4 x i32> %wide.load54.4, %69
  %74 = add <4 x i32> %wide.load55.4, %70
  %index.next56.4 = add nuw nsw i64 %index52, 40
  %75 = icmp eq i64 %index.next56.4, 1000
  br i1 %75, label %middle.block47, label %vector.body51, !llvm.loop !14

middle.block47:                                   ; preds = %vector.body51
  %bin.rdx = add <4 x i32> %74, %73
  %76 = tail call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %bin.rdx)
  call void @llvm.lifetime.end.p0(i64 4000, ptr nonnull %3) #4
  ret i32 %76
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

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

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.vector.reduce.add.v4i32(<4 x i32>) #3

attributes #0 = { 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 #1 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = distinct !{!9, !10, !11, !12}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!"llvm.loop.isvectorized", i32 1}
!12 = !{!"llvm.loop.unroll.runtime.disable"}
!13 = distinct !{!13, !10}
!14 = distinct !{!14, !10, !11, !12}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass