; LLVM IR from kazunetakahashi/atcoder :: 2017/1104_ddcc2017-final/A2.ll (Z5solvei)

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

@K = external dso_local global i32, align 4

; 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 noundef zeroext i1 @_Z2okiiii(i32 noundef, i32 noundef, i32 noundef, i32 noundef) #1

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z5solvei(i32 noundef %0) #1 {
  %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
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  %9 = load i32, ptr %2, align 4, !tbaa !5
  %10 = sdiv i32 %9, 2
  store i32 %10, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  %11 = load i32, ptr %2, align 4, !tbaa !5
  %12 = load i32, ptr @K, align 4, !tbaa !5
  %13 = sdiv i32 %11, %12
  store i32 %13, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  store i32 0, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  store i32 0, ptr %6, align 4, !tbaa !5
  br label %14

14:                                               ; preds = %43, %1
  %15 = load i32, ptr %6, 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
  store i32 2, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  br label %46

19:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  store i32 0, ptr %8, align 4, !tbaa !5
  br label %20

20:                                               ; preds = %39, %19
  %21 = load i32, ptr %8, align 4, !tbaa !5
  %22 = load i32, ptr %4, align 4, !tbaa !5
  %23 = icmp slt i32 %21, %22
  br i1 %23, label %25, label %24

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

25:                                               ; preds = %20
  %26 = load i32, ptr %6, align 4, !tbaa !5
  %27 = load i32, ptr @K, align 4, !tbaa !5
  %28 = mul nsw i32 %26, %27
  %29 = load i32, ptr %8, align 4, !tbaa !5
  %30 = load i32, ptr @K, align 4, !tbaa !5
  %31 = mul nsw i32 %29, %30
  %32 = load i32, ptr %3, align 4, !tbaa !5
  %33 = load i32, ptr %3, align 4, !tbaa !5
  %34 = call noundef zeroext i1 @_Z2okiiii(i32 noundef %28, i32 noundef %31, i32 noundef %32, i32 noundef %33)
  br i1 %34, label %35, label %38

35:                                               ; preds = %25
  %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 %38

38:                                               ; preds = %35, %25
  br label %39

39:                                               ; preds = %38
  %40 = load i32, ptr %8, align 4, !tbaa !5
  %41 = add nsw i32 %40, 1
  store i32 %41, ptr %8, align 4, !tbaa !5
  br label %20, !llvm.loop !9

42:                                               ; preds = %24
  br label %43

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

46:                                               ; preds = %18
  %47 = load i32, ptr %5, align 4, !tbaa !5
  store i32 1, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  ret i32 %47
}

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

@K = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_Z2okiiii(i32 noundef, i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z5solvei(i32 noundef %0) local_unnamed_addr #0 {
  %2 = sdiv i32 %0, 2
  %3 = load i32, ptr @K, align 4, !tbaa !5
  %4 = sdiv i32 %0, %3
  %5 = icmp sgt i32 %4, 0
  br i1 %5, label %.preheader.us, label %._crit_edge21

.preheader.us:                                    ; preds = %1, %._crit_edge.us
  %.01420.us = phi i32 [ %13, %._crit_edge.us ], [ 0, %1 ]
  %.01519.us = phi i32 [ %spec.select.us, %._crit_edge.us ], [ 0, %1 ]
  br label %6

6:                                                ; preds = %.preheader.us, %6
  %.018.us = phi i32 [ 0, %.preheader.us ], [ %12, %6 ]
  %.117.us = phi i32 [ %.01519.us, %.preheader.us ], [ %spec.select.us, %6 ]
  %7 = load i32, ptr @K, align 4, !tbaa !5
  %8 = mul nsw i32 %7, %.01420.us
  %9 = mul nsw i32 %7, %.018.us
  %10 = tail call noundef zeroext i1 @_Z2okiiii(i32 noundef %8, i32 noundef %9, i32 noundef %2, i32 noundef %2)
  %11 = zext i1 %10 to i32
  %spec.select.us = add nsw i32 %.117.us, %11
  %12 = add nuw nsw i32 %.018.us, 1
  %exitcond.not = icmp eq i32 %12, %4
  br i1 %exitcond.not, label %._crit_edge.us, label %6, !llvm.loop !9

._crit_edge.us:                                   ; preds = %6
  %13 = add nuw nsw i32 %.01420.us, 1
  %exitcond23.not = icmp eq i32 %13, %4
  br i1 %exitcond23.not, label %._crit_edge21, label %.preheader.us, !llvm.loop !11

._crit_edge21:                                    ; preds = %._crit_edge.us, %1
  %.015.lcssa = phi i32 [ 0, %1 ], [ %spec.select.us, %._crit_edge.us ]
  ret i32 %.015.lcssa
}

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

!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,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass