; LLVM IR from VastoLorde95/Competitive-Programming :: Hackerrank/Codesprints 2016/Codesprint April/Problem H/H.ll (Z4fastx)

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

@ma = external dso_local global [2 x [2 x i64]], align 16
@ret = external dso_local global [2 x [2 x i64]], 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 nounwind uwtable
declare dso_local void @_Z3mulPA2_xS0_(ptr noundef, ptr noundef) #1

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z4fastx(i64 noundef %0) #1 {
  %2 = alloca i64, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store i64 %0, ptr %2, align 8, !tbaa !5
  store i64 1, ptr @ret, align 16, !tbaa !5
  store i64 0, ptr getelementptr inbounds ([2 x i64], ptr @ret, i64 0, i64 1), align 8, !tbaa !5
  store i64 0, ptr getelementptr inbounds ([2 x [2 x i64]], ptr @ret, i64 0, i64 1), align 16, !tbaa !5
  store i64 1, ptr getelementptr inbounds ([2 x i64], ptr getelementptr inbounds ([2 x [2 x i64]], ptr @ret, i64 0, i64 1), i64 0, i64 1), align 8, !tbaa !5
  br label %6

6:                                                ; preds = %14, %1
  %7 = load i64, ptr %2, align 8, !tbaa !5
  %8 = icmp sgt i64 %7, 0
  br i1 %8, label %9, label %17

9:                                                ; preds = %6
  %10 = load i64, ptr %2, align 8, !tbaa !5
  %11 = srem i64 %10, 2
  %12 = icmp ne i64 %11, 0
  br i1 %12, label %13, label %14

13:                                               ; preds = %9
  call void @_Z3mulPA2_xS0_(ptr noundef @ret, ptr noundef @ma)
  br label %14

14:                                               ; preds = %13, %9
  call void @_Z3mulPA2_xS0_(ptr noundef @ma, ptr noundef @ma)
  %15 = load i64, ptr %2, align 8, !tbaa !5
  %16 = ashr i64 %15, 1
  store i64 %16, ptr %2, align 8, !tbaa !5
  br label %6, !llvm.loop !9

17:                                               ; preds = %6
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  store i32 0, ptr %3, align 4, !tbaa !11
  br label %18

18:                                               ; preds = %45, %17
  %19 = load i32, ptr %3, align 4, !tbaa !11
  %20 = icmp slt i32 %19, 2
  br i1 %20, label %22, label %21

21:                                               ; preds = %18
  store i32 4, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  br label %48

22:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  store i32 0, ptr %5, align 4, !tbaa !11
  br label %23

23:                                               ; preds = %41, %22
  %24 = load i32, ptr %5, align 4, !tbaa !11
  %25 = icmp slt i32 %24, 2
  br i1 %25, label %27, label %26

26:                                               ; preds = %23
  store i32 7, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  br label %44

27:                                               ; preds = %23
  %28 = load i32, ptr %3, align 4, !tbaa !11
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [2 x [2 x i64]], ptr @ret, i64 0, i64 %29
  %31 = load i32, ptr %5, align 4, !tbaa !11
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [2 x i64], ptr %30, i64 0, i64 %32
  %34 = load i64, ptr %33, align 8, !tbaa !5
  %35 = load i32, ptr %3, align 4, !tbaa !11
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [2 x [2 x i64]], ptr @ma, i64 0, i64 %36
  %38 = load i32, ptr %5, align 4, !tbaa !11
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [2 x i64], ptr %37, i64 0, i64 %39
  store i64 %34, ptr %40, align 8, !tbaa !5
  br label %41

41:                                               ; preds = %27
  %42 = load i32, ptr %5, align 4, !tbaa !11
  %43 = add nsw i32 %42, 1
  store i32 %43, ptr %5, align 4, !tbaa !11
  br label %23, !llvm.loop !13

44:                                               ; preds = %26
  br label %45

45:                                               ; preds = %44
  %46 = load i32, ptr %3, align 4, !tbaa !11
  %47 = add nsw i32 %46, 1
  store i32 %47, ptr %3, align 4, !tbaa !11
  br label %18, !llvm.loop !14

48:                                               ; preds = %21
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 = { 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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = distinct !{!13, !10}
!14 = distinct !{!14, !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"

@ma = external dso_local global [2 x [2 x i64]], align 16
@ret = external dso_local global [2 x [2 x i64]], align 16

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z4fastx(i64 noundef %0) local_unnamed_addr #0 {
  store i64 1, ptr @ret, align 16, !tbaa !5
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) getelementptr inbounds (i8, ptr @ret, i64 8), i8 0, i64 16, i1 false)
  store i64 1, ptr getelementptr inbounds (i8, ptr @ret, i64 24), align 8, !tbaa !5
  %2 = icmp sgt i64 %0, 0
  br i1 %2, label %.lr.ph, label %.preheader13

.preheader13:                                     ; preds = %5, %1
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(32) @ma, ptr noundef nonnull align 16 dereferenceable(32) @ret, i64 32, i1 false), !tbaa !5
  ret void

.lr.ph:                                           ; preds = %1, %5
  %.01214 = phi i64 [ %6, %5 ], [ %0, %1 ]
  %3 = and i64 %.01214, 1
  %.not = icmp eq i64 %3, 0
  br i1 %.not, label %5, label %4

4:                                                ; preds = %.lr.ph
  tail call void @_Z3mulPA2_xS0_(ptr noundef nonnull @ret, ptr noundef nonnull @ma)
  br label %5

5:                                                ; preds = %4, %.lr.ph
  tail call void @_Z3mulPA2_xS0_(ptr noundef nonnull @ma, ptr noundef nonnull @ma)
  %6 = lshr i64 %.01214, 1
  %.not17 = icmp ult i64 %.01214, 2
  br i1 %.not17, label %.preheader13, label %.lr.ph, !llvm.loop !9
}

; 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

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: write) }

!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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopIdiomRecognizePass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,MemCpyOptPass,SROAPass,SimplifyCFGPass,TailCallElimPass