; LLVM IR from abeaumont/competitive-programming :: codechef/feb18/broclk.ll (Z6modinvnn)

; 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 @_Z6modinvnn(i64 noundef %0, i64 noundef %1, i64 noundef %2, i64 noundef %3) #0 {
  %5 = alloca i32, align 4
  %6 = alloca i128, align 16
  %7 = alloca i128, align 16
  %8 = alloca i128, align 16
  %9 = alloca i128, align 16
  %10 = alloca i128, align 16
  %11 = alloca i128, align 16
  %12 = alloca i128, align 16
  %13 = alloca i128, align 16
  %14 = alloca i128, align 16
  %15 = alloca i32, align 4
  %16 = getelementptr inbounds { i64, i64 }, ptr %6, i32 0, i32 0
  store i64 %0, ptr %16, align 16
  %17 = getelementptr inbounds { i64, i64 }, ptr %6, i32 0, i32 1
  store i64 %1, ptr %17, align 8
  %18 = load i128, ptr %6, align 16, !tbaa !5
  %19 = getelementptr inbounds { i64, i64 }, ptr %7, i32 0, i32 0
  store i64 %2, ptr %19, align 16
  %20 = getelementptr inbounds { i64, i64 }, ptr %7, i32 0, i32 1
  store i64 %3, ptr %20, align 8
  %21 = load i128, ptr %7, align 16, !tbaa !5
  store i128 %18, ptr %8, align 16, !tbaa !5
  store i128 %21, ptr %9, align 16, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 16, ptr %10) #2
  %22 = load i128, ptr %9, align 16, !tbaa !5
  store i128 %22, ptr %10, align 16, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 16, ptr %11) #2
  call void @llvm.lifetime.start.p0(i64 16, ptr %12) #2
  call void @llvm.lifetime.start.p0(i64 16, ptr %13) #2
  store i128 0, ptr %13, align 16, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 16, ptr %14) #2
  store i128 1, ptr %14, align 16, !tbaa !5
  %23 = load i128, ptr %9, align 16, !tbaa !5
  %24 = icmp eq i128 %23, 1
  br i1 %24, label %25, label %26

25:                                               ; preds = %4
  store i32 1, ptr %5, align 4
  store i32 1, ptr %15, align 4
  br label %49

26:                                               ; preds = %4
  br label %27

27:                                               ; preds = %30, %26
  %28 = load i128, ptr %8, align 16, !tbaa !5
  %29 = icmp sgt i128 %28, 1
  br i1 %29, label %30, label %46

30:                                               ; preds = %27
  %31 = load i128, ptr %8, align 16, !tbaa !5
  %32 = load i128, ptr %9, align 16, !tbaa !5
  %33 = sdiv i128 %31, %32
  store i128 %33, ptr %12, align 16, !tbaa !5
  %34 = load i128, ptr %9, align 16, !tbaa !5
  store i128 %34, ptr %11, align 16, !tbaa !5
  %35 = load i128, ptr %8, align 16, !tbaa !5
  %36 = load i128, ptr %9, align 16, !tbaa !5
  %37 = srem i128 %35, %36
  store i128 %37, ptr %9, align 16, !tbaa !5
  %38 = load i128, ptr %11, align 16, !tbaa !5
  store i128 %38, ptr %8, align 16, !tbaa !5
  %39 = load i128, ptr %13, align 16, !tbaa !5
  store i128 %39, ptr %11, align 16, !tbaa !5
  %40 = load i128, ptr %14, align 16, !tbaa !5
  %41 = load i128, ptr %12, align 16, !tbaa !5
  %42 = load i128, ptr %13, align 16, !tbaa !5
  %43 = mul nsw i128 %41, %42
  %44 = sub nsw i128 %40, %43
  store i128 %44, ptr %13, align 16, !tbaa !5
  %45 = load i128, ptr %11, align 16, !tbaa !5
  store i128 %45, ptr %14, align 16, !tbaa !5
  br label %27, !llvm.loop !9

46:                                               ; preds = %27
  %47 = load i128, ptr %14, align 16, !tbaa !5
  %48 = trunc i128 %47 to i32
  store i32 %48, ptr %5, align 4
  store i32 1, ptr %15, align 4
  br label %49

49:                                               ; preds = %46, %25
  call void @llvm.lifetime.end.p0(i64 16, ptr %14) #2
  call void @llvm.lifetime.end.p0(i64 16, ptr %13) #2
  call void @llvm.lifetime.end.p0(i64 16, ptr %12) #2
  call void @llvm.lifetime.end.p0(i64 16, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 16, ptr %10) #2
  %50 = load i32, ptr %5, align 4
  ret i32 %50
}

; 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

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 = { 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 = !{!"__int128", !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"

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none) uwtable
define dso_local noundef i32 @_Z6modinvnn(i64 noundef %0, i64 noundef %1, i64 noundef %2, i64 noundef %3) local_unnamed_addr #0 {
  %.sroa.2.0.insert.ext = zext i64 %3 to i128
  %.sroa.2.0.insert.shift = shl nuw i128 %.sroa.2.0.insert.ext, 64
  %.sroa.0.0.insert.ext = zext i64 %2 to i128
  %.sroa.0.0.insert.insert = or disjoint i128 %.sroa.2.0.insert.shift, %.sroa.0.0.insert.ext
  %5 = icmp eq i128 %.sroa.0.0.insert.insert, 1
  br i1 %5, label %._crit_edge, label %.preheader

.preheader:                                       ; preds = %4
  %.sroa.216.0.insert.ext = zext i64 %1 to i128
  %.sroa.216.0.insert.shift = shl nuw i128 %.sroa.216.0.insert.ext, 64
  %.sroa.015.0.insert.ext = zext i64 %0 to i128
  %.sroa.015.0.insert.insert = or disjoint i128 %.sroa.216.0.insert.shift, %.sroa.015.0.insert.ext
  %6 = icmp sgt i128 %.sroa.015.0.insert.insert, 1
  br i1 %6, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %.025 = phi i128 [ %.01824, %.lr.ph ], [ 1, %.preheader ]
  %.01824 = phi i128 [ %10, %.lr.ph ], [ 0, %.preheader ]
  %.02023 = phi i128 [ %.02122, %.lr.ph ], [ %.sroa.015.0.insert.insert, %.preheader ]
  %.02122 = phi i128 [ %.decomposed, %.lr.ph ], [ %.sroa.0.0.insert.insert, %.preheader ]
  %.02023.frozen = freeze i128 %.02023
  %.02122.frozen = freeze i128 %.02122
  %7 = sdiv i128 %.02023.frozen, %.02122.frozen
  %8 = mul i128 %7, %.02122.frozen
  %.decomposed = sub i128 %.02023.frozen, %8
  %9 = mul nsw i128 %7, %.01824
  %10 = sub nsw i128 %.025, %9
  %11 = icmp sgt i128 %.02122, 1
  br i1 %11, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !5

._crit_edge.loopexit:                             ; preds = %.lr.ph
  %extract.t = trunc i128 %.01824 to i32
  br label %._crit_edge

._crit_edge:                                      ; preds = %.preheader, %._crit_edge.loopexit, %4
  %.019 = phi i32 [ 1, %4 ], [ 1, %.preheader ], [ %extract.t, %._crit_edge.loopexit ]
  ret i32 %.019
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(none) 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" }

!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: DivRemPairsPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass