; LLVM IR from Mohammed-Shoaib/Coding-Problems :: Codeforces/Solutions/1374B.ll (Z25multiply_by_2_divide_by_6i)

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z25multiply_by_2_divide_by_6i(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %4

4:                                                ; preds = %13, %1
  %5 = load i32, ptr %2, align 4, !tbaa !5
  %6 = icmp ne i32 %5, 0
  br i1 %6, label %7, label %11

7:                                                ; preds = %4
  %8 = load i32, ptr %2, align 4, !tbaa !5
  %9 = srem i32 %8, 6
  %10 = icmp eq i32 %9, 0
  br label %11

11:                                               ; preds = %7, %4
  %12 = phi i1 [ false, %4 ], [ %10, %7 ]
  br i1 %12, label %13, label %18

13:                                               ; preds = %11
  %14 = load i32, ptr %3, align 4, !tbaa !5
  %15 = add nsw i32 %14, 1
  store i32 %15, ptr %3, align 4, !tbaa !5
  %16 = load i32, ptr %2, align 4, !tbaa !5
  %17 = sdiv i32 %16, 6
  store i32 %17, ptr %2, align 4, !tbaa !5
  br label %4, !llvm.loop !9

18:                                               ; preds = %11
  br label %19

19:                                               ; preds = %28, %18
  %20 = load i32, ptr %2, align 4, !tbaa !5
  %21 = icmp ne i32 %20, 0
  br i1 %21, label %22, label %26

22:                                               ; preds = %19
  %23 = load i32, ptr %2, align 4, !tbaa !5
  %24 = srem i32 %23, 3
  %25 = icmp eq i32 %24, 0
  br label %26

26:                                               ; preds = %22, %19
  %27 = phi i1 [ false, %19 ], [ %25, %22 ]
  br i1 %27, label %28, label %33

28:                                               ; preds = %26
  %29 = load i32, ptr %3, align 4, !tbaa !5
  %30 = add nsw i32 %29, 2
  store i32 %30, ptr %3, align 4, !tbaa !5
  %31 = load i32, ptr %2, align 4, !tbaa !5
  %32 = sdiv i32 %31, 3
  store i32 %32, ptr %2, align 4, !tbaa !5
  br label %19, !llvm.loop !11

33:                                               ; preds = %26
  %34 = load i32, ptr %2, align 4, !tbaa !5
  %35 = icmp eq i32 %34, 1
  br i1 %35, label %36, label %38

36:                                               ; preds = %33
  %37 = load i32, ptr %3, align 4, !tbaa !5
  br label %39

38:                                               ; preds = %33
  br label %39

39:                                               ; preds = %38, %36
  %40 = phi i32 [ %37, %36 ], [ -1, %38 ]
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  ret i32 %40
}

; 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 willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"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}


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

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none) uwtable
define dso_local noundef i32 @_Z25multiply_by_2_divide_by_6i(i32 noundef %0) local_unnamed_addr #0 {
  %.not18 = icmp ne i32 %0, 0
  %2 = srem i32 %0, 6
  %3 = icmp eq i32 %2, 0
  %or.cond19 = and i1 %.not18, %3
  br i1 %or.cond19, label %.lr.ph, label %.critedge.preheader

.critedge.preheader:                              ; preds = %.lr.ph, %1
  %.012.lcssa = phi i32 [ %0, %1 ], [ %7, %.lr.ph ]
  %.0.lcssa = phi i32 [ 0, %1 ], [ %6, %.lr.ph ]
  %.not1423 = icmp ne i32 %.012.lcssa, 0
  %4 = srem i32 %.012.lcssa, 3
  %5 = icmp eq i32 %4, 0
  %or.cond1724 = and i1 %.not1423, %5
  br i1 %or.cond1724, label %.critedge, label %.critedge2

.lr.ph:                                           ; preds = %1, %.lr.ph
  %.021 = phi i32 [ %6, %.lr.ph ], [ 0, %1 ]
  %.01220 = phi i32 [ %7, %.lr.ph ], [ %0, %1 ]
  %6 = add nuw nsw i32 %.021, 1
  %7 = sdiv i32 %.01220, 6
  %8 = add i32 %.01220, -6
  %.not = icmp ult i32 %8, -11
  %9 = srem i32 %7, 6
  %10 = icmp eq i32 %9, 0
  %or.cond = and i1 %.not, %10
  br i1 %or.cond, label %.lr.ph, label %.critedge.preheader, !llvm.loop !5

.critedge:                                        ; preds = %.critedge.preheader, %.critedge
  %.126 = phi i32 [ %11, %.critedge ], [ %.0.lcssa, %.critedge.preheader ]
  %.11325 = phi i32 [ %12, %.critedge ], [ %.012.lcssa, %.critedge.preheader ]
  %11 = add nuw nsw i32 %.126, 2
  %12 = sdiv i32 %.11325, 3
  %13 = add i32 %.11325, -3
  %.not14 = icmp ult i32 %13, -5
  %14 = srem i32 %12, 3
  %15 = icmp eq i32 %14, 0
  %or.cond17 = and i1 %.not14, %15
  br i1 %or.cond17, label %.critedge, label %.critedge2, !llvm.loop !7

.critedge2:                                       ; preds = %.critedge, %.critedge.preheader
  %.113.lcssa = phi i32 [ %.012.lcssa, %.critedge.preheader ], [ %12, %.critedge ]
  %.1.lcssa = phi i32 [ %.0.lcssa, %.critedge.preheader ], [ %11, %.critedge ]
  %16 = icmp eq i32 %.113.lcssa, 1
  %17 = select i1 %16, i32 %.1.lcssa, i32 -1
  ret i32 %17
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(none) 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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = distinct !{!7, !6}


; Effective O3 passes: GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass