; LLVM IR from ippeb/ACM-ICPC :: algebra/bignum/bignum.ll (Z5shiftR6bignumi)

; 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.bignum = type { [12 x i64], i32 }

; 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 nounwind uwtable
define dso_local void @_Z5shiftR6bignumi(ptr noundef nonnull align 8 dereferenceable(104) %0, i32 noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  %7 = load ptr, ptr %3, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.bignum, ptr %7, i32 0, i32 1
  %9 = load i32, ptr %8, align 8, !tbaa !11
  %10 = sub nsw i32 %9, 1
  store i32 %10, ptr %5, align 4, !tbaa !9
  br label %11

11:                                               ; preds = %29, %2
  %12 = load i32, ptr %5, align 4, !tbaa !9
  %13 = icmp sge i32 %12, 0
  br i1 %13, label %15, label %14

14:                                               ; preds = %11
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  br label %32

15:                                               ; preds = %11
  %16 = load ptr, ptr %3, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.bignum, ptr %16, i32 0, i32 0
  %18 = load i32, ptr %5, align 4, !tbaa !9
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [12 x i64], ptr %17, i64 0, i64 %19
  %21 = load i64, ptr %20, align 8, !tbaa !13
  %22 = load ptr, ptr %3, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.bignum, ptr %22, i32 0, i32 0
  %24 = load i32, ptr %5, align 4, !tbaa !9
  %25 = load i32, ptr %4, align 4, !tbaa !9
  %26 = add nsw i32 %24, %25
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [12 x i64], ptr %23, i64 0, i64 %27
  store i64 %21, ptr %28, align 8, !tbaa !13
  br label %29

29:                                               ; preds = %15
  %30 = load i32, ptr %5, align 4, !tbaa !9
  %31 = add nsw i32 %30, -1
  store i32 %31, ptr %5, align 4, !tbaa !9
  br label %11, !llvm.loop !15

32:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %33 = load i32, ptr %4, align 4, !tbaa !9
  %34 = sub nsw i32 %33, 1
  store i32 %34, ptr %6, align 4, !tbaa !9
  br label %35

35:                                               ; preds = %45, %32
  %36 = load i32, ptr %6, align 4, !tbaa !9
  %37 = icmp sge i32 %36, 0
  br i1 %37, label %39, label %38

38:                                               ; preds = %35
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  br label %48

39:                                               ; preds = %35
  %40 = load ptr, ptr %3, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.bignum, ptr %40, i32 0, i32 0
  %42 = load i32, ptr %6, align 4, !tbaa !9
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds [12 x i64], ptr %41, i64 0, i64 %43
  store i64 0, ptr %44, align 8, !tbaa !13
  br label %45

45:                                               ; preds = %39
  %46 = load i32, ptr %6, align 4, !tbaa !9
  %47 = add nsw i32 %46, -1
  store i32 %47, ptr %6, align 4, !tbaa !9
  br label %35, !llvm.loop !17

48:                                               ; preds = %38
  %49 = load i32, ptr %4, align 4, !tbaa !9
  %50 = load ptr, ptr %3, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.bignum, ptr %50, i32 0, i32 1
  %52 = load i32, ptr %51, align 8, !tbaa !11
  %53 = add nsw i32 %52, %49
  store i32 %53, ptr %51, align 8, !tbaa !11
  ret void
}

