; LLVM IR from VastoLorde95/Competitive-Programming :: Codeforces/6 Beta Division 2/Problem D/D.ll (Z2goiiiii)

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

@n = external dso_local global i32, align 4
@h = external dso_local global [20 x i32], align 16
@p = external dso_local global [1000 x i32], align 16
@res = external dso_local global [1000 x i32], align 16
@ans = external dso_local global i32, align 4

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_Z6attackRiS_S_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) #0

; Function Attrs: mustprogress uwtable
define dso_local void @_Z2goiiiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4) #1 {
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store i32 %0, ptr %6, align 4, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !5
  store i32 %2, ptr %8, align 4, !tbaa !5
  store i32 %3, ptr %9, align 4, !tbaa !5
  store i32 %4, ptr %10, align 4, !tbaa !5
  %12 = load i32, ptr %10, align 4, !tbaa !5
  %13 = load i32, ptr @ans, align 4, !tbaa !5
  %14 = icmp sge i32 %12, %13
  br i1 %14, label %15, label %16

15:                                               ; preds = %5
  br label %89

16:                                               ; preds = %5
  %17 = load i32, ptr %6, align 4, !tbaa !5
  %18 = load i32, ptr @n, align 4, !tbaa !5
  %19 = icmp eq i32 %17, %18
  br i1 %19, label %20, label %47

20:                                               ; preds = %16
  %21 = load i32, ptr %7, align 4, !tbaa !5
  %22 = load i32, ptr %8, align 4, !tbaa !5
  %23 = add nsw i32 %21, %22
  %24 = load i32, ptr %9, align 4, !tbaa !5
  %25 = add nsw i32 %23, %24
  %26 = icmp eq i32 %25, 0
  br i1 %26, label %27, label %46

27:                                               ; preds = %20
  %28 = load i32, ptr %10, align 4, !tbaa !5
  store i32 %28, ptr @ans, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  store i32 0, ptr %11, align 4, !tbaa !5
  br label %29

29:                                               ; preds = %42, %27
  %30 = load i32, ptr %11, align 4, !tbaa !5
  %31 = load i32, ptr @ans, align 4, !tbaa !5
  %32 = icmp slt i32 %30, %31
  br i1 %32, label %34, label %33

33:                                               ; preds = %29
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  br label %45

34:                                               ; preds = %29
  %35 = load i32, ptr %11, align 4, !tbaa !5
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [1000 x i32], ptr @p, i64 0, i64 %36
  %38 = load i32, ptr %37, align 4, !tbaa !5
  %39 = load i32, ptr %11, align 4, !tbaa !5
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds [1000 x i32], ptr @res, i64 0, i64 %40
  store i32 %38, ptr %41, align 4, !tbaa !5
  br label %42

42:                                               ; preds = %34
  %43 = load i32, ptr %11, align 4, !tbaa !5
  %44 = add nsw i32 %43, 1
  store i32 %44, ptr %11, align 4, !tbaa !5
  br label %29, !llvm.loop !9

45:                                               ; preds = %33
  br label %46

46:                                               ; preds = %45, %20
  br label %89

47:                                               ; preds = %16
  br label %48

48:                                               ; preds = %51, %47
  %49 = load i32, ptr %7, align 4, !tbaa !5
  %50 = icmp sgt i32 %49, 0
  br i1 %50, label %51, label %57

51:                                               ; preds = %48
  call void @_Z6attackRiS_S_(ptr noundef nonnull align 4 dereferenceable(4) %7, ptr noundef nonnull align 4 dereferenceable(4) %8, ptr noundef nonnull align 4 dereferenceable(4) %9)
  %52 = load i32, ptr %6, align 4, !tbaa !5
  %53 = load i32, ptr %10, align 4, !tbaa !5
  %54 = add nsw i32 %53, 1
  store i32 %54, ptr %10, align 4, !tbaa !5
  %55 = sext i32 %53 to i64
  %56 = getelementptr inbounds [1000 x i32], ptr @p, i64 0, i64 %55
  store i32 %52, ptr %56, align 4, !tbaa !5
  br label %48, !llvm.loop !11

57:                                               ; preds = %48
  %58 = load i32, ptr %6, align 4, !tbaa !5
  %59 = add nsw i32 %58, 1
  %60 = load i32, ptr %8, align 4, !tbaa !5
  %61 = load i32, ptr %9, align 4, !tbaa !5
  %62 = load i32, ptr %6, align 4, !tbaa !5
  %63 = add nsw i32 %62, 2
  %64 = sext i32 %63 to i64
  %65 = getelementptr inbounds [20 x i32], ptr @h, i64 0, i64 %64
  %66 = load i32, ptr %65, align 4, !tbaa !5
  %67 = load i32, ptr %10, align 4, !tbaa !5
  call void @_Z2goiiiii(i32 noundef %59, i32 noundef %60, i32 noundef %61, i32 noundef %66, i32 noundef %67)
  br label %68

68:                                               ; preds = %73, %57
  %69 = load i32, ptr %8, align 4, !tbaa !5
  %70 = load i32, ptr %9, align 4, !tbaa !5
  %71 = add nsw i32 %69, %70
  %72 = icmp sgt i32 %71, 0
  br i1 %72, label %73, label %89

73:                                               ; preds = %68
  call void @_Z6attackRiS_S_(ptr noundef nonnull align 4 dereferenceable(4) %7, ptr noundef nonnull align 4 dereferenceable(4) %8, ptr noundef nonnull align 4 dereferenceable(4) %9)
  %74 = load i32, ptr %6, align 4, !tbaa !5
  %75 = load i32, ptr %10, align 4, !tbaa !5
  %76 = add nsw i32 %75, 1
  store i32 %76, ptr %10, align 4, !tbaa !5
  %77 = sext i32 %75 to i64
  %78 = getelementptr inbounds [1000 x i32], ptr @p, i64 0, i64 %77
  store i32 %74, ptr %78, align 4, !tbaa !5
  %79 = load i32, ptr %6, align 4, !tbaa !5
  %80 = add nsw i32 %79, 1
  %81 = load i32, ptr %8, align 4, !tbaa !5
  %82 = load i32, ptr %9, align 4, !tbaa !5
  %83 = load i32, ptr %6, align 4, !tbaa !5
  %84 = add nsw i32 %83, 2
  %85 = sext i32 %84 to i64
  %86 = getelementptr inbounds [20 x i32], ptr @h, i64 0, i64 %85
  %87 = load i32, ptr %86, align 4, !tbaa !5
  %88 = load i32, ptr %10, align 4, !tbaa !5
  call void @_Z2goiiiii(i32 noundef %80, i32 noundef %81, i32 noundef %82, i32 noundef %87, i32 noundef %88)
  br label %68, !llvm.loop !12

89:                                               ; preds = %68, %46, %15
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #2

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 = { 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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !10}


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

@n = external dso_local local_unnamed_addr global i32, align 4
@h = external dso_local local_unnamed_addr global [20 x i32], align 16
@p = external dso_local local_unnamed_addr global [1000 x i32], align 16
@res = external dso_local local_unnamed_addr global [1000 x i32], align 16
@ans = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_Z6attackRiS_S_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z2goiiiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4) local_unnamed_addr #0 {
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store i32 %1, ptr %6, align 4, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !5
  store i32 %3, ptr %8, align 4, !tbaa !5
  %9 = load i32, ptr @ans, align 4, !tbaa !5
  %.not = icmp sgt i32 %9, %4
  br i1 %.not, label %10, label %.loopexit

10:                                               ; preds = %5
  %11 = load i32, ptr @n, align 4, !tbaa !5
  %12 = icmp eq i32 %11, %0
  br i1 %12, label %15, label %.preheader

.preheader:                                       ; preds = %10
  %13 = icmp sgt i32 %1, 0
  br i1 %13, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %.preheader
  %14 = sext i32 %4 to i64
  br label %.lr.ph

15:                                               ; preds = %10
  %16 = add nsw i32 %2, %1
  %17 = sub i32 0, %3
  %18 = icmp eq i32 %16, %17
  br i1 %18, label %19, label %.loopexit

19:                                               ; preds = %15
  store i32 %4, ptr @ans, align 4, !tbaa !5
  %20 = icmp sgt i32 %4, 0
  br i1 %20, label %.lr.ph26.preheader, label %.loopexit

