; LLVM IR from tangjz/acm-icpc :: 51nod/1387.ll (Z5solveiiPii)

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

@mod = external dso_local global i32, align 4
@g = external dso_local global [131072 x ptr], align 16
@ans = external dso_local global i32, align 4
@_ZL4tail = external hidden global ptr, align 8
@_ZZ5solveiiPiiE3tmp = external hidden global [65536 x i32], align 16

; 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 uwtable
declare dso_local void @_Z7polyDiviPiiS_S_S_(i32 noundef, ptr noundef, i32 noundef, ptr noundef, ptr noundef, ptr noundef) #1

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef ptr @_Z10tail_alloci(i32 noundef) #2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local void @_Z9tail_freei(i32 noundef) #2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z7seg_idxii(i32 noundef, i32 noundef) #2

; Function Attrs: mustprogress uwtable
define dso_local void @_Z5solveiiPii(i32 noundef %0, i32 noundef %1, ptr noundef %2, i32 noundef %3) #1 {
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  store i32 %0, ptr %5, align 4, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !9
  store i32 %3, ptr %8, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  %15 = load i32, ptr %6, align 4, !tbaa !5
  %16 = load i32, ptr %5, align 4, !tbaa !5
  %17 = sub nsw i32 %15, %16
  %18 = add nsw i32 %17, 1
  store i32 %18, ptr %9, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %19 = load i32, ptr %9, align 4, !tbaa !5
  %20 = call noundef ptr @_Z10tail_alloci(i32 noundef %19)
  store ptr %20, ptr %10, align 8, !tbaa !9
  %21 = load i32, ptr %8, align 4, !tbaa !5
  %22 = load ptr, ptr %7, align 8, !tbaa !9
  %23 = load i32, ptr %6, align 4, !tbaa !5
  %24 = load i32, ptr %5, align 4, !tbaa !5
  %25 = sub nsw i32 %23, %24
  %26 = add nsw i32 %25, 2
  %27 = load i32, ptr %5, align 4, !tbaa !5
  %28 = load i32, ptr %6, align 4, !tbaa !5
  %29 = call noundef i32 @_Z7seg_idxii(i32 noundef %27, i32 noundef %28)
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [131072 x ptr], ptr @g, i64 0, i64 %30
  %32 = load ptr, ptr %31, align 8, !tbaa !9
  %33 = load ptr, ptr %10, align 8, !tbaa !9
  call void @_Z7polyDiviPiiS_S_S_(i32 noundef %21, ptr noundef %22, i32 noundef %26, ptr noundef %32, ptr noundef @_ZZ5solveiiPiiE3tmp, ptr noundef %33)
  %34 = load i32, ptr %5, align 4, !tbaa !5
  %35 = load i32, ptr %6, align 4, !tbaa !5
  %36 = icmp eq i32 %34, %35
  br i1 %36, label %37, label %49

37:                                               ; preds = %4
  %38 = load i32, ptr @ans, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = load ptr, ptr %10, align 8, !tbaa !9
  %41 = getelementptr inbounds i32, ptr %40, i64 0
  %42 = load i32, ptr %41, align 4, !tbaa !5
  %43 = sext i32 %42 to i64
  %44 = mul nsw i64 %39, %43
  %45 = load i32, ptr @mod, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = srem i64 %44, %46
  %48 = trunc i64 %47 to i32
  store i32 %48, ptr @ans, align 4, !tbaa !5
  store i32 1, ptr %11, align 4
  br label %96

49:                                               ; preds = %4
  br label %50

50:                                               ; preds = %65, %49
  %51 = load i32, ptr %9, align 4, !tbaa !5
  %52 = icmp sgt i32 %51, 1
  br i1 %52, label %53, label %62

53:                                               ; preds = %50
  %54 = load ptr, ptr %10, align 8, !tbaa !9
  %55 = load i32, ptr %9, align 4, !tbaa !5
  %56 = sub nsw i32 %55, 1
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds i32, ptr %54, i64 %57
  %59 = load i32, ptr %58, align 4, !tbaa !5
  %60 = icmp ne i32 %59, 0
  %61 = xor i1 %60, true
  br label %62

62:                                               ; preds = %53, %50
  %63 = phi i1 [ false, %50 ], [ %61, %53 ]
  br i1 %63, label %64, label %68

64:                                               ; preds = %62
  br label %65

65:                                               ; preds = %64
  %66 = load i32, ptr %9, align 4, !tbaa !5
  %67 = add nsw i32 %66, -1
  store i32 %67, ptr %9, align 4, !tbaa !5
  br label %50, !llvm.loop !11

68:                                               ; preds = %62
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  %69 = load i32, ptr %5, align 4, !tbaa !5
  %70 = load i32, ptr %6, align 4, !tbaa !5
  %71 = add nsw i32 %69, %70
  %72 = ashr i32 %71, 1
  store i32 %72, ptr %12, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #3
  %73 = load i32, ptr %5, align 4, !tbaa !5
  %74 = load i32, ptr %12, align 4, !tbaa !5
  %75 = call noundef i32 @_Z7seg_idxii(i32 noundef %73, i32 noundef %74)
  store i32 %75, ptr %13, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #3
  %76 = load i32, ptr %12, align 4, !tbaa !5
  %77 = add nsw i32 %76, 1
  %78 = load i32, ptr %6, align 4, !tbaa !5
  %79 = call noundef i32 @_Z7seg_idxii(i32 noundef %77, i32 noundef %78)
  store i32 %79, ptr %14, align 4, !tbaa !5
  %80 = load i32, ptr %5, align 4, !tbaa !5
  %81 = load i32, ptr %12, align 4, !tbaa !5
  %82 = load ptr, ptr %10, align 8, !tbaa !9
  %83 = load i32, ptr %9, align 4, !tbaa !5
  call void @_Z5solveiiPii(i32 noundef %80, i32 noundef %81, ptr noundef %82, i32 noundef %83)
  %84 = load i32, ptr %12, align 4, !tbaa !5
  %85 = add nsw i32 %84, 1
  %86 = load i32, ptr %6, align 4, !tbaa !5
  %87 = load ptr, ptr %10, align 8, !tbaa !9
  %88 = load i32, ptr %9, align 4, !tbaa !5
  call void @_Z5solveiiPii(i32 noundef %85, i32 noundef %86, ptr noundef %87, i32 noundef %88)
  %89 = load ptr, ptr @_ZL4tail, align 8, !tbaa !9
  %90 = load ptr, ptr %10, align 8, !tbaa !9
  %91 = ptrtoint ptr %89 to i64
  %92 = ptrtoint ptr %90 to i64
  %93 = sub i64 %91, %92
  %94 = sdiv exact i64 %93, 4
  %95 = trunc i64 %94 to i32
  call void @_Z9tail_freei(i32 noundef %95)
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  store i32 0, ptr %11, align 4
  br label %96

96:                                               ; preds = %68, %37
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  %97 = load i32, ptr %11, align 4
  switch i32 %97, label %99 [
    i32 0, label %98
    i32 1, label %98
  ]

98:                                               ; preds = %96, %96
  ret void

