; LLVM IR from tangjz/acm-icpc :: codeforces/complete/819C.ll (Z3pfsixi)

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

@tr = external dso_local global [65 x [65 x i32]], align 16
@ex = external dso_local global [65 x i32], align 16
@ans = external dso_local global i64, align 8
@N = external dso_local global %struct.Factor, align 8
@M = external dso_local global %struct.Factor, align 8

; Function Attrs: mustprogress uwtable
define dso_local void @_Z3pfsixi(i32 noundef %0, i64 noundef %1, i32 noundef %2) #0 {
  %4 = alloca i32, align 4
  %5 = alloca i64, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  store i32 %2, ptr %6, align 4, !tbaa !5
  %8 = load i32, ptr %6, align 4, !tbaa !5
  %9 = icmp ne i32 %8, 0
  br i1 %9, label %11, label %10

10:                                               ; preds = %3
  br label %71

11:                                               ; preds = %3
  %12 = load i32, ptr %4, align 4, !tbaa !5
  %13 = load i32, ptr getelementptr inbounds (%struct.Factor, ptr @N, i32 0, i32 1), align 8, !tbaa !11
  %14 = icmp eq i32 %12, %13
  br i1 %14, label %15, label %24

15:                                               ; preds = %11
  %16 = load i64, ptr @M, align 8, !tbaa !13
  %17 = load i64, ptr %5, align 8, !tbaa !9
  %18 = sdiv i64 %16, %17
  %19 = load i32, ptr %6, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = mul nsw i64 %18, %20
  %22 = load i64, ptr @ans, align 8, !tbaa !9
  %23 = add nsw i64 %22, %21
  store i64 %23, ptr @ans, align 8, !tbaa !9
  br label %71

24:                                               ; preds = %11
  %25 = load i32, ptr %4, align 4, !tbaa !5
  %26 = add nsw i32 %25, 1
  %27 = load i64, ptr %5, align 8, !tbaa !9
  %28 = load i32, ptr %6, align 4, !tbaa !5
  %29 = load i32, ptr %4, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [65 x i32], ptr @ex, i64 0, i64 %30
  %32 = load i32, ptr %31, align 4, !tbaa !5
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [65 x [65 x i32]], ptr @tr, i64 0, i64 %33
  %35 = getelementptr inbounds [65 x i32], ptr %34, i64 0, i64 0
  %36 = load i32, ptr %35, align 4, !tbaa !5
  %37 = mul nsw i32 %28, %36
  call void @_Z3pfsixi(i32 noundef %26, i64 noundef %27, i32 noundef %37)
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  store i32 1, ptr %7, align 4, !tbaa !5
  br label %38

38:                                               ; preds = %68, %24
  %39 = load i32, ptr %7, align 4, !tbaa !5
  %40 = load i32, ptr %4, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [23 x i32], ptr getelementptr inbounds (%struct.Factor, ptr @N, i32 0, i32 3), i64 0, i64 %41
  %43 = load i32, ptr %42, align 4, !tbaa !5
  %44 = icmp sle i32 %39, %43
  br i1 %44, label %46, label %45

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

46:                                               ; preds = %38
  %47 = load i32, ptr %4, align 4, !tbaa !5
  %48 = add nsw i32 %47, 1
  %49 = load i32, ptr %4, align 4, !tbaa !5
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds [23 x i32], ptr getelementptr inbounds (%struct.Factor, ptr @N, i32 0, i32 2), i64 0, i64 %50
  %52 = load i32, ptr %51, align 4, !tbaa !5
  %53 = sext i32 %52 to i64
  %54 = load i64, ptr %5, align 8, !tbaa !9
  %55 = mul nsw i64 %54, %53
  store i64 %55, ptr %5, align 8, !tbaa !9
  %56 = load i32, ptr %6, align 4, !tbaa !5
  %57 = load i32, ptr %4, align 4, !tbaa !5
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds [65 x i32], ptr @ex, i64 0, i64 %58
  %60 = load i32, ptr %59, align 4, !tbaa !5
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds [65 x [65 x i32]], ptr @tr, i64 0, i64 %61
  %63 = load i32, ptr %7, align 4, !tbaa !5
  %64 = sext i32 %63 to i64
  %65 = getelementptr inbounds [65 x i32], ptr %62, i64 0, i64 %64
  %66 = load i32, ptr %65, align 4, !tbaa !5
  %67 = mul nsw i32 %56, %66
  call void @_Z3pfsixi(i32 noundef %48, i64 noundef %55, i32 noundef %67)
  br label %68

68:                                               ; preds = %46
  %69 = load i32, ptr %7, align 4, !tbaa !5
  %70 = add nsw i32 %69, 1
  store i32 %70, ptr %7, align 4, !tbaa !5
  br label %38, !llvm.loop !14

71:                                               ; preds = %45, %15, %10
  ret void
}

; 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 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long long", !7, i64 0}
!11 = !{!12, !6, i64 8}
!12 = !{!"_ZTS6Factor", !10, i64 0, !6, i64 8, !7, i64 12, !7, i64 104}
!13 = !{!12, !10, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"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"

%struct.Factor = type { i64, i32, [23 x i32], [23 x i32] }

@tr = external dso_local local_unnamed_addr global [65 x [65 x i32]], align 16
@ex = external dso_local local_unnamed_addr global [65 x i32], align 16
@ans = external dso_local local_unnamed_addr global i64, align 8
@N = external dso_local local_unnamed_addr global %struct.Factor, align 8
@M = external dso_local local_unnamed_addr global %struct.Factor, align 8

; Function Attrs: mustprogress nofree nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z3pfsixi(i32 noundef %0, i64 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %.not = icmp eq i32 %2, 0
  br i1 %.not, label %.loopexit, label %4

4:                                                ; preds = %3
  %5 = load i32, ptr getelementptr inbounds (i8, ptr @N, i64 8), align 8, !tbaa !5
  %6 = icmp eq i32 %5, %0
  br i1 %6, label %7, label %14

7:                                                ; preds = %4
  %8 = load i64, ptr @M, align 8, !tbaa !11
  %9 = sdiv i64 %8, %1
  %10 = sext i32 %2 to i64
  %11 = mul nsw i64 %9, %10
  %12 = load i64, ptr @ans, align 8, !tbaa !12
  %13 = add nsw i64 %12, %11
  store i64 %13, ptr @ans, align 8, !tbaa !12
  br label %.loopexit

14:                                               ; preds = %4
  %15 = add nsw i32 %0, 1
  %16 = sext i32 %0 to i64
  %17 = getelementptr inbounds [65 x i32], ptr @ex, i64 0, i64 %16
  %18 = load i32, ptr %17, align 4, !tbaa !13
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [65 x [65 x i32]], ptr @tr, i64 0, i64 %19
  %21 = load i32, ptr %20, align 4, !tbaa !13
  %22 = mul nsw i32 %21, %2
  tail call void @_Z3pfsixi(i32 noundef %15, i64 noundef %1, i32 noundef %22)
  %23 = getelementptr inbounds [23 x i32], ptr getelementptr inbounds (i8, ptr @N, i64 104), i64 0, i64 %16
  %24 = load i32, ptr %23, align 4, !tbaa !13
  %.not2021 = icmp slt i32 %24, 1
  br i1 %.not2021, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %14
  %25 = getelementptr inbounds [23 x i32], ptr getelementptr inbounds (i8, ptr @N, i64 12), i64 0, i64 %16
  br label %26

26:                                               ; preds = %.lr.ph, %26
  %indvars.iv = phi i64 [ 1, %.lr.ph ], [ %indvars.iv.next, %26 ]
  %.01722 = phi i64 [ %1, %.lr.ph ], [ %29, %26 ]
  %27 = load i32, ptr %25, align 4, !tbaa !13
  %28 = sext i32 %27 to i64
  %29 = mul nsw i64 %.01722, %28
  %30 = load i32, ptr %17, align 4, !tbaa !13
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [65 x [65 x i32]], ptr @tr, i64 0, i64 %31, i64 %indvars.iv
  %33 = load i32, ptr %32, align 4, !tbaa !13
  %34 = mul nsw i32 %33, %2
  tail call void @_Z3pfsixi(i32 noundef %15, i64 noundef %29, i32 noundef %34)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %35 = load i32, ptr %23, align 4, !tbaa !13
  %36 = sext i32 %35 to i64
  %.not20.not = icmp slt i64 %indvars.iv, %36
  br i1 %.not20.not, label %26, label %.loopexit, !llvm.loop !14

.loopexit:                                        ; preds = %26, %14, %3, %7
  ret void
}

attributes #0 = { mustprogress nofree nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: 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 = !{!6, !10, i64 8}
!6 = !{!"_ZTS6Factor", !7, i64 0, !10, i64 8, !8, i64 12, !8, i64 104}
!7 = !{!"long long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!6, !7, i64 0}
!12 = !{!7, !7, i64 0}
!13 = !{!10, !10, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


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