; LLVM IR from VastoLorde95/Competitive-Programming :: Hackerrank/Company Contests/Morgan Stanley/Morgan Stanley 2015/Problem A/A.ll (Z2goi)

; 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
@p = external dso_local global [22 x i32], align 16
@mx = external dso_local global i32, align 4

; Function Attrs: mustprogress uwtable
define dso_local void @_Z2goi(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
  store i32 %0, ptr %2, align 4, !tbaa !5
  %7 = load i32, ptr %2, align 4, !tbaa !5
  %8 = load i32, ptr @n, align 4, !tbaa !5
  %9 = sub nsw i32 %8, 1
  %10 = icmp eq i32 %7, %9
  br i1 %10, label %11, label %41

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

14:                                               ; preds = %35, %11
  %15 = load i32, ptr %5, align 4, !tbaa !5
  %16 = load i32, ptr %4, align 4, !tbaa !5
  %17 = icmp slt i32 %15, %16
  br i1 %17, label %19, label %18

18:                                               ; preds = %14
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  br label %38

19:                                               ; preds = %14
  %20 = load i32, ptr %5, align 4, !tbaa !5
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [22 x i32], ptr @p, i64 0, i64 %21
  %23 = load i32, ptr %22, align 4, !tbaa !5
  %24 = load i32, ptr @n, align 4, !tbaa !5
  %25 = load i32, ptr %5, align 4, !tbaa !5
  %26 = sub nsw i32 %24, %25
  %27 = sub nsw i32 %26, 1
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [22 x i32], ptr @p, i64 0, i64 %28
  %30 = load i32, ptr %29, align 4, !tbaa !5
  %31 = sub nsw i32 %23, %30
  %32 = call i32 @llvm.abs.i32(i32 %31, i1 true)
  %33 = load i32, ptr %3, align 4, !tbaa !5
  %34 = add nsw i32 %33, %32
  store i32 %34, ptr %3, align 4, !tbaa !5
  br label %35

35:                                               ; preds = %19
  %36 = load i32, ptr %5, align 4, !tbaa !5
  %37 = add nsw i32 %36, 1
  store i32 %37, ptr %5, align 4, !tbaa !5
  br label %14, !llvm.loop !9

38:                                               ; preds = %18
  %39 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) @mx, ptr noundef nonnull align 4 dereferenceable(4) %3)
  %40 = load i32, ptr %39, align 4, !tbaa !5
  store i32 %40, ptr @mx, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  br label %94

41:                                               ; preds = %1
  %42 = load i32, ptr %2, align 4, !tbaa !5
  %43 = load i32, ptr @n, align 4, !tbaa !5
  %44 = sub nsw i32 %43, 1
  %45 = icmp sgt i32 %42, %44
  br i1 %45, label %46, label %47

46:                                               ; preds = %41
  br label %94

47:                                               ; preds = %41
  %48 = load i32, ptr %2, align 4, !tbaa !5
  %49 = sub nsw i32 %48, 1
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds [22 x i32], ptr @p, i64 0, i64 %50
  %52 = load i32, ptr %51, align 4, !tbaa !5
  %53 = srem i32 %52, 2
  %54 = icmp eq i32 %53, 0
  br i1 %54, label %55, label %91

55:                                               ; preds = %47
  %56 = load i32, ptr %2, align 4, !tbaa !5
  %57 = add nsw i32 %56, 1
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds [22 x i32], ptr @p, i64 0, i64 %58
  %60 = load i32, ptr %59, align 4, !tbaa !5
  %61 = srem i32 %60, 2
  %62 = icmp eq i32 %61, 0
  br i1 %62, label %63, label %91

63:                                               ; preds = %55
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  %64 = load i32, ptr %2, align 4, !tbaa !5
  %65 = sext i32 %64 to i64
  %66 = getelementptr inbounds [22 x i32], ptr @p, i64 0, i64 %65
  %67 = load i32, ptr %66, align 4, !tbaa !5
  store i32 %67, ptr %6, align 4, !tbaa !5
  %68 = load i32, ptr %2, align 4, !tbaa !5
  %69 = sub nsw i32 %68, 1
  %70 = sext i32 %69 to i64
  %71 = getelementptr inbounds [22 x i32], ptr @p, i64 0, i64 %70
  %72 = load i32, ptr %71, align 4, !tbaa !5
  %73 = load i32, ptr %2, align 4, !tbaa !5
  %74 = add nsw i32 %73, 1
  %75 = sext i32 %74 to i64
  %76 = getelementptr inbounds [22 x i32], ptr @p, i64 0, i64 %75
  %77 = load i32, ptr %76, align 4, !tbaa !5
  %78 = add nsw i32 %72, %77
  %79 = sdiv i32 %78, 2
  %80 = load i32, ptr %2, align 4, !tbaa !5
  %81 = sext i32 %80 to i64
  %82 = getelementptr inbounds [22 x i32], ptr @p, i64 0, i64 %81
  store i32 %79, ptr %82, align 4, !tbaa !5
  %83 = load i32, ptr %2, align 4, !tbaa !5
  %84 = add nsw i32 %83, 1
  call void @_Z2goi(i32 noundef %84)
  %85 = load i32, ptr %6, align 4, !tbaa !5
  %86 = load i32, ptr %2, align 4, !tbaa !5
  %87 = sext i32 %86 to i64
  %88 = getelementptr inbounds [22 x i32], ptr @p, i64 0, i64 %87
  store i32 %85, ptr %88, align 4, !tbaa !5
  %89 = load i32, ptr %2, align 4, !tbaa !5
  %90 = add nsw i32 %89, 1
  call void @_Z2goi(i32 noundef %90)
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  br label %94