99:                                               ; preds = %96
  unreachable
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { inlinehint 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 #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"}


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

@mod = external dso_local local_unnamed_addr global i32, align 4
@g = external dso_local local_unnamed_addr global [131072 x ptr], align 16
@ans = external dso_local local_unnamed_addr global i32, align 4
@_ZL4tail = external hidden local_unnamed_addr global ptr, align 8
@_ZZ5solveiiPiiE3tmp = external hidden global [65536 x i32], align 16

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z7polyDiviPiiS_S_S_(i32 noundef, ptr noundef, i32 noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef ptr @_Z10tail_alloci(i32 noundef) local_unnamed_addr #1

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local void @_Z9tail_freei(i32 noundef) local_unnamed_addr #1

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z7seg_idxii(i32 noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress uwtable
define dso_local void @_Z5solveiiPii(i32 noundef %0, i32 noundef %1, ptr noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = sub nsw i32 %1, %0
  %6 = add nsw i32 %5, 1
  %7 = tail call noundef ptr @_Z10tail_alloci(i32 noundef %6)
  %8 = add nsw i32 %5, 2
  %9 = tail call noundef i32 @_Z7seg_idxii(i32 noundef %0, i32 noundef %1)
  %10 = sext i32 %9 to i64
  %11 = getelementptr inbounds [131072 x ptr], ptr @g, i64 0, i64 %10
  %12 = load ptr, ptr %11, align 8, !tbaa !5
  tail call void @_Z7polyDiviPiiS_S_S_(i32 noundef %3, ptr noundef %2, i32 noundef %8, ptr noundef %12, ptr noundef nonnull @_ZZ5solveiiPiiE3tmp, ptr noundef %7)
  %13 = icmp eq i32 %0, %1
  br i1 %13, label %common.ret, label %.preheader

.preheader:                                       ; preds = %4
  %invariant.gep = getelementptr i8, ptr %7, i64 -4
  %14 = icmp sgt i32 %5, 0
  br i1 %14, label %.lr.ph, label %.critedge

common.ret:                                       ; preds = %4
  %15 = load i32, ptr @ans, align 4, !tbaa !9
  %16 = sext i32 %15 to i64
  %17 = load i32, ptr %7, align 4, !tbaa !9
  %18 = sext i32 %17 to i64
  %19 = mul nsw i64 %18, %16
  %20 = load i32, ptr @mod, align 4, !tbaa !9
  %21 = sext i32 %20 to i64
  %22 = srem i64 %19, %21
  %23 = trunc nsw i64 %22 to i32
  store i32 %23, ptr @ans, align 4, !tbaa !9
  br label %common.ret34

.lr.ph:                                           ; preds = %.preheader, %26
  %.031 = phi i32 [ %27, %26 ], [ %6, %.preheader ]
  %24 = zext nneg i32 %.031 to i64
  %gep = getelementptr i32, ptr %invariant.gep, i64 %24
  %25 = load i32, ptr %gep, align 4, !tbaa !9
  %.not = icmp eq i32 %25, 0
  br i1 %.not, label %26, label %.critedge

26:                                               ; preds = %.lr.ph
  %27 = add nsw i32 %.031, -1
  %28 = icmp sgt i32 %.031, 2
  br i1 %28, label %.lr.ph, label %.critedge, !llvm.loop !11

common.ret34:                                     ; preds = %.critedge, %common.ret
  ret void

.critedge:                                        ; preds = %.lr.ph, %26, %.preheader
  %.0.lcssa = phi i32 [ %6, %.preheader ], [ 1, %26 ], [ %.031, %.lr.ph ]
  %29 = add nsw i32 %1, %0
  %30 = ashr i32 %29, 1
  %31 = tail call noundef i32 @_Z7seg_idxii(i32 noundef %0, i32 noundef %30)
  %32 = add nsw i32 %30, 1
  %33 = tail call noundef i32 @_Z7seg_idxii(i32 noundef %32, i32 noundef %1)
  tail call void @_Z5solveiiPii(i32 noundef %0, i32 noundef %30, ptr noundef %7, i32 noundef %.0.lcssa)
  tail call void @_Z5solveiiPii(i32 noundef %32, i32 noundef %1, ptr noundef %7, i32 noundef %.0.lcssa)
  %34 = load ptr, ptr @_ZL4tail, align 8, !tbaa !5
  %35 = ptrtoint ptr %34 to i64
  %36 = ptrtoint ptr %7 to i64
  %37 = sub i64 %35, %36
  %38 = lshr exact i64 %37, 2
  %39 = trunc i64 %38 to i32
  tail call void @_Z9tail_freei(i32 noundef %39)
  br label %common.ret34
}

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 = { inlinehint 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" }

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


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