.lr.ph26.preheader:                               ; preds = %19
  %21 = zext nneg i32 %4 to i64
  %22 = shl nuw nsw i64 %21, 2
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 16 @res, ptr nonnull align 16 @p, i64 %22, i1 false), !tbaa !5
  br label %.loopexit

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %indvars.iv = phi i64 [ %14, %.lr.ph.preheader ], [ %indvars.iv.next, %.lr.ph ]
  call void @_Z6attackRiS_S_(ptr noundef nonnull align 4 dereferenceable(4) %6, ptr noundef nonnull align 4 dereferenceable(4) %7, ptr noundef nonnull align 4 dereferenceable(4) %8)
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %23 = getelementptr inbounds [1000 x i32], ptr @p, i64 0, i64 %indvars.iv
  store i32 %0, ptr %23, align 4, !tbaa !5
  %24 = load i32, ptr %6, align 4, !tbaa !5
  %25 = icmp sgt i32 %24, 0
  br i1 %25, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !9

._crit_edge.loopexit:                             ; preds = %.lr.ph
  %26 = trunc nsw i64 %indvars.iv.next to i32
  %.pre = load i32, ptr %7, align 4, !tbaa !5
  %.pre35 = load i32, ptr %8, align 4, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %.preheader
  %27 = phi i32 [ %3, %.preheader ], [ %.pre35, %._crit_edge.loopexit ]
  %28 = phi i32 [ %2, %.preheader ], [ %.pre, %._crit_edge.loopexit ]
  %.017.lcssa = phi i32 [ %4, %.preheader ], [ %26, %._crit_edge.loopexit ]
  %29 = add nsw i32 %0, 1
  %30 = add nsw i32 %0, 2
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [20 x i32], ptr @h, i64 0, i64 %31
  %33 = load i32, ptr %32, align 4, !tbaa !5
  call void @_Z2goiiiii(i32 noundef %29, i32 noundef %28, i32 noundef %27, i32 noundef %33, i32 noundef %.017.lcssa)
  %34 = load i32, ptr %7, align 4, !tbaa !5
  %35 = load i32, ptr %8, align 4, !tbaa !5
  %36 = add nsw i32 %35, %34
  %37 = icmp sgt i32 %36, 0
  br i1 %37, label %.lr.ph23.preheader, label %.loopexit

.lr.ph23.preheader:                               ; preds = %._crit_edge
  %38 = sext i32 %.017.lcssa to i64
  br label %.lr.ph23

.lr.ph23:                                         ; preds = %.lr.ph23.preheader, %.lr.ph23
  %indvars.iv29 = phi i64 [ %38, %.lr.ph23.preheader ], [ %indvars.iv.next30, %.lr.ph23 ]
  call void @_Z6attackRiS_S_(ptr noundef nonnull align 4 dereferenceable(4) %6, ptr noundef nonnull align 4 dereferenceable(4) %7, ptr noundef nonnull align 4 dereferenceable(4) %8)
  %indvars.iv.next30 = add nsw i64 %indvars.iv29, 1
  %39 = getelementptr inbounds [1000 x i32], ptr @p, i64 0, i64 %indvars.iv29
  store i32 %0, ptr %39, align 4, !tbaa !5
  %40 = load i32, ptr %7, align 4, !tbaa !5
  %41 = load i32, ptr %8, align 4, !tbaa !5
  %42 = load i32, ptr %32, align 4, !tbaa !5
  %43 = trunc nsw i64 %indvars.iv.next30 to i32
  call void @_Z2goiiiii(i32 noundef %29, i32 noundef %40, i32 noundef %41, i32 noundef %42, i32 noundef %43)
  %44 = load i32, ptr %7, align 4, !tbaa !5
  %45 = load i32, ptr %8, align 4, !tbaa !5
  %46 = add nsw i32 %45, %44
  %47 = icmp sgt i32 %46, 0
  br i1 %47, label %.lr.ph23, label %.loopexit, !llvm.loop !11

.loopexit:                                        ; preds = %.lr.ph23, %.lr.ph26.preheader, %._crit_edge, %19, %15, %5
  ret void
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #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 nounwind willreturn memory(argmem: readwrite) }

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