91:                                               ; preds = %55, %47
  %92 = load i32, ptr %2, align 4, !tbaa !5
  %93 = add nsw i32 %92, 1
  call void @_Z2goi(i32 noundef %93)
  br label %94

94:                                               ; preds = %91, %63, %46, %38
  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 speculatable willreturn memory(none)
declare i32 @llvm.abs.i32(i32, i1 immarg) #2

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) #3

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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #3 = { 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 #4 = { 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"}


; 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
@p = external dso_local local_unnamed_addr global [22 x i32], align 16
@mx = external dso_local global i32, align 4

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z2goi(i32 noundef %0) local_unnamed_addr #0 {
  %2 = alloca i32, align 4
  %3 = load i32, ptr @n, align 4, !tbaa !5
  %4 = add nsw i32 %3, -1
  %5 = icmp eq i32 %4, %0
  br i1 %5, label %tailrecurse._crit_edge, label %.lr.ph

tailrecurse._crit_edge:                           ; preds = %tailrecurse.backedge, %1
  %.lcssa = phi i32 [ %3, %1 ], [ %57, %tailrecurse.backedge ]
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %2) #5
  %6 = icmp sgt i32 %.lcssa, 1
  br i1 %6, label %.lr.ph24.preheader, label %._crit_edge

.lr.ph24.preheader:                               ; preds = %tailrecurse._crit_edge
  %7 = lshr i32 %.lcssa, 1
  %8 = zext nneg i32 %.lcssa to i64
  %wide.trip.count = zext nneg i32 %7 to i64
  %min.iters.check = icmp ult i32 %.lcssa, 16
  br i1 %min.iters.check, label %.lr.ph24.preheader40, label %vector.ph

.lr.ph24.preheader40:                             ; preds = %middle.block, %.lr.ph24.preheader
  %indvars.iv.ph = phi i64 [ 0, %.lr.ph24.preheader ], [ %n.vec, %middle.block ]
  %.ph = phi i32 [ 0, %.lr.ph24.preheader ], [ %23, %middle.block ]
  br label %.lr.ph24

vector.ph:                                        ; preds = %.lr.ph24.preheader
  %n.vec = and i64 %wide.trip.count, 1073741816
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.phi = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %20, %vector.body ]
  %vec.phi35 = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %21, %vector.body ]
  %9 = getelementptr inbounds [22 x i32], ptr @p, i64 0, i64 %index
  %10 = getelementptr inbounds i8, ptr %9, i64 16
  %wide.load = load <4 x i32>, ptr %9, align 16, !tbaa !5
  %wide.load36 = load <4 x i32>, ptr %10, align 16, !tbaa !5
  %11 = xor i64 %index, -1
  %12 = add nsw i64 %8, %11
  %13 = getelementptr inbounds [22 x i32], ptr @p, i64 0, i64 %12
  %14 = getelementptr inbounds i8, ptr %13, i64 -12
  %15 = getelementptr inbounds i8, ptr %13, i64 -28
  %wide.load37 = load <4 x i32>, ptr %14, align 4, !tbaa !5
  %reverse = shufflevector <4 x i32> %wide.load37, <4 x i32> poison, <4 x i32> <i32 3, i32 2, i32 1, i32 0>
  %wide.load38 = load <4 x i32>, ptr %15, align 4, !tbaa !5
  %reverse39 = shufflevector <4 x i32> %wide.load38, <4 x i32> poison, <4 x i32> <i32 3, i32 2, i32 1, i32 0>
  %16 = sub nsw <4 x i32> %wide.load, %reverse
  %17 = sub nsw <4 x i32> %wide.load36, %reverse39
  %18 = tail call <4 x i32> @llvm.abs.v4i32(<4 x i32> %16, i1 true)
  %19 = tail call <4 x i32> @llvm.abs.v4i32(<4 x i32> %17, i1 true)
  %20 = add <4 x i32> %18, %vec.phi
  %21 = add <4 x i32> %19, %vec.phi35
  %index.next = add nuw i64 %index, 8
  %22 = icmp eq i64 %index.next, %n.vec
  br i1 %22, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <4 x i32> %21, %20
  %23 = tail call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %bin.rdx)
  %cmp.n = icmp eq i64 %n.vec, %wide.trip.count
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph24.preheader40