attributes #0 = { 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 = { 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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 96}
!12 = !{!"_ZTS6bignum", !7, i64 0, !10, i64 96}
!13 = !{!14, !14, i64 0}
!14 = !{!"long long", !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = distinct !{!17, !16}


; 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 nofree norecurse nosync nounwind memory(argmem: readwrite) uwtable
define dso_local void @_Z5shiftR6bignumi(ptr nocapture noundef nonnull align 8 dereferenceable(104) %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 96
  %4 = load i32, ptr %3, align 8, !tbaa !5
  %5 = icmp sgt i32 %4, 0
  br i1 %5, label %.lr.ph.preheader, label %.preheader

.lr.ph.preheader:                                 ; preds = %2
  %6 = zext nneg i32 %4 to i64
  %7 = sext i32 %1 to i64
  %min.iters.check = icmp ult i32 %4, 40
  br i1 %min.iters.check, label %.lr.ph.preheader30, label %vector.scevcheck

.lr.ph.preheader30:                               ; preds = %middle.block, %vector.scevcheck, %.lr.ph.preheader
  %indvars.iv.ph = phi i64 [ %6, %vector.scevcheck ], [ %6, %.lr.ph.preheader ], [ %ind.end, %middle.block ]
  br label %.lr.ph

vector.scevcheck:                                 ; preds = %.lr.ph.preheader
  %8 = add nsw i64 %6, -1
  %9 = add nsw i64 %7, %6
  %10 = shl nsw i64 %9, 3
  %11 = getelementptr i8, ptr %0, i64 %10
  %scevgep = getelementptr i8, ptr %11, i64 -8
  %mul.result.neg = mul nsw i64 %8, -8
  %12 = getelementptr i8, ptr %scevgep, i64 %mul.result.neg
  %13 = icmp ugt ptr %12, %scevgep
  %14 = shl nuw nsw i64 %6, 3
  %15 = getelementptr i8, ptr %0, i64 %14
  %scevgep22 = getelementptr i8, ptr %15, i64 -8
  %mul.result24.neg = mul nsw i64 %8, -8
  %mul.overflow25 = icmp ugt i64 %8, 2305843009213693951
  %16 = getelementptr i8, ptr %scevgep22, i64 %mul.result24.neg
  %17 = icmp ugt ptr %16, %scevgep22
  %18 = or i1 %17, %mul.overflow25
  %19 = or i1 %13, %18
  %20 = mul nsw i64 %7, -8
  %diff.check = icmp ult i64 %20, 32
  %or.cond = select i1 %19, i1 true, i1 %diff.check
  br i1 %or.cond, label %.lr.ph.preheader30, label %vector.ph

vector.ph:                                        ; preds = %vector.scevcheck
  %n.vec = and i64 %6, 2147483644
  %ind.end = and i64 %6, 3
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %21 = xor i64 %index, -1
  %22 = add i64 %21, %6
  %23 = getelementptr inbounds [12 x i64], ptr %0, i64 0, i64 %22
  %24 = getelementptr inbounds i8, ptr %23, i64 -8
  %25 = getelementptr inbounds i8, ptr %23, i64 -24
  %wide.load = load <2 x i64>, ptr %24, align 8, !tbaa !10
  %wide.load26 = load <2 x i64>, ptr %25, align 8, !tbaa !10
  %26 = add nsw i64 %22, %7
  %27 = getelementptr inbounds [12 x i64], ptr %0, i64 0, i64 %26
  %28 = getelementptr inbounds i8, ptr %27, i64 -8
  %29 = getelementptr inbounds i8, ptr %27, i64 -24
  store <2 x i64> %wide.load, ptr %28, align 8, !tbaa !10
  store <2 x i64> %wide.load26, ptr %29, align 8, !tbaa !10
  %index.next = add nuw i64 %index, 4
  %30 = icmp eq i64 %index.next, %n.vec
  br i1 %30, label %middle.block, label %vector.body, !llvm.loop !12

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %n.vec, %6
  br i1 %cmp.n, label %.preheader, label %.lr.ph.preheader30

.preheader:                                       ; preds = %.lr.ph, %middle.block, %2
  %31 = icmp sgt i32 %1, 0
  br i1 %31, label %.lr.ph17.preheader, label %._crit_edge

.lr.ph17.preheader:                               ; preds = %.preheader
  %32 = zext nneg i32 %1 to i64
  %33 = shl nuw nsw i64 %32, 3
  tail call void @llvm.memset.p0.i64(ptr nonnull align 8 %0, i8 0, i64 %33, i1 false), !tbaa !10
  br label %._crit_edge

.lr.ph:                                           ; preds = %.lr.ph.preheader30, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ %indvars.iv.ph, %.lr.ph.preheader30 ]
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %34 = getelementptr inbounds [12 x i64], ptr %0, i64 0, i64 %indvars.iv.next
  %35 = load i64, ptr %34, align 8, !tbaa !10
  %36 = add nsw i64 %indvars.iv.next, %7
  %37 = getelementptr inbounds [12 x i64], ptr %0, i64 0, i64 %36
  store i64 %35, ptr %37, align 8, !tbaa !10
  %38 = icmp ugt i64 %indvars.iv, 1
  br i1 %38, label %.lr.ph, label %.preheader, !llvm.loop !16

._crit_edge:                                      ; preds = %.lr.ph17.preheader, %.preheader
  %39 = add nsw i32 %4, %1
  store i32 %39, ptr %3, align 8, !tbaa !5
  ret void
}

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

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(argmem: readwrite) 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 nounwind willreturn memory(argmem: write) }

!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, !9, i64 96}
!6 = !{!"_ZTS6bignum", !7, i64 0, !9, i64 96}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!"int", !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"long long", !7, i64 0}
!12 = distinct !{!12, !13, !14, !15}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!"llvm.loop.isvectorized", i32 1}
!15 = !{!"llvm.loop.unroll.runtime.disable"}
!16 = distinct !{!16, !13, !14}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalDCEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopIdiomRecognizePass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass