; LLVM IR from tangjz/acm-icpc :: TDL_training/2015-10-05/C - Cactus Reloaded - dby.ll (Z4calcii)

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

@dis = external dso_local global [50005 x i32], align 16
@q = external dso_local global [50005 x i32], align 16
@loop = external dso_local global [50005 x i32], align 16
@ans = external dso_local global i32, align 4

; Function Attrs: mustprogress uwtable
define dso_local void @_Z4calcii(i32 noundef %0, i32 noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  store i32 0, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 0, ptr %6, align 4, !tbaa !5
  %8 = load i32, ptr %6, align 4, !tbaa !5
  %9 = add nsw i32 %8, 1
  store i32 %9, ptr %6, align 4, !tbaa !5
  %10 = sext i32 %8 to i64
  %11 = getelementptr inbounds [50005 x i32], ptr @q, i64 0, i64 %10
  store i32 0, ptr %11, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 1, ptr %7, align 4, !tbaa !5
  br label %12

12:                                               ; preds = %107, %2
  %13 = load i32, ptr %7, align 4, !tbaa !5
  %14 = load i32, ptr %3, align 4, !tbaa !5
  %15 = icmp slt i32 %13, %14
  br i1 %15, label %17, label %16

16:                                               ; preds = %12
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  br label %110

17:                                               ; preds = %12
  br label %18

18:                                               ; preds = %33, %17
  %19 = load i32, ptr %5, align 4, !tbaa !5
  %20 = load i32, ptr %6, align 4, !tbaa !5
  %21 = icmp slt i32 %19, %20
  br i1 %21, label %22, label %31

22:                                               ; preds = %18
  %23 = load i32, ptr %7, align 4, !tbaa !5
  %24 = load i32, ptr %5, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [50005 x i32], ptr @q, i64 0, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !5
  %28 = sub nsw i32 %23, %27
  %29 = load i32, ptr %4, align 4, !tbaa !5
  %30 = icmp sgt i32 %28, %29
  br label %31

31:                                               ; preds = %22, %18
  %32 = phi i1 [ false, %18 ], [ %30, %22 ]
  br i1 %32, label %33, label %36

33:                                               ; preds = %31
  %34 = load i32, ptr %5, align 4, !tbaa !5
  %35 = add nsw i32 %34, 1
  store i32 %35, ptr %5, align 4, !tbaa !5
  br label %18, !llvm.loop !9

36:                                               ; preds = %31
  %37 = load i32, ptr @ans, align 4, !tbaa !5
  %38 = load i32, ptr %5, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [50005 x i32], ptr @q, i64 0, i64 %39
  %41 = load i32, ptr %40, align 4, !tbaa !5
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds [50005 x i32], ptr @loop, i64 0, i64 %42
  %44 = load i32, ptr %43, align 4, !tbaa !5
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [50005 x i32], ptr @dis, i64 0, i64 %45
  %47 = load i32, ptr %46, align 4, !tbaa !5
  %48 = load i32, ptr %7, align 4, !tbaa !5
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds [50005 x i32], ptr @loop, i64 0, i64 %49
  %51 = load i32, ptr %50, align 4, !tbaa !5
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds [50005 x i32], ptr @dis, i64 0, i64 %52
  %54 = load i32, ptr %53, align 4, !tbaa !5
  %55 = add nsw i32 %47, %54
  %56 = load i32, ptr %7, align 4, !tbaa !5
  %57 = add nsw i32 %55, %56
  %58 = load i32, ptr %5, align 4, !tbaa !5
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds [50005 x i32], ptr @q, i64 0, i64 %59
  %61 = load i32, ptr %60, align 4, !tbaa !5
  %62 = sub nsw i32 %57, %61
  %63 = call noundef i32 @_Z3maxii(i32 noundef %37, i32 noundef %62)
  store i32 %63, ptr @ans, align 4, !tbaa !5
  br label %64

64:                                               ; preds = %98, %36
  %65 = load i32, ptr %6, align 4, !tbaa !5
  %66 = load i32, ptr %5, align 4, !tbaa !5
  %67 = icmp sgt i32 %65, %66
  br i1 %67, label %68, label %96

68:                                               ; preds = %64
  %69 = load i32, ptr %6, align 4, !tbaa !5
  %70 = sub nsw i32 %69, 1
  %71 = sext i32 %70 to i64
  %72 = getelementptr inbounds [50005 x i32], ptr @q, i64 0, i64 %71
  %73 = load i32, ptr %72, align 4, !tbaa !5
  %74 = sext i32 %73 to i64
  %75 = getelementptr inbounds [50005 x i32], ptr @loop, i64 0, i64 %74
  %76 = load i32, ptr %75, align 4, !tbaa !5
  %77 = sext i32 %76 to i64
  %78 = getelementptr inbounds [50005 x i32], ptr @dis, i64 0, i64 %77
  %79 = load i32, ptr %78, align 4, !tbaa !5
  %80 = load i32, ptr %6, align 4, !tbaa !5
  %81 = sub nsw i32 %80, 1
  %82 = sext i32 %81 to i64
  %83 = getelementptr inbounds [50005 x i32], ptr @q, i64 0, i64 %82
  %84 = load i32, ptr %83, align 4, !tbaa !5
  %85 = sub nsw i32 %79, %84
  %86 = load i32, ptr %7, align 4, !tbaa !5
  %87 = sext i32 %86 to i64
  %88 = getelementptr inbounds [50005 x i32], ptr @loop, i64 0, i64 %87
  %89 = load i32, ptr %88, align 4, !tbaa !5
  %90 = sext i32 %89 to i64
  %91 = getelementptr inbounds [50005 x i32], ptr @dis, i64 0, i64 %90
  %92 = load i32, ptr %91, align 4, !tbaa !5
  %93 = load i32, ptr %7, align 4, !tbaa !5
  %94 = sub nsw i32 %92, %93
  %95 = icmp slt i32 %85, %94
  br label %96

96:                                               ; preds = %68, %64
  %97 = phi i1 [ false, %64 ], [ %95, %68 ]
  br i1 %97, label %98, label %101

98:                                               ; preds = %96
  %99 = load i32, ptr %6, align 4, !tbaa !5
  %100 = add nsw i32 %99, -1
  store i32 %100, ptr %6, align 4, !tbaa !5
  br label %64, !llvm.loop !11

101:                                              ; preds = %96
  %102 = load i32, ptr %7, align 4, !tbaa !5
  %103 = load i32, ptr %6, align 4, !tbaa !5
  %104 = add nsw i32 %103, 1
  store i32 %104, ptr %6, align 4, !tbaa !5
  %105 = sext i32 %103 to i64
  %106 = getelementptr inbounds [50005 x i32], ptr @q, i64 0, i64 %105
  store i32 %102, ptr %106, align 4, !tbaa !5
  br label %107

107:                                              ; preds = %101
  %108 = load i32, ptr %7, align 4, !tbaa !5
  %109 = add nsw i32 %108, 1
  store i32 %109, ptr %7, align 4, !tbaa !5
  br label %12, !llvm.loop !12

110:                                              ; preds = %16
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  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: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z3maxii(i32 noundef, i32 noundef) #2

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

@dis = external dso_local local_unnamed_addr global [50005 x i32], align 16
@q = external dso_local local_unnamed_addr global [50005 x i32], align 16
@loop = external dso_local local_unnamed_addr global [50005 x i32], align 16
@ans = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z4calcii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  store i32 0, ptr @q, align 16, !tbaa !5
  %3 = icmp sgt i32 %0, 1
  br i1 %3, label %.preheader.preheader, label %._crit_edge

.preheader.preheader:                             ; preds = %2
  %wide.trip.count = zext nneg i32 %0 to i64
  br label %.preheader

.preheader:                                       ; preds = %.preheader.preheader, %.critedge2
  %indvars.iv38 = phi i64 [ 1, %.preheader.preheader ], [ %indvars.iv.next39, %.critedge2 ]
  %.02532 = phi i32 [ 1, %.preheader.preheader ], [ %52, %.critedge2 ]
  %.02631 = phi i32 [ 0, %.preheader.preheader ], [ %.127.lcssa, %.critedge2 ]
  %4 = icmp slt i32 %.02631, %.02532
  %5 = sext i32 %.02532 to i64
  br i1 %4, label %.lr.ph.preheader, label %.preheader..critedge_crit_edge

.preheader..critedge_crit_edge:                   ; preds = %.preheader
  %.pre = trunc nuw nsw i64 %indvars.iv38 to i32
  br label %.critedge

.lr.ph.preheader:                                 ; preds = %.preheader
  %6 = sext i32 %.02631 to i64
  %7 = trunc nuw nsw i64 %indvars.iv38 to i32
  br label %.lr.ph

._crit_edge:                                      ; preds = %.critedge2, %2
  ret void

.lr.ph:                                           ; preds = %.lr.ph.preheader, %12
  %indvars.iv = phi i64 [ %6, %.lr.ph.preheader ], [ %indvars.iv.next, %12 ]
  %8 = getelementptr inbounds [50005 x i32], ptr @q, i64 0, i64 %indvars.iv
  %9 = load i32, ptr %8, align 4, !tbaa !5
  %10 = sub nsw i32 %7, %9
  %11 = icmp sgt i32 %10, %1
  br i1 %11, label %12, label %.critedge.loopexit.split.loop.exit44

12:                                               ; preds = %.lr.ph
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %5
  br i1 %exitcond.not, label %.critedge, label %.lr.ph, !llvm.loop !9

.critedge.loopexit.split.loop.exit44:             ; preds = %.lr.ph
  %13 = trunc nsw i64 %indvars.iv to i32
  br label %.critedge

.critedge:                                        ; preds = %12, %.critedge.loopexit.split.loop.exit44, %.preheader..critedge_crit_edge
  %.pre-phi = phi i32 [ %.pre, %.preheader..critedge_crit_edge ], [ %7, %.critedge.loopexit.split.loop.exit44 ], [ %7, %12 ]
  %.127.lcssa = phi i32 [ %.02631, %.preheader..critedge_crit_edge ], [ %13, %.critedge.loopexit.split.loop.exit44 ], [ %.02532, %12 ]
  %14 = load i32, ptr @ans, align 4, !tbaa !5
  %15 = sext i32 %.127.lcssa to i64
  %16 = getelementptr inbounds [50005 x i32], ptr @q, i64 0, i64 %15
  %17 = load i32, ptr %16, align 4, !tbaa !5
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [50005 x i32], ptr @loop, i64 0, i64 %18
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [50005 x i32], ptr @dis, i64 0, i64 %21
  %23 = load i32, ptr %22, align 4, !tbaa !5
  %24 = getelementptr inbounds [50005 x i32], ptr @loop, i64 0, i64 %indvars.iv38
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [50005 x i32], ptr @dis, i64 0, i64 %26
  %28 = load i32, ptr %27, align 4, !tbaa !5
  %29 = sub i32 %.pre-phi, %17
  %30 = add i32 %29, %23
  %31 = add i32 %30, %28
  %32 = tail call noundef i32 @_Z3maxii(i32 noundef %14, i32 noundef %31)
  store i32 %32, ptr @ans, align 4, !tbaa !5
  %smin = tail call i32 @llvm.smin.i32(i32 %.127.lcssa, i32 %.02532)
  br label %33

33:                                               ; preds = %35, %.critedge
  %indvars.iv35 = phi i64 [ %indvars.iv.next36, %35 ], [ %5, %.critedge ]
  %34 = icmp sgt i64 %indvars.iv35, %15
  br i1 %34, label %35, label %.critedge2

35:                                               ; preds = %33
  %indvars.iv.next36 = add nsw i64 %indvars.iv35, -1
  %36 = getelementptr inbounds [50005 x i32], ptr @q, i64 0, i64 %indvars.iv.next36
  %37 = load i32, ptr %36, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [50005 x i32], ptr @loop, i64 0, i64 %38
  %40 = load i32, ptr %39, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [50005 x i32], ptr @dis, i64 0, i64 %41
  %43 = load i32, ptr %42, align 4, !tbaa !5
  %44 = sub nsw i32 %43, %37
  %45 = load i32, ptr %24, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [50005 x i32], ptr @dis, i64 0, i64 %46
  %48 = load i32, ptr %47, align 4, !tbaa !5
  %49 = sub nsw i32 %48, %.pre-phi
  %50 = icmp slt i32 %44, %49
  br i1 %50, label %33, label %.critedge2.split.loop.exit47, !llvm.loop !11

.critedge2.split.loop.exit47:                     ; preds = %35
  %51 = trunc nsw i64 %indvars.iv35 to i32
  br label %.critedge2

.critedge2:                                       ; preds = %33, %.critedge2.split.loop.exit47
  %.1.lcssa = phi i32 [ %51, %.critedge2.split.loop.exit47 ], [ %smin, %33 ]
  %52 = add nsw i32 %.1.lcssa, 1
  %53 = sext i32 %.1.lcssa to i64
  %54 = getelementptr inbounds [50005 x i32], ptr @q, i64 0, i64 %53
  store i32 %.pre-phi, ptr %54, align 4, !tbaa !5
  %indvars.iv.next39 = add nuw nsw i64 %indvars.iv38, 1
  %exitcond41.not = icmp eq i64 %indvars.iv.next39, %wide.trip.count
  br i1 %exitcond41.not, label %._crit_edge, label %.preheader, !llvm.loop !12
}

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smin.i32(i32, i32) #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 = { 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 #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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}


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