._crit_edge:                                      ; preds = %.lr.ph24, %middle.block, %tailrecurse._crit_edge
  %.lcssa22 = phi i32 [ 0, %tailrecurse._crit_edge ], [ %23, %middle.block ], [ %35, %.lr.ph24 ]
  store i32 %.lcssa22, ptr %2, align 4, !tbaa !5
  %24 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) @mx, ptr noundef nonnull align 4 dereferenceable(4) %2)
  %25 = load i32, ptr %24, align 4, !tbaa !5
  store i32 %25, ptr @mx, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %2) #5
  br label %.loopexit

.lr.ph24:                                         ; preds = %.lr.ph24.preheader40, %.lr.ph24
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph24 ], [ %indvars.iv.ph, %.lr.ph24.preheader40 ]
  %26 = phi i32 [ %35, %.lr.ph24 ], [ %.ph, %.lr.ph24.preheader40 ]
  %27 = getelementptr inbounds [22 x i32], ptr @p, i64 0, i64 %indvars.iv
  %28 = load i32, ptr %27, align 4, !tbaa !5
  %29 = xor i64 %indvars.iv, -1
  %30 = add nsw i64 %8, %29
  %31 = getelementptr inbounds [22 x i32], ptr @p, i64 0, i64 %30
  %32 = load i32, ptr %31, align 4, !tbaa !5
  %33 = sub nsw i32 %28, %32
  %34 = tail call i32 @llvm.abs.i32(i32 %33, i1 true)
  %35 = add nuw nsw i32 %34, %26
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph24, !llvm.loop !13

.lr.ph:                                           ; preds = %1, %tailrecurse.backedge
  %36 = phi i32 [ %57, %tailrecurse.backedge ], [ %3, %1 ]
  %.tr21 = phi i32 [ %44, %tailrecurse.backedge ], [ %0, %1 ]
  %.not = icmp sgt i32 %36, %.tr21
  br i1 %.not, label %37, label %.loopexit

37:                                               ; preds = %.lr.ph
  %38 = add nsw i32 %.tr21, -1
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [22 x i32], ptr @p, i64 0, i64 %39
  %41 = load i32, ptr %40, align 4, !tbaa !5
  %42 = and i32 %41, 1
  %43 = icmp eq i32 %42, 0
  %44 = add nsw i32 %.tr21, 1
  br i1 %43, label %45, label %tailrecurse.backedge

45:                                               ; preds = %37
  %46 = sext i32 %44 to i64
  %47 = getelementptr inbounds [22 x i32], ptr @p, i64 0, i64 %46
  %48 = load i32, ptr %47, align 4, !tbaa !5
  %49 = and i32 %48, 1
  %50 = icmp eq i32 %49, 0
  br i1 %50, label %51, label %tailrecurse.backedge

51:                                               ; preds = %45
  %52 = sext i32 %.tr21 to i64
  %53 = getelementptr inbounds [22 x i32], ptr @p, i64 0, i64 %52
  %54 = load i32, ptr %53, align 4, !tbaa !5
  %55 = add nsw i32 %48, %41
  %56 = ashr exact i32 %55, 1
  store i32 %56, ptr %53, align 4, !tbaa !5
  tail call void @_Z2goi(i32 noundef %44)
  store i32 %54, ptr %53, align 4, !tbaa !5
  %.pre = load i32, ptr @n, align 4, !tbaa !5
  br label %tailrecurse.backedge

tailrecurse.backedge:                             ; preds = %37, %45, %51
  %57 = phi i32 [ %.pre, %51 ], [ %36, %45 ], [ %36, %37 ]
  %58 = add i32 %.tr21, 2
  %59 = icmp eq i32 %57, %58
  br i1 %59, label %tailrecurse._crit_edge, label %.lr.ph

.loopexit:                                        ; preds = %.lr.ph, %._crit_edge
  ret void
}

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

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.abs.i32(i32, i1 immarg) #2

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #3

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare <4 x i32> @llvm.abs.v4i32(<4 x i32>, i1 immarg) #4

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.vector.reduce.add.v4i32(<4 x i32>) #4

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 nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #3 = { 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 #4 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #5 = { 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, !11, !12}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!"llvm.loop.isvectorized", i32 1}
!12 = !{!"llvm.loop.unroll.runtime.disable"}
!13 = distinct !{!13, !10, !12, !11}


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