; LLVM IR from tangjz/acm-icpc :: codeforces/complete/756E.ll (Z15divideAndRemaini)

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

@len = external dso_local global i32, align 4
@num = external dso_local global [10001 x i32], align 16

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

10:                                               ; preds = %1
  store i32 0, ptr %2, align 4
  br label %64

11:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 0, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  store i32 1000000000, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %12 = load i32, ptr @len, align 4, !tbaa !5
  %13 = sub nsw i32 %12, 1
  store i32 %13, ptr %6, align 4, !tbaa !5
  br label %14

14:                                               ; preds = %41, %11
  %15 = load i32, ptr %6, align 4, !tbaa !5
  %16 = icmp sge i32 %15, 0
  br i1 %16, label %18, label %17

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

18:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  %19 = load i32, ptr %4, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = mul nsw i64 %20, 1000000000
  %22 = load i32, ptr %6, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [10001 x i32], ptr @num, i64 0, i64 %23
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = add nsw i64 %21, %26
  store i64 %27, ptr %7, align 8, !tbaa !9
  %28 = load i64, ptr %7, align 8, !tbaa !9
  %29 = load i32, ptr %3, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = sdiv i64 %28, %30
  %32 = trunc i64 %31 to i32
  %33 = load i32, ptr %6, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [10001 x i32], ptr @num, i64 0, i64 %34
  store i32 %32, ptr %35, align 4, !tbaa !5
  %36 = load i64, ptr %7, align 8, !tbaa !9
  %37 = load i32, ptr %3, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = srem i64 %36, %38
  %40 = trunc i64 %39 to i32
  store i32 %40, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  br label %41

41:                                               ; preds = %18
  %42 = load i32, ptr %6, align 4, !tbaa !5
  %43 = add nsw i32 %42, -1
  store i32 %43, ptr %6, align 4, !tbaa !5
  br label %14, !llvm.loop !11

44:                                               ; preds = %17
  br label %45

45:                                               ; preds = %59, %44
  %46 = load i32, ptr @len, align 4, !tbaa !5
  %47 = icmp sgt i32 %46, 1
  br i1 %47, label %48, label %56

48:                                               ; preds = %45
  %49 = load i32, ptr @len, align 4, !tbaa !5
  %50 = sub nsw i32 %49, 1
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds [10001 x i32], ptr @num, i64 0, i64 %51
  %53 = load i32, ptr %52, align 4, !tbaa !5
  %54 = icmp ne i32 %53, 0
  %55 = xor i1 %54, true
  br label %56

56:                                               ; preds = %48, %45
  %57 = phi i1 [ false, %45 ], [ %55, %48 ]
  br i1 %57, label %58, label %62

58:                                               ; preds = %56
  br label %59

59:                                               ; preds = %58
  %60 = load i32, ptr @len, align 4, !tbaa !5
  %61 = add nsw i32 %60, -1
  store i32 %61, ptr @len, align 4, !tbaa !5
  br label %45, !llvm.loop !13

62:                                               ; preds = %56
  %63 = load i32, ptr %4, align 4, !tbaa !5
  store i32 %63, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  br label %64

64:                                               ; preds = %62, %10
  %65 = load i32, ptr %2, align 4
  ret i32 %65
}

; 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 = !{!"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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !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"

@len = external dso_local local_unnamed_addr global i32, align 4
@num = external dso_local local_unnamed_addr global [10001 x i32], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef range(i32 -2147483647, -2147483648) i32 @_Z15divideAndRemaini(i32 noundef %0) local_unnamed_addr #0 {
  %2 = icmp eq i32 %0, 1
  br i1 %2, label %.critedge, label %3

3:                                                ; preds = %1
  %4 = load i32, ptr @len, align 4, !tbaa !5
  %5 = icmp sgt i32 %4, 0
  br i1 %5, label %.lr.ph, label %.critedge

.lr.ph:                                           ; preds = %3
  %6 = sext i32 %0 to i64
  %7 = zext nneg i32 %4 to i64
  br label %8

8:                                                ; preds = %.lr.ph, %8
  %indvars.iv = phi i64 [ %7, %.lr.ph ], [ %indvars.iv.next, %8 ]
  %.01316 = phi i64 [ 0, %.lr.ph ], [ %16, %8 ]
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %9 = mul nsw i64 %.01316, 1000000000
  %10 = getelementptr inbounds [10001 x i32], ptr @num, i64 0, i64 %indvars.iv.next
  %11 = load i32, ptr %10, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = add nsw i64 %9, %12
  %14 = sdiv i64 %13, %6
  %15 = trunc i64 %14 to i32
  store i32 %15, ptr %10, align 4, !tbaa !5
  %16 = srem i64 %13, %6
  %17 = icmp ugt i64 %indvars.iv, 1
  br i1 %17, label %8, label %thread-pre-split, !llvm.loop !9

thread-pre-split:                                 ; preds = %8
  %18 = trunc nsw i64 %16 to i32
  %.not25 = icmp eq i32 %4, 1
  br i1 %.not25, label %.critedge, label %.lr.ph18.preheader

.lr.ph18.preheader:                               ; preds = %thread-pre-split
  %19 = zext nneg i32 %4 to i64
  br label %.lr.ph18

.lr.ph18:                                         ; preds = %.lr.ph18.preheader, %22
  %indvars.iv21 = phi i64 [ %19, %.lr.ph18.preheader ], [ %indvars.iv.next22, %22 ]
  %indvars.iv.next22 = add nsw i64 %indvars.iv21, -1
  %20 = getelementptr inbounds [10001 x i32], ptr @num, i64 0, i64 %indvars.iv.next22
  %21 = load i32, ptr %20, align 4, !tbaa !5
  %.not = icmp eq i32 %21, 0
  br i1 %.not, label %22, label %.critedge

22:                                               ; preds = %.lr.ph18
  %23 = trunc nuw nsw i64 %indvars.iv.next22 to i32
  store i32 %23, ptr @len, align 4, !tbaa !5
  %24 = icmp ugt i64 %indvars.iv21, 2
  br i1 %24, label %.lr.ph18, label %.critedge, !llvm.loop !11

.critedge:                                        ; preds = %22, %.lr.ph18, %3, %thread-pre-split, %1
  %.0 = phi i32 [ 0, %1 ], [ %18, %thread-pre-split ], [ 0, %3 ], [ %18, %.lr.ph18 ], [ %18, %22 ]
  ret i32 %.0
}

attributes #0 = { mustprogress nofree norecurse 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, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


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